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: CssPropertyPage.java,v 1.1 2004-09-02 18:11:49 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.properties;
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.internal.profiles.ProfileManager;
22 import net.sourceforge.phpeclipse.css.core.profiles.IProfileDescriptor;
23 import net.sourceforge.phpeclipse.css.core.profiles.IProfileManager;
24 import net.sourceforge.phpeclipse.css.ui.internal.CssUIMessages;
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.core.runtime.Preferences;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Group;
38 import org.eclipse.ui.dialogs.PropertyPage;
41 * Property page for CSS projects and files.
43 public class CssPropertyPage extends PropertyPage {
45 // Instance Variables ------------------------------------------------------
47 private IProfileManager profileManager;
49 private boolean useCustomSettings;
50 private Group profileGroup;
51 private Button[] profileButtons;
52 private String selectedProfile;
54 // Constructors ------------------------------------------------------------
59 public CssPropertyPage() {
60 profileManager = CssCore.getDefault().getProfileManager();
63 // PropertyPage Implementation ---------------------------------------------
66 * @see org.eclipse.jface.preference.PreferencePage#createContents(Composite)
68 protected Control createContents(Composite parent) {
70 Composite composite = new Composite(parent, SWT.NONE);
71 GridLayout layout = new GridLayout();
72 layout.numColumns = 1;
73 composite.setLayout(layout);
75 Object element = getElement();
76 if (element instanceof IResource) {
77 IResource resource = (IResource) element;
79 selectedProfile = resource.getPersistentProperty(
80 ProfileManager.PROFILE_PROPERTY);
81 } catch (CoreException e) {
85 useCustomSettings = (selectedProfile != null);
86 if (selectedProfile == null) {
87 selectedProfile = getInheritedProfile();
90 Button useInheritedSettingsButton =
91 new Button(composite, SWT.RADIO | SWT.LEFT);
92 if (element instanceof IProject) {
93 useInheritedSettingsButton.setText(
94 getString("useWorkspaceSettings")); //$NON-NLS-1$
95 } else if (element instanceof IResource) {
96 useInheritedSettingsButton.setText(
97 getString("useProjectSettings")); //$NON-NLS-1$
99 useInheritedSettingsButton.setSelection(!useCustomSettings);
100 Button useCustomSettingsButton =
101 new Button(composite, SWT.RADIO | SWT.LEFT);
102 useCustomSettingsButton.setText(
103 getString("useCustomSettings")); //$NON-NLS-1$
104 useCustomSettingsButton.addSelectionListener(new SelectionAdapter() {
105 public void widgetSelected(SelectionEvent event) {
107 ((Button) event.widget).getSelection();
108 updateProfileGroup();
111 useCustomSettingsButton.setSelection(useCustomSettings);
113 profileGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);
114 layout = new GridLayout();
115 layout.numColumns = 1;
116 profileGroup.setLayout(layout);
117 profileGroup.setText(getString("profile")); //$NON-NLS-1$
119 IProfileManager mgr = CssCore.getDefault().getProfileManager();
120 IProfileDescriptor[] profiles = mgr.getProfileDescriptors();
121 Arrays.sort(profiles, new Comparator() {
122 public int compare(Object o1, Object o2) {
123 return ((IProfileDescriptor) o1).getName().compareTo(
124 ((IProfileDescriptor) o2).getName());
128 profileButtons = new Button[profiles.length];
129 for (int i = 0; i < profiles.length; i++) {
130 Button button = new Button(profileGroup, SWT.RADIO | SWT.LEFT);
131 button.setText(profiles[i].getName());
132 button.setData(profiles[i].getId());
133 if (profiles[i].getId().equals(selectedProfile)) {
134 button.setSelection(true);
136 button.addSelectionListener(new SelectionAdapter() {
137 public void widgetSelected(SelectionEvent event) {
138 selectedProfile = (String) event.widget.getData();
141 profileButtons[i] = button;
143 updateProfileGroup();
149 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
151 public boolean performOk() {
152 Object element = getElement();
153 if (element instanceof IResource) {
154 IResource resource = (IResource) element;
155 if (useCustomSettings) {
156 profileManager.setProfile(resource, selectedProfile);
158 profileManager.setProfile(resource, null);
164 // Private Methods ---------------------------------------------------------
166 private String getInheritedProfile() {
167 Object element = getElement();
168 String retVal = null;
169 if (element instanceof IResource) {
170 IProject project = ((IResource) element).getProject();
172 retVal = project.getPersistentProperty(
173 ProfileManager.PROFILE_PROPERTY);
174 } catch (CoreException e) {
178 if (retVal == null) {
179 Preferences preferences =
180 CssCore.getDefault().getPluginPreferences();
181 retVal = preferences.getString(CssCorePreferences.PROFILE);
186 private String getString(String key) {
187 return CssUIMessages.getString("CssPropertyPage." + key); //$NON-NLS-1$
190 private void updateProfileGroup() {
191 profileGroup.setEnabled(useCustomSettings);
192 for (int i = 0; i < profileButtons.length; i++) {
193 profileButtons[i].setEnabled(useCustomSettings);