KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > preferences > SyntaxColorTab


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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 package org.eclipse.pde.internal.ui.preferences;
12
13 import org.eclipse.jface.preference.ColorSelector;
14 import org.eclipse.jface.preference.IPreferenceStore;
15 import org.eclipse.jface.preference.PreferenceConverter;
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.source.SourceViewer;
19 import org.eclipse.jface.util.PropertyChangeEvent;
20 import org.eclipse.jface.viewers.ArrayContentProvider;
21 import org.eclipse.jface.viewers.ISelectionChangedListener;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.jface.viewers.TableViewer;
27 import org.eclipse.jface.viewers.ViewerComparator;
28 import org.eclipse.pde.internal.ui.PDEPlugin;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.pde.internal.ui.editor.text.ChangeAwareSourceViewerConfiguration;
31 import org.eclipse.pde.internal.ui.editor.text.IColorManager;
32 import org.eclipse.pde.internal.ui.editor.text.IPDEColorConstants;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.graphics.RGB;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.Label;
43
44 public abstract class SyntaxColorTab {
45
46     protected IColorManager fColorManager;
47     private TableViewer fElementViewer;
48     private SourceViewer fPreviewViewer;
49     private ChangeAwareSourceViewerConfiguration fSourceViewerConfiguration;
50     private Button fBoldButton;
51     private Button fItalicButton;
52
53     class ColorElement {
54         private String JavaDoc fDisplayName;
55         private String JavaDoc fColorKey;
56         private RGB fColorValue;
57         private boolean fBold;
58         private boolean fItalic;
59         
60         public ColorElement(String JavaDoc displayName, String JavaDoc colorKey, RGB colorValue, boolean bold, boolean italic) {
61             fDisplayName = displayName;
62             fColorKey = colorKey;
63             fColorValue = colorValue;
64             fBold = bold;
65             fItalic = italic;
66         }
67         public String JavaDoc getColorKey() {
68             return fColorKey;
69         }
70         public String JavaDoc getDisplayName() {
71             return fDisplayName;
72         }
73         public RGB getColorValue() {
74             return fColorValue;
75         }
76         public void setColorValue(RGB rgb) {
77             if (fColorValue.equals(rgb))
78                 return;
79             RGB oldrgb = fColorValue;
80             fColorValue = rgb;
81             firePropertyChange(new PropertyChangeEvent(this, fColorKey, oldrgb, rgb));
82         }
83         
84         public void setBold(boolean bold) {
85             if (bold == fBold)
86                 return;
87             Boolean JavaDoc oldValue = Boolean.valueOf(fBold);
88             fBold = bold;
89             Boolean JavaDoc newValue = Boolean.valueOf(bold);
90             String JavaDoc property = fColorKey + IPDEColorConstants.P_BOLD_SUFFIX;
91             firePropertyChange(new PropertyChangeEvent(this, property, oldValue, newValue));
92         }
93         
94         public boolean isBold() {
95             return fBold;
96         }
97         
98         public void setItalic(boolean italic) {
99             if (italic == fItalic)
100                 return;
101             Boolean JavaDoc oldValue = Boolean.valueOf(fItalic);
102             fItalic = italic;
103             Boolean JavaDoc newValue = Boolean.valueOf(italic);
104             String JavaDoc property = fColorKey + IPDEColorConstants.P_ITALIC_SUFFIX;
105             firePropertyChange(new PropertyChangeEvent(this, property, oldValue, newValue));
106         }
107         
108         public boolean isItalic() {
109             return fItalic;
110         }
111         
112         public String JavaDoc toString() {
113             return getDisplayName();
114         }
115         
116         public void firePropertyChange(PropertyChangeEvent event) {
117             if (fSourceViewerConfiguration != null) {
118                 fSourceViewerConfiguration.adaptToPreferenceChange(event);
119                 fPreviewViewer.invalidateTextPresentation();
120             }
121         }
122     }
123     
124     public SyntaxColorTab(IColorManager manager) {
125         fColorManager = manager;
126     }
127     
128     protected abstract String JavaDoc[][] getColorStrings();
129
130     private ColorElement[] getColorData() {
131         String JavaDoc[][] colors = getColorStrings();
132         IPreferenceStore store = PDEPlugin.getDefault().getPreferenceStore();
133         ColorElement[] list = new ColorElement[colors.length];
134         for (int i = 0; i < colors.length; i++) {
135             String JavaDoc displayName = colors[i][0];
136             String JavaDoc key = colors[i][1];
137             RGB setting = PreferenceConverter.getColor(store, key);
138             boolean bold = store.getBoolean(key + IPDEColorConstants.P_BOLD_SUFFIX);
139             boolean italic = store.getBoolean(key + IPDEColorConstants.P_ITALIC_SUFFIX);
140             list[i] = new ColorElement(displayName, key, setting, bold, italic);
141         }
142         return list;
143     }
144     
145     public Control createContents(Composite parent) {
146         Composite container = new Composite(parent, SWT.NONE);
147         container.setLayout(new GridLayout());
148         container.setLayoutData(new GridData(GridData.FILL_BOTH));
149         createElementTable(container);
150         createPreviewer(container);
151         return container;
152     }
153     
154     private void createElementTable(Composite parent) {
155         Composite container = new Composite(parent, SWT.NONE);
156         GridLayout layout = new GridLayout(2, true);
157         layout.marginWidth = layout.marginHeight = 0;
158         container.setLayout(layout);
159         container.setLayoutData(new GridData(GridData.FILL_BOTH));
160             
161         Label label = new Label(container, SWT.LEFT);
162         label.setText(PDEUIMessages.SyntaxColorTab_elements);
163         GridData gd = new GridData();
164         gd.horizontalSpan = 2;
165         label.setLayoutData(gd);
166
167         fElementViewer = new TableViewer(container, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
168         fElementViewer.setLabelProvider(new LabelProvider());
169         fElementViewer.setContentProvider(new ArrayContentProvider());
170         fElementViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
171
172         Composite colorComposite = new Composite(container, SWT.NONE);
173         colorComposite.setLayout(new GridLayout(2, false));
174         colorComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
175         
176         label = new Label(colorComposite, SWT.LEFT);
177         label.setText(PDEUIMessages.SyntaxColorTab_color);
178         
179         final ColorSelector colorSelector = new ColorSelector(colorComposite);
180         Button colorButton = colorSelector.getButton();
181         colorButton.setLayoutData(new GridData(GridData.BEGINNING));
182
183         colorButton.addSelectionListener(new SelectionAdapter() {
184             public void widgetSelected(SelectionEvent e) {
185                 ColorElement item = getColorElement(fElementViewer);
186                 item.setColorValue(colorSelector.getColorValue());
187             }
188         });
189         
190         fBoldButton = new Button(colorComposite, SWT.CHECK);
191         gd = new GridData();
192         gd.horizontalSpan = 2;
193         fBoldButton.setLayoutData(gd);
194         fBoldButton.setText(PDEUIMessages.SyntaxColorTab_bold);
195         fBoldButton.addSelectionListener(new SelectionAdapter() {
196             public void widgetSelected(SelectionEvent e) {
197                 ColorElement item = getColorElement(fElementViewer);
198                 item.setBold(fBoldButton.getSelection());
199             }
200         });
201         
202         fItalicButton = new Button(colorComposite, SWT.CHECK);
203         gd = new GridData();
204         gd.horizontalSpan = 2;
205         fItalicButton.setLayoutData(gd);
206         fItalicButton.setText(PDEUIMessages.SyntaxColorTab_italic);
207         fItalicButton.addSelectionListener(new SelectionAdapter() {
208             public void widgetSelected(SelectionEvent e) {
209                 ColorElement item = getColorElement(fElementViewer);
210                 item.setItalic(fItalicButton.getSelection());
211             }
212         });
213         
214         fElementViewer.addSelectionChangedListener(new ISelectionChangedListener() {
215             public void selectionChanged(SelectionChangedEvent event) {
216                 ColorElement item = getColorElement(fElementViewer);
217                 colorSelector.setColorValue(item.getColorValue());
218                 fBoldButton.setSelection(item.isBold());
219                 fItalicButton.setSelection(item.isItalic());
220             }
221         });
222         fElementViewer.setInput(getColorData());
223         fElementViewer.setComparator(new ViewerComparator());
224         fElementViewer.setSelection(new StructuredSelection(fElementViewer.getElementAt(0)));
225     }
226     
227     private void createPreviewer(Composite parent) {
228         Composite previewComp = new Composite(parent, SWT.NONE);
229         GridLayout layout = new GridLayout();
230         layout.marginHeight = layout.marginWidth = 0;
231         previewComp.setLayout(layout);
232         previewComp.setLayoutData(new GridData(GridData.FILL_BOTH));
233         
234         Label label = new Label(previewComp, SWT.NONE);
235         label.setText(PDEUIMessages.SyntaxColorTab_preview);
236         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
237
238         fPreviewViewer = new SourceViewer(previewComp, null, SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
239         fSourceViewerConfiguration = getSourceViewerConfiguration();
240         
241         if (fSourceViewerConfiguration != null)
242             fPreviewViewer.configure(fSourceViewerConfiguration);
243     
244         fPreviewViewer.setEditable(false);
245         fPreviewViewer.getTextWidget().setFont(JFaceResources.getFont(JFaceResources.TEXT_FONT));
246         fPreviewViewer.setDocument(getDocument());
247         
248         Control control = fPreviewViewer.getControl();
249         control.setLayoutData(new GridData(GridData.FILL_BOTH));
250     }
251     
252     protected abstract ChangeAwareSourceViewerConfiguration getSourceViewerConfiguration();
253     
254     public void performOk() {
255         IPreferenceStore store = PDEPlugin.getDefault().getPreferenceStore();
256         int count = fElementViewer.getTable().getItemCount();
257         for (int i = 0; i < count; i++) {
258             ColorElement item = (ColorElement)fElementViewer.getElementAt(i);
259             PreferenceConverter.setValue(store, item.getColorKey(), item.getColorValue());
260             store.setValue(item.getColorKey() + IPDEColorConstants.P_BOLD_SUFFIX, item.isBold());
261             store.setValue(item.getColorKey() + IPDEColorConstants.P_ITALIC_SUFFIX, item.isItalic());
262         }
263     }
264     
265     public void performDefaults() {
266         IPreferenceStore store = PDEPlugin.getDefault().getPreferenceStore();
267         int count = fElementViewer.getTable().getItemCount();
268         for (int i = 0; i < count; i++) {
269             ColorElement item = (ColorElement)fElementViewer.getElementAt(i);
270             RGB rgb = PreferenceConverter.getDefaultColor(store, item.getColorKey());
271             item.setColorValue(rgb);
272             item.setBold(store.getDefaultBoolean(item.getColorKey() + IPDEColorConstants.P_BOLD_SUFFIX));
273             item.setItalic(store.getDefaultBoolean(item.getColorKey() + IPDEColorConstants.P_ITALIC_SUFFIX));
274         }
275         ColorElement element = getColorElement(fElementViewer);
276         fBoldButton.setSelection(element.isBold());
277         fItalicButton.setSelection(element.isItalic());
278     }
279     
280     public void dispose() {
281         fSourceViewerConfiguration.dispose();
282     }
283     
284     protected abstract IDocument getDocument();
285     
286     private ColorElement getColorElement(TableViewer viewer) {
287         IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
288         return (ColorElement) selection.getFirstElement();
289     }
290
291 }
292
Popular Tags