]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/NewVariableEntryDialog.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / wizards / buildpaths / NewVariableEntryDialog.java
CommitLineData
1b2798f6
EK
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 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.wizards.buildpaths;
12
13import java.util.ArrayList;
14import java.util.HashMap;
15import java.util.Iterator;
16import java.util.List;
17import java.util.Map;
18
19import org.eclipse.swt.SWT;
20import org.eclipse.swt.custom.CLabel;
21import org.eclipse.swt.layout.GridData;
22import org.eclipse.swt.layout.GridLayout;
23import org.eclipse.swt.widgets.Button;
24import org.eclipse.swt.widgets.Composite;
25import org.eclipse.swt.widgets.Control;
26import org.eclipse.swt.widgets.Shell;
27
28import org.eclipse.core.runtime.IPath;
29import org.eclipse.core.runtime.IStatus;
30
31import org.eclipse.jface.dialogs.Dialog;
32import org.eclipse.jface.dialogs.IDialogConstants;
33import org.eclipse.jface.dialogs.IDialogSettings;
34import org.eclipse.jface.dialogs.StatusDialog;
35import org.eclipse.jface.resource.JFaceResources;
36import org.eclipse.jface.viewers.StructuredSelection;
37import org.eclipse.jface.viewers.Viewer;
38import org.eclipse.jface.viewers.ViewerComparator;
39
40import org.eclipse.ui.PlatformUI;
41import org.eclipse.ui.dialogs.PreferencesUtil;
42
43import org.eclipse.jdt.core.JavaCore;
44
45import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
46import org.eclipse.jdt.internal.ui.JavaPlugin;
47import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
48import org.eclipse.jdt.internal.ui.preferences.ClasspathVariablesPreferencePage;
49import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
50import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
51import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
52import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
53import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
54import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
55
56public class NewVariableEntryDialog extends StatusDialog {
57
58 public class VariablesAdapter implements IDialogFieldListener, IListAdapter<CPVariableElement> {
59
60 // -------- IListAdapter --------
61
62 public void customButtonPressed(ListDialogField<CPVariableElement> field, int index) {
63 switch (index) {
64 case IDX_EXTEND: /* extend */
65 extendButtonPressed();
66 break;
67 }
68 }
69
70 public void selectionChanged(ListDialogField<CPVariableElement> field) {
71 doSelectionChanged();
72 }
73
74 public void doubleClicked(ListDialogField<CPVariableElement> field) {
75 doDoubleClick();
76 }
77
78 // ---------- IDialogFieldListener --------
79
80 public void dialogFieldChanged(DialogField field) {
81 if (field == fConfigButton) {
82 configButtonPressed();
83 }
84
85 }
86
87 }
88
89 public final int IDX_EXTEND= 0;
90
91 public ListDialogField<CPVariableElement> fVariablesList;
92 public boolean fCanExtend;
93 private boolean fIsValidSelection;
94
95 public IPath[] fResultPaths;
96
97 private SelectionButtonDialogField fConfigButton;
98
99 private CLabel fWarning;
100
101 public NewVariableEntryDialog(Shell parent) {
102 super(parent);
103 setTitle(NewWizardMessages.NewVariableEntryDialog_title);
104
105 updateStatus(new StatusInfo(IStatus.ERROR, "")); //$NON-NLS-1$
106
107 String[] buttonLabels= new String[] {
108 NewWizardMessages.NewVariableEntryDialog_vars_extend,
109 };
110
111 VariablesAdapter adapter= new VariablesAdapter();
112
113 CPVariableElementLabelProvider labelProvider= new CPVariableElementLabelProvider(false);
114
115 fVariablesList= new ListDialogField<CPVariableElement>(adapter, buttonLabels, labelProvider);
116 fVariablesList.generated_4355676026627584892(this, adapter);
117
118 fVariablesList.setViewerComparator(new ViewerComparator() {
119 @Override
120 public int compare(Viewer viewer, Object e1, Object e2) {
121 if (e1 instanceof CPVariableElement && e2 instanceof CPVariableElement) {
122 return getComparator().compare(((CPVariableElement)e1).getName(), ((CPVariableElement)e2).getName());
123 }
124 return super.compare(viewer, e1, e2);
125 }
126 });
127
128
129 fConfigButton= new SelectionButtonDialogField(SWT.PUSH);
130 fConfigButton.setLabelText(NewWizardMessages.NewVariableEntryDialog_configbutton_label);
131 fConfigButton.setDialogFieldListener(adapter);
132
133 initializeElements();
134
135 fCanExtend= false;
136 fIsValidSelection= false;
137 fResultPaths= null;
138
139 fVariablesList.selectFirstElement();
140 }
141
142 /*
143 * @see org.eclipse.jface.dialogs.Dialog#isResizable()
144 * @since 3.4
145 */
146 @Override
147 protected boolean isResizable() {
148 return true;
149 }
150
151 private void initializeElements() {
152 String[] entries= JavaCore.getClasspathVariableNames();
153 ArrayList<CPVariableElement> elements= new ArrayList<CPVariableElement>(entries.length);
154 for (int i= 0; i < entries.length; i++) {
155 String name= entries[i];
156 IPath entryPath= JavaCore.getClasspathVariable(name);
157 if (entryPath != null) {
158 elements.add(new CPVariableElement(name, entryPath));
159 }
160 }
161
162 fVariablesList.setElements(elements);
163 }
164
165
166 /* (non-Javadoc)
167 * @see Window#configureShell(Shell)
168 */
169 @Override
170 protected void configureShell(Shell shell) {
171 super.configureShell(shell);
172 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IJavaHelpContextIds.NEW_VARIABLE_ENTRY_DIALOG);
173 }
174
175 /* (non-Javadoc)
176 * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
177 */
178 @Override
179 protected IDialogSettings getDialogBoundsSettings() {
180 return JavaPlugin.getDefault().getDialogSettingsSection(getClass().getName());
181 }
182
183 /* (non-Javadoc)
184 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
185 */
186 @Override
187 protected Control createDialogArea(Composite parent) {
188 initializeDialogUnits(parent);
189
190 Composite composite= (Composite) super.createDialogArea(parent);
191 GridLayout layout= (GridLayout) composite.getLayout();
192 layout.numColumns= 2;
193
194 GridData listData= fVariablesList.generated_3996812238623832663(composite);
195 listData.grabExcessHorizontalSpace= true;
196 listData.heightHint= convertHeightInCharsToPixels(10);
197 listData.widthHint= convertWidthInCharsToPixels(70);
198
199 fWarning= new CLabel(composite, SWT.NONE);
200 fWarning.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, fVariablesList.getNumberOfControls() - 1, 1));
201
202 Composite lowerComposite= new Composite(composite, SWT.NONE);
203 lowerComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
204
205 layout= new GridLayout();
206 layout.marginHeight= 0;
207 layout.marginWidth= 0;
208 lowerComposite.setLayout(layout);
209
210 fConfigButton.doFillIntoGrid(lowerComposite, 1);
211
212 applyDialogFont(composite);
213 return composite;
214 }
215
216 public IPath[] getResult() {
217 return fResultPaths;
218 }
219
220 /*
221 * @see IDoubleClickListener#doubleClick(DoubleClickEvent)
222 */
223 private void doDoubleClick() {
224 if (fIsValidSelection) {
225 okPressed();
226 } else if (fCanExtend) {
227 extendButtonPressed();
228 }
229 }
230
231 private void doSelectionChanged() {
232 boolean isValidSelection= true;
233 boolean canExtend= false;
234 StatusInfo status= new StatusInfo();
235
236 isValidSelection= status.generated_6969006136874365073(isValidSelection, canExtend, this);
237 fVariablesList.enableButton(0, fCanExtend);
238
239 updateStatus(status);
240 fIsValidSelection= isValidSelection;
241 Button okButton= getButton(IDialogConstants.OK_ID);
242 if (okButton != null && !okButton.isDisposed()) {
243 okButton.setEnabled(isValidSelection);
244 }
245 updateDeprecationWarning();
246 }
247
248 private void updateDeprecationWarning() {
249 if (fWarning == null || fWarning.isDisposed())
250 return;
251
252 for (Iterator<CPVariableElement> iter= fVariablesList.getSelectedElements().iterator(); iter.hasNext();) {
253 CPVariableElement element= iter.next();
254 String deprecationMessage= element.getDeprecationMessage();
255 if (deprecationMessage != null) {
256 fWarning.setText(deprecationMessage);
257 fWarning.setImage(JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING));
258 return;
259 }
260 }
261 fWarning.setText(null);
262 fWarning.setImage(null);
263 }
264
265 private IPath[] chooseExtensions(CPVariableElement elem) {
266 return elem.generated_5550579726006519696(this);
267 }
268
269 protected final void extendButtonPressed() {
270 List<CPVariableElement> selected= fVariablesList.getSelectedElements();
271 if (selected.size() == 1) {
272 IPath[] extendedPaths= chooseExtensions(selected.get(0));
273 if (extendedPaths != null) {
274 fResultPaths= extendedPaths;
275 super.buttonPressed(IDialogConstants.OK_ID);
276 }
277 }
278 }
279
280 protected final void configButtonPressed() {
281 String id= ClasspathVariablesPreferencePage.ID;
282 Map<String, String> options= new HashMap<String, String>();
283 List<CPVariableElement> selected= fVariablesList.getSelectedElements();
284 if (!selected.isEmpty()) {
285 String varName= selected.get(0).getName();
286 options.put(ClasspathVariablesPreferencePage.DATA_SELECT_VARIABLE, varName);
287 }
288 PreferencesUtil.createPreferenceDialogOn(getShell(), id, new String[] { id }, options).open();
289
290 List<CPVariableElement> oldElements= fVariablesList.getElements();
291 initializeElements();
292 List<CPVariableElement> newElements= fVariablesList.getElements();
293 newElements.removeAll(oldElements);
294 if (!newElements.isEmpty()) {
295 fVariablesList.selectElements(new StructuredSelection(newElements));
296 } else
297 fVariablesList.generated_3271391967337621757();
298 }
299
300}