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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.preferences;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.StringTokenizer;
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;
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;
43 public class MembersOrderPreferencePage extends PreferencePage implements
44 IWorkbenchPreferencePage {
46 private static final String ALL_ENTRIES = "T,SI,SF,SM,I,F,C,M"; //$NON-NLS-1$
48 private static final String PREF_OUTLINE_SORT_OPTION = PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER;
50 public static final String CONSTRUCTORS = "C"; //$NON-NLS-1$
52 public static final String FIELDS = "F"; //$NON-NLS-1$
54 public static final String METHODS = "M"; //$NON-NLS-1$
56 public static final String STATIC_METHODS = "SM"; //$NON-NLS-1$
58 public static final String STATIC_FIELDS = "SF"; //$NON-NLS-1$
60 public static final String INIT = "I"; //$NON-NLS-1$
62 public static final String STATIC_INIT = "SI"; //$NON-NLS-1$
64 public static final String TYPES = "T"; //$NON-NLS-1$
66 private ListDialogField fSortOrderList;
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();
78 private static boolean isValidEntries(List entries) {
79 StringTokenizer tokenizer = new StringTokenizer(ALL_ENTRIES, ","); //$NON-NLS-1$
81 for (; tokenizer.hasMoreTokens(); i++) {
82 String token = tokenizer.nextToken();
83 if (!entries.contains(token))
86 return i == entries.size();
89 public MembersOrderPreferencePage() {
90 // set the preference store
91 setPreferenceStore(WebUI.getDefault().getPreferenceStore());
93 setDescription(PreferencesMessages
94 .getString("MembersOrderPreferencePage.label.description")); //$NON-NLS-1$
96 String string = getPreferenceStore()
97 .getString(PREF_OUTLINE_SORT_OPTION);
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 };
105 fSortOrderList = new ListDialogField(null, buttonlabels,
106 new MemberSortLabelProvider());
107 fSortOrderList.setDownButtonIndex(1);
108 fSortOrderList.setUpButtonIndex(0);
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);
118 fSortOrderList.setElements(entries);
122 * @see PreferencePage#createControl(Composite)
124 public void createControl(Composite parent) {
125 super.createControl(parent);
126 // WorkbenchHelp.setHelp(getControl(),
127 // IJavaHelpContextIds.SORT_ORDER_PREFERENCE_PAGE);
131 * @see org.eclipse.jface.preference.PreferencePage#createContents(Composite)
133 protected Control createContents(Composite parent) {
135 Composite composite = new Composite(parent, SWT.NONE);
137 GridLayout layout = new GridLayout();
138 layout.numColumns = 3;
139 layout.marginWidth = 0;
140 layout.marginHeight = 0;
141 composite.setLayout(layout);
143 GridData data = new GridData();
144 data.verticalAlignment = GridData.FILL;
145 data.horizontalAlignment = GridData.FILL_HORIZONTAL;
146 composite.setLayoutData(data);
148 createSortOrderListDialogField(composite, 3);
149 Dialog.applyDialogFont(composite);
153 private void createSortOrderListDialogField(Composite composite, int span) {
154 fSortOrderList.doFillIntoGrid(composite, span);
156 fSortOrderList.getLabelControl(null).dispose();
158 GridData data = (GridData) fSortOrderList.getListControl(null)
160 data.grabExcessHorizontalSpace = true;
161 data.verticalAlignment = 0;
162 data.heightHint = SWTUtil.getTableHeightHint(fSortOrderList
163 .getTableViewer().getTable(), 8);
167 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(IWorkbench)
169 public void init(IWorkbench workbench) {
173 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
175 protected void performDefaults() {
176 String string = getPreferenceStore().getDefaultString(
177 PREF_OUTLINE_SORT_OPTION);
178 fSortOrderList.setElements(getSortOrderList(string));
182 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
184 // reorders elements in the Outline based on selection
185 public boolean performOk() {
186 // update outline view
189 IPreferenceStore store = getPreferenceStore();
191 StringBuffer buf = new StringBuffer();
192 List curr = fSortOrderList.getElements();
193 for (Iterator iter = curr.iterator(); iter.hasNext();) {
194 String s = (String) iter.next();
198 store.setValue(PREF_OUTLINE_SORT_OPTION, buf.toString());
199 WebUI.getDefault().savePluginPreferences();
203 private class MemberSortLabelProvider extends LabelProvider {
205 public MemberSortLabelProvider() {
209 * @see org.eclipse.jface.viewers.ILabelProvider#getText(Object)
211 public String getText(Object element) {
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$
241 return ""; //$NON-NLS-1$
245 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(Object)
247 public Image getImage(Object element) {
248 // access to image registry
249 ImageDescriptorRegistry registry = WebUI
250 .getImageDescriptorRegistry();
251 ImageDescriptor descriptor = null;
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,
298 descriptor = JavaElementImageProvider
299 .getMethodImageDescriptor(false, Flags.AccPublic);
301 return registry.get(descriptor);