KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > MembersOrderPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
12 package org.eclipse.jdt.internal.ui.preferences;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.jface.preference.PreferencePage;
29 import org.eclipse.jface.resource.ImageDescriptor;
30 import org.eclipse.jface.viewers.LabelProvider;
31
32 import org.eclipse.ui.IWorkbench;
33 import org.eclipse.ui.IWorkbenchPreferencePage;
34 import org.eclipse.ui.PlatformUI;
35
36 import org.eclipse.jdt.core.Flags;
37
38 import org.eclipse.jdt.ui.JavaElementImageDescriptor;
39 import org.eclipse.jdt.ui.PreferenceConstants;
40
41 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
42 import org.eclipse.jdt.internal.ui.JavaPlugin;
43 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
44 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
45 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
46 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
47 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
48 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
49
50 public class MembersOrderPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
51     
52     public static final String JavaDoc PREF_ID= "org.eclipse.jdt.ui.preferences.MembersOrderPreferencePage"; //$NON-NLS-1$
53

54     private static final String JavaDoc ALL_SORTMEMBER_ENTRIES= "T,SI,SF,SM,I,F,C,M"; //$NON-NLS-1$
55
private static final String JavaDoc ALL_VISIBILITY_ENTRIES= "B,V,R,D"; //$NON-NLS-1$
56
private static final String JavaDoc PREF_OUTLINE_SORT_OPTION= PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER;
57     private static final String JavaDoc PREF_VISIBILITY_SORT_OPTION= PreferenceConstants.APPEARANCE_VISIBILITY_SORT_ORDER;
58     private static final String JavaDoc PREF_USE_VISIBILITY_SORT_OPTION= PreferenceConstants.APPEARANCE_ENABLE_VISIBILITY_SORT_ORDER;
59         
60     public static final String JavaDoc CONSTRUCTORS= "C"; //$NON-NLS-1$
61
public static final String JavaDoc FIELDS= "F"; //$NON-NLS-1$
62
public static final String JavaDoc METHODS= "M"; //$NON-NLS-1$
63
public static final String JavaDoc STATIC_METHODS= "SM"; //$NON-NLS-1$
64
public static final String JavaDoc STATIC_FIELDS= "SF"; //$NON-NLS-1$
65
public static final String JavaDoc INIT= "I"; //$NON-NLS-1$
66
public static final String JavaDoc STATIC_INIT= "SI"; //$NON-NLS-1$
67
public static final String JavaDoc TYPES= "T"; //$NON-NLS-1$
68

69     public static final String JavaDoc PUBLIC= "B"; //$NON-NLS-1$
70
public static final String JavaDoc PRIVATE= "V"; //$NON-NLS-1$
71
public static final String JavaDoc PROTECTED= "R"; //$NON-NLS-1$
72
public static final String JavaDoc DEFAULT= "D"; //$NON-NLS-1$
73

74     private boolean fUseVisibilitySort;
75     private ListDialogField fSortOrderList;
76     private ListDialogField fVisibilityOrderList;
77     private SelectionButtonDialogField fUseVisibilitySortField;
78
79     private static boolean isValidEntries(List JavaDoc entries, String JavaDoc entryString) {
80         StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(entryString, ","); //$NON-NLS-1$
81
int i= 0;
82         for (; tokenizer.hasMoreTokens(); i++) {
83             String JavaDoc token= tokenizer.nextToken();
84             if (!entries.contains(token))
85                 return false;
86         }
87         return i == entries.size();
88     }
89
90     public MembersOrderPreferencePage() {
91         //set the preference store
92
setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore());
93         
94         setDescription(PreferencesMessages.MembersOrderPreferencePage_label_description);
95
96         String JavaDoc memberSortString= getPreferenceStore().getString(PREF_OUTLINE_SORT_OPTION);
97         
98         String JavaDoc upLabel= PreferencesMessages.MembersOrderPreferencePage_category_button_up;
99         String JavaDoc downLabel= PreferencesMessages.MembersOrderPreferencePage_category_button_down;
100
101         // category sort
102

