]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/EditVariableEntryDialog.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / wizards / buildpaths / EditVariableEntryDialog.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.wizards.buildpaths;
12
13 import java.io.File;
14 import java.util.HashSet;
15 import java.util.Set;
16
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;
25
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Path;
29
30 import org.eclipse.jface.dialogs.StatusDialog;
31
32 import org.eclipse.jdt.core.JavaCore;
33
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;
40
41
42 public class EditVariableEntryDialog extends StatusDialog {
43
44         /**
45          * The path to which the archive variable points.
46          * Null if invalid path or not resolvable. Must not exist.
47          */
48         public IPath fFileVariablePath;
49
50         private IStatus fNameStatus;
51
52         private Set<IPath> fExistingEntries;
53         VariablePathDialogField fFileNameField;
54         CLabel fFullPathResolvedLabel;
55
56
57         public EditVariableEntryDialog(Shell parent, IPath initialEntry, IPath[] existingEntries) {
58                 super(parent);
59                 setTitle(NewWizardMessages.EditVariableEntryDialog_title);
60
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);
67                                 }
68                         }
69                 }
70
71                 SourceAttachmentAdapter adapter= new SourceAttachmentAdapter();
72
73                 fFileNameField= new VariablePathDialogField(adapter);
74                 fFileNameField.generated_2048284015087341986(initialEntry, adapter);
75         }
76
77         /*
78          * @see org.eclipse.jface.dialogs.Dialog#isResizable()
79          * @since 3.4
80          */
81         @Override
82         protected boolean isResizable() {
83                 return true;
84         }
85
86         public IPath getPath() {
87                 return Path.fromOSString(fFileNameField.getText());
88         }
89
90
91         /* (non-Javadoc)
92          * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
93          */
94         @Override
95         protected Control createDialogArea(Composite parent) {
96                 initializeDialogUnits(parent);
97                 Composite composite= (Composite) super.createDialogArea(parent);
98
99                 GridLayout layout= (GridLayout) composite.getLayout();
100                 layout.numColumns= 3;
101
102                 int widthHint= convertWidthInCharsToPixels(50);
103
104                 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
105                 fFileNameField.generated_1529213546050682918(this, parent, composite, widthHint, gd);
106                 applyDialogFont(composite);
107                 return composite;
108         }
109
110         class SourceAttachmentAdapter implements IStringButtonAdapter, IDialogFieldListener {
111
112                 // -------- IStringButtonAdapter --------
113                 public void changeControlPressed(DialogField field) {
114                         attachmentChangeControlPressed(field);
115                 }
116
117                 // ---------- IDialogFieldListener --------
118                 public void dialogFieldChanged(DialogField field) {
119                         attachmentDialogFieldChanged(field);
120                 }
121         }
122
123         private void attachmentChangeControlPressed(DialogField field) {
124                 if (field == fFileNameField) {
125                         IPath jarFilePath= chooseExtJarFile();
126                         if (jarFilePath != null) {
127                                 fFileNameField.setText(jarFilePath.toString());
128                         }
129                 }
130         }
131
132         // ---------- IDialogFieldListener --------
133
134         private void attachmentDialogFieldChanged(DialogField field) {
135                 if (field == fFileNameField) {
136                         fNameStatus= updateFileNameStatus();
137                 }
138                 doStatusLineUpdate();
139         }
140
141
142         private IPath chooseExtJarFile() {
143                 IPath currPath= getPath();
144                 IPath resolvedPath= getResolvedPath(currPath);
145                 File initialSelection= resolvedPath != null ? resolvedPath.toFile() : null;
146
147                 String currVariable= currPath.segment(0);
148                 JARFileSelectionDialog dialog= new JARFileSelectionDialog(getShell(), false, false, true);
149                 return dialog.generated_2783122226626291602(initialSelection, currVariable, this);
150         }
151
152         private IPath getResolvedPath(IPath path) {
153                 if (path != null) {
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));
159                                 }
160                         }
161                 }
162                 return null;
163         }
164
165         /**
166          * Takes a path and replaces the beginning with a variable name
167          * (if the beginning matches with the variables value).
168          *
169          * @param path the path
170          * @param varName the variable name
171          * @return the modified path
172          */
173         IPath modifyPath(IPath path, String varName) {
174                 if (varName == null || path == null) {
175                         return null;
176                 }
177                 if (path.isEmpty()) {
178                         return new Path(varName);
179                 }
180
181                 IPath varPath= JavaCore.getClasspathVariable(varName);
182                 if (varPath != null) {
183                         if (varPath.isPrefixOf(path)) {
184                                 path= path.removeFirstSegments(varPath.segmentCount());
185                         } else {
186                                 path= new Path(path.lastSegment());
187                         }
188                 } else {
189                         path= new Path(path.lastSegment());
190                 }
191                 return new Path(varName).append(path);
192         }
193
194         private IStatus updateFileNameStatus() {
195                 StatusInfo status= new StatusInfo();
196                 fFileVariablePath= null;
197
198                 String fileName= fFileNameField.getText();
199                 return status.generated_9097598671688101024(this, fileName);
200         }
201
202         String getResolvedLabelString() {
203                 IPath resolvedPath= getResolvedPath(getPath());
204                 if (resolvedPath != null) {
205                         return BasicElementLabels.getPathLabel(resolvedPath, true);
206                 }
207                 return ""; //$NON-NLS-1$
208         }
209
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();
214                 }
215                 return false;
216         }
217
218         private void doStatusLineUpdate() {
219                 fFileNameField.enableButton(canBrowseFileName());
220
221                 // set the resolved path for variable jars
222                 if (fFullPathResolvedLabel != null) {
223                         fFullPathResolvedLabel.setText(getResolvedLabelString());
224                 }
225
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);
232                         }
233                 }
234                 updateStatus(status);
235         }
236
237         /*
238          * overridden to ensure full message is visible
239          * @see org.eclipse.jface.dialogs.StatusDialog#updateStatus(org.eclipse.core.runtime.IStatus)
240          */
241         @Override
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);
250                         }
251                 }
252         }
253 }