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
9 * Christopher Lenz - initial API and implementation
11 * $Id: CssPreferencePage.java,v 1.1 2004-09-02 18:11:50 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.preferences;
16 import java.util.Arrays;
17 import java.util.Comparator;
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;
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;
41 public class CssPreferencePage extends PreferencePage
42 implements IWorkbenchPreferencePage {
44 // Instance Variables ------------------------------------------------------
47 * The CSS core preferences.
49 private Preferences preferences;
52 * The radio buttons used to select the default profile.
54 private Button[] profileButtons;
57 * The ID of the currently selected CSS profile.
59 private String selectedProfile;
61 // Constructors ------------------------------------------------------------
64 * Default constructor.
66 public CssPreferencePage() {
67 super(getString("description")); //$NON-NLS-1$
68 preferences = CssCore.getDefault().getPluginPreferences();
71 // PreferencePage Implementation -------------------------------------------
74 * @see PreferencePage#createContents(Composite)
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);
82 createProfileRadioGroup(composite);
88 * @see PreferencePage#performDefaults()
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);
99 button.setSelection(false);
106 * @see PreferencePage#performOk()
108 public boolean performOk() {
109 preferences.setValue(CssCorePreferences.PROFILE, selectedProfile);
110 return super.performOk();
113 // IWorkbenchPreferencePage Implementation ---------------------------------
116 * @see IWorkbenchPreferencePage#init(IWorkbench)
118 public void init(IWorkbench workbench) {
119 selectedProfile = preferences.getString(CssCorePreferences.PROFILE);
122 // Private Methods ---------------------------------------------------------
125 * Creates the radio button group for selecting the default CSS profile.
127 * @param composite the composite to add the group to
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$
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);
145 button.addSelectionListener(new SelectionAdapter() {
146 public void widgetSelected(SelectionEvent event) {
147 selectedProfile = (String) event.widget.getData();
150 profileButtons[i] = button;
155 * Returns all available profile descriptors, alphabetically sorted by
158 * @return the profile descriptors
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());
172 private static String getString(String key) {
173 return CssUIMessages.getString(
174 "CssPreferencePage." + key); //$NON-NLS-1$