1 11 12 package org.eclipse.ui.internal.dialogs; 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.jface.dialogs.Dialog; 17 import org.eclipse.jface.dialogs.IDialogConstants; 18 import org.eclipse.jface.dialogs.IDialogSettings; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.graphics.Cursor; 21 import org.eclipse.swt.graphics.Font; 22 import org.eclipse.swt.graphics.Image; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.Button; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Control; 28 import org.eclipse.swt.widgets.Event; 29 import org.eclipse.swt.widgets.FileDialog; 30 import org.eclipse.swt.widgets.Label; 31 import org.eclipse.swt.widgets.Listener; 32 import org.eclipse.swt.widgets.Shell; 33 import org.eclipse.swt.widgets.Table; 34 import org.eclipse.swt.widgets.TableItem; 35 import org.eclipse.ui.IEditorDescriptor; 36 import org.eclipse.ui.PlatformUI; 37 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 38 import org.eclipse.ui.internal.WorkbenchMessages; 39 import org.eclipse.ui.internal.WorkbenchPlugin; 40 import org.eclipse.ui.internal.registry.EditorDescriptor; 41 import org.eclipse.ui.internal.registry.EditorRegistry; 42 43 50 51 public class EditorSelectionDialog extends Dialog implements Listener { 52 private EditorDescriptor selectedEditor; 53 54 private Button externalButton; 55 56 private Table editorTable; 57 58 private Button browseExternalEditorsButton; 59 60 private Button internalButton; 61 62 private Button okButton; 63 64 private static final String STORE_ID_INTERNAL_EXTERNAL = "EditorSelectionDialog.STORE_ID_INTERNAL_EXTERNAL"; 66 private String message = WorkbenchMessages.EditorSelection_chooseAnEditor; 67 68 private IEditorDescriptor[] externalEditors; 70 71 private IEditorDescriptor[] internalEditors; 72 73 private Image[] externalEditorImages; 74 75 private Image[] internalEditorImages; 76 77 private IEditorDescriptor[] editorsToFilter; 78 79 private static final String [] Executable_Filters; 80 81 private static final int TABLE_WIDTH = 200; 82 static { 83 if (SWT.getPlatform().equals("win32")) { Executable_Filters = new String [] { "*.exe", "*.bat", "*.*" }; } else { 86 Executable_Filters = new String [] { "*" }; } 88 } 89 90 95 public EditorSelectionDialog(Shell parentShell) { 96 super(parentShell); 97 } 98 99 102 protected void buttonPressed(int buttonId) { 103 if (buttonId == IDialogConstants.OK_ID) { 104 saveWidgetValues(); 105 } 106 super.buttonPressed(buttonId); 107 } 108 109 112 public boolean close() { 113 if (internalEditorImages != null) { 114 for (int i = 0; i < internalEditorImages.length; i++) { 115 internalEditorImages[i].dispose(); 116 } 117 internalEditorImages = null; 118 } 119 if (externalEditorImages != null) { 120 for (int i = 0; i < externalEditorImages.length; i++) { 121 externalEditorImages[i].dispose(); 122 } 123 externalEditorImages = null; 124 } 125 return super.close(); 126 } 127 128 131 protected void configureShell(Shell shell) { 132 super.configureShell(shell); 133 shell.setText(WorkbenchMessages.EditorSelection_title); 134 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 135 IWorkbenchHelpContextIds.EDITOR_SELECTION_DIALOG); 136 } 137 138 147 protected Control createDialogArea(Composite parent) { 148 Font font = parent.getFont(); 149 Composite contents = (Composite) super.createDialogArea(parent); 151 ((GridLayout) contents.getLayout()).numColumns = 2; 152 153 Label textLabel = new Label(contents, SWT.NONE); 155 textLabel.setText(message); 156 GridData data = new GridData(); 157 data.horizontalSpan = 2; 158 textLabel.setLayoutData(data); 159 textLabel.setFont(font); 160 161 internalButton = new Button(contents, SWT.RADIO | SWT.LEFT); 162 internalButton.setText(WorkbenchMessages.EditorSelection_internal); 163 internalButton.addListener(SWT.Selection, this); 164 data = new GridData(); 165 data.horizontalSpan = 1; 166 internalButton.setLayoutData(data); 167 internalButton.setFont(font); 168 169 externalButton = new Button(contents, SWT.RADIO | SWT.LEFT); 170 externalButton.setText(WorkbenchMessages.EditorSelection_external); 171 externalButton.addListener(SWT.Selection, this); 172 data = new GridData(); 173 data.horizontalSpan = 1; 174 externalButton.setLayoutData(data); 175 externalButton.setFont(font); 176 177 editorTable = new Table(contents, SWT.SINGLE | SWT.BORDER); 178 editorTable.addListener(SWT.Selection, this); 179 editorTable.addListener(SWT.DefaultSelection, this); 180 editorTable.addListener(SWT.MouseDoubleClick, this); 181 data = new GridData(); 182 data.widthHint = convertHorizontalDLUsToPixels(TABLE_WIDTH); 183 data.horizontalAlignment = GridData.FILL; 184 data.grabExcessHorizontalSpace = true; 185 data.verticalAlignment = GridData.FILL; 186 data.grabExcessVerticalSpace = true; 187 data.horizontalSpan = 2; 188 editorTable.setLayoutData(data); 189 editorTable.setFont(font); 190 data.heightHint = editorTable.getItemHeight() * 12; 191 192 browseExternalEditorsButton = new Button(contents, SWT.PUSH); 193 browseExternalEditorsButton.setText(WorkbenchMessages.EditorSelection_browse); 194 browseExternalEditorsButton.addListener(SWT.Selection, this); 195 data = new GridData(); 196 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 197 data.widthHint = Math.max(widthHint, browseExternalEditorsButton 198 .computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 199 browseExternalEditorsButton.setLayoutData(data); 200 browseExternalEditorsButton.setFont(font); 201 202 restoreWidgetValues(); 204 fillEditorTable(); 205 206 updateEnableState(); 207 208 return contents; 209 } 210 211 protected void fillEditorTable() { 212 editorTable.removeAll(); 213 editorTable.update(); 214 IEditorDescriptor[] editors; 215 Image[] images; 216 if (internalButton.getSelection()) { 217 editors = getInternalEditors(); 218 images = internalEditorImages; 219 } else { 220 editors = getExternalEditors(); 221 images = externalEditorImages; 222 } 223 224 editorTable.setRedraw(false); 226 for (int i = 0; i < editors.length; i++) { 227 TableItem item = new TableItem(editorTable, SWT.NULL); 228 item.setData(editors[i]); 229 item.setText(editors[i].getLabel()); 230 item.setImage(images[i]); 231 } 232 editorTable.setRedraw(true); 233 } 234 235 238 239 protected IDialogSettings getDialogSettings() { 240 IDialogSettings workbenchSettings = WorkbenchPlugin.getDefault() 241 .getDialogSettings(); 242 IDialogSettings section = workbenchSettings 243 .getSection("EditorSelectionDialog"); if (section == null) { 245 section = workbenchSettings.addNewSection("EditorSelectionDialog"); } 247 return section; 248 } 249 250 253 protected IEditorDescriptor[] getExternalEditors() { 254 if (externalEditors == null) { 255 Control shell = getShell(); 259 if (!shell.isVisible()) { 260 Control topShell = shell.getParent(); 261 if (topShell != null) { 262 shell = topShell; 263 } 264 } 265 Cursor busy = new Cursor(shell.getDisplay(), SWT.CURSOR_WAIT); 266 shell.setCursor(busy); 267 EditorRegistry reg = (EditorRegistry) WorkbenchPlugin.getDefault() 269 .getEditorRegistry(); 270 externalEditors = reg.getSortedEditorsFromOS(); 271 externalEditors = filterEditors(externalEditors); 272 externalEditorImages = getImages(externalEditors); 273 shell.setCursor(null); 275 busy.dispose(); 276 } 277 return externalEditors; 278 } 279 280 287 protected IEditorDescriptor[] filterEditors(IEditorDescriptor[] editors) { 288 if ((editors == null) || (editors.length < 1)) { 289 return editors; 290 } 291 292 if ((editorsToFilter == null) || (editorsToFilter.length < 1)) { 293 return editors; 294 } 295 296 ArrayList filteredList = new ArrayList (); 297 for (int i = 0; i < editors.length; i++) { 298 boolean add = true; 299 for (int j = 0; j < editorsToFilter.length; j++) { 300 if (editors[i].getId().equals(editorsToFilter[j].getId())) { 301 add = false; 302 } 303 } 304 if (add) { 305 filteredList.add(editors[i]); 306 } 307 } 308 309 return (IEditorDescriptor[]) filteredList 310 .toArray(new IEditorDescriptor[filteredList.size()]); 311 } 312 313 316 protected Image[] getImages(IEditorDescriptor[] editors) { 317 Image[] images = new Image[editors.length]; 318 for (int i = 0; i < editors.length; i++) { 319 images[i] = editors[i].getImageDescriptor().createImage(); 320 } 321 return images; 322 } 323 324 327 protected IEditorDescriptor[] getInternalEditors() { 328 if (internalEditors == null) { 329 EditorRegistry reg = (EditorRegistry) WorkbenchPlugin.getDefault() 330 .getEditorRegistry(); 331 internalEditors = reg.getSortedEditorsFromPlugins(); 332 internalEditors = filterEditors(internalEditors); 333 internalEditorImages = getImages(internalEditors); 334 } 335 return internalEditors; 336 } 337 338 343 public IEditorDescriptor getSelectedEditor() { 344 return selectedEditor; 345 } 346 347 public void handleEvent(Event event) { 348 if (event.type == SWT.MouseDoubleClick) { 349 handleDoubleClickEvent(); 350 return; 351 } 352 if (event.widget == externalButton) { 353 fillEditorTable(); 354 } else if (event.widget == browseExternalEditorsButton) { 355 promptForExternalEditor(); 356 } else if (event.widget == editorTable) { 357 if (editorTable.getSelectionIndex() != -1) { 358 selectedEditor = (EditorDescriptor) editorTable.getSelection()[0] 359 .getData(); 360 } else { 361 selectedEditor = null; 362 okButton.setEnabled(false); 363 } 364 } 365 updateEnableState(); 366 } 367 368 protected void promptForExternalEditor() { 369 FileDialog dialog = new FileDialog(getShell(), SWT.OPEN 370 | SWT.PRIMARY_MODAL); 371 dialog.setFilterExtensions(Executable_Filters); 372 String result = dialog.open(); 373 if (result != null) { 374 EditorDescriptor editor = EditorDescriptor.createForProgram(result); 375 TableItem ti = new TableItem(editorTable, SWT.NULL); 377 ti.setData(editor); 378 ti.setText(editor.getLabel()); 379 Image image = editor.getImageDescriptor().createImage(); 380 ti.setImage(image); 381 382 editorTable.setSelection(new TableItem[] { ti }); 384 editorTable.showSelection(); 385 editorTable.setFocus(); 386 selectedEditor = editor; 387 388 390 IEditorDescriptor[] newEditors = new IEditorDescriptor[externalEditors.length + 1]; 391 System.arraycopy(externalEditors, 0, newEditors, 0, 392 externalEditors.length); 393 newEditors[newEditors.length - 1] = editor; 394 externalEditors = newEditors; 395 396 Image[] newImages = new Image[externalEditorImages.length + 1]; 397 System.arraycopy(externalEditorImages, 0, newImages, 0, 398 externalEditorImages.length); 399 newImages[newImages.length - 1] = image; 400 externalEditorImages = newImages; 401 } 402 } 403 404 407 protected void handleDoubleClickEvent() { 408 buttonPressed(IDialogConstants.OK_ID); 409 } 410 411 415 protected void restoreWidgetValues() { 416 IDialogSettings settings = getDialogSettings(); 417 boolean wasExternal = settings.getBoolean(STORE_ID_INTERNAL_EXTERNAL); 418 internalButton.setSelection(!wasExternal); 419 externalButton.setSelection(wasExternal); 420 } 421 422 426 protected void saveWidgetValues() { 427 IDialogSettings settings = getDialogSettings(); 428 settings 430 .put(STORE_ID_INTERNAL_EXTERNAL, !internalButton.getSelection()); 431 } 432 433 438 public void setMessage(String aMessage) { 439 message = aMessage; 440 } 441 442 447 public void setEditorsToFilter(IEditorDescriptor[] editors) { 448 editorsToFilter = editors; 449 } 450 451 454 public void updateEnableState() { 455 boolean enableExternal = externalButton.getSelection(); 456 browseExternalEditorsButton.setEnabled(enableExternal); 457 updateOkButton(); 458 } 459 460 protected void createButtonsForButtonBar(Composite parent) { 461 okButton = createButton(parent, IDialogConstants.OK_ID, 462 IDialogConstants.OK_LABEL, true); 463 createButton(parent, IDialogConstants.CANCEL_ID, 464 IDialogConstants.CANCEL_LABEL, false); 465 okButton.setEnabled(false); 467 468 } 469 470 473 protected void updateOkButton() { 474 if (okButton == null) { 476 return; 477 } 478 if (editorTable.getSelectionCount() == 0) { 480 okButton.setEnabled(false); 481 return; 482 } 483 okButton.setEnabled(selectedEditor != null); 485 } 486 } 487 | Popular Tags |