1 13 14 package org.eclipse.ui.dialogs; 15 16 import java.util.ArrayList ; 17 18 import org.eclipse.jface.dialogs.Dialog; 19 import org.eclipse.jface.dialogs.IDialogConstants; 20 import org.eclipse.jface.dialogs.IDialogSettings; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.graphics.Cursor; 23 import org.eclipse.swt.graphics.Font; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.layout.GridLayout; 27 import org.eclipse.swt.widgets.Button; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Event; 31 import org.eclipse.swt.widgets.FileDialog; 32 import org.eclipse.swt.widgets.Label; 33 import org.eclipse.swt.widgets.Listener; 34 import org.eclipse.swt.widgets.Shell; 35 import org.eclipse.swt.widgets.Table; 36 import org.eclipse.swt.widgets.TableItem; 37 import org.eclipse.ui.IEditorDescriptor; 38 import org.eclipse.ui.PlatformUI; 39 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 40 import org.eclipse.ui.internal.WorkbenchMessages; 41 import org.eclipse.ui.internal.WorkbenchPlugin; 42 import org.eclipse.ui.internal.registry.EditorDescriptor; 43 import org.eclipse.ui.internal.registry.EditorRegistry; 44 45 51 52 public final class EditorSelectionDialog extends Dialog { 53 private EditorDescriptor selectedEditor; 54 55 private Button externalButton; 56 57 private Table editorTable; 58 59 private Button browseExternalEditorsButton; 60 61 private Button internalButton; 62 63 private Button okButton; 64 65 private static final String STORE_ID_INTERNAL_EXTERNAL = "EditorSelectionDialog.STORE_ID_INTERNAL_EXTERNAL"; 67 private String message = WorkbenchMessages.EditorSelection_chooseAnEditor; 68 69 private IEditorDescriptor[] externalEditors; 71 72 private IEditorDescriptor[] internalEditors; 73 74 private Image[] externalEditorImages; 75 76 private Image[] internalEditorImages; 77 78 private IEditorDescriptor[] editorsToFilter; 79 80 private DialogListener listener = new DialogListener(); 81 82 private static final String [] Executable_Filters; 83 84 private static final int TABLE_WIDTH = 200; 85 static { 86 if (SWT.getPlatform().equals("win32")) { Executable_Filters = new String [] { "*.exe", "*.bat", "*.*" }; } else { 89 Executable_Filters = new String [] { "*" }; } 91 } 92 93 99 public EditorSelectionDialog(Shell parentShell) { 100 super(parentShell); 101 } 102 103 106 protected void buttonPressed(int buttonId) { 107 if (buttonId == IDialogConstants.OK_ID) { 108 saveWidgetValues(); 109 } 110 super.buttonPressed(buttonId); 111 } 112 113 116 public boolean close() { 117 if (internalEditorImages != null) { 118 for (int i = 0; i < internalEditorImages.length; i++) { 119 internalEditorImages[i].dispose(); 120 } 121 internalEditorImages = null; 122 } 123 if (externalEditorImages != null) { 124 for (int i = 0; i < externalEditorImages.length; i++) { 125 externalEditorImages[i].dispose(); 126 } 127 externalEditorImages = null; 128 } 129 return super.close(); 130 } 131 132 135 protected void configureShell(Shell shell) { 136 super.configureShell(shell); 137 shell.setText(WorkbenchMessages.EditorSelection_title); 138 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 139 IWorkbenchHelpContextIds.EDITOR_SELECTION_DIALOG); 140 } 141 142 152 protected Control createDialogArea(Composite parent) { 153 Font font = parent.getFont(); 154 Composite contents = (Composite) super.createDialogArea(parent); 156 ((GridLayout) contents.getLayout()).numColumns = 2; 157 158 Label textLabel = new Label(contents, SWT.NONE); 160 textLabel.setText(message); 161 GridData data = new GridData(); 162 data.horizontalSpan = 2; 163 textLabel.setLayoutData(data); 164 textLabel.setFont(font); 165 166 internalButton = new Button(contents, SWT.RADIO | SWT.LEFT); 167 internalButton.setText(WorkbenchMessages.EditorSelection_internal); 168 internalButton.addListener(SWT.Selection, listener); 169 data = new GridData(); 170 data.horizontalSpan = 1; 171 internalButton.setLayoutData(data); 172 internalButton.setFont(font); 173 174 externalButton = new Button(contents, SWT.RADIO | SWT.LEFT); 175 externalButton.setText(WorkbenchMessages.EditorSelection_external); 176 externalButton.addListener(SWT.Selection, listener); 177 data = new GridData(); 178 data.horizontalSpan = 1; 179 externalButton.setLayoutData(data); 180 externalButton.setFont(font); 181 182 editorTable = new Table(contents, SWT.SINGLE | SWT.BORDER); 183 editorTable.addListener(SWT.Selection, listener); 184 editorTable.addListener(SWT.DefaultSelection, listener); 185 editorTable.addListener(SWT.MouseDoubleClick, listener); 186 data = new GridData(); 187 data.widthHint = convertHorizontalDLUsToPixels(TABLE_WIDTH); 188 data.horizontalAlignment = GridData.FILL; 189 data.grabExcessHorizontalSpace = true; 190 data.verticalAlignment = GridData.FILL; 191 data.grabExcessVerticalSpace = true; 192 data.horizontalSpan = 2; 193 editorTable.setLayoutData(data); 194 editorTable.setFont(font); 195 data.heightHint = editorTable.getItemHeight() * 12; 196 197 browseExternalEditorsButton = new Button(contents, SWT.PUSH); 198 browseExternalEditorsButton 199 .setText(WorkbenchMessages.EditorSelection_browse); 200 browseExternalEditorsButton.addListener(SWT.Selection, listener); 201 data = new GridData(); 202 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 203 data.widthHint = Math.max(widthHint, browseExternalEditorsButton 204 .computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 205 browseExternalEditorsButton.setLayoutData(data); 206 browseExternalEditorsButton.setFont(font); 207 208 restoreWidgetValues(); 210 fillEditorTable(); 211 212 updateEnableState(); 213 214 return contents; 215 } 216 217 protected void fillEditorTable() { 218 editorTable.removeAll(); 219 editorTable.update(); 220 IEditorDescriptor[] editors; 221 Image[] images; 222 if (internalButton.getSelection()) { 223 editors = getInternalEditors(); 224 images = internalEditorImages; 225 } else { 226 editors = getExternalEditors(); 227 images = externalEditorImages; 228 } 229 230 editorTable.setRedraw(false); 232 for (int i = 0; i < editors.length; i++) { 233 TableItem item = new TableItem(editorTable, SWT.NULL); 234 item.setData(editors[i]); 235 item.setText(editors[i].getLabel()); 236 item.setImage(images[i]); 237 } 238 editorTable.setRedraw(true); 239 } 240 241 244 245 protected IDialogSettings getDialogSettings() { 246 IDialogSettings workbenchSettings = WorkbenchPlugin.getDefault() 247 .getDialogSettings(); 248 IDialogSettings section = workbenchSettings 249 .getSection("EditorSelectionDialog"); if (section == null) { 251 section = workbenchSettings.addNewSection("EditorSelectionDialog"); } 253 return section; 254 } 255 256 259 protected IEditorDescriptor[] getExternalEditors() { 260 if (externalEditors == null) { 261 Control shell = getShell(); 265 if (!shell.isVisible()) { 266 Control topShell = shell.getParent(); 267 if (topShell != null) { 268 shell = topShell; 269 } 270 } 271 Cursor busy = new Cursor(shell.getDisplay(), SWT.CURSOR_WAIT); 272 shell.setCursor(busy); 273 EditorRegistry reg = (EditorRegistry) WorkbenchPlugin.getDefault() 275 .getEditorRegistry(); 276 externalEditors = reg.getSortedEditorsFromOS(); 277 externalEditors = filterEditors(externalEditors); 278 externalEditorImages = getImages(externalEditors); 279 shell.setCursor(null); 281 busy.dispose(); 282 } 283 return externalEditors; 284 } 285 286 294 protected IEditorDescriptor[] filterEditors(IEditorDescriptor[] editors) { 295 if ((editors == null) || (editors.length < 1)) { 296 return editors; 297 } 298 299 if ((editorsToFilter == null) || (editorsToFilter.length < 1)) { 300 return editors; 301 } 302 303 ArrayList filteredList = new ArrayList (); 304 for (int i = 0; i < editors.length; i++) { 305 boolean add = true; 306 for (int j = 0; j < editorsToFilter.length; j++) { 307 if (editors[i].getId().equals(editorsToFilter[j].getId())) { 308 add = false; 309 } 310 } 311 if (add) { 312 filteredList.add(editors[i]); 313 } 314 } 315 316 return (IEditorDescriptor[]) filteredList 317 .toArray(new IEditorDescriptor[filteredList.size()]); 318 } 319 320 323 protected Image[] getImages(IEditorDescriptor[] editors) { 324 Image[] images = new Image[editors.length]; 325 for (int i = 0; i < editors.length; i++) { 326 images[i] = editors[i].getImageDescriptor().createImage(); 327 } 328 return images; 329 } 330 331 334 protected IEditorDescriptor[] getInternalEditors() { 335 if (internalEditors == null) { 336 EditorRegistry reg = (EditorRegistry) WorkbenchPlugin.getDefault() 337 .getEditorRegistry(); 338 internalEditors = reg.getSortedEditorsFromPlugins(); 339 internalEditors = filterEditors(internalEditors); 340 internalEditorImages = getImages(internalEditors); 341 } 342 return internalEditors; 343 } 344 345 350 public IEditorDescriptor getSelectedEditor() { 351 return selectedEditor; 352 } 353 354 protected void promptForExternalEditor() { 355 FileDialog dialog = new FileDialog(getShell(), SWT.OPEN 356 | SWT.PRIMARY_MODAL); 357 dialog.setFilterExtensions(Executable_Filters); 358 String result = dialog.open(); 359 if (result != null) { 360 EditorDescriptor editor = EditorDescriptor.createForProgram(result); 361 TableItem ti = new TableItem(editorTable, SWT.NULL); 363 ti.setData(editor); 364 ti.setText(editor.getLabel()); 365 Image image = editor.getImageDescriptor().createImage(); 366 ti.setImage(image); 367 368 editorTable.setSelection(new TableItem[] { ti }); 371 editorTable.showSelection(); 372 editorTable.setFocus(); 373 selectedEditor = editor; 374 375 379 IEditorDescriptor[] newEditors = new IEditorDescriptor[externalEditors.length + 1]; 380 System.arraycopy(externalEditors, 0, newEditors, 0, 381 externalEditors.length); 382 newEditors[newEditors.length - 1] = editor; 383 externalEditors = newEditors; 384 385 Image[] newImages = new Image[externalEditorImages.length + 1]; 386 System.arraycopy(externalEditorImages, 0, newImages, 0, 387 externalEditorImages.length); 388 newImages[newImages.length - 1] = image; 389 externalEditorImages = newImages; 390 } 391 } 392 393 396 protected void handleDoubleClickEvent() { 397 buttonPressed(IDialogConstants.OK_ID); 398 } 399 400 404 protected void restoreWidgetValues() { 405 IDialogSettings settings = getDialogSettings(); 406 boolean wasExternal = settings.getBoolean(STORE_ID_INTERNAL_EXTERNAL); 407 internalButton.setSelection(!wasExternal); 408 externalButton.setSelection(wasExternal); 409 } 410 411 415 protected void saveWidgetValues() { 416 IDialogSettings settings = getDialogSettings(); 417 settings 419 .put(STORE_ID_INTERNAL_EXTERNAL, !internalButton.getSelection()); 420 } 421 422 428 public void setMessage(String aMessage) { 429 message = aMessage; 430 } 431 432 438 public void setEditorsToFilter(IEditorDescriptor[] editors) { 439 editorsToFilter = editors; 440 } 441 442 445 protected void updateEnableState() { 446 boolean enableExternal = externalButton.getSelection(); 447 browseExternalEditorsButton.setEnabled(enableExternal); 448 updateOkButton(); 449 } 450 451 protected void createButtonsForButtonBar(Composite parent) { 452 okButton = createButton(parent, IDialogConstants.OK_ID, 453 IDialogConstants.OK_LABEL, true); 454 createButton(parent, IDialogConstants.CANCEL_ID, 455 IDialogConstants.CANCEL_LABEL, false); 456 okButton.setEnabled(false); 458 459 } 460 461 464 protected void updateOkButton() { 465 if (okButton == null) { 467 return; 468 } 469 if (editorTable.getSelectionCount() == 0) { 471 okButton.setEnabled(false); 472 return; 473 } 474 okButton.setEnabled(selectedEditor != null); 476 } 477 478 private class DialogListener implements Listener { 479 480 485 public void handleEvent(Event event) { 486 if (event.type == SWT.MouseDoubleClick) { 487 handleDoubleClickEvent(); 488 return; 489 } 490 if (event.widget == externalButton) { 491 fillEditorTable(); 492 } else if (event.widget == browseExternalEditorsButton) { 493 promptForExternalEditor(); 494 } else if (event.widget == editorTable) { 495 if (editorTable.getSelectionIndex() != -1) { 496 selectedEditor = (EditorDescriptor) editorTable 497 .getSelection()[0].getData(); 498 } else { 499 selectedEditor = null; 500 okButton.setEnabled(false); 501 } 502 } 503 updateEnableState(); 504 } 505 506 } 507 } 508 | Popular Tags |