1 13 package org.eclipse.ui.internal.dialogs; 14 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.eclipse.core.runtime.Assert; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.core.runtime.content.IContentType; 24 import org.eclipse.jface.dialogs.MessageDialog; 25 import org.eclipse.jface.preference.IPreferenceStore; 26 import org.eclipse.jface.preference.PreferencePage; 27 import org.eclipse.jface.window.Window; 28 import org.eclipse.osgi.util.NLS; 29 import org.eclipse.swt.SWT; 30 import org.eclipse.swt.graphics.Image; 31 import org.eclipse.swt.layout.GridData; 32 import org.eclipse.swt.layout.GridLayout; 33 import org.eclipse.swt.widgets.Button; 34 import org.eclipse.swt.widgets.Composite; 35 import org.eclipse.swt.widgets.Control; 36 import org.eclipse.swt.widgets.Event; 37 import org.eclipse.swt.widgets.Label; 38 import org.eclipse.swt.widgets.Listener; 39 import org.eclipse.swt.widgets.Table; 40 import org.eclipse.swt.widgets.TableItem; 41 import org.eclipse.ui.IEditorDescriptor; 42 import org.eclipse.ui.IFileEditorMapping; 43 import org.eclipse.ui.IWorkbench; 44 import org.eclipse.ui.IWorkbenchPreferencePage; 45 import org.eclipse.ui.dialogs.EditorSelectionDialog; 46 import org.eclipse.ui.dialogs.PreferenceLinkArea; 47 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 48 import org.eclipse.ui.internal.WorkbenchMessages; 49 import org.eclipse.ui.internal.WorkbenchPlugin; 50 import org.eclipse.ui.internal.registry.EditorDescriptor; 51 import org.eclipse.ui.internal.registry.EditorRegistry; 52 import org.eclipse.ui.internal.registry.FileEditorMapping; 53 import org.eclipse.ui.internal.util.PrefUtil; 54 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer; 55 56 67 public class FileEditorsPreferencePage extends PreferencePage implements 68 IWorkbenchPreferencePage, Listener { 69 70 private static final String DATA_EDITOR = "editor"; 72 private static final String DATA_FROM_CONTENT_TYPE = "type"; 74 protected Table resourceTypeTable; 75 76 protected Button addResourceTypeButton; 77 78 protected Button removeResourceTypeButton; 79 80 protected Table editorTable; 81 82 protected Button addEditorButton; 83 84 protected Button removeEditorButton; 85 86 protected Button defaultEditorButton; 87 88 protected Label editorLabel; 89 90 protected IWorkbench workbench; 91 92 protected List imagesToDispose; 93 94 protected Map editorsToImages; 95 96 103 public void addResourceType(String newName, String newExtension) { 104 Assert.isTrue((newName != null && newName.length() != 0) 106 || (newExtension != null && newExtension.length() != 0)); 107 108 int index = newName.indexOf('*'); 111 if (index > -1) { 112 Assert.isTrue(index == 0 && newName.length() == 1); 113 Assert.isTrue(newExtension != null && newExtension.length() != 0); 114 } 115 116 String newFilename = (newName + (newExtension == null 118 || newExtension.length() == 0 ? "" : "." + newExtension)).toUpperCase(); IFileEditorMapping resourceType; 120 TableItem[] items = resourceTypeTable.getItems(); 121 boolean found = false; 122 int i = 0; 123 124 while (i < items.length && !found) { 125 resourceType = (IFileEditorMapping) items[i].getData(); 126 int result = newFilename.compareToIgnoreCase(resourceType 127 .getLabel()); 128 if (result == 0) { 129 MessageDialog 131 .openInformation( 132 getControl().getShell(), 133 WorkbenchMessages.FileEditorPreference_existsTitle, 134 WorkbenchMessages.FileEditorPreference_existsMessage); 135 return; 136 } 137 138 if (result < 0) { 139 found = true; 140 } else { 141 i++; 142 } 143 } 144 145 resourceType = new FileEditorMapping(newName, newExtension); 147 TableItem item = newResourceTableItem(resourceType, i, true); 148 resourceTypeTable.setFocus(); 149 resourceTypeTable.showItem(item); 150 fillEditorTable(); 151 } 152 153 156 protected Control createContents(Composite parent) { 157 imagesToDispose = new ArrayList (); 158 editorsToImages = new HashMap (50); 159 160 Composite pageComponent = new Composite(parent, SWT.NULL); 162 GridLayout layout = new GridLayout(); 163 layout.numColumns = 2; 164 layout.marginWidth = 0; 165 layout.marginHeight = 0; 166 pageComponent.setLayout(layout); 167 GridData data = new GridData(); 168 data.verticalAlignment = GridData.FILL; 169 data.horizontalAlignment = GridData.FILL; 170 pageComponent.setLayoutData(data); 171 172 174 PreferenceLinkArea contentTypeArea = new PreferenceLinkArea(pageComponent, SWT.NONE, 175 "org.eclipse.ui.preferencePages.ContentTypes", WorkbenchMessages.FileEditorPreference_contentTypesRelatedLink, (IWorkbenchPreferenceContainer) getContainer(),null); 177 178 data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL); 179 contentTypeArea.getControl().setLayoutData(data); 180 181 Label label = new Label(pageComponent, SWT.LEFT); 183 label.setText(WorkbenchMessages.FileEditorPreference_fileTypes); 184 data = new GridData(); 185 data.horizontalAlignment = GridData.FILL; 186 data.horizontalSpan = 2; 187 label.setLayoutData(data); 188 189 resourceTypeTable = new Table(pageComponent, SWT.SINGLE | SWT.BORDER 190 | SWT.FULL_SELECTION); 191 resourceTypeTable.addListener(SWT.Selection, this); 192 resourceTypeTable.addListener(SWT.DefaultSelection, this); 193 data = new GridData(GridData.FILL_HORIZONTAL); 194 195 int availableRows = DialogUtil.availableRows(pageComponent); 196 197 data.heightHint = resourceTypeTable.getItemHeight() 198 * (availableRows / 8); 199 resourceTypeTable.setLayoutData(data); 200 201 Composite groupComponent = new Composite(pageComponent, SWT.NULL); 202 GridLayout groupLayout = new GridLayout(); 203 groupLayout.marginWidth = 0; 204 groupLayout.marginHeight = 0; 205 groupComponent.setLayout(groupLayout); 206 data = new GridData(); 207 data.verticalAlignment = GridData.FILL; 208 data.horizontalAlignment = GridData.FILL; 209 groupComponent.setLayoutData(data); 210 211 addResourceTypeButton = new Button(groupComponent, SWT.PUSH); 212 addResourceTypeButton.setText(WorkbenchMessages.FileEditorPreference_add); 213 addResourceTypeButton.addListener(SWT.Selection, this); 214 addResourceTypeButton.setLayoutData(data); 215 setButtonLayoutData(addResourceTypeButton); 216 217 removeResourceTypeButton = new Button(groupComponent, SWT.PUSH); 218 removeResourceTypeButton.setText(WorkbenchMessages.FileEditorPreference_remove); 219 removeResourceTypeButton.addListener(SWT.Selection, this); 220 setButtonLayoutData(removeResourceTypeButton); 221 222 label = new Label(pageComponent, SWT.LEFT); 224 data = new GridData(); 225 data.horizontalAlignment = GridData.FILL; 226 data.horizontalSpan = 2; 227 label.setLayoutData(data); 228 229 editorLabel = new Label(pageComponent, SWT.LEFT); 231 editorLabel.setText(WorkbenchMessages.FileEditorPreference_associatedEditors); 232 data = new GridData(); 233 data.horizontalAlignment = GridData.FILL; 234 data.horizontalSpan = 2; 235 editorLabel.setLayoutData(data); 236 237 editorTable = new Table(pageComponent, SWT.SINGLE | SWT.BORDER); 238 editorTable.addListener(SWT.Selection, this); 239 editorTable.addListener(SWT.DefaultSelection, this); 240 data = new GridData(GridData.FILL_BOTH); 241 data.heightHint = editorTable.getItemHeight() * 7; 242 editorTable.setLayoutData(data); 243 244 groupComponent = new Composite(pageComponent, SWT.NULL); 245 groupLayout = new GridLayout(); 246 groupLayout.marginWidth = 0; 247 groupLayout.marginHeight = 0; 248 groupComponent.setLayout(groupLayout); 249 data = new GridData(); 250 data.verticalAlignment = GridData.FILL; 251 data.horizontalAlignment = GridData.FILL; 252 groupComponent.setLayoutData(data); 253 254 addEditorButton = new Button(groupComponent, SWT.PUSH); 255 addEditorButton.setText(WorkbenchMessages.FileEditorPreference_addEditor); 256 addEditorButton.addListener(SWT.Selection, this); 257 addEditorButton.setLayoutData(data); 258 setButtonLayoutData(addEditorButton); 259 260 removeEditorButton = new Button(groupComponent, SWT.PUSH); 261 removeEditorButton.setText(WorkbenchMessages.FileEditorPreference_removeEditor); 262 removeEditorButton.addListener(SWT.Selection, this); 263 setButtonLayoutData(removeEditorButton); 264 265 defaultEditorButton = new Button(groupComponent, SWT.PUSH); 266 defaultEditorButton.setText(WorkbenchMessages.FileEditorPreference_default); 267 defaultEditorButton.addListener(SWT.Selection, this); 268 setButtonLayoutData(defaultEditorButton); 269 270 fillResourceTypeTable(); 271 if (resourceTypeTable.getItemCount() > 0) { 272 resourceTypeTable.setSelection(0); 273 } 274 fillEditorTable(); 275 updateEnabledState(); 276 277 workbench.getHelpSystem().setHelp(parent, 278 IWorkbenchHelpContextIds.FILE_EDITORS_PREFERENCE_PAGE); 279 applyDialogFont(pageComponent); 280 281 return pageComponent; 282 } 283 284 290 public void dispose() { 291 super.dispose(); 292 if (imagesToDispose != null) { 293 for (Iterator e = imagesToDispose.iterator(); e.hasNext();) { 294 ((Image) e.next()).dispose(); 295 } 296 imagesToDispose = null; 297 } 298 if (editorsToImages != null) { 299 for (Iterator e = editorsToImages.values().iterator(); e.hasNext();) { 300 ((Image) e.next()).dispose(); 301 } 302 editorsToImages = null; 303 } 304 } 305 306 310 protected IPreferenceStore doGetPreferenceStore() { 311 return WorkbenchPlugin.getDefault().getPreferenceStore(); 312 } 313 314 protected void fillEditorTable() { 315 editorTable.removeAll(); 316 FileEditorMapping resourceType = getSelectedResourceType(); 317 if (resourceType != null) { 318 IEditorDescriptor[] array = resourceType.getEditors(); 319 for (int i = 0; i < array.length; i++) { 320 IEditorDescriptor editor = array[i]; 321 TableItem item = new TableItem(editorTable, SWT.NULL); 322 item.setData(DATA_EDITOR, editor); 323 String defaultString = null; 325 if (resourceType != null) { 326 if (resourceType.getDefaultEditor() == editor && resourceType.isDeclaredDefaultEditor(editor)) { 327 defaultString = WorkbenchMessages.FileEditorPreference_defaultLabel; 328 } 329 } 330 331 if (defaultString != null) { 332 item.setText(editor.getLabel() + " " + defaultString); } else { 334 item.setText(editor.getLabel()); 335 } 336 item.setImage(getImage(editor)); 337 } 338 339 EditorRegistry registry = (EditorRegistry) WorkbenchPlugin 341 .getDefault().getEditorRegistry(); 342 IContentType[] contentTypes = Platform.getContentTypeManager() 343 .findContentTypesFor(resourceType.getLabel()); 344 for (int i = 0; i < contentTypes.length; i++) { 345 array = registry.getEditorsForContentType(contentTypes[i]); 346 for (int j = 0; j < array.length; j++) { 347 IEditorDescriptor editor = array[j]; 348 TableItem[] items = editorTable.getItems(); 350 TableItem foundItem = null; 351 for (int k = 0; k < items.length; k++) { 352 if (items[k].getData(DATA_EDITOR).equals(editor)) { 353 foundItem = items[k]; 354 break; 355 } 356 } 357 if (foundItem == null) { 358 TableItem item = new TableItem(editorTable, SWT.NULL); 359 item.setData(DATA_EDITOR, editor); 360 item.setData(DATA_FROM_CONTENT_TYPE, contentTypes[i]); 361 setLockedItemText(item, editor.getLabel()); 362 item.setImage(getImage(editor)); 363 } else { foundItem.setData(DATA_FROM_CONTENT_TYPE, contentTypes[i]); 365 setLockedItemText(foundItem, foundItem.getText()); 366 } 367 } 368 } 369 370 } 371 } 372 373 380 private void setLockedItemText(TableItem item, String baseLabel) { 381 item.setText(NLS 382 .bind(WorkbenchMessages.FileEditorPreference_isLocked, 383 baseLabel, ((IContentType) item 384 .getData(DATA_FROM_CONTENT_TYPE)).getName())); 385 } 386 387 390 protected void fillResourceTypeTable() { 391 IFileEditorMapping[] array = WorkbenchPlugin.getDefault() 393 .getEditorRegistry().getFileEditorMappings(); 394 for (int i = 0; i < array.length; i++) { 395 FileEditorMapping mapping = (FileEditorMapping) array[i]; 396 mapping = (FileEditorMapping) mapping.clone(); newResourceTableItem(mapping, i, false); 398 } 399 } 400 401 404 protected Image getImage(IEditorDescriptor editor) { 405 Image image = (Image) editorsToImages.get(editor); 406 if (image == null) { 407 image = editor.getImageDescriptor().createImage(); 408 editorsToImages.put(editor, image); 409 } 410 return image; 411 } 412 413 protected FileEditorMapping getSelectedResourceType() { 414 TableItem[] items = resourceTypeTable.getSelection(); 415 if (items.length > 0) { 416 return (FileEditorMapping) items[0].getData(); } 418 return null; 419 } 420 421 protected IEditorDescriptor[] getAssociatedEditors() { 422 if (getSelectedResourceType() == null) { 423 return null; 424 } 425 if (editorTable.getItemCount() > 0) { 426 ArrayList editorList = new ArrayList (); 427 for (int i = 0; i < editorTable.getItemCount(); i++) { 428 editorList.add(editorTable.getItem(i).getData(DATA_EDITOR)); 429 } 430 431 return (IEditorDescriptor[]) editorList 432 .toArray(new IEditorDescriptor[editorList.size()]); 433 } 434 return null; 435 } 436 437 public void handleEvent(Event event) { 438 if (event.widget == addResourceTypeButton) { 439 promptForResourceType(); 440 } else if (event.widget == removeResourceTypeButton) { 441 removeSelectedResourceType(); 442 } else if (event.widget == addEditorButton) { 443 promptForEditor(); 444 } else if (event.widget == removeEditorButton) { 445 removeSelectedEditor(); 446 } else if (event.widget == defaultEditorButton) { 447 setSelectedEditorAsDefault(); 448 } else if (event.widget == resourceTypeTable) { 449 fillEditorTable(); 450 } 451 452 updateEnabledState(); 453 454 } 455 456 459 public void init(IWorkbench aWorkbench) { 460 this.workbench = aWorkbench; 461 noDefaultAndApplyButton(); 462 } 463 464 468 protected TableItem newResourceTableItem(IFileEditorMapping mapping, 469 int index, boolean selected) { 470 Image image = mapping.getImageDescriptor().createImage(false); 471 if (image != null) { 472 imagesToDispose.add(image); 473 } 474 475 TableItem item = new TableItem(resourceTypeTable, SWT.NULL, index); 476 if (image != null) { 477 item.setImage(image); 478 } 479 item.setText(mapping.getLabel()); 480 item.setData(mapping); 481 if (selected) { 482 resourceTypeTable.setSelection(index); 483 } 484 485 return item; 486 } 487 488 494 public boolean performOk() { 495 TableItem[] items = resourceTypeTable.getItems(); 496 FileEditorMapping[] resourceTypes = new FileEditorMapping[items.length]; 497 for (int i = 0; i < items.length; i++) { 498 resourceTypes[i] = (FileEditorMapping) (items[i].getData()); 499 } 500 EditorRegistry registry = (EditorRegistry) WorkbenchPlugin.getDefault() 501 .getEditorRegistry(); registry.setFileEditorMappings(resourceTypes); 503 registry.saveAssociations(); 504 505 PrefUtil.savePrefs(); 506 return true; 507 } 508 509 512 public void promptForEditor() { 513 EditorSelectionDialog dialog = new EditorSelectionDialog(getControl() 514 .getShell()); 515 dialog.setEditorsToFilter(getAssociatedEditors()); 516 dialog 517 .setMessage(NLS.bind(WorkbenchMessages.Choose_the_editor_for_file,getSelectedResourceType().getLabel() )); 518 if (dialog.open() == Window.OK) { 519 EditorDescriptor editor = (EditorDescriptor) dialog 520 .getSelectedEditor(); 521 if (editor != null) { 522 int i = editorTable.getItemCount(); 523 boolean isEmpty = i < 1; 524 TableItem item = new TableItem(editorTable, SWT.NULL, i); 525 item.setData(DATA_EDITOR, editor); 526 if (isEmpty) { 527 item 528 .setText(editor.getLabel() 529 + " " + WorkbenchMessages.FileEditorPreference_defaultLabel); } else { 531 item.setText(editor.getLabel()); 532 } 533 item.setImage(getImage(editor)); 534 editorTable.setSelection(i); 535 editorTable.setFocus(); 536 getSelectedResourceType().addEditor(editor); 537 if (isEmpty) { 538 getSelectedResourceType().setDefaultEditor(editor); 539 } 540 updateSelectedResourceType(); } 542 } 543 } 544 545 548 public void promptForResourceType() { 549 FileExtensionDialog dialog = new FileExtensionDialog(getControl() 550 .getShell()); 551 if (dialog.open() == Window.OK) { 552 String name = dialog.getName(); 553 String extension = dialog.getExtension(); 554 addResourceType(name, extension); 555 } 556 } 557 558 561 public void removeSelectedEditor() { 562 TableItem[] items = editorTable.getSelection(); 563 boolean defaultEditor = editorTable.getSelectionIndex() == 0; 564 if (items.length > 0) { 565 getSelectedResourceType().removeEditor( 566 (EditorDescriptor) items[0].getData(DATA_EDITOR)); 567 items[0].dispose(); } 569 if (defaultEditor && editorTable.getItemCount() > 0) { 570 TableItem item = editorTable.getItem(0); 571 getSelectedResourceType().setDefaultEditor( 573 (EditorDescriptor) item.getData(DATA_EDITOR)); 574 if (item != null) { 575 item 576 .setText(((EditorDescriptor) (item.getData(DATA_EDITOR))) 577 .getLabel() 578 + " " + WorkbenchMessages.FileEditorPreference_defaultLabel); } 580 if (!isEditorRemovable(item)) { 581 setLockedItemText(item, item.getText()); 582 } 583 } 584 585 } 586 587 590 public void removeSelectedResourceType() { 591 TableItem[] items = resourceTypeTable.getSelection(); 592 if (items.length > 0) { 593 items[0].dispose(); } 595 editorTable.removeAll(); 597 } 598 599 602 public void setSelectedEditorAsDefault() { 603 TableItem[] items = editorTable.getSelection(); 604 if (items.length > 0) { 605 TableItem oldDefaultItem = editorTable.getItem(0); 607 oldDefaultItem 608 .setText(((EditorDescriptor) oldDefaultItem.getData(DATA_EDITOR)) 609 .getLabel()); 610 if (!isEditorRemovable(oldDefaultItem)) { 612 setLockedItemText(oldDefaultItem, oldDefaultItem.getText()); 613 } 614 EditorDescriptor editor = (EditorDescriptor) items[0].getData(DATA_EDITOR); 616 getSelectedResourceType().setDefaultEditor(editor); 617 IContentType fromContentType = (IContentType) items[0].getData(DATA_FROM_CONTENT_TYPE); 618 items[0].dispose(); TableItem item = new TableItem(editorTable, SWT.NULL, 0); 620 item.setData(DATA_EDITOR, editor); 621 if (fromContentType != null) { 622 item.setData(DATA_FROM_CONTENT_TYPE, fromContentType); 623 } 624 item 625 .setText(editor.getLabel() 626 + " " + WorkbenchMessages.FileEditorPreference_defaultLabel); item.setImage(getImage(editor)); 628 if (!isEditorRemovable(item)) { 629 setLockedItemText(item, item.getText()); 630 } 631 editorTable.setSelection(new TableItem[] { item }); 632 } 633 } 634 635 638 public void updateEnabledState() { 639 boolean resourceTypeSelected = resourceTypeTable.getSelectionIndex() != -1; 641 boolean editorSelected = editorTable.getSelectionIndex() != -1; 642 643 removeResourceTypeButton.setEnabled(resourceTypeSelected); 644 editorLabel.setEnabled(resourceTypeSelected); 645 addEditorButton.setEnabled(resourceTypeSelected); 646 removeEditorButton.setEnabled(editorSelected && isEditorRemovable()); 647 defaultEditorButton.setEnabled(editorSelected); 648 } 649 650 657 private boolean isEditorRemovable() { 658 TableItem[] items = editorTable.getSelection(); 659 if (items.length > 0) { 660 return isEditorRemovable(items[0]); 661 } 662 return false; 663 } 664 665 673 private boolean isEditorRemovable(TableItem item) { 674 IContentType fromContentType = (IContentType) item.getData(DATA_FROM_CONTENT_TYPE); 675 return fromContentType == null; 676 } 677 678 681 public void updateSelectedResourceType() { 682 } 687 } 688 | Popular Tags |