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