intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / properties / CssPropertyPage.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: CssPropertyPage.java,v 1.1 2004-09-02 18:11:49 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.properties;
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.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;
25
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;
39
40 /**
41  * Property page for CSS projects and files.
42  */
43 public class CssPropertyPage extends PropertyPage {
44
45         // Instance Variables ------------------------------------------------------
46
47         private IProfileManager profileManager;
48
49         private boolean useCustomSettings;
50         private Group profileGroup;
51         private Button[] profileButtons;
52         private String selectedProfile;
53
54         // Constructors ------------------------------------------------------------
55
56         /**
57          * Constructor.
58          */
59         public CssPropertyPage() {
60                 profileManager = CssCore.getDefault().getProfileManager();
61         }
62
63         // PropertyPage Implementation ---------------------------------------------
64
65         /**
66          * @see org.eclipse.jface.preference.PreferencePage#createContents(Composite)
67          */
68         protected Control createContents(Composite parent) {
69
70                 Composite composite = new Composite(parent, SWT.NONE);
71                 GridLayout layout = new GridLayout();
72                 layout.numColumns = 1;
73                 composite.setLayout(layout);
74
75                 Object element = getElement();
76                 if (element instanceof IResource) {
77                         IResource resource = (IResource) element;
78                         try {
79                                 selectedProfile = resource.getPersistentProperty(
80                                         ProfileManager.PROFILE_PROPERTY);
81                         } catch (CoreException e) {
82                                 // ignore
83                         }
84                 }
85                 useCustomSettings = (selectedProfile != null);
86                 if (selectedProfile == null) {
87                         selectedProfile = getInheritedProfile();
88                 }
89
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$
98                 }
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) {
106                                 useCustomSettings =
107                                         ((Button) event.widget).getSelection();
108                                 updateProfileGroup();
109                         }
110                 });
111                 useCustomSettingsButton.setSelection(useCustomSettings);
112
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$
118
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());
125                         }
126                 });
127
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);
135                         }
136                         button.addSelectionListener(new SelectionAdapter() {
137                                 public void widgetSelected(SelectionEvent event) {
138                                         selectedProfile = (String) event.widget.getData();
139                                 }
140                         });
141                         profileButtons[i] = button;
142                 }
143                 updateProfileGroup();
144
145                 return composite;
146         }
147
148         /**
149          * @see org.eclipse.jface.preference.IPreferencePage#performOk()
150          */
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);
157                         } else {
158                                 profileManager.setProfile(resource, null);
159                         }
160                 }
161                 return true;
162         }
163
164         // Private Methods ---------------------------------------------------------
165
166         private String getInheritedProfile() {
167                 Object element = getElement();
168                 String retVal = null;            
169                 if (element instanceof IResource) {
170                         IProject project = ((IResource) element).getProject();
171                         try {
172                                 retVal = project.getPersistentProperty(
173                                         ProfileManager.PROFILE_PROPERTY);
174                         } catch (CoreException e) {
175                                 // ignore
176                         }
177                 }
178                 if (retVal == null) {
179                         Preferences preferences =
180                                         CssCore.getDefault().getPluginPreferences();
181                         retVal = preferences.getString(CssCorePreferences.PROFILE);
182                 }
183                 return retVal;
184         }
185
186         private String getString(String key) {
187                 return CssUIMessages.getString("CssPropertyPage." + key); //$NON-NLS-1$
188         }
189
190         private void updateProfileGroup() {
191                 profileGroup.setEnabled(useCustomSettings);
192                 for (int i = 0; i < profileButtons.length; i++) {
193                         profileButtons[i].setEnabled(useCustomSettings);
194                 }
195         }
196
197 }