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