Removed
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2008, 2009 Standards for Technology in Automotive Retail and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* David Carver (STAR) - bug 230072 - initial API and implementation |
10 |
* based on org.eclipse.wst.html.ui class of |
11 |
* the same name. |
12 |
* David Carver - bug 297714 - Values not being loaded from preferences. |
13 |
*******************************************************************************/ |
14 |
package org.eclipse.wst.xsl.ui.internal.preferences; |
15 |
|
16 |
import java.util.ArrayList; |
17 |
import java.util.Iterator; |
18 |
import java.util.List; |
19 |
import java.util.concurrent.CopyOnWriteArrayList; |
20 |
|
21 |
import org.eclipse.core.resources.IProject; |
22 |
import org.eclipse.core.resources.ProjectScope; |
23 |
import org.eclipse.core.resources.ResourcesPlugin; |
24 |
import org.eclipse.core.runtime.CoreException; |
25 |
import org.eclipse.core.runtime.IProgressMonitor; |
26 |
import org.eclipse.core.runtime.IStatus; |
27 |
import org.eclipse.core.runtime.Platform; |
28 |
import org.eclipse.core.runtime.Status; |
29 |
import org.eclipse.core.runtime.jobs.Job; |
30 |
import org.eclipse.core.runtime.preferences.DefaultScope; |
31 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
32 |
import org.eclipse.core.runtime.preferences.IPreferencesService; |
33 |
import org.eclipse.core.runtime.preferences.IScopeContext; |
34 |
import org.eclipse.jface.dialogs.IDialogSettings; |
35 |
import org.eclipse.jface.resource.JFaceResources; |
36 |
import org.eclipse.swt.SWT; |
37 |
import org.eclipse.swt.events.SelectionEvent; |
38 |
import org.eclipse.swt.events.SelectionListener; |
39 |
import org.eclipse.swt.layout.GridData; |
40 |
import org.eclipse.swt.widgets.Combo; |
41 |
import org.eclipse.swt.widgets.Composite; |
42 |
import org.eclipse.swt.widgets.Control; |
43 |
import org.eclipse.swt.widgets.Label; |
44 |
import org.eclipse.swt.widgets.MessageBox; |
45 |
import org.eclipse.swt.widgets.Widget; |
46 |
import org.eclipse.ui.forms.events.ExpansionAdapter; |
47 |
import org.eclipse.ui.forms.events.ExpansionEvent; |
48 |
import org.eclipse.ui.forms.widgets.ExpandableComposite; |
49 |
import org.eclipse.wst.sse.core.internal.validate.ValidationMessage; |
50 |
import org.eclipse.wst.sse.ui.internal.preferences.ui.ScrolledPageContent; |
51 |
import org.eclipse.wst.validation.ValidationFramework; |
52 |
import org.eclipse.wst.xsl.ui.internal.Messages; |
53 |
import org.osgi.service.prefs.BackingStoreException; |
54 |
|
55 |
/** |
56 |
* Based on org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock |
57 |
*/ |
58 |
abstract class AbstractValidationSettingsPage extends PropertyPreferencePage { |
59 |
|
60 |
private List fCombos; |
61 |
private List fExpandables; |
62 |
|
63 |
private SelectionListener fSelectionListener; |
64 |
|
65 |
private IPreferencesService fPreferencesService = null; |
66 |
|
67 |
private static final String SETTINGS_EXPANDED = "expanded"; //$NON-NLS-1$ |
68 |
|
69 |
private ValidationFramework fValidation; |
70 |
|
71 |
public AbstractValidationSettingsPage() { |
72 |
super(); |
73 |
fCombos = new CopyOnWriteArrayList<Combo>(); |
74 |
fExpandables = new CopyOnWriteArrayList<ExpandableComposite>(); |
75 |
fPreferencesService = Platform.getPreferencesService(); |
76 |
fValidation = ValidationFramework.getDefault(); |
77 |
} |
78 |
|
79 |
/** |
80 |
* Creates a Combo widget in the composite <code>parent</code>. The data |
81 |
* in the Combo is associated with <code>key</code>. The Combo data is |
82 |
* generated based on the integer <code>values</code> where the index |
83 |
* of <code>values</code> corresponds to the index of <code>valueLabels</code> |
84 |
* |
85 |
* @param parent the composite to create the combo box in |
86 |
* @param label the label to give the combo box |
87 |
* @param key the unique key to identify the combo box |
88 |
* @param values the values represented by the combo options |
89 |
* @param valueLabels the calues displayed in the combo box |
90 |
* @param indent how far to indent the combo box label |
91 |
* |
92 |
* @return the generated combo box |
93 |
*/ |
94 |
protected Combo addComboBox(Composite parent, String label, String key, int[] values, String[] valueLabels, int indent) { |
95 |
GridData gd= new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1); |
96 |
gd.horizontalIndent= indent; |
97 |
|
98 |
Label labelControl= new Label(parent, SWT.LEFT); |
99 |
labelControl.setFont(JFaceResources.getDialogFont()); |
100 |
labelControl.setText(label); |
101 |
labelControl.setLayoutData(gd); |
102 |
|
103 |
Combo comboBox= newComboControl(parent, key, values, valueLabels); |
104 |
comboBox.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); |
105 |
|
106 |
return comboBox; |
107 |
} |
108 |
|
109 |
/** |
110 |
* Creates a combo box and associates the combo data with the |
111 |
* combo box. |
112 |
* |
113 |
* @param composite the composite to create the combo box in |
114 |
* @param key the unique key to identify the combo box |
115 |
* @param values the values represented by the combo options |
116 |
* @param valueLabels the values displayed in the combo box |
117 |
* |
118 |
* @return the generated combo box |
119 |
*/ |
120 |
protected Combo newComboControl(Composite composite, String key, int[] values, String[] valueLabels) { |
121 |
ComboData data = new ComboData(key, values, -1); |
122 |
|
123 |
Combo comboBox= new Combo(composite, SWT.READ_ONLY); |
124 |
comboBox.setItems(valueLabels); |
125 |
comboBox.setData(data); |
126 |
comboBox.addSelectionListener(getSelectionListener()); |
127 |
comboBox.setFont(JFaceResources.getDialogFont()); |
128 |
|
129 |
makeScrollableCompositeAware(comboBox); |
130 |
|
131 |
int severity = -1; |
132 |
if(key != null) |
133 |
severity = fPreferencesService.getInt(getPreferenceNodeQualifier(), key, ValidationMessage.WARNING, createPreferenceScopes()); |
134 |
|
135 |
if (severity == ValidationMessage.ERROR || severity == ValidationMessage.WARNING || severity == ValidationMessage.IGNORE) { |
136 |
data.setSeverity(severity); |
137 |
data.originalSeverity = severity; |
138 |
} |
139 |
|
140 |
if(data.getIndex() >= 0) |
141 |
comboBox.select(data.getIndex()); |
142 |
|
143 |
fCombos.add(comboBox); |
144 |
return comboBox; |
145 |
} |
146 |
|
147 |
protected SelectionListener getSelectionListener() { |
148 |
if (fSelectionListener == null) { |
149 |
fSelectionListener= new SelectionListener() { |
150 |
public void widgetDefaultSelected(SelectionEvent e) {} |
151 |
|
152 |
public void widgetSelected(SelectionEvent e) { |
153 |
controlChanged(e.widget); |
154 |
} |
155 |
}; |
156 |
} |
157 |
return fSelectionListener; |
158 |
} |
159 |
|
160 |
protected void controlChanged(Widget widget) { |
161 |
ComboData data= (ComboData) widget.getData(); |
162 |
if (widget instanceof Combo) { |
163 |
data.setIndex(((Combo)widget).getSelectionIndex()); |
164 |
} else { |
165 |
return; |
166 |
} |
167 |
} |
168 |
|
169 |
/* |
170 |
* (non-Javadoc) |
171 |
* @see org.eclipse.wst.sse.ui.internal.preferences.ui.AbstractSettingsPage#storeValues() |
172 |
*/ |
173 |
protected void storeValues() { |
174 |
if(fCombos == null || fCombos.size() == 0) |
175 |
return; |
176 |
|
177 |
Iterator it = fCombos.iterator(); |
178 |
|
179 |
IScopeContext[] contexts = createPreferenceScopes(); |
180 |
|
181 |
while(it.hasNext()) { |
182 |
ComboData data = (ComboData) ((Combo)it.next()).getData(); |
183 |
if(data.getKey() != null) { |
184 |
contexts[0].getNode(getPreferenceNodeQualifier()).putInt(data.getKey(), data.getSeverity()); |
185 |
} |
186 |
} |
187 |
|
188 |
for(int i = 0; i < contexts.length; i++) { |
189 |
try { |
190 |
contexts[i].getNode(getPreferenceNodeQualifier()).flush(); |
191 |
} |
192 |
catch (BackingStoreException e) { |
193 |
|
194 |
} |
195 |
} |
196 |
} |
197 |
|
198 |
protected ExpandableComposite getParentExpandableComposite(Control control) { |
199 |
Control parent= control.getParent(); |
200 |
while (!(parent instanceof ExpandableComposite) && parent != null) { |
201 |
parent= parent.getParent(); |
202 |
} |
203 |
if (parent instanceof ExpandableComposite) { |
204 |
return (ExpandableComposite) parent; |
205 |
} |
206 |
return null; |
207 |
} |
208 |
|
209 |
protected ExpandableComposite createTwistie(Composite parent, String label, int nColumns) { |
210 |
ExpandableComposite excomposite= new ExpandableComposite(parent, SWT.NONE, ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT); |
211 |
excomposite.setText(label); |
212 |
excomposite.setExpanded(false); |
213 |
excomposite.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT)); |
214 |
excomposite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, nColumns, 1)); |
215 |
excomposite.addExpansionListener(new ExpansionAdapter() { |
216 |
@Override |
217 |
public void expansionStateChanged(ExpansionEvent e) { |
218 |
expandedStateChanged((ExpandableComposite) e.getSource()); |
219 |
} |
220 |
}); |
221 |
fExpandables.add(excomposite); |
222 |
makeScrollableCompositeAware(excomposite); |
223 |
return excomposite; |
224 |
} |
225 |
|
226 |
protected final void expandedStateChanged(ExpandableComposite expandable) { |
227 |
ScrolledPageContent parentScrolledComposite= getParentScrolledComposite(expandable); |
228 |
if (parentScrolledComposite != null) { |
229 |
parentScrolledComposite.reflow(true); |
230 |
} |
231 |
} |
232 |
|
233 |
private void makeScrollableCompositeAware(Control control) { |
234 |
ScrolledPageContent parentScrolledComposite= getParentScrolledComposite(control); |
235 |
if (parentScrolledComposite != null) { |
236 |
parentScrolledComposite.adaptChild(control); |
237 |
} |
238 |
} |
239 |
|
240 |
protected ScrolledPageContent getParentScrolledComposite(Control control) { |
241 |
Control parent= control.getParent(); |
242 |
while (!(parent instanceof ScrolledPageContent) && parent != null) { |
243 |
parent= parent.getParent(); |
244 |
} |
245 |
if (parent instanceof ScrolledPageContent) { |
246 |
return (ScrolledPageContent) parent; |
247 |
} |
248 |
return null; |
249 |
} |
250 |
|
251 |
protected void storeSectionExpansionStates(IDialogSettings section) { |
252 |
for(int i = 0; i < fExpandables.size(); i++) { |
253 |
ExpandableComposite comp = (ExpandableComposite) fExpandables.get(i); |
254 |
section.put(SETTINGS_EXPANDED + String.valueOf(i), comp.isExpanded()); |
255 |
} |
256 |
} |
257 |
|
258 |
protected void restoreSectionExpansionStates(IDialogSettings settings) { |
259 |
for (int i= 0; i < fExpandables.size(); i++) { |
260 |
ExpandableComposite excomposite= (ExpandableComposite) fExpandables.get(i); |
261 |
if (settings == null) { |
262 |
excomposite.setExpanded(i == 0); // only expand the first node by default |
263 |
} else { |
264 |
excomposite.setExpanded(settings.getBoolean(SETTINGS_EXPANDED + String.valueOf(i))); |
265 |
} |
266 |
} |
267 |
} |
268 |
|
269 |
protected void resetSeverities() { |
270 |
IEclipsePreferences defaultContext = new DefaultScope().getNode(getPreferenceNodeQualifier()); |
271 |
for(int i = 0; i < fCombos.size(); i++) { |
272 |
ComboData data = (ComboData)((Combo)fCombos.get(i)).getData(); |
273 |
int severity = defaultContext.getInt(data.getKey(), ValidationMessage.WARNING); |
274 |
data.setSeverity(severity); |
275 |
((Combo)fCombos.get(i)).select(data.getIndex()); |
276 |
} |
277 |
} |
278 |
|
279 |
protected boolean shouldRevalidateOnSettingsChange() { |
280 |
Iterator it = fCombos.iterator(); |
281 |
|
282 |
while (it.hasNext()) { |
283 |
ComboData data = (ComboData) ((Combo) it.next()).getData(); |
284 |
if (data.isChanged()) |
285 |
return true; |
286 |
} |
287 |
return false; |
288 |
} |
289 |
|
290 |
@Override |
291 |
public boolean performOk() { |
292 |
storeValues(); |
293 |
if(super.performOk() && shouldRevalidateOnSettingsChange()) { |
294 |
MessageBox mb = new MessageBox(this.getShell(), SWT.APPLICATION_MODAL | SWT.YES | SWT.NO | SWT.CANCEL | SWT.ICON_INFORMATION | SWT.RIGHT); |
295 |
mb.setText(Messages.AbstractValidationSettingsPageMbText); |
296 |
/* Choose which message to use based on if its project or workspace settings */ |
297 |
String msg = (getProject() == null) ? Messages.AbstractValidationSettingsPageWorkspace : Messages.AbstractValidationSettingsPageProject; |
298 |
mb.setMessage(msg); |
299 |
switch(mb.open()) { |
300 |
case SWT.CANCEL: |
301 |
return false; |
302 |
case SWT.YES: |
303 |
ValidateJob job = new ValidateJob(Messages.AbstractValidationSettingsPageJob); |
304 |
job.schedule(); |
305 |
case SWT.NO: |
306 |
default: |
307 |
return true; |
308 |
} |
309 |
} |
310 |
return true; |
311 |
} |
312 |
|
313 |
/** |
314 |
* Performs validation after validation preferences have been modified. |
315 |
*/ |
316 |
private class ValidateJob extends Job { |
317 |
|
318 |
public ValidateJob(String name) { |
319 |
super(name); |
320 |
} |
321 |
|
322 |
@Override |
323 |
protected IStatus run(IProgressMonitor monitor) { |
324 |
IStatus status = Status.OK_STATUS; |
325 |
try { |
326 |
IProject[] projects = null; |
327 |
/* Changed preferences for a single project, only validate it */ |
328 |
if(getProject() != null) |
329 |
projects = new IProject[] {getProject()}; |
330 |
/* Workspace-wide preferences changed */ |
331 |
else { |
332 |
/* Get all of the projects in the workspace */ |
333 |
projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); |
334 |
IEclipsePreferences prefs = null; |
335 |
List projectList = new ArrayList(); |
336 |
|
337 |
/* Filter out projects that use project-specific settings or have been closed */ |
338 |
for(int i = 0; i < projects.length; i++) { |
339 |
prefs = new ProjectScope(projects[i]).getNode(getPreferenceNodeQualifier()); |
340 |
if(projects[i].isAccessible() && !prefs.getBoolean(getProjectSettingsKey(), false)) |
341 |
projectList.add(projects[i]); |
342 |
} |
343 |
projects = (IProject[]) projectList.toArray(new IProject[projectList.size()]); |
344 |
} |
345 |
fValidation.validate(projects, true, false, monitor); |
346 |
} |
347 |
catch (CoreException ce) { |
348 |
status = Status.CANCEL_STATUS; |
349 |
} |
350 |
|
351 |
return status; |
352 |
} |
353 |
|
354 |
} |
355 |
|
356 |
} |