KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > ContentTypesPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
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 /**
63  * Preference page that allows manipulation of core content types. Unlike most
64  * preference pages, it does not work on the preference store itself but rather
65  * the content type manager. As such, there are no apply/default buttons and all
66  * changes made take effect immediately.
67  *
68  * @since 3.1
69  */

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 JavaDoc name;
89
90         String JavaDoc ext;
91
92         boolean isPredefined;
93
94         int sortValue;
95
96         /*
97          * (non-Javadoc)
98          *
99          * @see java.lang.Object#toString()
100          */

101         public String JavaDoc toString() {
102             String JavaDoc toString;
103             if (name != null) {
104                 toString = name;
105             } else {
106                 toString = "*." + ext; //$NON-NLS-1$
107
}
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 JavaDoc element) {
120             // only Spec objects in here - unchecked cast
121
return ((Spec) element).sortValue;
122         }
123     }
124
125     private class FileSpecLabelProvider extends LabelProvider {
126         /*
127          * (non-Javadoc)
128          *
129          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
130          */

131         public String JavaDoc getText(Object JavaDoc element) {
132             String JavaDoc label = super.getText(element);
133             return TextProcessor.process(label, "*."); //$NON-NLS-1$
134
}
135     }
136
137     private class FileSpecContentProvider implements IStructuredContentProvider {
138
139         /*
140          * (non-Javadoc)
141          *
142          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
143          */

144         public void dispose() {
145         }
146
147         /*
148          * (non-Javadoc)
149          *
150          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
151          * java.lang.Object, java.lang.Object)
152          */

153         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
154         }
155
156         /*
157          * (non-Javadoc)
158          *
159          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
160          */

161         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
162             IContentType contentType = (IContentType) inputElement;
163             String JavaDoc[] userextfileSpecs = contentType
164                     .getFileSpecs(IContentType.FILE_EXTENSION_SPEC
165                             | IContentType.IGNORE_PRE_DEFINED);
166             String JavaDoc[] usernamefileSpecs = contentType
167                     .getFileSpecs(IContentType.FILE_NAME_SPEC
168                             | IContentType.IGNORE_PRE_DEFINED);
169             String JavaDoc[] preextfileSpecs = contentType
170                     .getFileSpecs(IContentType.FILE_EXTENSION_SPEC
171                             | IContentType.IGNORE_USER_DEFINED);
172             String JavaDoc[] 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 JavaDoc[] createSpecs(String JavaDoc[] userextfileSpecs,
181                 String JavaDoc[] usernamefileSpecs, String JavaDoc[] preextfileSpecs,
182                 String JavaDoc[] prenamefileSpecs) {
183             List JavaDoc returnValues = new ArrayList JavaDoc();
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         /*
222          * (non-Javadoc)
223          *
224          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
225          */

226         public String JavaDoc getText(Object JavaDoc 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         /*
237          * (non-Javadoc)
238          *
239          * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
240          */

241         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
242             List JavaDoc elements = new ArrayList JavaDoc();
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         /*
255          * (non-Javadoc)
256          *
257          * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
258          */

259         public Object JavaDoc getParent(Object JavaDoc element) {
260             IContentType contentType = (IContentType) element;
261             return contentType.getBaseType();
262         }
263
264         /*
265          * (non-Javadoc)
266          *
267          * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
268          */

269         public boolean hasChildren(Object JavaDoc element) {
270             return getChildren(element).length > 0;
271         }
272
273         /*
274          * (non-Javadoc)
275          *
276          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
277          */

278         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
279             return getChildren(null);
280         }
281
282         /*
283          * (non-Javadoc)
284          *
285          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
286          */

287         public void dispose() {
288
289         }
290
291         /*
292          * (non-Javadoc)
293          *
294          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
295          * java.lang.Object, java.lang.Object)
296          */

297         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
298             manager = (IContentTypeManager) newInput;
299         }
300     }
301
302     /*
303      * (non-Javadoc)
304      *
305      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
306      */

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,//$NON-NLS-1$
317
(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             /*
360              * (non-Javadoc)
361              *
362              * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
363              */

364             public void widgetSelected(SelectionEvent e) {
365                 try {
366                     String JavaDoc 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             /*
381              * (non-Javadoc)
382              *
383              * @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent)
384              */

385             public void keyReleased(KeyEvent e) {
386                 IContentType contentType = getSelectedContentType();
387                 String JavaDoc charset = contentType.getDefaultCharset();
388                 if (charset == null) {
389                     charset = ""; //$NON-NLS-1$
390
}
391                 setButton.setEnabled(!charset.equals(charsetField.getText()));
392             }
393         });
394
395     }
396
397     /**
398      * @param composite
399      */

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 JavaDoc elements = selection.toList();
430                             for (Iterator JavaDoc 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                 /*
456                  * (non-Javadoc)
457                  *
458                  * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
459                  */

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 JavaDoc name = dialog.getName();
466                         String JavaDoc extension = dialog.getExtension();
467                         try {
468                             if (name.equals("*")) { //$NON-NLS-1$
469
selectedContentType.addFileSpec(extension,
470                                         IContentType.FILE_EXTENSION_SPEC);
471                             } else {
472                                 selectedContentType
473                                         .addFileSpec(
474                                                 name
475                                                         + (extension.length() > 0 ? ('.' + extension)
476                                                                 : ""), //$NON-NLS-1$
477
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                 /*
497                  * (non-Javadoc)
498                  *
499                  * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
500                  */

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 JavaDoc 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     /**
544      * @param composite
545      */

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                         /*
573                          * (non-Javadoc)
574                          *
575                          * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
576                          */

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 JavaDoc charset = contentType
584                                         .getDefaultCharset();
585                                 if (charset == null) {
586                                     charset = ""; //$NON-NLS-1$
587
}
588                                 charsetField.setText(charset);
589                             } else {
590                                 charsetField.setText(""); //$NON-NLS-1$
591
}
592
593                             charsetField.setEnabled(contentType != null);
594                             addButton.setEnabled(contentType != null);
595                             setButton.setEnabled(false);
596                         }
597                     });
598         }
599     }
600
601     /*
602      * (non-Javadoc)
603      *
604      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
605      */

606     public void init(IWorkbench workbench) {
607         this.workbench = workbench;
608         noDefaultAndApplyButton();
609     }
610 }
611
Popular Tags