1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.MultiStatus; 20 import org.eclipse.core.runtime.Platform; 21 import org.eclipse.core.runtime.content.IContentType; 22 import org.eclipse.core.runtime.content.IContentTypeManager; 23 import org.eclipse.jface.preference.PreferencePage; 24 import org.eclipse.jface.viewers.ISelectionChangedListener; 25 import org.eclipse.jface.viewers.IStructuredContentProvider; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.jface.viewers.ITreeContentProvider; 28 import org.eclipse.jface.viewers.LabelProvider; 29 import org.eclipse.jface.viewers.ListViewer; 30 import org.eclipse.jface.viewers.SelectionChangedEvent; 31 import org.eclipse.jface.viewers.TreeViewer; 32 import org.eclipse.jface.viewers.Viewer; 33 import org.eclipse.jface.viewers.ViewerComparator; 34 import org.eclipse.jface.window.Window; 35 import org.eclipse.osgi.util.NLS; 36 import org.eclipse.osgi.util.TextProcessor; 37 import org.eclipse.swt.SWT; 38 import org.eclipse.swt.events.KeyAdapter; 39 import org.eclipse.swt.events.KeyEvent; 40 import org.eclipse.swt.events.SelectionAdapter; 41 import org.eclipse.swt.events.SelectionEvent; 42 import org.eclipse.swt.layout.GridData; 43 import org.eclipse.swt.layout.GridLayout; 44 import org.eclipse.swt.widgets.Button; 45 import org.eclipse.swt.widgets.Composite; 46 import org.eclipse.swt.widgets.Control; 47 import org.eclipse.swt.widgets.Label; 48 import org.eclipse.swt.widgets.Shell; 49 import org.eclipse.swt.widgets.Text; 50 import org.eclipse.ui.IWorkbench; 51 import org.eclipse.ui.IWorkbenchPreferencePage; 52 import org.eclipse.ui.PlatformUI; 53 import org.eclipse.ui.dialogs.PreferenceLinkArea; 54 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 55 import org.eclipse.ui.internal.WorkbenchMessages; 56 import org.eclipse.ui.internal.WorkbenchPlugin; 57 import org.eclipse.ui.internal.misc.StatusUtil; 58 import org.eclipse.ui.internal.util.Util; 59 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer; 60 import org.eclipse.ui.statushandlers.StatusManager; 61 62 70 public class ContentTypesPreferencePage extends PreferencePage implements 71 IWorkbenchPreferencePage { 72 73 private ListViewer fileAssociationViewer; 74 75 private Button removeButton; 76 77 private TreeViewer contentTypesViewer; 78 79 private Button addButton; 80 81 private Text charsetField; 82 83 private Button setButton; 84 85 private IWorkbench workbench; 86 87 private class Spec { 88 String name; 89 90 String ext; 91 92 boolean isPredefined; 93 94 int sortValue; 95 96 101 public String toString() { 102 String toString; 103 if (name != null) { 104 toString = name; 105 } else { 106 toString = "*." + ext; } 108 109 if (isPredefined) { 110 toString = NLS.bind( 111 WorkbenchMessages.ContentTypes_lockedFormat, toString); 112 } 113 114 return toString; 115 } 116 } 117 118 private class FileSpecComparator extends ViewerComparator { 119 public int category(Object element) { 120 return ((Spec) element).sortValue; 122 } 123 } 124 125 private class FileSpecLabelProvider extends LabelProvider { 126 131 public String getText(Object element) { 132 String label = super.getText(element); 133 return TextProcessor.process(label, "*."); } 135 } 136 137 private class FileSpecContentProvider implements IStructuredContentProvider { 138 139 144 public void dispose() { 145 } 146 147 153 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 154 } 155 156 161 public Object [] getElements(Object inputElement) { 162 IContentType contentType = (IContentType) inputElement; 163 String [] userextfileSpecs = contentType 164 .getFileSpecs(IContentType.FILE_EXTENSION_SPEC 165 | IContentType.IGNORE_PRE_DEFINED); 166 String [] usernamefileSpecs = contentType 167 .getFileSpecs(IContentType.FILE_NAME_SPEC 168 | IContentType.IGNORE_PRE_DEFINED); 169 String [] preextfileSpecs = contentType 170 .getFileSpecs(IContentType.FILE_EXTENSION_SPEC 171 | IContentType.IGNORE_USER_DEFINED); 172 String [] prenamefileSpecs = contentType 173 .getFileSpecs(IContentType.FILE_NAME_SPEC 174 | IContentType.IGNORE_USER_DEFINED); 175 176 return createSpecs(userextfileSpecs, usernamefileSpecs, 177 preextfileSpecs, prenamefileSpecs); 178 } 179 180 private Object [] createSpecs(String [] userextfileSpecs, 181 String [] usernamefileSpecs, String [] preextfileSpecs, 182 String [] prenamefileSpecs) { 183 List returnValues = new ArrayList (); 184 for (int i = 0; i < usernamefileSpecs.length; i++) { 185 Spec spec = new Spec(); 186 spec.name = usernamefileSpecs[i]; 187 spec.isPredefined = false; 188 spec.sortValue = 0; 189 returnValues.add(spec); 190 } 191 192 for (int i = 0; i < prenamefileSpecs.length; i++) { 193 Spec spec = new Spec(); 194 spec.name = prenamefileSpecs[i]; 195 spec.isPredefined = true; 196 spec.sortValue = 1; 197 returnValues.add(spec); 198 } 199 200 for (int i = 0; i < userextfileSpecs.length; i++) { 201 Spec spec = new Spec(); 202 spec.ext = userextfileSpecs[i]; 203 spec.isPredefined = false; 204 spec.sortValue = 2; 205 returnValues.add(spec); 206 } 207 208 for (int i = 0; i < preextfileSpecs.length; i++) { 209 Spec spec = new Spec(); 210 spec.ext = preextfileSpecs[i]; 211 spec.isPredefined = true; 212 spec.sortValue = 3; 213 returnValues.add(spec); 214 } 215 216 return returnValues.toArray(); 217 } 218 } 219 220 private class ContentTypesLabelProvider extends LabelProvider { 221 226 public String getText(Object element) { 227 IContentType contentType = (IContentType) element; 228 return contentType.getName(); 229 } 230 } 231 232 private class ContentTypesContentProvider implements ITreeContentProvider { 233 234 private IContentTypeManager manager; 235 236 241 public Object [] getChildren(Object parentElement) { 242 List elements = new ArrayList (); 243 IContentType baseType = (IContentType) parentElement; 244 IContentType[] contentTypes = manager.getAllContentTypes(); 245 for (int i = 0; i < contentTypes.length; i++) { 246 IContentType type = contentTypes[i]; 247 if (Util.equals(type.getBaseType(), baseType)) { 248 elements.add(type); 249 } 250 } 251 return elements.toArray(); 252 } 253 254 259 public Object getParent(Object element) { 260 IContentType contentType = (IContentType) element; 261 return contentType.getBaseType(); 262 } 263 264 269 public boolean hasChildren(Object element) { 270 return getChildren(element).length > 0; 271 } 272 273 278 public Object [] getElements(Object inputElement) { 279 return getChildren(null); 280 } 281 282 287 public void dispose() { 288 289 } 290 291 297 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 298 manager = (IContentTypeManager) newInput; 299 } 300 } 301 302 307 protected Control createContents(Composite parent) { 308 Composite composite = new Composite(parent, SWT.NONE); 309 GridLayout layout = new GridLayout(2, false); 310 layout.marginHeight = layout.marginWidth = 0; 311 composite.setLayout(layout); 312 313 PreferenceLinkArea contentTypeArea = new PreferenceLinkArea( 314 composite, 315 SWT.NONE, 316 "org.eclipse.ui.preferencePages.FileEditors", WorkbenchMessages.ContentTypes_FileEditorsRelatedLink, (IWorkbenchPreferenceContainer) getContainer(), null); 318 319 GridData data = new GridData(GridData.FILL_HORIZONTAL 320 | GridData.GRAB_HORIZONTAL); 321 contentTypeArea.getControl().setLayoutData(data); 322 323 createContentTypesTree(composite); 324 createFileAssociations(composite); 325 createCharset(composite); 326 327 workbench.getHelpSystem().setHelp(parent, 328 IWorkbenchHelpContextIds.CONTENT_TYPES_PREFERENCE_PAGE); 329 330 applyDialogFont(composite); 331 return composite; 332 } 333 334 private void createCharset(final Composite parent) { 335 Composite composite = new Composite(parent, SWT.NONE); 336 GridLayout layout = new GridLayout(3, false); 337 layout.marginHeight = layout.marginWidth = 0; 338 GridData compositeData = new GridData(GridData.FILL_HORIZONTAL); 339 compositeData.horizontalSpan = 2; 340 composite.setLayoutData(compositeData); 341 composite.setLayout(layout); 342 343 Label label = new Label(composite, SWT.NONE); 344 label.setFont(parent.getFont()); 345 label.setText(WorkbenchMessages.ContentTypes_characterSetLabel); 346 charsetField = new Text(composite, SWT.SINGLE | SWT.BORDER); 347 charsetField.setFont(parent.getFont()); 348 charsetField.setEnabled(false); 349 GridData data = new GridData(GridData.FILL_HORIZONTAL); 350 charsetField.setLayoutData(data); 351 setButton = new Button(composite, SWT.PUSH); 352 setButton.setFont(parent.getFont()); 353 setButton 354 .setText(WorkbenchMessages.ContentTypes_characterSetUpdateLabel); 355 setButton.setEnabled(false); 356 setButtonLayoutData(setButton); 357 setButton.addSelectionListener(new SelectionAdapter() { 358 359 364 public void widgetSelected(SelectionEvent e) { 365 try { 366 String text = charsetField.getText().trim(); 367 if (text.length() == 0) { 368 text = null; 369 } 370 getSelectedContentType().setDefaultCharset(text); 371 setButton.setEnabled(false); 372 } catch (CoreException e1) { 373 StatusUtil.handleStatus(e1.getStatus(), StatusManager.SHOW, 374 parent.getShell()); 375 } 376 } 377 }); 378 379 charsetField.addKeyListener(new KeyAdapter() { 380 385 public void keyReleased(KeyEvent e) { 386 IContentType contentType = getSelectedContentType(); 387 String charset = contentType.getDefaultCharset(); 388 if (charset == null) { 389 charset = ""; } 391 setButton.setEnabled(!charset.equals(charsetField.getText())); 392 } 393 }); 394 395 } 396 397 400 private void createFileAssociations(final Composite composite) { 401 { 402 Label label = new Label(composite, SWT.NONE); 403 label.setText(WorkbenchMessages.ContentTypes_fileAssociationsLabel); 404 GridData data = new GridData(); 405 data.horizontalSpan = 2; 406 label.setLayoutData(data); 407 } 408 { 409 fileAssociationViewer = new ListViewer(composite); 410 fileAssociationViewer.setComparator(new FileSpecComparator()); 411 fileAssociationViewer.getControl().setFont(composite.getFont()); 412 fileAssociationViewer 413 .setContentProvider(new FileSpecContentProvider()); 414 fileAssociationViewer.setLabelProvider(new FileSpecLabelProvider()); 415 GridData data = new GridData(GridData.FILL_BOTH); 416 data.horizontalSpan = 1; 417 fileAssociationViewer.getControl().setLayoutData(data); 418 fileAssociationViewer 419 .addSelectionChangedListener(new ISelectionChangedListener() { 420 421 public void selectionChanged(SelectionChangedEvent event) { 422 IStructuredSelection selection = (IStructuredSelection) event 423 .getSelection(); 424 if (selection.isEmpty()) { 425 removeButton.setEnabled(false); 426 return; 427 } 428 boolean enabled = true; 429 List elements = selection.toList(); 430 for (Iterator i = elements.iterator(); i.hasNext();) { 431 Spec spec = (Spec) i.next(); 432 if (spec.isPredefined) { 433 enabled = false; 434 } 435 } 436 removeButton.setEnabled(enabled); 437 } 438 }); 439 } 440 { 441 Composite buttonArea = new Composite(composite, SWT.NONE); 442 GridLayout layout = new GridLayout(1, false); 443 layout.marginHeight = layout.marginWidth = 0; 444 buttonArea.setLayout(layout); 445 GridData data = new GridData(GridData.VERTICAL_ALIGN_BEGINNING); 446 buttonArea.setLayoutData(data); 447 448 addButton = new Button(buttonArea, SWT.PUSH); 449 addButton.setFont(composite.getFont()); 450 addButton 451 .setText(WorkbenchMessages.ContentTypes_fileAssociationsAddLabel); 452 addButton.setEnabled(false); 453 setButtonLayoutData(addButton); 454 addButton.addSelectionListener(new SelectionAdapter() { 455 460 public void widgetSelected(SelectionEvent e) { 461 Shell shell = composite.getShell(); 462 IContentType selectedContentType = getSelectedContentType(); 463 FileExtensionDialog dialog = new FileExtensionDialog(shell); 464 if (dialog.open() == Window.OK) { 465 String name = dialog.getName(); 466 String extension = dialog.getExtension(); 467 try { 468 if (name.equals("*")) { selectedContentType.addFileSpec(extension, 470 IContentType.FILE_EXTENSION_SPEC); 471 } else { 472 selectedContentType 473 .addFileSpec( 474 name 475 + (extension.length() > 0 ? ('.' + extension) 476 : ""), IContentType.FILE_NAME_SPEC); 478 } 479 } catch (CoreException ex) { 480 StatusUtil.handleStatus(ex.getStatus(), 481 StatusManager.SHOW, shell); 482 WorkbenchPlugin.log(ex); 483 } finally { 484 fileAssociationViewer.setInput(selectedContentType); 485 } 486 } 487 } 488 }); 489 490 removeButton = new Button(buttonArea, SWT.PUSH); 491 removeButton.setEnabled(false); 492 removeButton 493 .setText(WorkbenchMessages.ContentTypes_fileAssociationsRemoveLabel); 494 setButtonLayoutData(removeButton); 495 removeButton.addSelectionListener(new SelectionAdapter() { 496 501 public void widgetSelected(SelectionEvent event) { 502 IContentType contentType = getSelectedContentType(); 503 Spec[] specs = getSelectedSpecs(); 504 MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, 505 0, new IStatus[0], 506 WorkbenchMessages.ContentTypes_errorDialogMessage, 507 null); 508 for (int i = 0; i < specs.length; i++) { 509 Spec spec = specs[i]; 510 try { 511 if (spec.name != null) { 512 contentType.removeFileSpec(spec.name, 513 IContentType.FILE_NAME_SPEC); 514 } else if (spec.ext != null) { 515 contentType.removeFileSpec(spec.ext, 516 IContentType.FILE_EXTENSION_SPEC); 517 } 518 } catch (CoreException e) { 519 result.add(e.getStatus()); 520 } 521 } 522 if (!result.isOK()) { 523 StatusUtil.handleStatus(result, StatusManager.SHOW, 524 composite.getShell()); 525 } 526 fileAssociationViewer.setInput(contentType); 527 } 528 }); 529 } 530 } 531 532 protected Spec[] getSelectedSpecs() { 533 List list = ((IStructuredSelection) fileAssociationViewer 534 .getSelection()).toList(); 535 return (Spec[]) list.toArray(new Spec[list.size()]); 536 } 537 538 protected IContentType getSelectedContentType() { 539 return (IContentType) ((IStructuredSelection) contentTypesViewer 540 .getSelection()).getFirstElement(); 541 } 542 543 546 private void createContentTypesTree(Composite composite) { 547 { 548 Label label = new Label(composite, SWT.NONE); 549 label.setFont(composite.getFont()); 550 label.setText(WorkbenchMessages.ContentTypes_contentTypesLabel); 551 GridData data = new GridData(); 552 data.horizontalSpan = 2; 553 label.setLayoutData(data); 554 } 555 { 556 contentTypesViewer = new TreeViewer(composite, SWT.SINGLE 557 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); 558 contentTypesViewer.getControl().setFont(composite.getFont()); 559 contentTypesViewer 560 .setContentProvider(new ContentTypesContentProvider()); 561 contentTypesViewer 562 .setLabelProvider(new ContentTypesLabelProvider()); 563 contentTypesViewer.setComparator(new ViewerComparator()); 564 contentTypesViewer.setInput(Platform.getContentTypeManager()); 565 GridData data = new GridData(GridData.FILL_BOTH); 566 data.horizontalSpan = 2; 567 contentTypesViewer.getControl().setLayoutData(data); 568 569 contentTypesViewer 570 .addSelectionChangedListener(new ISelectionChangedListener() { 571 572 577 public void selectionChanged(SelectionChangedEvent event) { 578 IContentType contentType = (IContentType) ((IStructuredSelection) event 579 .getSelection()).getFirstElement(); 580 fileAssociationViewer.setInput(contentType); 581 582 if (contentType != null) { 583 String charset = contentType 584 .getDefaultCharset(); 585 if (charset == null) { 586 charset = ""; } 588 charsetField.setText(charset); 589 } else { 590 charsetField.setText(""); } 592 593 charsetField.setEnabled(contentType != null); 594 addButton.setEnabled(contentType != null); 595 setButton.setEnabled(false); 596 } 597 }); 598 } 599 } 600 601 606 public void init(IWorkbench workbench) { 607 this.workbench = workbench; 608 noDefaultAndApplyButton(); 609 } 610 } 611 | Popular Tags |