103         fSortOrderList= new ListDialogField(null, new String JavaDoc[] { upLabel, downLabel }, new MemberSortLabelProvider());
104         fSortOrderList.setDownButtonIndex(1);
105         fSortOrderList.setUpButtonIndex(0);
106         
107         //validate entries stored in store, false get defaults
108
List JavaDoc entries= parseList(memberSortString);
109         if (!isValidEntries(entries, ALL_SORTMEMBER_ENTRIES)) {
110             memberSortString= getPreferenceStore().getDefaultString(PREF_OUTLINE_SORT_OPTION);
111             entries= parseList(memberSortString);
112         }
113         
114         fSortOrderList.setElements(entries);
115         
116         // visibility sort
117

118         fUseVisibilitySort= getPreferenceStore().getBoolean(PREF_USE_VISIBILITY_SORT_OPTION);
119
120         String JavaDoc visibilitySortString= getPreferenceStore().getString(PREF_VISIBILITY_SORT_OPTION);
121         
122         upLabel= PreferencesMessages.MembersOrderPreferencePage_visibility_button_up;
123         downLabel= PreferencesMessages.MembersOrderPreferencePage_visibility_button_down;
124         
125         fVisibilityOrderList= new ListDialogField(null, new String JavaDoc[] { upLabel, downLabel }, new VisibilitySortLabelProvider());
126         fVisibilityOrderList.setDownButtonIndex(1);
127         fVisibilityOrderList.setUpButtonIndex(0);
128         
129         //validate entries stored in store, false get defaults
130
entries= parseList(visibilitySortString);
131         if (!isValidEntries(entries, ALL_VISIBILITY_ENTRIES)) {
132             visibilitySortString= getPreferenceStore().getDefaultString(PREF_VISIBILITY_SORT_OPTION);
133             entries= parseList(visibilitySortString);
134         }
135         fVisibilityOrderList.setElements(entries);
136     }
137     
138     private static List JavaDoc parseList(String JavaDoc string) {
139         StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(string, ","); //$NON-NLS-1$
140
List JavaDoc entries= new ArrayList JavaDoc();
141         for (int i= 0; tokenizer.hasMoreTokens(); i++) {
142             String JavaDoc token= tokenizer.nextToken();
143             entries.add(token);
144         }
145         return entries;
146     }
147     
148     /*
149      * @see PreferencePage#createControl(Composite)
150      */

151     public void createControl(Composite parent) {
152         super.createControl(parent);
153         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.SORT_ORDER_PREFERENCE_PAGE);
154     }
155
156     /*
157      * @see org.eclipse.jface.preference.PreferencePage#createContents(Composite)
158      */

159     protected Control createContents(Composite parent) {
160         // Create both the dialog lists
161
Composite sortComposite= new Composite(parent, SWT.NONE);
162         sortComposite.setFont(parent.getFont());
163
164         GridLayout layout= new GridLayout();
165         layout.numColumns= 2;
166         layout.marginWidth= 0;
167         layout.marginHeight= 0;
168         sortComposite.setLayout(layout);
169
170         GridData gd= new GridData();
171         gd.verticalAlignment= GridData.FILL;
172         gd.horizontalAlignment= GridData.FILL_HORIZONTAL;
173         sortComposite.setLayoutData(gd);
174
175         createListDialogField(sortComposite, fSortOrderList);
176         
177         fUseVisibilitySortField= new SelectionButtonDialogField(SWT.CHECK);
178         fUseVisibilitySortField.setDialogFieldListener(new IDialogFieldListener() {
179             public void dialogFieldChanged(DialogField field) {
180                 fVisibilityOrderList.setEnabled(fUseVisibilitySortField.isSelected());
181             }
182         });
183         fUseVisibilitySortField.setLabelText(PreferencesMessages.MembersOrderPreferencePage_usevisibilitysort_label);
184         fUseVisibilitySortField.doFillIntoGrid(sortComposite, 2);
185         fUseVisibilitySortField.setSelection(fUseVisibilitySort);
186         
187         createListDialogField(sortComposite, fVisibilityOrderList);
188         fVisibilityOrderList.setEnabled(fUseVisibilitySortField.isSelected());
189         
190         Dialog.applyDialogFont(sortComposite);
191         
192         return sortComposite;
193     }
194     
195
196     private void createListDialogField(Composite composite, ListDialogField dialogField) {
197         Control list= dialogField.getListControl(composite);
198         GridData gd= new GridData();
199         gd.horizontalAlignment= GridData.FILL;
200         gd.grabExcessHorizontalSpace= true;
201         gd.verticalAlignment= GridData.FILL;
202         gd.grabExcessVerticalSpace= true;
203         gd.widthHint= convertWidthInCharsToPixels(50);
204
205         list.setLayoutData(gd);
206         
207         Composite buttons= dialogField.getButtonBox(composite);
208         gd= new GridData();
209         gd.horizontalAlignment= GridData.FILL;
210         gd.grabExcessHorizontalSpace= false;
211         gd.verticalAlignment= GridData.FILL;
212         gd.grabExcessVerticalSpace= true;
213         
214         buttons.setLayoutData(gd);
215     }
216
217     /*
218      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(IWorkbench)
219      */

