intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / preferences / CssEditorAnnotationsConfigurationBlock.java
1 /*
2  * Copyright (c) 2003-2004 Christopher Lenz 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  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: CssEditorAnnotationsConfigurationBlock.java,v 1.1 2004-09-02 18:11:50 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.preferences;
15
16 import java.util.ArrayList;
17 import java.util.Iterator;
18
19 import net.sourceforge.phpeclipse.css.ui.internal.CssUIMessages;
20
21 import org.eclipse.jface.preference.ColorSelector;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.preference.PreferenceConverter;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.graphics.RGB;
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 import org.eclipse.swt.widgets.List;
35 import org.eclipse.ui.texteditor.AnnotationPreference;
36 import org.eclipse.ui.texteditor.MarkerAnnotationPreferences;
37
38 /**
39  * 
40  */
41 final class CssEditorAnnotationsConfigurationBlock
42         extends AbstractConfigurationBlock {
43
44         // Instance Variables ------------------------------------------------------
45
46         private List fColorList;
47         private String[][] fColorListModel;
48         private ColorSelector fColorSelector;
49         private Button fShowInTextCheckBox;
50         private Button fShowInOverviewRulerCheckBox;
51
52         // Constructors ------------------------------------------------------------
53
54         public CssEditorAnnotationsConfigurationBlock(IPreferenceStore store) {
55                 super(store);
56                 MarkerAnnotationPreferences preferences =
57                         new MarkerAnnotationPreferences();
58                 fColorListModel = createAnnotationTypeListModel(preferences);
59         }
60
61         // Public Methods ----------------------------------------------------------
62
63         public Control createControl(Composite parent) {
64
65                 Composite composite = new Composite(parent, SWT.NONE);
66                 GridLayout layout = new GridLayout();
67                 layout.numColumns = 2;
68                 composite.setLayout(layout);
69
70                 Label label = new Label(composite, SWT.LEFT);
71                 label.setText(getString("presentationOptions")); //$NON-NLS-1$
72                 GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
73                 gridData.horizontalSpan = 2;
74                 label.setLayoutData(gridData);
75
76                 Composite editorComposite = new Composite(composite, SWT.NONE);
77                 layout = new GridLayout();
78                 layout.numColumns = 2;
79                 layout.marginHeight = 0;
80                 layout.marginWidth = 0;
81                 editorComposite.setLayout(layout);
82                 gridData = new GridData(
83                         GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
84                 gridData.horizontalSpan = 2;
85                 editorComposite.setLayoutData(gridData);          
86
87                 fColorList = new List(editorComposite,
88                         SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
89                 gridData = new GridData(
90                         GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL);
91                 gridData.heightHint = convertHeightInCharsToPixels(composite, 8);
92                 fColorList.setLayoutData(gridData);
93                 fColorList.addSelectionListener(new SelectionListener() {
94                         public void widgetDefaultSelected(SelectionEvent e) { }
95                         public void widgetSelected(SelectionEvent e) {
96                                 handleAnnotationColorListSelection();
97                         }
98                 });
99
100                 Composite optionsComposite = new Composite(editorComposite, SWT.NONE);
101                 layout = new GridLayout();
102                 layout.marginHeight = 0;
103                 layout.marginWidth = 0;
104                 layout.numColumns = 2;
105                 optionsComposite.setLayout(layout);
106                 optionsComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
107                 
108                 fShowInTextCheckBox = new Button(optionsComposite, SWT.CHECK);
109                 fShowInTextCheckBox.setText(getString(
110                         "showInText")); //$NON-NLS-1$
111                 gridData = new GridData(GridData.FILL_HORIZONTAL);
112                 gridData.horizontalAlignment = GridData.BEGINNING;
113                 gridData.horizontalSpan = 2;
114                 fShowInTextCheckBox.setLayoutData(gridData);
115                 fShowInTextCheckBox.addSelectionListener(new SelectionListener() {
116                         public void widgetDefaultSelected(SelectionEvent e) { }
117                         public void widgetSelected(SelectionEvent e) {
118                                 int i = fColorList.getSelectionIndex();
119                                 String key = fColorListModel[i][2];
120                                 getPreferenceStore().setValue(key,
121                                         fShowInTextCheckBox.getSelection());
122                         }
123                 });
124                 
125                 fShowInOverviewRulerCheckBox = new Button(optionsComposite, SWT.CHECK);
126                 fShowInOverviewRulerCheckBox.setText(getString(
127                         "showInOverviewRuler")); //$NON-NLS-1$
128                 gridData = new GridData(GridData.FILL_HORIZONTAL);
129                 gridData.horizontalAlignment = GridData.BEGINNING;
130                 gridData.horizontalSpan = 2;
131                 fShowInOverviewRulerCheckBox.setLayoutData(gridData);
132                 fShowInOverviewRulerCheckBox.addSelectionListener(
133                         new SelectionListener() {
134                                 public void widgetDefaultSelected(SelectionEvent e) { }
135                                 public void widgetSelected(SelectionEvent e) {
136                                         int i = fColorList.getSelectionIndex();
137                                         String key = fColorListModel[i][3];
138                                         getPreferenceStore().setValue(key,
139                                                 fShowInOverviewRulerCheckBox.getSelection());
140                                 }
141                         });
142                 
143                 label = new Label(optionsComposite, SWT.LEFT);
144                 label.setText(getString("color")); //$NON-NLS-1$
145                 gridData = new GridData();
146                 gridData.horizontalAlignment = GridData.BEGINNING;
147                 label.setLayoutData(gridData);
148
149                 fColorSelector = new ColorSelector(optionsComposite);
150                 Button foregroundColorButton = fColorSelector.getButton();
151                 gridData = new GridData(GridData.FILL_HORIZONTAL);
152                 gridData.horizontalAlignment = GridData.BEGINNING;
153                 foregroundColorButton.setLayoutData(gridData);
154                 foregroundColorButton.addSelectionListener(new SelectionListener() {
155                         public void widgetDefaultSelected(SelectionEvent e) { }
156                         public void widgetSelected(SelectionEvent e) {
157                                 int i = fColorList.getSelectionIndex();
158                                 String key = fColorListModel[i][1];
159                                 PreferenceConverter.setValue(getPreferenceStore(), key,
160                                         fColorSelector.getColorValue());
161                         }
162                 });
163
164                 initialize();
165
166                 return composite;
167         }
168
169         // Event Handlers ----------------------------------------------------------
170
171         void handleAnnotationColorListSelection() {
172                 int i = fColorList.getSelectionIndex();
173                 String key = fColorListModel[i][1];
174                 RGB rgb = PreferenceConverter.getColor(getPreferenceStore(), key);
175                 fColorSelector.setColorValue(rgb);
176                 key = fColorListModel[i][2];
177                 fShowInTextCheckBox.setSelection(getPreferenceStore().getBoolean(key));
178                 key = fColorListModel[i][3];
179                 fShowInOverviewRulerCheckBox.setSelection(
180                         getPreferenceStore().getBoolean(key));                           
181         }
182         
183         // Private Methods ---------------------------------------------------------
184
185         private String[][] createAnnotationTypeListModel(
186                 MarkerAnnotationPreferences preferences) {
187                 
188                 ArrayList listModelItems = new ArrayList();
189                 Iterator i = preferences.getAnnotationPreferences().iterator();
190                 while (i.hasNext()) {
191                         AnnotationPreference info = (AnnotationPreference) i.next();
192                         listModelItems.add(new String[] {
193                                 info.getPreferenceLabel(),
194                                 info.getColorPreferenceKey(),
195                                 info.getTextPreferenceKey(),
196                                 info.getOverviewRulerPreferenceKey()
197                         });
198                 }
199
200                 String[][] listModel = new String[listModelItems.size()][];
201                 listModelItems.toArray(listModel);
202                 return listModel;
203         }
204
205         private static String getString(String key) {
206                 return CssUIMessages.getString(
207                         "CssEditorPreferencePage.annotations." + key); //$NON-NLS-1$
208         }
209
210         private void initialize() {
211                 for (int i = 0; i < fColorListModel.length; i++) {
212                         fColorList.add(fColorListModel[i][0]);
213                 }
214                 fColorList.getDisplay().asyncExec(new Runnable() {
215                         public void run() {
216                                 if ((fColorList != null)
217                                  && !fColorList.isDisposed()) {
218                                         fColorList.select(0);
219                                         handleAnnotationColorListSelection();
220                                 }
221                         }
222                 });
223                 initializeFields();
224         }
225
226 }