intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / preferences / CssPreferencePage.java
1 /*
2  * Copyright (c) 2003-2004 Christopher Lenz 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  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: CssPreferencePage.java,v 1.1 2004-09-02 18:11:50 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.preferences;
15
16 import java.util.Arrays;
17 import java.util.Comparator;
18
19 import net.sourceforge.phpeclipse.css.core.CssCore;
20 import net.sourceforge.phpeclipse.css.core.internal.CssCorePreferences;
21 import net.sourceforge.phpeclipse.css.core.profiles.IProfileDescriptor;
22 import net.sourceforge.phpeclipse.css.core.profiles.IProfileManager;
23 import net.sourceforge.phpeclipse.css.ui.internal.CssUIMessages;
24
25 import org.eclipse.core.runtime.Preferences;
26 import org.eclipse.jface.preference.PreferencePage;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Group;
35 import org.eclipse.ui.IWorkbench;
36 import org.eclipse.ui.IWorkbenchPreferencePage;
37
38 /**
39  * 
40  */
41 public class CssPreferencePage extends PreferencePage
42         implements IWorkbenchPreferencePage {
43
44         // Instance Variables ------------------------------------------------------
45
46         /**
47          * The CSS core preferences.
48          */
49         private Preferences preferences;
50
51         /**
52          * The radio buttons used to select the default profile.
53          */
54         private Button[] profileButtons;
55
56         /**
57          * The ID of the currently selected CSS profile.
58          */
59         private String selectedProfile;
60
61         // Constructors ------------------------------------------------------------
62
63         /**
64          * Default constructor.
65          */
66         public CssPreferencePage() {
67                 super(getString("description")); //$NON-NLS-1$
68                 preferences = CssCore.getDefault().getPluginPreferences();
69         }
70
71         // PreferencePage Implementation -------------------------------------------
72
73         /* 
74          * @see PreferencePage#createContents(Composite)
75          */
76         protected Control createContents(Composite parent) {
77                 Composite composite = new Composite(parent, SWT.NONE);
78                 GridLayout layout = new GridLayout();
79                 layout.numColumns = 1;
80                 composite.setLayout(layout);
81
82                 createProfileRadioGroup(composite);
83
84                 return composite;
85         }
86
87         /* 
88          * @see PreferencePage#performDefaults()
89          */
90         protected void performDefaults() {
91                 preferences.setToDefault(CssCorePreferences.PROFILE);
92                 selectedProfile = preferences.getString(CssCorePreferences.PROFILE);
93                 if (selectedProfile != null) {
94                         for (int i = 0; i < profileButtons.length; i++) {
95                                 Button button = profileButtons[i];
96                                 if (((String) button.getData()).equals(selectedProfile)) {
97                                         button.setSelection(true);
98                                 } else {
99                                         button.setSelection(false);
100                                 }
101                         }
102                 }
103         }
104
105         /* 
106          * @see PreferencePage#performOk()
107          */
108         public boolean performOk() {
109                 preferences.setValue(CssCorePreferences.PROFILE, selectedProfile);
110                 return super.performOk();
111         }
112
113         // IWorkbenchPreferencePage Implementation ---------------------------------
114
115         /**
116          * @see IWorkbenchPreferencePage#init(IWorkbench)
117          */
118         public void init(IWorkbench workbench) {
119                 selectedProfile = preferences.getString(CssCorePreferences.PROFILE);
120         }
121
122         // Private Methods ---------------------------------------------------------
123
124         /**
125          * Creates the radio button group for selecting the default CSS profile.
126          * 
127          * @param composite the composite to add the group to
128          */
129         private void createProfileRadioGroup(Composite composite) {
130                 Group profileGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);
131                 GridLayout layout = new GridLayout();
132                 layout.numColumns = 1;
133                 profileGroup.setLayout(layout);
134                 profileGroup.setText(getString("profile")); //$NON-NLS-1$
135
136                 IProfileDescriptor[] profiles = getProfileDescriptors();
137                 profileButtons = new Button[profiles.length];
138                 for (int i = 0; i < profiles.length; i++) {
139                         Button button = new Button(profileGroup, SWT.RADIO | SWT.LEFT);
140                         button.setText(profiles[i].getName());
141                         button.setData(profiles[i].getId());
142                         if (profiles[i].getId().equals(selectedProfile)) {
143                                 button.setSelection(true);
144                         }
145                         button.addSelectionListener(new SelectionAdapter() {
146                                 public void widgetSelected(SelectionEvent event) {
147                                         selectedProfile = (String) event.widget.getData();
148                                 }
149                         });
150                         profileButtons[i] = button;
151                 }
152         }
153
154         /**
155          * Returns all available profile descriptors, alphabetically sorted by 
156          * display name.
157          * 
158          * @return the profile descriptors
159          */
160         private IProfileDescriptor[] getProfileDescriptors() {
161                 IProfileManager mgr = CssCore.getDefault().getProfileManager();
162                 IProfileDescriptor[] profiles = mgr.getProfileDescriptors();
163                 Arrays.sort(profiles, new Comparator() {
164                         public int compare(Object o1, Object o2) {
165                                 return ((IProfileDescriptor) o1).getName().compareTo(
166                                                 ((IProfileDescriptor) o2).getName());
167                         }
168                 });
169                 return profiles;
170         }
171
172         private static String getString(String key) {
173                 return CssUIMessages.getString(
174                         "CssPreferencePage." + key); //$NON-NLS-1$
175         }
176
177 }