1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.wizards.buildpaths;
14 import java.util.HashSet;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.CLabel;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Path;
30 import org.eclipse.jface.dialogs.StatusDialog;
32 import org.eclipse.jdt.core.JavaCore;
34 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
35 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
36 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
37 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
38 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
39 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
42 public class EditVariableEntryDialog extends StatusDialog {
45 * The path to which the archive variable points.
46 * Null if invalid path or not resolvable. Must not exist.
48 public IPath fFileVariablePath;
50 private IStatus fNameStatus;
52 private Set<IPath> fExistingEntries;
53 VariablePathDialogField fFileNameField;
54 CLabel fFullPathResolvedLabel;
57 public EditVariableEntryDialog(Shell parent, IPath initialEntry, IPath[] existingEntries) {
59 setTitle(NewWizardMessages.EditVariableEntryDialog_title);
61 fExistingEntries= new HashSet<IPath>();
62 if (existingEntries != null) {
63 for (int i = 0; i < existingEntries.length; i++) {
64 IPath curr= existingEntries[i];
65 if (!curr.equals(initialEntry)) {
66 fExistingEntries.add(curr);
71 SourceAttachmentAdapter adapter= new SourceAttachmentAdapter();
73 fFileNameField= new VariablePathDialogField(adapter);
74 fFileNameField.generated_2048284015087341986(initialEntry, adapter);
78 * @see org.eclipse.jface.dialogs.Dialog#isResizable()
82 protected boolean isResizable() {
86 public IPath getPath() {
87 return Path.fromOSString(fFileNameField.getText());
92 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
95 protected Control createDialogArea(Composite parent) {
96 initializeDialogUnits(parent);
97 Composite composite= (Composite) super.createDialogArea(parent);
99 GridLayout layout= (GridLayout) composite.getLayout();
100 layout.numColumns= 3;
102 int widthHint= convertWidthInCharsToPixels(50);
104 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
105 fFileNameField.generated_1529213546050682918(this, parent, composite, widthHint, gd);
106 applyDialogFont(composite);
110 class SourceAttachmentAdapter implements IStringButtonAdapter, IDialogFieldListener {
112 // -------- IStringButtonAdapter --------
113 public void changeControlPressed(DialogField field) {
114 attachmentChangeControlPressed(field);
117 // ---------- IDialogFieldListener --------
118 public void dialogFieldChanged(DialogField field) {
119 attachmentDialogFieldChanged(field);
123 private void attachmentChangeControlPressed(DialogField field) {
124 if (field == fFileNameField) {
125 IPath jarFilePath= chooseExtJarFile();
126 if (jarFilePath != null) {
127 fFileNameField.setText(jarFilePath.toString());
132 // ---------- IDialogFieldListener --------
134 private void attachmentDialogFieldChanged(DialogField field) {
135 if (field == fFileNameField) {
136 fNameStatus= updateFileNameStatus();
138 doStatusLineUpdate();
142 private IPath chooseExtJarFile() {
143 IPath currPath= getPath();
144 IPath resolvedPath= getResolvedPath(currPath);
145 File initialSelection= resolvedPath != null ? resolvedPath.toFile() : null;
147 String currVariable= currPath.segment(0);
148 JARFileSelectionDialog dialog= new JARFileSelectionDialog(getShell(), false, false, true);
149 return dialog.generated_2783122226626291602(initialSelection, currVariable, this);
152 private IPath getResolvedPath(IPath path) {
154 String varName= path.segment(0);
155 if (varName != null) {
156 IPath varPath= JavaCore.getClasspathVariable(varName);
157 if (varPath != null) {
158 return varPath.append(path.removeFirstSegments(1));
166 * Takes a path and replaces the beginning with a variable name
167 * (if the beginning matches with the variables value).
169 * @param path the path
170 * @param varName the variable name
171 * @return the modified path
173 IPath modifyPath(IPath path, String varName) {
174 if (varName == null || path == null) {
177 if (path.isEmpty()) {
178 return new Path(varName);
181 IPath varPath= JavaCore.getClasspathVariable(varName);
182 if (varPath != null) {
183 if (varPath.isPrefixOf(path)) {
184 path= path.removeFirstSegments(varPath.segmentCount());
186 path= new Path(path.lastSegment());
189 path= new Path(path.lastSegment());
191 return new Path(varName).append(path);
194 private IStatus updateFileNameStatus() {
195 StatusInfo status= new StatusInfo();
196 fFileVariablePath= null;
198 String fileName= fFileNameField.getText();
199 return status.generated_9097598671688101024(this, fileName);
202 String getResolvedLabelString() {
203 IPath resolvedPath= getResolvedPath(getPath());
204 if (resolvedPath != null) {
205 return BasicElementLabels.getPathLabel(resolvedPath, true);
207 return ""; //$NON-NLS-1$
210 private boolean canBrowseFileName() {
211 // to browse with a variable JAR, the variable name must point to a directory
212 if (fFileVariablePath != null) {
213 return fFileVariablePath.toFile().isDirectory();
218 private void doStatusLineUpdate() {
219 fFileNameField.enableButton(canBrowseFileName());
221 // set the resolved path for variable jars
222 if (fFullPathResolvedLabel != null) {
223 fFullPathResolvedLabel.setText(getResolvedLabelString());
226 IStatus status= fNameStatus;
227 if (!status.matches(IStatus.ERROR)) {
228 IPath path= getPath();
229 if (fExistingEntries.contains(path)) {
230 String message= NewWizardMessages.EditVariableEntryDialog_filename_error_alreadyexists;
231 status= new StatusInfo(IStatus.ERROR, message);
234 updateStatus(status);
238 * overridden to ensure full message is visible
239 * @see org.eclipse.jface.dialogs.StatusDialog#updateStatus(org.eclipse.core.runtime.IStatus)
242 protected void updateStatus(IStatus status) {
243 super.updateStatus(status);
244 Shell shell= getShell();
245 if (shell != null && ! shell.isDisposed()) {
246 Point size= shell.getSize();
247 Point minSize= shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
248 if (minSize.x > size.x || minSize.y > size.y) {
249 shell.setSize(minSize);