]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/VariablePathDialogField.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / wizards / buildpaths / VariablePathDialogField.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 import java.util.List;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.swt.widgets.Text;
24
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.Path;
27
28 import org.eclipse.jface.dialogs.StatusDialog;
29 import org.eclipse.jface.viewers.DoubleClickEvent;
30 import org.eclipse.jface.viewers.IDoubleClickListener;
31 import org.eclipse.jface.viewers.ISelectionChangedListener;
32 import org.eclipse.jface.viewers.SelectionChangedEvent;
33 import org.eclipse.jface.window.Window;
34
35 import org.eclipse.ui.PlatformUI;
36
37 import org.eclipse.jdt.core.JavaCore;
38
39 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
42 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
43 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
44 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
45
46 public class VariablePathDialogField extends StringButtonDialogField {
47
48         public static class ChooseVariableDialog extends StatusDialog implements ISelectionChangedListener, IDoubleClickListener {
49                 private VariableBlock fVariableBlock;
50
51                 public ChooseVariableDialog(Shell parent, String variableSelection) {
52                         super(parent);
53
54                         setTitle(NewWizardMessages.VariablePathDialogField_variabledialog_title);
55                         fVariableBlock= new VariableBlock(false, variableSelection);
56                 }
57
58                 @Override
59                 protected Control createDialogArea(Composite parent) {
60                         Composite composite= (Composite) super.createDialogArea(parent);
61                         Control control= fVariableBlock.createContents(composite);
62
63                         GridData data= new GridData(GridData.FILL_BOTH);
64                         data.widthHint= convertWidthInCharsToPixels(80);
65                         data.heightHint= convertHeightInCharsToPixels(15);
66                         control.setLayoutData(data);
67
68                         fVariableBlock.addDoubleClickListener(this);
69                         fVariableBlock.addSelectionChangedListener(this);
70                         applyDialogFont(composite);
71                         return composite;
72                 }
73
74                 /*
75                  * @see org.eclipse.jface.dialogs.Dialog#isResizable()
76                  * @since 3.4
77                  */
78                 @Override
79                 protected boolean isResizable() {
80                         return true;
81                 }
82
83                 @Override
84                 protected void okPressed() {
85                         fVariableBlock.performOk();
86                         super.okPressed();
87                 }
88
89                 public String getSelectedVariable() {
90                         List<CPVariableElement> elements= fVariableBlock.getSelectedElements();
91                         return elements.get(0).getName();
92                 }
93
94                 /*
95                  * @see IDoubleClickListener#doubleClick(DoubleClickEvent)
96                  */
97                 public void doubleClick(DoubleClickEvent event) {
98                         if (getStatus().isOK()) {
99                                 okPressed();
100                         }
101                 }
102
103                 /* (non-Javadoc)
104                  * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
105                  */
106                 public void selectionChanged(SelectionChangedEvent event) {
107                         List<CPVariableElement> elements= fVariableBlock.getSelectedElements();
108                         StatusInfo status= new StatusInfo();
109                         if (elements.size() != 1) {
110                                 status.setError(""); //$NON-NLS-1$
111                         }
112                         updateStatus(status);
113                 }
114                 /*
115                  * @see org.eclipse.jface.window.Window#configureShell(Shell)
116                  */
117                 @Override
118                 protected void configureShell(Shell newShell) {
119                         super.configureShell(newShell);
120                         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.CHOOSE_VARIABLE_DIALOG);
121                 }
122         }
123
124         private Button fBrowseVariableButton;
125         private String fVariableButtonLabel;
126
127         public VariablePathDialogField(IStringButtonAdapter adapter) {
128                 super(adapter);
129         }
130
131         public void setVariableButtonLabel(String label) {
132                 fVariableButtonLabel= label;
133         }
134
135         // ------- layout helpers
136
137         @Override
138         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
139                 assertEnoughColumns(nColumns);
140
141                 Label label= getLabelControl(parent);
142                 label.setLayoutData(gridDataForLabel(1));
143                 Text text= getTextControl(parent);
144                 text.setLayoutData(gridDataForText(nColumns - 3));
145                 Button variableButton= getBrowseVariableControl(parent);
146                 variableButton.setLayoutData(gridDataForButton(variableButton, 1));
147                 Button browseButton= getChangeControl(parent);
148                 browseButton.setLayoutData(gridDataForButton(browseButton, 1));
149                 return new Control[] { label, text, variableButton, browseButton };
150         }
151
152         @Override
153         public int getNumberOfControls() {
154                 return 4;
155         }
156         public Button getBrowseVariableControl(Composite parent) {
157                 if (fBrowseVariableButton == null) {
158                         assertCompositeNotNull(parent);
159
160                         fBrowseVariableButton= new Button(parent, SWT.PUSH);
161                         fBrowseVariableButton.setText(fVariableButtonLabel);
162                         fBrowseVariableButton.setEnabled(isEnabled());
163                         fBrowseVariableButton.addSelectionListener(new SelectionListener() {
164                                 public void widgetDefaultSelected(SelectionEvent e) {
165                                         chooseVariablePressed();
166                                 }
167                                 public void widgetSelected(SelectionEvent e) {
168                                         chooseVariablePressed();
169                                 }
170                         });
171
172                 }
173                 return fBrowseVariableButton;
174         }
175
176         public IPath getPath() {
177                 return new Path(getText());
178         }
179
180         public String getVariable() {
181                 IPath path= getPath();
182                 if (!path.isEmpty()) {
183                         return path.segment(0);
184                 }
185                 return null;
186         }
187
188         public IPath getPathExtension() {
189                 return new Path(getText()).removeFirstSegments(1).setDevice(null);
190         }
191
192         public IPath getResolvedPath() {
193                 String variable= getVariable();
194                 if (variable != null) {
195                         IPath path= JavaCore.getClasspathVariable(variable);
196                         if (path != null) {
197                                 return path.append(getPathExtension());
198                         }
199                 }
200                 return null;
201         }
202
203         private Shell getShell() {
204                 if (fBrowseVariableButton != null) {
205                         return fBrowseVariableButton.getShell();
206                 }
207                 return JavaPlugin.getActiveWorkbenchShell();
208         }
209
210         private void chooseVariablePressed() {
211                 String variable= getVariable();
212                 ChooseVariableDialog dialog= new ChooseVariableDialog(getShell(), variable);
213                 if (dialog.open() == Window.OK) {
214                         IPath newPath= new Path(dialog.getSelectedVariable()).append(getPathExtension());
215                         setText(newPath.toString());
216                 }
217         }
218
219         @Override
220         protected void updateEnableState() {
221                 super.updateEnableState();
222                 if (isOkToUse(fBrowseVariableButton)) {
223                         fBrowseVariableButton.setEnabled(isEnabled());
224                 }
225         }
226
227 }