220     public void init(IWorkbench workbench) {
221     }
222
223     /*
224      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
225      */

226     protected void performDefaults() {
227         IPreferenceStore prefs= JavaPlugin.getDefault().getPreferenceStore();
228         String JavaDoc str= prefs.getDefaultString(PREF_OUTLINE_SORT_OPTION);
229         if (str != null)
230             fSortOrderList.setElements(parseList(str));
231         else
232             fSortOrderList.setElements(parseList(ALL_SORTMEMBER_ENTRIES));
233     
234         str= prefs.getDefaultString(PREF_VISIBILITY_SORT_OPTION);
235         if (str != null)
236             fVisibilityOrderList.setElements(parseList(str));
237         else
238             fVisibilityOrderList.setElements(parseList(ALL_VISIBILITY_ENTRIES));
239     
240         fUseVisibilitySortField.setSelection(prefs.getDefaultBoolean(PREF_USE_VISIBILITY_SORT_OPTION));
241         
242         super.performDefaults();
243     }
244
245     /*
246      * @see org.eclipse.jface.preference.IPreferencePage#performOk()
247      */

248     //reorders elements in the Outline based on selection
249
public boolean performOk() {
250
251         //save preferences for both dialog lists
252
IPreferenceStore store= getPreferenceStore();
253         updateList(store, fSortOrderList, PREF_OUTLINE_SORT_OPTION);
254         updateList(store, fVisibilityOrderList, PREF_VISIBILITY_SORT_OPTION);
255         
256         //update the button setting
257
store.setValue(PREF_USE_VISIBILITY_SORT_OPTION, fUseVisibilitySortField.isSelected());
258         JavaPlugin.getDefault().savePluginPreferences();
259         
260         return true;
261     }
262     
263     private void updateList(IPreferenceStore store, ListDialogField list, String JavaDoc str) {
264         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
265         List JavaDoc curr= list.getElements();
266         for (Iterator JavaDoc iter= curr.iterator(); iter.hasNext();) {
267             String JavaDoc s= (String JavaDoc) iter.next();
268             buf.append(s);
269             buf.append(',');
270         }
271         store.setValue(str, buf.toString());
272     }
273
274     private class MemberSortLabelProvider extends LabelProvider {
275
276         public MemberSortLabelProvider() {
277         }
278         
279         /*
280         * @see org.eclipse.jface.viewers.ILabelProvider#getImage(Object)
281         */

282         public Image getImage(Object JavaDoc element) {
283             //access to image registry
284
ImageDescriptorRegistry registry= JavaPlugin.getImageDescriptorRegistry();
285             ImageDescriptor descriptor= null;
286
287             if (element instanceof String JavaDoc) {
288                 int visibility= Flags.AccPublic;
289                 String JavaDoc s= (String JavaDoc) element;
290                 if (s.equals(FIELDS)) {
291                     //0 will give the default field image
292
descriptor= JavaElementImageProvider.getFieldImageDescriptor(false, visibility);
293                 } else if (s.equals(CONSTRUCTORS)) {
294                     descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, visibility);
295                     //add a constructor adornment to the image descriptor
296
descriptor= new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.CONSTRUCTOR, JavaElementImageProvider.SMALL_SIZE);
297                 } else if (s.equals(METHODS)) {
298                     descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, visibility);
299                 } else if (s.equals(STATIC_FIELDS)) {
300                     descriptor= JavaElementImageProvider.getFieldImageDescriptor(false, visibility);
301                     //add a static fields adornment to the image descriptor
302
descriptor= new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.STATIC, JavaElementImageProvider.SMALL_SIZE);
303                 } else if (s.equals(STATIC_METHODS)) {
304                     descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, visibility);
305                     //add a static methods adornment to the image descriptor
306
descriptor= new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.STATIC, JavaElementImageProvider.SMALL_SIZE);
307                 } else if (s.equals(INIT)) {
308                     descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, visibility);
309                 } else if (s.equals(STATIC_INIT)) {
310                     descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, visibility);
311                     descriptor= new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.STATIC, JavaElementImageProvider.SMALL_SIZE);
312                 } else if (s.equals(TYPES)) {
313                     descriptor= JavaElementImageProvider.getTypeImageDescriptor(true, false, Flags.AccPublic, false);
314                 } else {
315                     descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, Flags.AccPublic);
316                 }
317                 return registry.get(descriptor);
318             }
319             return null;
320         }
321
322         /*
323          * @see org.eclipse.jface.viewers.ILabelProvider#getText(Object)
324          */

