/* * Copyright (c) 2003-2004 Christopher Lenz and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * Christopher Lenz - initial API and implementation * * $Id: CssPreferencePage.java,v 1.1 2004-09-02 18:11:50 jsurfer Exp $ */ package net.sourceforge.phpeclipse.css.ui.internal.preferences; import java.util.Arrays; import java.util.Comparator; import net.sourceforge.phpeclipse.css.core.CssCore; import net.sourceforge.phpeclipse.css.core.internal.CssCorePreferences; import net.sourceforge.phpeclipse.css.core.profiles.IProfileDescriptor; import net.sourceforge.phpeclipse.css.core.profiles.IProfileManager; import net.sourceforge.phpeclipse.css.ui.internal.CssUIMessages; import org.eclipse.core.runtime.Preferences; import org.eclipse.jface.preference.PreferencePage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Group; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPreferencePage; /** * */ public class CssPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { // Instance Variables ------------------------------------------------------ /** * The CSS core preferences. */ private Preferences preferences; /** * The radio buttons used to select the default profile. */ private Button[] profileButtons; /** * The ID of the currently selected CSS profile. */ private String selectedProfile; // Constructors ------------------------------------------------------------ /** * Default constructor. */ public CssPreferencePage() { super(getString("description")); //$NON-NLS-1$ preferences = CssCore.getDefault().getPluginPreferences(); } // PreferencePage Implementation ------------------------------------------- /* * @see PreferencePage#createContents(Composite) */ protected Control createContents(Composite parent) { Composite composite = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(); layout.numColumns = 1; composite.setLayout(layout); createProfileRadioGroup(composite); return composite; } /* * @see PreferencePage#performDefaults() */ protected void performDefaults() { preferences.setToDefault(CssCorePreferences.PROFILE); selectedProfile = preferences.getString(CssCorePreferences.PROFILE); if (selectedProfile != null) { for (int i = 0; i < profileButtons.length; i++) { Button button = profileButtons[i]; if (((String) button.getData()).equals(selectedProfile)) { button.setSelection(true); } else { button.setSelection(false); } } } } /* * @see PreferencePage#performOk() */ public boolean performOk() { preferences.setValue(CssCorePreferences.PROFILE, selectedProfile); return super.performOk(); } // IWorkbenchPreferencePage Implementation --------------------------------- /** * @see IWorkbenchPreferencePage#init(IWorkbench) */ public void init(IWorkbench workbench) { selectedProfile = preferences.getString(CssCorePreferences.PROFILE); } // Private Methods --------------------------------------------------------- /** * Creates the radio button group for selecting the default CSS profile. * * @param composite the composite to add the group to */ private void createProfileRadioGroup(Composite composite) { Group profileGroup = new Group(composite, SWT.SHADOW_ETCHED_IN); GridLayout layout = new GridLayout(); layout.numColumns = 1; profileGroup.setLayout(layout); profileGroup.setText(getString("profile")); //$NON-NLS-1$ IProfileDescriptor[] profiles = getProfileDescriptors(); profileButtons = new Button[profiles.length]; for (int i = 0; i < profiles.length; i++) { Button button = new Button(profileGroup, SWT.RADIO | SWT.LEFT); button.setText(profiles[i].getName()); button.setData(profiles[i].getId()); if (profiles[i].getId().equals(selectedProfile)) { button.setSelection(true); } button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { selectedProfile = (String) event.widget.getData(); } }); profileButtons[i] = button; } } /** * Returns all available profile descriptors, alphabetically sorted by * display name. * * @return the profile descriptors */ private IProfileDescriptor[] getProfileDescriptors() { IProfileManager mgr = CssCore.getDefault().getProfileManager(); IProfileDescriptor[] profiles = mgr.getProfileDescriptors(); Arrays.sort(profiles, new Comparator() { public int compare(Object o1, Object o2) { return ((IProfileDescriptor) o1).getName().compareTo( ((IProfileDescriptor) o2).getName()); } }); return profiles; } private static String getString(String key) { return CssUIMessages.getString( "CssPreferencePage." + key); //$NON-NLS-1$ } }