1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation 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 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.Hashtable;
18 import java.util.StringTokenizer;
20 import net.sourceforge.phpdt.core.IJavaProject;
21 import net.sourceforge.phpdt.core.JavaCore;
22 import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
23 import net.sourceforge.phpdt.internal.ui.wizards.IStatusChangeListener;
24 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
26 import org.eclipse.core.resources.IncrementalProjectBuilder;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.SubProgressMonitor;
30 import org.eclipse.jface.dialogs.IDialogConstants;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
33 import org.eclipse.jface.operation.IRunnableWithProgress;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.events.ModifyEvent;
36 import org.eclipse.swt.events.ModifyListener;
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.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Combo;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Control;
45 import org.eclipse.swt.widgets.Label;
46 import org.eclipse.swt.widgets.Shell;
47 import org.eclipse.swt.widgets.Text;
48 import org.eclipse.swt.widgets.Widget;
52 public abstract class OptionsConfigurationBlock {
54 protected static class ControlData {
56 private String[] fValues;
58 public ControlData(String key, String[] values) {
63 public String getKey() {
67 public String getValue(boolean selection) {
68 int index= selection ? 0 : 1;
69 return fValues[index];
72 public String getValue(int index) {
73 return fValues[index];
76 public int getSelection(String value) {
77 for (int i= 0; i < fValues.length; i++) {
78 if (value.equals(fValues[i])) {
87 protected Map fWorkingValues;
89 protected ArrayList fCheckBoxes;
90 protected ArrayList fComboBoxes;
91 protected ArrayList fTextBoxes;
92 protected HashMap fLabels;
94 private SelectionListener fSelectionListener;
95 private ModifyListener fTextModifyListener;
97 protected IStatusChangeListener fContext;
98 protected IJavaProject fProject; // project or null
100 private Shell fShell;
102 public OptionsConfigurationBlock(IStatusChangeListener context, IJavaProject project) {
106 fWorkingValues= getOptions(true);
108 fCheckBoxes= new ArrayList();
109 fComboBoxes= new ArrayList();
110 fTextBoxes= new ArrayList(2);
111 fLabels= new HashMap();
114 protected abstract String[] getAllKeys();
116 protected Map getOptions(boolean inheritJavaCoreOptions) {
117 if (fProject != null) {
118 return fProject.getOptions(inheritJavaCoreOptions);
120 return JavaCore.getOptions();
124 protected Map getDefaultOptions() {
125 return JavaCore.getDefaultOptions();
128 public final boolean hasProjectSpecificOptions() {
129 if (fProject != null) {
130 Map settings= fProject.getOptions(false);
131 String[] allKeys= getAllKeys();
132 for (int i= 0; i < allKeys.length; i++) {
133 if (settings.get(allKeys[i]) != null) {
141 protected void setOptions(Map map) {
142 if (fProject != null) {
143 fProject.setOptions(map);
145 JavaCore.setOptions((Hashtable) map);
149 protected Shell getShell() {
153 protected void setShell(Shell shell) {
157 protected abstract Control createContents(Composite parent);
159 protected Button addCheckBox(Composite parent, String label, String key, String[] values, int indent) {
160 ControlData data= new ControlData(key, values);
162 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
163 gd.horizontalSpan= 3;
164 gd.horizontalIndent= indent;
166 Button checkBox= new Button(parent, SWT.CHECK);
167 checkBox.setText(label);
168 checkBox.setData(data);
169 checkBox.setLayoutData(gd);
170 checkBox.addSelectionListener(getSelectionListener());
172 String currValue= (String)fWorkingValues.get(key);
173 checkBox.setSelection(data.getSelection(currValue) == 0);
175 fCheckBoxes.add(checkBox);
180 protected Combo addComboBox(Composite parent, String label, String key, String[] values, String[] valueLabels, int indent) {
181 ControlData data= new ControlData(key, values);
183 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
184 gd.horizontalIndent= indent;
186 Label labelControl= new Label(parent, SWT.LEFT | SWT.WRAP);
187 labelControl.setText(label);
188 labelControl.setLayoutData(gd);
190 Combo comboBox= new Combo(parent, SWT.READ_ONLY);
191 comboBox.setItems(valueLabels);
192 comboBox.setData(data);
193 comboBox.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
194 comboBox.addSelectionListener(getSelectionListener());
196 fLabels.put(comboBox, labelControl);
198 Label placeHolder= new Label(parent, SWT.NONE);
199 placeHolder.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
201 String currValue= (String)fWorkingValues.get(key);
202 comboBox.select(data.getSelection(currValue));
204 fComboBoxes.add(comboBox);
208 protected void addInversedComboBox(Composite parent, String label, String key, String[] values, String[] valueLabels, int indent) {
209 ControlData data= new ControlData(key, values);
211 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
212 gd.horizontalIndent= indent;
213 gd.horizontalSpan= 3;
215 Composite composite= new Composite(parent, SWT.NONE);
216 GridLayout layout= new GridLayout();
217 layout.marginHeight= 0;
218 layout.marginWidth= 0;
219 layout.numColumns= 2;
220 composite.setLayout(layout);
221 composite.setLayoutData(gd);
223 Combo comboBox= new Combo(composite, SWT.READ_ONLY);
224 comboBox.setItems(valueLabels);
225 comboBox.setData(data);
226 comboBox.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
227 comboBox.addSelectionListener(getSelectionListener());
229 Label labelControl= new Label(composite, SWT.LEFT | SWT.WRAP);
230 labelControl.setText(label);
231 labelControl.setLayoutData(new GridData());
233 fLabels.put(comboBox, labelControl);
235 String currValue= (String)fWorkingValues.get(key);
236 comboBox.select(data.getSelection(currValue));
238 fComboBoxes.add(comboBox);
241 protected Text addTextField(Composite parent, String label, String key, int indent, int widthHint) {
242 Label labelControl= new Label(parent, SWT.NONE);
243 labelControl.setText(label);
244 labelControl.setLayoutData(new GridData());
246 Text textBox= new Text(parent, SWT.BORDER | SWT.SINGLE);
247 textBox.setData(key);
248 textBox.setLayoutData(new GridData());
250 fLabels.put(textBox, labelControl);
252 String currValue= (String) fWorkingValues.get(key);
253 textBox.setText(currValue);
254 textBox.addModifyListener(getTextModifyListener());
256 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
257 if (widthHint != 0) {
258 data.widthHint= widthHint;
260 data.horizontalIndent= indent;
261 data.horizontalSpan= 2;
262 textBox.setLayoutData(data);
264 fTextBoxes.add(textBox);
268 protected SelectionListener getSelectionListener() {
269 if (fSelectionListener == null) {
270 fSelectionListener= new SelectionListener() {
271 public void widgetDefaultSelected(SelectionEvent e) {}
273 public void widgetSelected(SelectionEvent e) {
274 controlChanged(e.widget);
278 return fSelectionListener;
281 protected ModifyListener getTextModifyListener() {
282 if (fTextModifyListener == null) {
283 fTextModifyListener= new ModifyListener() {
284 public void modifyText(ModifyEvent e) {
285 textChanged((Text) e.widget);
289 return fTextModifyListener;
292 protected void controlChanged(Widget widget) {
293 ControlData data= (ControlData) widget.getData();
294 String newValue= null;
295 if (widget instanceof Button) {
296 newValue= data.getValue(((Button)widget).getSelection());
297 } else if (widget instanceof Combo) {
298 newValue= data.getValue(((Combo)widget).getSelectionIndex());
302 fWorkingValues.put(data.getKey(), newValue);
304 validateSettings(data.getKey(), newValue);
307 protected void textChanged(Text textControl) {
308 String key= (String) textControl.getData();
309 String number= textControl.getText();
310 fWorkingValues.put(key, number);
311 validateSettings(key, number);
314 protected boolean checkValue(String key, String value) {
315 return value.equals(fWorkingValues.get(key));
319 * Update fields and validate.
320 * @param changedKey Key that changed, or null, if all changed.
322 protected abstract void validateSettings(String changedKey, String newValue);
325 protected String[] getTokens(String text, String separator) {
326 StringTokenizer tok= new StringTokenizer(text, separator); //$NON-NLS-1$
327 int nTokens= tok.countTokens();
328 String[] res= new String[nTokens];
329 for (int i= 0; i < res.length; i++) {
330 res[i]= tok.nextToken().trim();
336 public boolean performOk(boolean enabled) {
337 String[] allKeys= getAllKeys();
338 Map actualOptions= getOptions(false);
340 // preserve other options
341 boolean hasChanges= false;
342 for (int i= 0; i < allKeys.length; i++) {
343 String key= allKeys[i];
344 String oldVal= (String) actualOptions.get(key);
347 val= (String) fWorkingValues.get(key);
348 if (!val.equals(oldVal)) {
350 actualOptions.put(key, val);
353 if (oldVal != null) {
354 actualOptions.remove(key);
362 boolean doBuild= false;
363 String[] strings= getFullBuildDialogStrings(fProject == null);
364 if (strings != null) {
365 MessageDialog dialog= new MessageDialog(getShell(), strings[0], null, strings[1], MessageDialog.QUESTION, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }, 2);
366 int res= dialog.open();
369 } else if (res != 1) {
370 return false; // cancel pressed
373 setOptions(actualOptions);
381 protected abstract String[] getFullBuildDialogStrings(boolean workspaceSettings);
383 protected void doFullBuild() {
384 ProgressMonitorDialog dialog= new ProgressMonitorDialog(getShell());
386 dialog.run(true, true, new IRunnableWithProgress() {
387 public void run(IProgressMonitor monitor) throws InvocationTargetException {
388 monitor.beginTask("", 2); //$NON-NLS-1$
390 if (fProject != null) {
391 monitor.setTaskName(PreferencesMessages.getFormattedString("OptionsConfigurationBlock.buildproject.taskname", fProject.getElementName())); //$NON-NLS-1$
392 fProject.getProject().build(IncrementalProjectBuilder.FULL_BUILD, new SubProgressMonitor(monitor,1));
393 PHPeclipsePlugin.getWorkspace().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, new SubProgressMonitor(monitor,1));
395 monitor.setTaskName(PreferencesMessages.getString("OptionsConfigurationBlock.buildall.taskname")); //$NON-NLS-1$
396 PHPeclipsePlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, new SubProgressMonitor(monitor, 2));
398 } catch (CoreException e) {
399 throw new InvocationTargetException(e);
405 } catch (InterruptedException e) {
407 } catch (InvocationTargetException e) {
408 String title= PreferencesMessages.getString("OptionsConfigurationBlock.builderror.title"); //$NON-NLS-1$
409 String message= PreferencesMessages.getString("OptionsConfigurationBlock.builderror.message"); //$NON-NLS-1$
410 ExceptionHandler.handle(e, getShell(), title, message);
414 public void performDefaults() {
415 fWorkingValues= getDefaultOptions();
417 validateSettings(null, null);
420 protected void updateControls() {
422 for (int i= fCheckBoxes.size() - 1; i >= 0; i--) {
423 Button curr= (Button) fCheckBoxes.get(i);
424 ControlData data= (ControlData) curr.getData();
426 String currValue= (String) fWorkingValues.get(data.getKey());
427 curr.setSelection(data.getSelection(currValue) == 0);
429 for (int i= fComboBoxes.size() - 1; i >= 0; i--) {
430 Combo curr= (Combo) fComboBoxes.get(i);
431 ControlData data= (ControlData) curr.getData();
433 String currValue= (String) fWorkingValues.get(data.getKey());
434 curr.select(data.getSelection(currValue));
436 for (int i= fTextBoxes.size() - 1; i >= 0; i--) {
437 Text curr= (Text) fTextBoxes.get(i);
438 String key= (String) curr.getData();
440 String currValue= (String) fWorkingValues.get(key);
441 curr.setText(currValue);
445 protected Button getCheckBox(String key) {
446 for (int i= fCheckBoxes.size() - 1; i >= 0; i--) {
447 Button curr= (Button) fCheckBoxes.get(i);
448 ControlData data= (ControlData) curr.getData();
449 if (key.equals(data.getKey())) {
456 protected Combo getComboBox(String key) {
457 for (int i= fComboBoxes.size() - 1; i >= 0; i--) {
458 Combo curr= (Combo) fComboBoxes.get(i);
459 ControlData data= (ControlData) curr.getData();
460 if (key.equals(data.getKey())) {
467 protected void setComboEnabled(String key, boolean enabled) {
468 Combo combo= getComboBox(key);
469 Label label= (Label) fLabels.get(combo);
470 combo.setEnabled(enabled);
471 label.setEnabled(enabled);