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