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;
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;
42 public class MembersOrderPreferencePage extends PreferencePage implements
43 IWorkbenchPreferencePage {
45 private static final String ALL_ENTRIES = "T,SI,SF,SM,I,F,C,M"; //$NON-NLS-1$
47 private static final String PREF_OUTLINE_SORT_OPTION = PreferenceConstants.APPEARANCE_MEMBER_SORT_ORDER;
49 public static final String CONSTRUCTORS = "C"; //$NON-NLS-1$
51 public static final String FIELDS = "F"; //$NON-NLS-1$
53 public static final String METHODS = "M"; //$NON-NLS-1$
55 public static final String STATIC_METHODS = "SM"; //$NON-NLS-1$
57 public static final String STATIC_FIELDS = "SF"; //$NON-NLS-1$
59 public static final String INIT = "I"; //$NON-NLS-1$
61 public static final String STATIC_INIT = "SI"; //$NON-NLS-1$
63 public static final String TYPES = "T"; //$NON-NLS-1$
65 private ListDialogField fSortOrderList;
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();
77 private static boolean isValidEntries(List entries) {
78 StringTokenizer tokenizer = new StringTokenizer(ALL_ENTRIES, ","); //$NON-NLS-1$
80 for (; tokenizer.hasMoreTokens(); i++) {
81 String token = tokenizer.nextToken();
82 if (!entries.contains(token))
85 return i == entries.size();
88 public MembersOrderPreferencePage() {
89 // set the preference store
90 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
92 setDescription(PreferencesMessages
93 .getString("MembersOrderPreferencePage.label.description")); //$NON-NLS-1$
95 String string = getPreferenceStore()
96 .getString(PREF_OUTLINE_SORT_OPTION);
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 };
104 fSortOrderList = new ListDialogField(null, buttonlabels,
105 new MemberSortLabelProvider());
106 fSortOrderList.setDownButtonIndex(1);
107 fSortOrderList.setUpButtonIndex(0);
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);
117 fSortOrderList.setElements(entries);
121 * @see PreferencePage#createControl(Composite)
123 public void createControl(Composite parent) {
124 super.createControl(parent);
125 // WorkbenchHelp.setHelp(getControl(),
126 // IJavaHelpContextIds.SORT_ORDER_PREFERENCE_PAGE);
130 * @see org.eclipse.jface.preference.PreferencePage#createContents(Composite)
132 protected Control createContents(Composite parent) {
134 Composite composite = new Composite(parent, SWT.NONE);
136 GridLayout layout = new GridLayout();
137 layout.numColumns = 3;
138 layout.marginWidth = 0;
139 layout.marginHeight = 0;
140 composite.setLayout(layout);
142 GridData data = new GridData();
143 data.verticalAlignment = GridData.FILL;
144 data.horizontalAlignment = GridData.FILL_HORIZONTAL;
145 composite.setLayoutData(data);
147 createSortOrderListDialogField(composite, 3);
148 Dialog.applyDialogFont(composite);
152 private void createSortOrderListDialogField(Composite composite, int span) {
153 fSortOrderList.doFillIntoGrid(composite, span);
155 fSortOrderList.getLabelControl(null).dispose();
157 GridData data = (GridData) fSortOrderList.getListControl(null)
159 data.grabExcessHorizontalSpace = true;
160 data.verticalAlignment = 0;
161 data.heightHint = SWTUtil.getTableHeightHint(fSortOrderList
162 .getTableViewer().getTable(), 8);
166 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(IWorkbench)
168 public void init(IWorkbench workbench) {
172 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
174 protected void performDefaults() {
175 String string = getPreferenceStore().getDefaultString(
176 PREF_OUTLINE_SORT_OPTION);
177 fSortOrderList.setElements(getSortOrderList(string));
181 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
183 // reorders elements in the Outline based on selection
184 public boolean performOk() {
185 // update outline view
188 IPreferenceStore store = getPreferenceStore();
190 StringBuffer buf = new StringBuffer();
191 List curr = fSortOrderList.getElements();
192 for (Iterator iter = curr.iterator(); iter.hasNext();) {
193 String s = (String) iter.next();
197 store.setValue(PREF_OUTLINE_SORT_OPTION, buf.toString());
198 PHPeclipsePlugin.getDefault().savePluginPreferences();
202 private class MemberSortLabelProvider extends LabelProvider {
204 public MemberSortLabelProvider() {
208 * @see org.eclipse.jface.viewers.ILabelProvider#getText(Object)
210 public String getText(Object element) {
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$
240 return ""; //$NON-NLS-1$
244 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(Object)
246 public Image getImage(Object element) {
247 // access to image registry
248 ImageDescriptorRegistry registry = PHPeclipsePlugin
249 .getImageDescriptorRegistry();
250 ImageDescriptor descriptor = null;
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,
297 descriptor = JavaElementImageProvider
298 .getMethodImageDescriptor(false, Flags.AccPublic);
300 return registry.get(descriptor);