1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / preferences / FoldingConfigurationBlock.java
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
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.text.folding.JavaFoldingStructureProviderDescriptor;
20 import net.sourceforge.phpdt.internal.ui.text.folding.JavaFoldingStructureProviderRegistry;
21 import net.sourceforge.phpdt.internal.ui.util.PixelConverter;
22 import net.sourceforge.phpdt.ui.PreferenceConstants;
23 import net.sourceforge.phpdt.ui.text.folding.IJavaFoldingPreferenceBlock;
24 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
25
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 //incastrix
30 //import org.eclipse.jface.text.Assert;
31 import org.eclipse.core.runtime.Assert;
32 import org.eclipse.jface.viewers.ComboViewer;
33 import org.eclipse.jface.viewers.ISelectionChangedListener;
34 import org.eclipse.jface.viewers.IStructuredContentProvider;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.jface.viewers.LabelProvider;
37 import org.eclipse.jface.viewers.SelectionChangedEvent;
38 import org.eclipse.jface.viewers.StructuredSelection;
39 import org.eclipse.jface.viewers.Viewer;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.custom.StackLayout;
42 import org.eclipse.swt.events.SelectionEvent;
43 import org.eclipse.swt.events.SelectionListener;
44 import org.eclipse.swt.graphics.Image;
45 import org.eclipse.swt.layout.FillLayout;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.layout.GridLayout;
48 import org.eclipse.swt.widgets.Button;
49 import org.eclipse.swt.widgets.Combo;
50 import org.eclipse.swt.widgets.Composite;
51 import org.eclipse.swt.widgets.Control;
52 import org.eclipse.swt.widgets.Label;
53
54 /**
55  * Configures Java Editor folding preferences.
56  * 
57  * @since 3.0
58  */
59 class FoldingConfigurationBlock {
60
61         private static class ErrorPreferences implements
62                         IJavaFoldingPreferenceBlock {
63                 private String fMessage;
64
65                 protected ErrorPreferences(String message) {
66                         fMessage = message;
67                 }
68
69                 /*
70                  * @see net.sourceforge.phpdt.internal.ui.text.folding.IJavaFoldingPreferences#createControl(org.eclipse.swt.widgets.Group)
71                  */
72                 public Control createControl(Composite composite) {
73                         Composite inner = new Composite(composite, SWT.NONE);
74                         inner.setLayout(new FillLayout(SWT.VERTICAL));
75
76                         Label label = new Label(inner, SWT.CENTER);
77                         label.setText(fMessage);
78
79                         return inner;
80                 }
81
82                 public void initialize() {
83                 }
84
85                 public void performOk() {
86                 }
87
88                 public void performDefaults() {
89                 }
90
91                 public void dispose() {
92                 }
93
94         }
95
96         /** The overlay preference store. */
97         private final OverlayPreferenceStore fStore;
98
99         /* The controls */
100         private Combo fProviderCombo;
101
102         private Button fFoldingCheckbox;
103
104         private ComboViewer fProviderViewer;
105
106         private Map fProviderDescriptors;
107
108         private Composite fGroup;
109
110         private Map fProviderPreferences;
111
112         private Map fProviderControls;
113
114         private StackLayout fStackLayout;
115
116         public FoldingConfigurationBlock(OverlayPreferenceStore store) {
117                 Assert.isNotNull(store);
118                 fStore = store;
119                 fStore.addKeys(createOverlayStoreKeys());
120                 fProviderDescriptors = createListModel();
121                 fProviderPreferences = new HashMap();
122                 fProviderControls = new HashMap();
123         }
124
125         private Map createListModel() {
126                 JavaFoldingStructureProviderRegistry reg = PHPeclipsePlugin
127                                 .getDefault().getFoldingStructureProviderRegistry();
128                 reg.reloadExtensions();
129                 JavaFoldingStructureProviderDescriptor[] descs = reg
130                                 .getFoldingProviderDescriptors();
131                 Map map = new HashMap();
132                 for (int i = 0; i < descs.length; i++) {
133                         map.put(descs[i].getId(), descs[i]);
134                 }
135                 return map;
136         }
137
138         private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
139
140                 ArrayList overlayKeys = new ArrayList();
141
142                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
143                                 OverlayPreferenceStore.BOOLEAN,
144                                 PreferenceConstants.EDITOR_FOLDING_ENABLED));
145                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
146                                 OverlayPreferenceStore.STRING,
147                                 PreferenceConstants.EDITOR_FOLDING_PROVIDER));
148
149                 OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys
150                                 .size()];
151                 overlayKeys.toArray(keys);
152                 return keys;
153         }
154
155         /**
156          * Creates page for folding preferences.
157          * 
158          * @param parent
159          *            the parent composite
160          * @return the control for the preference page
161          */
162         Control createControl(Composite parent) {
163
164                 Composite composite = new Composite(parent, SWT.NULL);
165                 // assume parent page uses griddata
166                 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER
167                                 | GridData.VERTICAL_ALIGN_FILL);
168                 composite.setLayoutData(gd);
169                 GridLayout layout = new GridLayout();
170                 layout.numColumns = 2;
171                 PixelConverter pc = new PixelConverter(composite);
172                 layout.verticalSpacing = pc.convertHeightInCharsToPixels(1) / 2;
173                 composite.setLayout(layout);
174
175                 /* check box for new editors */
176                 fFoldingCheckbox = new Button(composite, SWT.CHECK);
177                 fFoldingCheckbox.setText(PreferencesMessages
178                                 .getString("FoldingConfigurationBlock.enable")); //$NON-NLS-1$
179                 gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
180                                 | GridData.VERTICAL_ALIGN_BEGINNING);
181                 fFoldingCheckbox.setLayoutData(gd);
182                 fFoldingCheckbox.addSelectionListener(new SelectionListener() {
183                         public void widgetSelected(SelectionEvent e) {
184                                 boolean enabled = fFoldingCheckbox.getSelection();
185                                 fStore.setValue(PreferenceConstants.EDITOR_FOLDING_ENABLED,
186                                                 enabled);
187                                 updateCheckboxDependencies();
188                         }
189
190                         public void widgetDefaultSelected(SelectionEvent e) {
191                         }
192                 });
193
194                 Label label = new Label(composite, SWT.CENTER);
195                 gd = new GridData(GridData.FILL_HORIZONTAL
196                                 | GridData.VERTICAL_ALIGN_BEGINNING);
197                 label.setLayoutData(gd);
198
199                 /* list */
200                 Composite comboComp = new Composite(composite, SWT.NONE);
201                 gd = new GridData(GridData.FILL_HORIZONTAL
202                                 | GridData.VERTICAL_ALIGN_BEGINNING);
203                 GridLayout gridLayout = new GridLayout(2, false);
204                 gridLayout.marginWidth = 0;
205                 comboComp.setLayout(gridLayout);
206
207                 Label comboLabel = new Label(comboComp, SWT.CENTER);
208                 gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
209                                 | GridData.VERTICAL_ALIGN_CENTER);
210                 comboLabel.setLayoutData(gd);
211                 comboLabel.setText(PreferencesMessages
212                                 .getString("FoldingConfigurationBlock.combo_caption")); //$NON-NLS-1$
213
214                 label = new Label(composite, SWT.CENTER);
215                 gd = new GridData(GridData.FILL_HORIZONTAL
216                                 | GridData.VERTICAL_ALIGN_BEGINNING);
217                 label.setLayoutData(gd);
218
219                 fProviderCombo = new Combo(comboComp, SWT.READ_ONLY | SWT.DROP_DOWN);
220                 gd = new GridData(GridData.HORIZONTAL_ALIGN_END
221                                 | GridData.VERTICAL_ALIGN_CENTER);
222                 fProviderCombo.setLayoutData(gd);
223
224                 /* list viewer */
225                 fProviderViewer = new ComboViewer(fProviderCombo);
226                 fProviderViewer.setContentProvider(new IStructuredContentProvider() {
227
228                         /*
229                          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
230                          */
231                         public void dispose() {
232                         }
233
234                         /*
235                          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
236                          *      java.lang.Object, java.lang.Object)
237                          */
238                         public void inputChanged(Viewer viewer, Object oldInput,
239                                         Object newInput) {
240                         }
241
242                         /*
243                          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
244                          */
245                         public Object[] getElements(Object inputElement) {
246                                 return fProviderDescriptors.values().toArray();
247                         }
248                 });
249                 fProviderViewer.setLabelProvider(new LabelProvider() {
250                         /*
251                          * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
252                          */
253                         public Image getImage(Object element) {
254                                 return null;
255                         }
256
257                         /*
258                          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
259                          */
260                         public String getText(Object element) {
261                                 return ((JavaFoldingStructureProviderDescriptor) element)
262                                                 .getName();
263                         }
264                 });
265                 fProviderViewer
266                                 .addSelectionChangedListener(new ISelectionChangedListener() {
267
268                                         public void selectionChanged(SelectionChangedEvent event) {
269                                                 IStructuredSelection sel = (IStructuredSelection) event
270                                                                 .getSelection();
271                                                 if (!sel.isEmpty()) {
272                                                         fStore
273                                                                         .setValue(
274                                                                                         PreferenceConstants.EDITOR_FOLDING_PROVIDER,
275                                                                                         ((JavaFoldingStructureProviderDescriptor) sel
276                                                                                                         .getFirstElement()).getId());
277                                                         updateListDependencies();
278                                                 }
279                                         }
280                                 });
281                 fProviderViewer.setInput(fProviderDescriptors);
282                 fProviderViewer.refresh();
283
284                 Composite groupComp = new Composite(composite, SWT.NONE);
285                 gd = new GridData(GridData.FILL_BOTH);
286                 gd.horizontalSpan = 2;
287                 groupComp.setLayoutData(gd);
288                 gridLayout = new GridLayout(1, false);
289                 gridLayout.marginWidth = 0;
290                 groupComp.setLayout(gridLayout);
291
292                 /* contributed provider preferences. */
293                 fGroup = new Composite(groupComp, SWT.NONE);
294                 gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
295                                 | GridData.VERTICAL_ALIGN_BEGINNING);
296                 fGroup.setLayoutData(gd);
297                 fStackLayout = new StackLayout();
298                 fGroup.setLayout(fStackLayout);
299
300                 return composite;
301         }
302
303         private void updateCheckboxDependencies() {
304         }
305
306         void updateListDependencies() {
307                 String id = fStore
308                                 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
309                 JavaFoldingStructureProviderDescriptor desc = (JavaFoldingStructureProviderDescriptor) fProviderDescriptors
310                                 .get(id);
311                 IJavaFoldingPreferenceBlock prefs;
312
313                 if (desc == null) {
314                         // safety in case there is no such descriptor
315                         String message = PreferencesMessages
316                                         .getString("FoldingConfigurationBlock.error.not_exist"); //$NON-NLS-1$
317                         PHPeclipsePlugin.log(new Status(IStatus.WARNING, PHPeclipsePlugin
318                                         .getPluginId(), IStatus.OK, message, null));
319                         prefs = new ErrorPreferences(message);
320                 } else {
321                         prefs = (IJavaFoldingPreferenceBlock) fProviderPreferences.get(id);
322                         if (prefs == null) {
323                                 try {
324                                         prefs = desc.createPreferences();
325                                         fProviderPreferences.put(id, prefs);
326                                 } catch (CoreException e) {
327                                         PHPeclipsePlugin.log(e);
328                                         prefs = new ErrorPreferences(e.getLocalizedMessage());
329                                 }
330                         }
331                 }
332
333                 Control control = (Control) fProviderControls.get(id);
334                 if (control == null) {
335                         control = prefs.createControl(fGroup);
336                         if (control == null) {
337                                 String message = PreferencesMessages
338                                                 .getString("FoldingConfigurationBlock.info.no_preferences"); //$NON-NLS-1$
339                                 control = new ErrorPreferences(message).createControl(fGroup);
340                         } else {
341                                 fProviderControls.put(id, control);
342                         }
343                 }
344                 fStackLayout.topControl = control;
345                 control.pack();
346                 fGroup.layout();
347                 fGroup.getParent().layout();
348
349                 prefs.initialize();
350         }
351
352         void initialize() {
353                 restoreFromPreferences();
354         }
355
356         void performOk() {
357                 for (Iterator it = fProviderPreferences.values().iterator(); it
358                                 .hasNext();) {
359                         IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
360                                         .next();
361                         prefs.performOk();
362                 }
363         }
364
365         void performDefaults() {
366                 restoreFromPreferences();
367                 for (Iterator it = fProviderPreferences.values().iterator(); it
368                                 .hasNext();) {
369                         IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
370                                         .next();
371                         prefs.performDefaults();
372                 }
373         }
374
375         void dispose() {
376                 for (Iterator it = fProviderPreferences.values().iterator(); it
377                                 .hasNext();) {
378                         IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) it
379                                         .next();
380                         prefs.dispose();
381                 }
382         }
383
384         private void restoreFromPreferences() {
385                 boolean enabled = fStore
386                                 .getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED);
387                 fFoldingCheckbox.setSelection(enabled);
388                 updateCheckboxDependencies();
389
390                 String id = fStore
391                                 .getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
392                 Object provider = fProviderDescriptors.get(id);
393                 if (provider != null) {
394                         fProviderViewer.setSelection(new StructuredSelection(provider),
395                                         true);
396                         updateListDependencies();
397                 }
398         }
399 }