1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Path; 23 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.custom.CLabel; 26 import org.eclipse.swt.layout.GridData; 27 import org.eclipse.swt.layout.GridLayout; 28 import org.eclipse.swt.widgets.Button; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.swt.widgets.Control; 31 import org.eclipse.swt.widgets.Shell; 32 33 import org.eclipse.jface.dialogs.Dialog; 34 import org.eclipse.jface.dialogs.IDialogConstants; 35 import org.eclipse.jface.dialogs.IDialogSettings; 36 import org.eclipse.jface.dialogs.StatusDialog; 37 import org.eclipse.jface.resource.JFaceResources; 38 import org.eclipse.jface.viewers.StructuredSelection; 39 import org.eclipse.jface.viewers.Viewer; 40 import org.eclipse.jface.viewers.ViewerComparator; 41 import org.eclipse.jface.window.Window; 42 43 import org.eclipse.ui.PlatformUI; 44 import org.eclipse.ui.dialogs.PreferencesUtil; 45 46 import org.eclipse.jdt.core.JavaCore; 47 48 import org.eclipse.jdt.internal.corext.util.Messages; 49 50 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 51 import org.eclipse.jdt.internal.ui.JavaPlugin; 52 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 53 import org.eclipse.jdt.internal.ui.preferences.ClasspathVariablesPreferencePage; 54 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 55 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 56 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 57 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter; 58 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 59 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField; 60 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 61 62 public class NewVariableEntryDialog extends StatusDialog { 63 64 private class VariablesAdapter implements IDialogFieldListener, IListAdapter { 65 66 68 public void customButtonPressed(ListDialogField field, int index) { 69 switch (index) { 70 case IDX_EXTEND: 71 extendButtonPressed(); 72 break; 73 } 74 } 75 76 public void selectionChanged(ListDialogField field) { 77 doSelectionChanged(); 78 } 79 80 public void doubleClicked(ListDialogField field) { 81 doDoubleClick(); 82 } 83 84 86 public void dialogFieldChanged(DialogField field) { 87 if (field == fConfigButton) { 88 configButtonPressed(); 89 } 90 91 } 92 93 } 94 95 private final int IDX_EXTEND= 0; 96 97 private ListDialogField fVariablesList; 98 private boolean fCanExtend; 99 private boolean fIsValidSelection; 100 101 private IPath[] fResultPaths; 102 103 private SelectionButtonDialogField fConfigButton; 104 105 private CLabel fWarning; 106 107 public NewVariableEntryDialog(Shell parent) { 108 super(parent); 109 setTitle(NewWizardMessages.NewVariableEntryDialog_title); 110 111 int shellStyle= getShellStyle(); 112 setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE); 113 updateStatus(new StatusInfo(IStatus.ERROR, "")); 115 String [] buttonLabels= new String [] { 116 NewWizardMessages.NewVariableEntryDialog_vars_extend, 117 }; 118 119 VariablesAdapter adapter= new VariablesAdapter(); 120 121 CPVariableElementLabelProvider labelProvider= new CPVariableElementLabelProvider(false); 122 123 fVariablesList= new ListDialogField(adapter, buttonLabels, labelProvider); 124 fVariablesList.setDialogFieldListener(adapter); 125 fVariablesList.setLabelText(NewWizardMessages.NewVariableEntryDialog_vars_label); 126 127 fVariablesList.enableButton(IDX_EXTEND, false); 128 129 fVariablesList.setViewerComparator(new ViewerComparator() { 130 public int compare(Viewer viewer, Object e1, Object e2) { 131 if (e1 instanceof CPVariableElement && e2 instanceof CPVariableElement) { 132 return getComparator().compare(((CPVariableElement)e1).getName(), ((CPVariableElement)e2).getName()); 133 } 134 return super.compare(viewer, e1, e2); 135 } 136 }); 137 138 139 fConfigButton= new SelectionButtonDialogField(SWT.PUSH); 140 fConfigButton.setLabelText(NewWizardMessages.NewVariableEntryDialog_configbutton_label); 141 fConfigButton.setDialogFieldListener(adapter); 142 143 initializeElements(); 144 145 fCanExtend= false; 146 fIsValidSelection= false; 147 fResultPaths= null; 148 149 fVariablesList.selectFirstElement(); 150 } 151 152 private void initializeElements() { 153 String [] entries= JavaCore.getClasspathVariableNames(); 154 ArrayList elements= new ArrayList (entries.length); 155 for (int i= 0; i < entries.length; i++) { 156 String name= entries[i]; 157 IPath entryPath= JavaCore.getClasspathVariable(name); 158 if (entryPath != null) { 159 elements.add(new CPVariableElement(name, entryPath)); 160 } 161 } 162 163 fVariablesList.setElements(elements); 164 } 165 166 167 170 protected void configureShell(Shell shell) { 171 super.configureShell(shell); 172 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IJavaHelpContextIds.NEW_VARIABLE_ENTRY_DIALOG); 173 } 174 175 178 protected IDialogSettings getDialogBoundsSettings() { 179 return JavaPlugin.getDefault().getDialogSettingsSection(getClass().getName()); 180 } 181 182 185 protected Control createDialogArea(Composite parent) { 186 initializeDialogUnits(parent); 187 188 Composite composite= (Composite) super.createDialogArea(parent); 189 GridLayout layout= (GridLayout) composite.getLayout(); 190 layout.numColumns= 2; 191 192 fVariablesList.doFillIntoGrid(composite, 3); 193 194 LayoutUtil.setHorizontalSpan(fVariablesList.getLabelControl(null), 2); 195 196 GridData listData= (GridData) fVariablesList.getListControl(null).getLayoutData(); 197 listData.grabExcessHorizontalSpace= true; 198 listData.heightHint= convertHeightInCharsToPixels(10); 199 listData.widthHint= convertWidthInCharsToPixels(70); 200 201 fWarning= new CLabel(composite, SWT.NONE); 202 fWarning.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, fVariablesList.getNumberOfControls() - 1, 1)); 203 204 Composite lowerComposite= new Composite(composite, SWT.NONE); 205 lowerComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 206 207 layout= new GridLayout(); 208 layout.marginHeight= 0; 209 layout.marginWidth= 0; 210 lowerComposite.setLayout(layout); 211 212 fConfigButton.doFillIntoGrid(lowerComposite, 1); 213 214 applyDialogFont(composite); 215 return composite; 216 } 217 218 public IPath[] getResult() { 219 return fResultPaths; 220 } 221 222 225 private void doDoubleClick() { 226 if (fIsValidSelection) { 227 okPressed(); 228 } else if (fCanExtend) { 229 extendButtonPressed(); 230 } 231 } 232 233 private void doSelectionChanged() { 234 boolean isValidSelection= true; 235 boolean canExtend= false; 236 StatusInfo status= new StatusInfo(); 237 238 List selected= fVariablesList.getSelectedElements(); 239 int nSelected= selected.size(); 240 241 if (nSelected > 0) { 242 fResultPaths= new Path[nSelected]; 243 for (int i= 0; i < nSelected; i++) { 244 CPVariableElement curr= (CPVariableElement) selected.get(i); 245 fResultPaths[i]= new Path(curr.getName()); 246 File file= curr.getPath().toFile(); 247 if (!file.exists()) { 248 status.setError(NewWizardMessages.NewVariableEntryDialog_info_notexists); 249 isValidSelection= false; 250 break; 251 } 252 if (file.isDirectory()) { 253 status.setError(NewWizardMessages.NewVariableEntryDialog_info_isfolder); 254 canExtend= true; 255 isValidSelection= false; 256 break; 257 } 258 } 259 } else { 260 isValidSelection= false; 261 status.setInfo(NewWizardMessages.NewVariableEntryDialog_info_noselection); 262 } 263 if (isValidSelection && nSelected > 1) { 264 String str= Messages.format(NewWizardMessages.NewVariableEntryDialog_info_selected, String.valueOf(nSelected)); 265 status.setInfo(str); 266 } 267 fCanExtend= nSelected == 1 && canExtend; 268 fVariablesList.enableButton(0, fCanExtend); 269 270 updateStatus(status); 271 fIsValidSelection= isValidSelection; 272 Button okButton= getButton(IDialogConstants.OK_ID); 273 if (okButton != null && !okButton.isDisposed()) { 274 okButton.setEnabled(isValidSelection); 275 } 276 updateDeprecationWarning(); 277 } 278 279 private void updateDeprecationWarning() { 280 if (fWarning == null || fWarning.isDisposed()) 281 return; 282 283 for (Iterator iter= fVariablesList.getSelectedElements().iterator(); iter.hasNext();) { 284 CPVariableElement element= (CPVariableElement) iter.next(); 285 String deprecationMessage= element.getDeprecationMessage(); 286 if (deprecationMessage != null) { 287 fWarning.setText(deprecationMessage); 288 fWarning.setImage(JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING)); 289 return; 290 } 291 } 292 fWarning.setText(null); 293 fWarning.setImage(null); 294 } 295 296 private IPath[] chooseExtensions(CPVariableElement elem) { 297 File file= elem.getPath().toFile(); 298 299 JARFileSelectionDialog dialog= new JARFileSelectionDialog(getShell(), true, false); 300 dialog.setTitle(NewWizardMessages.NewVariableEntryDialog_ExtensionDialog_title); 301 dialog.setMessage(Messages.format(NewWizardMessages.NewVariableEntryDialog_ExtensionDialog_description, elem.getName())); 302 dialog.setInput(file); 303 if (dialog.open() == Window.OK) { 304 Object [] selected= dialog.getResult(); 305 IPath[] paths= new IPath[selected.length]; 306 for (int i= 0; i < selected.length; i++) { 307 IPath filePath= Path.fromOSString(((File ) selected[i]).getPath()); 308 IPath resPath= new Path(elem.getName()); 309 for (int k= elem.getPath().segmentCount(); k < filePath.segmentCount(); k++) { 310 resPath= resPath.append(filePath.segment(k)); 311 } 312 paths[i]= resPath; 313 } 314 return paths; 315 } 316 return null; 317 } 318 319 protected final void extendButtonPressed() { 320 List selected= fVariablesList.getSelectedElements(); 321 if (selected.size() == 1) { 322 IPath[] extendedPaths= chooseExtensions((CPVariableElement) selected.get(0)); 323 if (extendedPaths != null) { 324 fResultPaths= extendedPaths; 325 super.buttonPressed(IDialogConstants.OK_ID); 326 } 327 } 328 } 329 330 protected final void configButtonPressed() { 331 String id= ClasspathVariablesPreferencePage.ID; 332 Map options= new HashMap (); 333 List selected= fVariablesList.getSelectedElements(); 334 if (!selected.isEmpty()) { 335 String varName= ((CPVariableElement) selected.get(0)).getName(); 336 options.put(ClasspathVariablesPreferencePage.DATA_SELECT_VARIABLE, varName); 337 } 338 PreferencesUtil.createPreferenceDialogOn(getShell(), id, new String [] { id }, options).open(); 339 340 List oldElements= fVariablesList.getElements(); 341 initializeElements(); 342 List newElements= fVariablesList.getElements(); 343 newElements.removeAll(oldElements); 344 if (!newElements.isEmpty()) { 345 fVariablesList.selectElements(new StructuredSelection(newElements)); 346 } else if (fVariablesList.getSelectedElements().isEmpty()) { 347 fVariablesList.selectFirstElement(); 348 } 349 } 350 351 } 352 | Popular Tags |