325         public String JavaDoc getText(Object JavaDoc element) {
326
327             if (element instanceof String JavaDoc) {
328                 String JavaDoc s= (String JavaDoc) element;
329                 if (s.equals(FIELDS)) {
330                     return PreferencesMessages.MembersOrderPreferencePage_fields_label;
331                 } else if (s.equals(METHODS)) {
332                     return PreferencesMessages.MembersOrderPreferencePage_methods_label;
333                 } else if (s.equals(STATIC_FIELDS)) {
334                     return PreferencesMessages.MembersOrderPreferencePage_staticfields_label;
335                 } else if (s.equals(STATIC_METHODS)) {
336                     return PreferencesMessages.MembersOrderPreferencePage_staticmethods_label;
337                 } else if (s.equals(CONSTRUCTORS)) {
338                     return PreferencesMessages.MembersOrderPreferencePage_constructors_label;
339                 } else if (s.equals(INIT)) {
340                     return PreferencesMessages.MembersOrderPreferencePage_initialisers_label;
341                 } else if (s.equals(STATIC_INIT)) {
342                     return PreferencesMessages.MembersOrderPreferencePage_staticinitialisers_label;
343                 } else if (s.equals(TYPES)) {
344                     return PreferencesMessages.MembersOrderPreferencePage_types_label;
345                 }
346             }
347             return ""; //$NON-NLS-1$
348
}
349     }
350
351     
352     private class VisibilitySortLabelProvider extends LabelProvider {
353         
354         public VisibilitySortLabelProvider() {
355         }
356         
357         /*
358          * @see org.eclipse.jface.viewers.ILabelProvider#getImage(Object)
359          */

360         public Image getImage(Object JavaDoc element) {
361             //access to image registry
362
ImageDescriptorRegistry registry= JavaPlugin.getImageDescriptorRegistry();
363             ImageDescriptor descriptor= null;
364             
365             if (element instanceof String JavaDoc) {
366                 String JavaDoc s= (String JavaDoc) element;
367                 if (s.equals(PUBLIC)) {
368                     descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, Flags.AccPublic);
369                 } else if (s.equals(PRIVATE)) {
370                     descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, Flags.AccPrivate);
371                 } else if (s.equals(PROTECTED)) {
372                     descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, Flags.AccProtected);
373                 } else if (s.equals(DEFAULT)) {
374                     descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, Flags.AccDefault);
375                 }
376                 return registry.get(descriptor);
377             }
378             return null;
379         }
380         
381         /*
382          * @see org.eclipse.jface.viewers.ILabelProvider#getText(Object)
383          */

384         public String JavaDoc getText(Object JavaDoc element) {
385             if (element instanceof String JavaDoc) {
386                 String JavaDoc s= (String JavaDoc) element;
387                 
388                 if (s.equals(PUBLIC)) {
389                     return PreferencesMessages.MembersOrderPreferencePage_public_label;
390                 } else if (s.equals(PRIVATE)) {
391                     return PreferencesMessages.MembersOrderPreferencePage_private_label;
392                 } else if (s.equals(PROTECTED)) {
393                     return PreferencesMessages.MembersOrderPreferencePage_protected_label;
394                 } else if (s.equals(DEFAULT)) {
395                     return PreferencesMessages.MembersOrderPreferencePage_default_label;
396                 }
397             }
398             return ""; //$NON-NLS-1$
399
}
400     }
401
402     
403
404 }
405
Popular Tags