1 11 package org.eclipse.ui.internal.ide.dialogs; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.Set ; 17 import java.util.SortedMap ; 18 import java.util.TreeMap ; 19 20 import org.eclipse.core.filesystem.IFileInfo; 21 import org.eclipse.core.resources.IPathVariableManager; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.ResourcesPlugin; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.core.runtime.Path; 27 import org.eclipse.jface.dialogs.Dialog; 28 import org.eclipse.jface.dialogs.ErrorDialog; 29 import org.eclipse.jface.dialogs.IDialogConstants; 30 import org.eclipse.jface.resource.ImageDescriptor; 31 import org.eclipse.jface.window.Window; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.events.SelectionAdapter; 34 import org.eclipse.swt.events.SelectionEvent; 35 import org.eclipse.swt.graphics.Font; 36 import org.eclipse.swt.graphics.FontMetrics; 37 import org.eclipse.swt.graphics.GC; 38 import org.eclipse.swt.graphics.Image; 39 import org.eclipse.swt.layout.GridData; 40 import org.eclipse.swt.layout.GridLayout; 41 import org.eclipse.swt.widgets.Button; 42 import org.eclipse.swt.widgets.Composite; 43 import org.eclipse.swt.widgets.Control; 44 import org.eclipse.swt.widgets.Event; 45 import org.eclipse.swt.widgets.Label; 46 import org.eclipse.swt.widgets.Listener; 47 import org.eclipse.swt.widgets.Shell; 48 import org.eclipse.swt.widgets.Table; 49 import org.eclipse.swt.widgets.TableItem; 50 import org.eclipse.ui.ISharedImages; 51 import org.eclipse.ui.PlatformUI; 52 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 53 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 54 import org.eclipse.ui.plugin.AbstractUIPlugin; 55 56 62 public class PathVariablesGroup { 63 66 public static class PathVariableElement { 67 70 public String name; 71 72 75 public IPath path; 76 } 77 78 private static final int SIZING_SELECTION_PANE_WIDTH = 400; 80 81 private Shell shell; 83 84 private Label variableLabel; 85 86 private Table variableTable; 87 88 private Button addButton; 89 90 private Button editButton; 91 92 private Button removeButton; 93 94 private FontMetrics fontMetrics; 96 97 private boolean multiSelect; 99 100 private int variableType; 102 103 private Listener selectionListener; 105 106 private SortedMap tempPathVariables; 108 109 private Set removedVariableNames; 111 112 private IPathVariableManager pathVariableManager; 114 115 private final Image FILE_IMG = PlatformUI.getWorkbench().getSharedImages() 117 .getImage(ISharedImages.IMG_OBJ_FILE); 118 119 private final Image FOLDER_IMG = PlatformUI.getWorkbench() 121 .getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER); 122 123 private Image imageUnkown; 125 126 134 public PathVariablesGroup(boolean multiSelect, int variableType) { 135 this.multiSelect = multiSelect; 136 this.variableType = variableType; 137 pathVariableManager = ResourcesPlugin.getWorkspace() 138 .getPathVariableManager(); 139 removedVariableNames = new HashSet (); 140 tempPathVariables = new TreeMap (); 141 initTemporaryState(); 143 } 144 145 155 public PathVariablesGroup(boolean multiSelect, int variableType, 156 Listener selectionListener) { 157 this(multiSelect, variableType); 158 this.selectionListener = selectionListener; 159 } 160 161 164 private void addNewVariable() { 165 PathVariableDialog dialog = new PathVariableDialog(shell, 167 PathVariableDialog.NEW_VARIABLE, variableType, 168 pathVariableManager, tempPathVariables.keySet()); 169 170 if (dialog.open() == Window.CANCEL) { 172 return; 173 } 174 175 String newVariableName = dialog.getVariableName(); 178 IPath newVariableValue = new Path(dialog.getVariableValue()); 179 tempPathVariables.put(newVariableName, newVariableValue); 180 181 updateWidgetState(newVariableName); 183 } 184 185 193 public Control createContents(Composite parent) { 194 Font font = parent.getFont(); 195 196 if (imageUnkown == null) { 197 ImageDescriptor descriptor = AbstractUIPlugin 198 .imageDescriptorFromPlugin( 199 IDEWorkbenchPlugin.IDE_WORKBENCH, 200 "$nl$/icons/full/obj16/warning.gif"); imageUnkown = descriptor.createImage(); 202 } 203 initializeDialogUnits(parent); 204 shell = parent.getShell(); 205 206 Composite pageComponent = new Composite(parent, SWT.NULL); 208 GridLayout layout = new GridLayout(); 209 layout.numColumns = 2; 210 layout.marginWidth = 0; 211 layout.marginHeight = 0; 212 pageComponent.setLayout(layout); 213 GridData data = new GridData(GridData.FILL_BOTH); 214 data.widthHint = SIZING_SELECTION_PANE_WIDTH; 215 pageComponent.setLayoutData(data); 216 pageComponent.setFont(font); 217 218 variableLabel = new Label(pageComponent, SWT.LEFT); 220 variableLabel.setText(IDEWorkbenchMessages.PathVariablesBlock_variablesLabel); 221 data = new GridData(); 222 data.horizontalAlignment = GridData.FILL; 223 data.horizontalSpan = 2; 224 variableLabel.setLayoutData(data); 225 variableLabel.setFont(font); 226 227 int tableStyle = SWT.BORDER | SWT.FULL_SELECTION; 228 if (multiSelect) { 229 tableStyle |= SWT.MULTI; 230 } 231 variableTable = new Table(pageComponent, tableStyle); 232 variableTable.addSelectionListener(new SelectionAdapter() { 233 public void widgetSelected(SelectionEvent e) { 234 updateEnabledState(); 235 if (selectionListener != null) { 236 selectionListener.handleEvent(new Event()); 237 } 238 } 239 }); 240 data = new GridData(GridData.FILL_BOTH); 241 data.heightHint = variableTable.getItemHeight() * 7; 242 variableTable.setLayoutData(data); 243 variableTable.setFont(font); 244 245 createButtonGroup(pageComponent); 246 updateWidgetState(null); 248 249 return pageComponent; 250 } 251 252 255 public void dispose() { 256 if (imageUnkown != null) { 257 imageUnkown.dispose(); 258 imageUnkown = null; 259 } 260 } 261 262 267 private void editSelectedVariable() { 268 TableItem item = variableTable.getItem(variableTable 270 .getSelectionIndex()); 271 String variableName = (String ) item.getData(); 272 IPath variableValue = (IPath) tempPathVariables.get(variableName); 273 274 PathVariableDialog dialog = new PathVariableDialog(shell, 276 PathVariableDialog.EXISTING_VARIABLE, variableType, 277 pathVariableManager, tempPathVariables.keySet()); 278 dialog.setVariableName(variableName); 279 dialog.setVariableValue(variableValue.toOSString()); 280 281 if (dialog.open() == Window.CANCEL) { 283 return; 284 } 285 286 removedVariableNames.add(variableName); 288 tempPathVariables.remove(variableName); 289 290 String newVariableName = dialog.getVariableName(); 291 IPath newVariableValue = new Path(dialog.getVariableValue()); 292 293 tempPathVariables.put(newVariableName, newVariableValue); 295 296 updateWidgetState(newVariableName); 298 299 } 300 301 309 public boolean getEnabled() { 310 if (variableTable != null && !variableTable.isDisposed()) { 311 return variableTable.getEnabled(); 312 } 313 return true; 314 } 315 316 323 public PathVariableElement[] getSelection() { 324 if (variableTable == null) { 325 return new PathVariableElement[0]; 326 } 327 TableItem[] items = variableTable.getSelection(); 328 PathVariableElement[] selection = new PathVariableElement[items.length]; 329 330 for (int i = 0; i < items.length; i++) { 331 String name = (String ) items[i].getData(); 332 selection[i] = new PathVariableElement(); 333 selection[i].name = name; 334 selection[i].path = (IPath) tempPathVariables.get(name); 335 } 336 return selection; 337 } 338 339 344 private void createButtonGroup(Composite parent) { 345 Font font = parent.getFont(); 346 Composite groupComponent = new Composite(parent, SWT.NULL); 347 GridLayout groupLayout = new GridLayout(); 348 groupLayout.marginWidth = 0; 349 groupLayout.marginHeight = 0; 350 groupComponent.setLayout(groupLayout); 351 GridData data = new GridData(); 352 data.verticalAlignment = GridData.FILL; 353 data.horizontalAlignment = GridData.FILL; 354 groupComponent.setLayoutData(data); 355 groupComponent.setFont(font); 356 357 addButton = new Button(groupComponent, SWT.PUSH); 358 addButton.setText(IDEWorkbenchMessages.PathVariablesBlock_addVariableButton); 359 addButton.addSelectionListener(new SelectionAdapter() { 360 public void widgetSelected(SelectionEvent e) { 361 addNewVariable(); 362 } 363 }); 364 addButton.setFont(font); 365 setButtonLayoutData(addButton); 366 367 editButton = new Button(groupComponent, SWT.PUSH); 368 editButton.setText(IDEWorkbenchMessages.PathVariablesBlock_editVariableButton); 369 editButton.addSelectionListener(new SelectionAdapter() { 370 public void widgetSelected(SelectionEvent e) { 371 editSelectedVariable(); 372 } 373 }); 374 editButton.setFont(font); 375 setButtonLayoutData(editButton); 376 377 removeButton = new Button(groupComponent, SWT.PUSH); 378 removeButton.setText(IDEWorkbenchMessages.PathVariablesBlock_removeVariableButton); 379 removeButton.addSelectionListener(new SelectionAdapter() { 380 public void widgetSelected(SelectionEvent e) { 381 removeSelectedVariables(); 382 } 383 }); 384 removeButton.setFont(font); 385 setButtonLayoutData(removeButton); 386 } 387 388 398 protected void initializeDialogUnits(Control control) { 399 GC gc = new GC(control); 401 gc.setFont(control.getFont()); 402 fontMetrics = gc.getFontMetrics(); 403 gc.dispose(); 404 } 405 406 409 private void initTemporaryState() { 410 String [] varNames = pathVariableManager.getPathVariableNames(); 411 412 tempPathVariables.clear(); 413 for (int i = 0; i < varNames.length; i++) { 414 IPath value = pathVariableManager.getValue(varNames[i]); 415 416 if (value != null) { 418 boolean isFile = value.toFile().isFile(); 419 if ((isFile && (variableType & IResource.FILE) != 0) 420 || (isFile == false && (variableType & IResource.FOLDER) != 0)) { 421 422 tempPathVariables.put(varNames[i], value); 423 } 424 } 425 } 426 removedVariableNames.clear(); 427 } 428 429 433 private void updateEnabledState() { 434 int itemsSelectedCount = variableTable.getSelectionCount(); 435 editButton.setEnabled(itemsSelectedCount == 1); 436 removeButton.setEnabled(itemsSelectedCount > 0); 437 } 438 439 450 private void updateVariableTable(String selectedVarName) { 451 variableTable.removeAll(); 452 int selectedVarIndex = 0; 453 for (Iterator varNames = tempPathVariables.keySet().iterator(); varNames 454 .hasNext();) { 455 TableItem item = new TableItem(variableTable, SWT.NONE); 456 String varName = (String ) varNames.next(); 457 IPath value = (IPath) tempPathVariables.get(varName); 458 IFileInfo file = IDEResourceInfoUtils.getFileInfo(value); 459 460 item.setText(varName + " - " + value.toOSString()); item.setData(varName); 463 item.setImage(file.exists() ? (file.isDirectory() ? FOLDER_IMG 464 : FILE_IMG ) : imageUnkown); 465 if (varName.equals(selectedVarName)) { 466 selectedVarIndex = variableTable.getItemCount() - 1; 467 } 468 } 469 if (variableTable.getItemCount() > selectedVarIndex) { 470 variableTable.setSelection(selectedVarIndex); 471 if (selectionListener != null) { 472 selectionListener.handleEvent(new Event()); 473 } 474 } else if (variableTable.getItemCount() == 0 475 && selectionListener != null) { 476 selectionListener.handleEvent(new Event()); 477 } 478 } 479 480 486 public boolean performOk() { 487 try { 488 for (Iterator removed = removedVariableNames.iterator(); removed 490 .hasNext();) { 491 String removedVariableName = (String ) removed.next(); 492 if (!tempPathVariables.containsKey(removedVariableName)) { 494 pathVariableManager.setValue(removedVariableName, null); 495 } 496 } 497 498 for (Iterator current = tempPathVariables.entrySet().iterator(); current 500 .hasNext();) { 501 Map.Entry entry = (Map.Entry ) current.next(); 502 String variableName = (String ) entry.getKey(); 503 IPath variableValue = (IPath) entry.getValue(); 504 pathVariableManager.setValue(variableName, variableValue); 505 } 506 initTemporaryState(); 508 509 return true; 511 } catch (CoreException ce) { 512 ErrorDialog.openError(shell, null, null, ce.getStatus()); 513 } 514 return false; 515 } 516 517 520 private void removeSelectedVariables() { 521 int[] selectedIndices = variableTable.getSelectionIndices(); 523 for (int i = 0; i < selectedIndices.length; i++) { 524 TableItem selectedItem = variableTable.getItem(selectedIndices[i]); 525 String varName = (String ) selectedItem.getData(); 526 removedVariableNames.add(varName); 527 tempPathVariables.remove(varName); 528 } 529 updateWidgetState(null); 530 } 531 532 541 private GridData setButtonLayoutData(Button button) { 542 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 543 int widthHint = Dialog.convertHorizontalDLUsToPixels(fontMetrics, 544 IDialogConstants.BUTTON_WIDTH); 545 data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT, 546 SWT.DEFAULT, true).x); 547 button.setLayoutData(data); 548 return data; 549 } 550 551 557 public void setEnabled(boolean enabled) { 558 if (variableTable != null && !variableTable.isDisposed()) { 559 variableLabel.setEnabled(enabled); 560 variableTable.setEnabled(enabled); 561 addButton.setEnabled(enabled); 562 if (enabled) { 563 updateEnabledState(); 564 } else { 565 editButton.setEnabled(enabled); 566 removeButton.setEnabled(enabled); 567 } 568 } 569 } 570 571 579 private void updateWidgetState(String selectedVarName) { 580 updateVariableTable(selectedVarName); 581 updateEnabledState(); 582 } 583 } 584 | Popular Tags |