1bc2806ab38827e1e6a518ba530fe247931e70d7
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.internal.ui.preferences;
13
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.StringTokenizer;
18
19 import net.sourceforge.phpdt.core.Flags;
20 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
21 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
22 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementImageProvider;
23 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.ListDialogField;
24 import net.sourceforge.phpdt.ui.JavaElementImageDescriptor;
25 import net.sourceforge.phpdt.ui.PreferenceConstants;
26 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
27 import net.sourceforge.phpeclipse.ui.WebUI;
28
29 import org.eclipse.jface.dialogs.Dialog;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.preference.PreferencePage;
32 import org.eclipse.jface.resource.ImageDescriptor;
33 import org.eclipse.jface.viewers.LabelProvider;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.ui.IWorkbench;
41 import org.eclipse.ui.IWorkbenchPreferencePage;
42
43 public class MembersOrderPreferencePage extends PreferencePage implements
44                 IWorkbenchPreferencePage {
45
46         private static final String ALL_ENTRIES = "T,SI,SF,SM,I,F,C,M"; //$NON-NLS-1$
47
48         private static final String PREF_OUTLINE_SORT_OPTION = PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER;
49
50         public static final String CONSTRUCTORS = "C"; //$NON-NLS-1$
51
52         public static final String FIELDS = "F"; //$NON-NLS-1$
53
54         public static final String METHODS = "M"; //$NON-NLS-1$
55
56         public static final String STATIC_METHODS = "SM"; //$NON-NLS-1$
57
58         public static final String STATIC_FIELDS = "SF"; //$NON-NLS-1$
59
60         public static final String INIT = "I"; //$NON-NLS-1$
61
62         public static final String STATIC_INIT = "SI"; //$NON-NLS-1$
63
64         public static final String TYPES = "T"; //$NON-NLS-1$
65
66         private ListDialogField fSortOrderList;
67
68         private static List getSortOrderList(String string) {
69                 StringTokenizer tokenizer = new StringTokenizer(string, ","); //$NON-NLS-1$
70                 List entries = new ArrayList();
71                 for (int i = 0; tokenizer.hasMoreTokens(); i++) {
72                         String token = tokenizer.nextToken();
73                         entries.add(token);
74                 }
75                 return entries;
76         }
77
78         private static boolean isValidEntries(List entries) {
79                 StringTokenizer tokenizer = new StringTokenizer(ALL_ENTRIES, ","); //$NON-NLS-1$
80                 int i = 0;
81                 for (; tokenizer.hasMoreTokens(); i++) {
82                         String token = tokenizer.nextToken();
83                         if (!entries.contains(token))
84                                 return false;
85                 }
86                 return i == entries.size();
87         }
88
89         public MembersOrderPreferencePage() {
90                 // set the preference store
91                 setPreferenceStore(WebUI.getDefault().getPreferenceStore());
92
93                 setDescription(PreferencesMessages
94                                 .getString("MembersOrderPreferencePage.label.description")); //$NON-NLS-1$
95
96                 String string = getPreferenceStore()
97                                 .getString(PREF_OUTLINE_SORT_OPTION);
98
99                 String upLabel = PreferencesMessages
100                                 .getString("MembersOrderPreferencePage.button.up"); //$NON-NLS-1$
101                 String downLabel = PreferencesMessages
102                                 .getString("MembersOrderPreferencePage.button.down"); //$NON-NLS-1$
103                 String[] buttonlabels = new String[] { upLabel, downLabel };
104
105                 fSortOrderList = new ListDialogField(null, buttonlabels,
106                                 new MemberSortLabelProvider());
107                 fSortOrderList.setDownButtonIndex(1);
108                 fSortOrderList.setUpButtonIndex(0);
109
110                 // validate entries stored in store, false get defaults
111                 List entries = getSortOrderList(string);
112                 if (!isValidEntries(entries)) {
113                         string = WebUI.getDefault().getPreferenceStore()
114                                         .getDefaultString(PREF_OUTLINE_SORT_OPTION);
115                         entries = getSortOrderList(string);
116                 }
117
118                 fSortOrderList.setElements(entries);
119         }
120
121         /*
122          * @see PreferencePage#createControl(Composite)
123          */
124         public void createControl(Composite parent) {
125                 super.createControl(parent);
126                 // WorkbenchHelp.setHelp(getControl(),
127                 // IJavaHelpContextIds.SORT_ORDER_PREFERENCE_PAGE);
128         }
129
130         /*
131          * @see org.eclipse.jface.preference.PreferencePage#createContents(Composite)
132          */
133         protected Control createContents(Composite parent) {
134
135                 Composite composite = new Composite(parent, SWT.NONE);
136
137                 GridLayout layout = new GridLayout();
138                 layout.numColumns = 3;
139                 layout.marginWidth = 0;
140                 layout.marginHeight = 0;
141                 composite.setLayout(layout);
142
143                 GridData data = new GridData();
144                 data.verticalAlignment = GridData.FILL;
145                 data.horizontalAlignment = GridData.FILL_HORIZONTAL;
146                 composite.setLayoutData(data);
147
148                 createSortOrderListDialogField(composite, 3);
149                 Dialog.applyDialogFont(composite);
150                 return composite;
151         }
152
153         private void createSortOrderListDialogField(Composite composite, int span) {
154                 fSortOrderList.doFillIntoGrid(composite, span);
155
156                 fSortOrderList.getLabelControl(null).dispose();
157
158                 GridData data = (GridData) fSortOrderList.getListControl(null)
159                                 .getLayoutData();
160                 data.grabExcessHorizontalSpace = true;
161                 data.verticalAlignment = 0;
162                 data.heightHint = SWTUtil.getTableHeightHint(fSortOrderList
163                                 .getTableViewer().getTable(), 8);
164         }
165
166         /*
167          * @see org.eclipse.ui.IWorkbenchPreferencePage#init(IWorkbench)
168          */
169         public void init(IWorkbench workbench) {
170         }
171
172         /*
173          * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
174          */
175         protected void performDefaults() {
176                 String string = getPreferenceStore().getDefaultString(
177                                 PREF_OUTLINE_SORT_OPTION);
178                 fSortOrderList.setElements(getSortOrderList(string));
179         }
180
181         /*
182          * @see org.eclipse.jface.preference.IPreferencePage#performOk()
183          */
184         // reorders elements in the Outline based on selection
185         public boolean performOk() {
186                 // update outline view
187
188                 // save preferences
189                 IPreferenceStore store = getPreferenceStore();
190
191                 StringBuffer buf = new StringBuffer();
192                 List curr = fSortOrderList.getElements();
193                 for (Iterator iter = curr.iterator(); iter.hasNext();) {
194                         String s = (String) iter.next();
195                         buf.append(s);
196                         buf.append(',');
197                 }
198                 store.setValue(PREF_OUTLINE_SORT_OPTION, buf.toString());
199                 WebUI.getDefault().savePluginPreferences();
200                 return true;
201         }
202
203         private class MemberSortLabelProvider extends LabelProvider {
204
205                 public MemberSortLabelProvider() {
206                 }
207
208                 /*
209                  * @see org.eclipse.jface.viewers.ILabelProvider#getText(Object)
210                  */
211                 public String getText(Object element) {
212
213                         if (element instanceof String) {
214                                 String s = (String) element;
215                                 if (s.equals(FIELDS)) {
216                                         return PreferencesMessages
217                                                         .getString("MembersOrderPreferencePage.fields.label"); //$NON-NLS-1$
218                                 } else if (s.equals(CONSTRUCTORS)) {
219                                         return PreferencesMessages
220                                                         .getString("MembersOrderPreferencePage.constructors.label"); //$NON-NLS-1$
221                                 } else if (s.equals(METHODS)) {
222                                         return PreferencesMessages
223                                                         .getString("MembersOrderPreferencePage.methods.label"); //$NON-NLS-1$
224                                 } else if (s.equals(STATIC_FIELDS)) {
225                                         return PreferencesMessages
226                                                         .getString("MembersOrderPreferencePage.staticfields.label"); //$NON-NLS-1$
227                                 } else if (s.equals(STATIC_METHODS)) {
228                                         return PreferencesMessages
229                                                         .getString("MembersOrderPreferencePage.staticmethods.label"); //$NON-NLS-1$
230                                 } else if (s.equals(INIT)) {
231                                         return PreferencesMessages
232                                                         .getString("MembersOrderPreferencePage.initialisers.label"); //$NON-NLS-1$
233                                 } else if (s.equals(STATIC_INIT)) {
234                                         return PreferencesMessages
235                                                         .getString("MembersOrderPreferencePage.staticinitialisers.label"); //$NON-NLS-1$
236                                 } else if (s.equals(TYPES)) {
237                                         return PreferencesMessages
238                                                         .getString("MembersOrderPreferencePage.types.label"); //$NON-NLS-1$
239                                 }
240                         }
241                         return ""; //$NON-NLS-1$
242                 }
243
244                 /*
245                  * @see org.eclipse.jface.viewers.ILabelProvider#getImage(Object)
246                  */
247                 public Image getImage(Object element) {
248                         // access to image registry
249                         ImageDescriptorRegistry registry = WebUI
250                                         .getImageDescriptorRegistry();
251                         ImageDescriptor descriptor = null;
252
253                         if (element instanceof String) {
254                                 int visibility = Flags.AccPublic;
255                                 String s = (String) element;
256                                 if (s.equals(FIELDS)) {
257                                         // 0 will give the default field image
258                                         descriptor = JavaElementImageProvider
259                                                         .getFieldImageDescriptor(false, visibility);
260                                 } else if (s.equals(CONSTRUCTORS)) {
261                                         descriptor = JavaElementImageProvider
262                                                         .getMethodImageDescriptor(false, visibility);
263                                         // add a constructor adornment to the image descriptor
264                                         descriptor = new JavaElementImageDescriptor(descriptor,
265                                                         JavaElementImageDescriptor.CONSTRUCTOR,
266                                                         JavaElementImageProvider.SMALL_SIZE);
267                                 } else if (s.equals(METHODS)) {
268                                         descriptor = JavaElementImageProvider
269                                                         .getMethodImageDescriptor(false, visibility);
270                                 } else if (s.equals(STATIC_FIELDS)) {
271                                         descriptor = JavaElementImageProvider
272                                                         .getFieldImageDescriptor(false, visibility);
273                                         // add a constructor adornment to the image descriptor
274                                         descriptor = new JavaElementImageDescriptor(descriptor,
275                                                         JavaElementImageDescriptor.STATIC,
276                                                         JavaElementImageProvider.SMALL_SIZE);
277                                 } else if (s.equals(STATIC_METHODS)) {
278                                         descriptor = JavaElementImageProvider
279                                                         .getMethodImageDescriptor(false, visibility);
280                                         // add a constructor adornment to the image descriptor
281                                         descriptor = new JavaElementImageDescriptor(descriptor,
282                                                         JavaElementImageDescriptor.STATIC,
283                                                         JavaElementImageProvider.SMALL_SIZE);
284                                 } else if (s.equals(INIT)) {
285                                         descriptor = JavaElementImageProvider
286                                                         .getMethodImageDescriptor(false, visibility);
287                                 } else if (s.equals(STATIC_INIT)) {
288                                         descriptor = JavaElementImageProvider
289                                                         .getMethodImageDescriptor(false, visibility);
290                                         descriptor = new JavaElementImageDescriptor(descriptor,
291                                                         JavaElementImageDescriptor.STATIC,
292                                                         JavaElementImageProvider.SMALL_SIZE);
293                                 } else if (s.equals(TYPES)) {
294                                         descriptor = JavaElementImageProvider
295                                                         .getTypeImageDescriptor(false, true,
296                                                                         Flags.AccPublic);
297                                 } else {
298                                         descriptor = JavaElementImageProvider
299                                                         .getMethodImageDescriptor(false, Flags.AccPublic);
300                                 }
301                                 return registry.get(descriptor);
302                         }
303                         return null;
304                 }
305
306         }
307
308 }