1) Fixed issue #872.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / preferences / MarkOccurrencesConfigurationBlock.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation 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  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.internal.ui.preferences;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.Map;
18
19 import net.sourceforge.phpdt.internal.ui.dialogs.StatusInfo;
20 //import net.sourceforge.phpdt.internal.ui.util.PixelConverter;
21 import net.sourceforge.phpdt.ui.PreferenceConstants;
22
23 import org.eclipse.core.runtime.IStatus;
24 //incastrix
25 //import org.eclipse.jface.text.Assert;
26 import org.eclipse.core.runtime.Assert;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.events.SelectionListener;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Control;
35 //import org.eclipse.swt.widgets.Label;
36
37 /**
38  * Configures Java Editor hover preferences.
39  * 
40  * @since 2.1
41  */
42 class MarkOccurrencesConfigurationBlock implements
43                 IPreferenceConfigurationBlock {
44
45         private OverlayPreferenceStore fStore;
46
47         private Map fCheckBoxes = new HashMap();
48
49         private SelectionListener fCheckBoxListener = new SelectionListener() {
50                 public void widgetDefaultSelected(SelectionEvent e) {
51                 }
52
53                 public void widgetSelected(SelectionEvent e) {
54                         Button button = (Button) e.widget;
55                         fStore.setValue((String) fCheckBoxes.get(button), button
56                                         .getSelection());
57                 }
58         };
59
60         /**
61          * List of master/slave listeners when there's a dependency.
62          * 
63          * @see #createDependency(Button, String, Control)
64          * @since 3.0
65          */
66         private ArrayList fMasterSlaveListeners = new ArrayList();
67
68         private StatusInfo fStatus;
69
70         public MarkOccurrencesConfigurationBlock(OverlayPreferenceStore store) {
71                 Assert.isNotNull(store);
72                 fStore = store;
73
74                 fStore.addKeys(createOverlayStoreKeys());
75         }
76
77         private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
78
79                 ArrayList overlayKeys = new ArrayList();
80
81                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
82                                 OverlayPreferenceStore.BOOLEAN,
83                                 PreferenceConstants.EDITOR_MARK_OCCURRENCES));
84                 // overlayKeys.add(new
85                 // OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
86                 // PreferenceConstants.EDITOR_MARK_TYPE_OCCURRENCES));
87                 // overlayKeys.add(new
88                 // OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
89                 // PreferenceConstants.EDITOR_MARK_METHOD_OCCURRENCES));
90                 // overlayKeys.add(new
91                 // OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
92                 // PreferenceConstants.EDITOR_MARK_CONSTANT_OCCURRENCES));
93                 // overlayKeys.add(new
94                 // OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
95                 // PreferenceConstants.EDITOR_MARK_FIELD_OCCURRENCES));
96                 // overlayKeys.add(new
97                 // OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
98                 // PreferenceConstants.EDITOR_MARK_LOCAL_VARIABLE_OCCURRENCES));
99                 // overlayKeys.add(new
100                 // OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
101                 // PreferenceConstants.EDITOR_MARK_EXCEPTION_OCCURRENCES));
102                 // overlayKeys.add(new
103                 // OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
104                 // PreferenceConstants.EDITOR_MARK_METHOD_EXIT_POINTS));
105                 // overlayKeys.add(new
106                 // OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
107                 // PreferenceConstants.EDITOR_MARK_IMPLEMENTORS));
108                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
109                                 OverlayPreferenceStore.BOOLEAN,
110                                 PreferenceConstants.EDITOR_STICKY_OCCURRENCES));
111
112                 OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys
113                                 .size()];
114                 overlayKeys.toArray(keys);
115                 return keys;
116         }
117
118         /**
119          * Creates page for mark occurrences preferences.
120          * 
121          * @param parent
122          *            the parent composite
123          * @return the control for the preference page
124          */
125         public Control createControl(Composite parent) {
126
127                 Composite composite = new Composite(parent, SWT.NONE);
128                 GridLayout layout = new GridLayout();
129                 layout.numColumns = 1;
130                 composite.setLayout(layout);
131
132                 GridData gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_VERTICAL);
133                 composite.setLayoutData(gd);
134
135                 String label;
136
137                 label = PreferencesMessages
138                                 .getString("JavaEditorPreferencePage.markOccurrences");
139                 Button master = addCheckBox(composite, label,
140                                 PreferenceConstants.EDITOR_MARK_OCCURRENCES, 0); //$NON-NLS-1$
141
142                 // label=
143                 // PreferencesMessages.MarkOccurrencesConfigurationBlock_markTypeOccurrences;
144                 // Button slave= addCheckBox(composite, label,
145                 // PreferenceConstants.EDITOR_MARK_TYPE_OCCURRENCES, 0); //$NON-NLS-1$
146                 // createDependency(master,
147                 // PreferenceConstants.EDITOR_STICKY_OCCURRENCES, slave);
148                 //
149                 // label=
150                 // PreferencesMessages.MarkOccurrencesConfigurationBlock_markMethodOccurrences;
151                 // slave= addCheckBox(composite, label,
152                 // PreferenceConstants.EDITOR_MARK_METHOD_OCCURRENCES, 0); //$NON-NLS-1$
153                 // createDependency(master,
154                 // PreferenceConstants.EDITOR_MARK_METHOD_OCCURRENCES, slave);
155                 //
156                 // label=
157                 // PreferencesMessages.MarkOccurrencesConfigurationBlock_markConstantOccurrences;
158                 // slave= addCheckBox(composite, label,
159                 // PreferenceConstants.EDITOR_MARK_CONSTANT_OCCURRENCES, 0);
160                 // //$NON-NLS-1$
161                 // createDependency(master,
162                 // PreferenceConstants.EDITOR_MARK_CONSTANT_OCCURRENCES, slave);
163                 //
164                 // label=
165                 // PreferencesMessages.MarkOccurrencesConfigurationBlock_markFieldOccurrences;
166                 // slave= addCheckBox(composite, label,
167                 // PreferenceConstants.EDITOR_MARK_FIELD_OCCURRENCES, 0); //$NON-NLS-1$
168                 // createDependency(master,
169                 // PreferenceConstants.EDITOR_MARK_FIELD_OCCURRENCES, slave);
170                 //
171                 // label=
172                 // PreferencesMessages.MarkOccurrencesConfigurationBlock_markLocalVariableOccurrences;
173                 // slave= addCheckBox(composite, label,
174                 // PreferenceConstants.EDITOR_MARK_LOCAL_VARIABLE_OCCURRENCES, 0);
175                 // //$NON-NLS-1$
176                 // createDependency(master,
177                 // PreferenceConstants.EDITOR_MARK_LOCAL_VARIABLE_OCCURRENCES, slave);
178                 //
179                 // label=
180                 // PreferencesMessages.MarkOccurrencesConfigurationBlock_markExceptionOccurrences;
181                 // slave= addCheckBox(composite, label,
182                 // PreferenceConstants.EDITOR_MARK_EXCEPTION_OCCURRENCES, 0);
183                 // //$NON-NLS-1$
184                 // createDependency(master,
185                 // PreferenceConstants.EDITOR_MARK_EXCEPTION_OCCURRENCES, slave);
186                 //
187                 // label=
188                 // PreferencesMessages.MarkOccurrencesConfigurationBlock_markMethodExitPoints;
189                 // slave= addCheckBox(composite, label,
190                 // PreferenceConstants.EDITOR_MARK_METHOD_EXIT_POINTS, 0); //$NON-NLS-1$
191                 // createDependency(master,
192                 // PreferenceConstants.EDITOR_MARK_METHOD_EXIT_POINTS, slave);
193                 //
194                 // label=
195                 // PreferencesMessages.MarkOccurrencesConfigurationBlock_markImplementors;
196                 // slave= addCheckBox(composite, label,
197                 // PreferenceConstants.EDITOR_MARK_IMPLEMENTORS, 0); //$NON-NLS-1$
198                 // createDependency(master,
199                 // PreferenceConstants.EDITOR_MARK_IMPLEMENTORS, slave);
200
201                 //addFiller(composite);
202
203                 label = PreferencesMessages
204                                 .getString("JavaEditorPreferencePage.stickyOccurrences");
205                 Button slave = addCheckBox(composite, label,
206                                 PreferenceConstants.EDITOR_STICKY_OCCURRENCES, 0); //$NON-NLS-1$
207                 createDependency(master, PreferenceConstants.EDITOR_STICKY_OCCURRENCES,
208                                 slave);
209
210                 return composite;
211         }
212
213 //      private void addFiller(Composite composite) {
214 //              PixelConverter pixelConverter = new PixelConverter(composite);
215 //
216 //              Label filler = new Label(composite, SWT.LEFT);
217 //              GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
218 //              gd.horizontalSpan = 2;
219 //              gd.heightHint = pixelConverter.convertHeightInCharsToPixels(1) / 2;
220 //              filler.setLayoutData(gd);
221 //      }
222
223         private Button addCheckBox(Composite parent, String label, String key,
224                         int indentation) {
225                 Button checkBox = new Button(parent, SWT.CHECK);
226                 checkBox.setText(label);
227
228                 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
229                 gd.horizontalIndent = indentation;
230                 gd.horizontalSpan = 2;
231                 checkBox.setLayoutData(gd);
232                 checkBox.addSelectionListener(fCheckBoxListener);
233
234                 fCheckBoxes.put(checkBox, key);
235
236                 return checkBox;
237         }
238
239         private void createDependency(final Button master, String masterKey,
240                         final Control slave) {
241                 indent(slave);
242                 boolean masterState = fStore.getBoolean(masterKey);
243                 slave.setEnabled(masterState);
244                 SelectionListener listener = new SelectionListener() {
245                         public void widgetSelected(SelectionEvent e) {
246                                 slave.setEnabled(master.getSelection());
247                         }
248
249                         public void widgetDefaultSelected(SelectionEvent e) {
250                         }
251                 };
252                 master.addSelectionListener(listener);
253                 fMasterSlaveListeners.add(listener);
254         }
255
256         private static void indent(Control control) {
257                 GridData gridData = new GridData();
258                 gridData.horizontalIndent = 20;
259                 control.setLayoutData(gridData);
260         }
261
262         public void initialize() {
263                 initializeFields();
264         }
265
266         void initializeFields() {
267
268                 Iterator iter = fCheckBoxes.keySet().iterator();
269                 while (iter.hasNext()) {
270                         Button b = (Button) iter.next();
271                         String key = (String) fCheckBoxes.get(b);
272                         b.setSelection(fStore.getBoolean(key));
273                 }
274
275                 // Update slaves
276                 iter = fMasterSlaveListeners.iterator();
277                 while (iter.hasNext()) {
278                         SelectionListener listener = (SelectionListener) iter.next();
279                         listener.widgetSelected(null);
280                 }
281
282         }
283
284         public void performOk() {
285         }
286
287         public void performDefaults() {
288                 restoreFromPreferences();
289                 initializeFields();
290         }
291
292         private void restoreFromPreferences() {
293
294         }
295
296         IStatus getStatus() {
297                 if (fStatus == null)
298                         fStatus = new StatusInfo();
299                 return fStatus;
300         }
301
302         /*
303          * @see net.sourceforge.phpdt.internal.ui.preferences.IPreferenceConfigurationBlock#dispose()
304          * @since 3.0
305          */
306         public void dispose() {
307         }
308 }