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 / CssEditorContentAssistConfigurationBlock.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: CssEditorContentAssistConfigurationBlock.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 net.sourceforge.phpeclipse.css.ui.internal.CssUIMessages;
17 import net.sourceforge.phpeclipse.css.ui.internal.CssUIPreferences;
18
19 import org.eclipse.jface.preference.ColorSelector;
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.preference.PreferenceConverter;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.events.SelectionListener;
26 import org.eclipse.swt.graphics.RGB;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Label;
33 import org.eclipse.swt.widgets.List;
34
35 /**
36  * Configuration block for setting the preferences related to content assist.
37  */
38 final class CssEditorContentAssistConfigurationBlock
39         extends AbstractConfigurationBlock {
40
41         // Instance Variables ------------------------------------------------------
42
43         private List fColorList;
44         private final String[][] fColorListModel = new String[][] {
45                 { getString("backgroundForCompletionProposals"), //$NON-NLS-1$
46                   CssUIPreferences.CONTENTASSIST_PROPOSALS_BACKGROUND },
47                 { getString("foregroundForCompletionProposals"), //$NON-NLS-1$
48                   CssUIPreferences.CONTENTASSIST_PROPOSALS_FOREGROUND },
49         };
50         private ColorSelector fColorSelector;
51         private Control fAutoActivationDelayField;
52         private Control fAutoActivationTriggersField;
53
54         // Constructors ------------------------------------------------------------
55
56         public CssEditorContentAssistConfigurationBlock(IPreferenceStore store) {
57                 super(store);
58         }
59
60         // Public Methods ----------------------------------------------------------
61
62         public Control createControl(Composite parent) {
63
64                 Composite composite = new Composite(parent, SWT.NONE);
65                 GridLayout layout = new GridLayout();
66                 layout.numColumns = 2;
67                 composite.setLayout(layout);
68
69                 //addCompletionRadioButtons(contentAssistComposite);
70
71                 addBooleanField(composite,
72                         getString("insertSingleProposalsAutomatically"), //$NON-NLS-1$
73                         CssUIPreferences.CONTENTASSIST_AUTOINSERT, 0);
74                 addBooleanField(composite,
75                         getString("presentProposalsInAlphabeticalOrder"), //$NON-NLS-1$
76                         CssUIPreferences.CONTENTASSIST_ORDER_PROPOSALS, 0);
77                 addBooleanField(composite,
78                         getString("enableAutoActivation"), //$NON-NLS-1$
79                         CssUIPreferences.CONTENTASSIST_AUTOACTIVATION,
80                         0).addSelectionListener(new SelectionAdapter() {
81                                 public void widgetSelected(SelectionEvent e) {
82                                         updateAutoactivationControls();
83                                 }
84                         });
85                 fAutoActivationDelayField = addIntegerField(composite,
86                         getString("autoActivationDelay"), //$NON-NLS-1$
87                         CssUIPreferences.CONTENTASSIST_AUTOACTIVATION_DELAY, 4, 0);
88                 fAutoActivationTriggersField = addTextField(composite,
89                         getString("autoActivationTriggers"), //$NON-NLS-1$
90                         CssUIPreferences.CONTENTASSIST_AUTOACTIVATION_TRIGGERS, 4, 0);
91
92                 Label label = new Label(composite, SWT.LEFT);
93                 label.setText(getString("colorOptions")); //$NON-NLS-1$
94                 GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
95                 gridData.horizontalSpan = 2;
96                 label.setLayoutData(gridData);
97
98                 Composite editorComposite = new Composite(composite, SWT.NONE);
99                 layout = new GridLayout();
100                 layout.numColumns = 2;
101                 layout.marginHeight = 0;
102                 layout.marginWidth = 0;
103                 editorComposite.setLayout(layout);
104                 gridData = new GridData(
105                         GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
106                 gridData.horizontalSpan = 2;
107                 editorComposite.setLayoutData(gridData);
108
109                 fColorList = new List(editorComposite,
110                         SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
111                 gridData = new GridData(
112                         GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL);
113                 gridData.heightHint = convertHeightInCharsToPixels(composite, 8);
114                 fColorList.setLayoutData(gridData);
115                 fColorList.addSelectionListener(new SelectionListener() {
116                         public void widgetDefaultSelected(SelectionEvent e) {
117                                 // do nothing
118                         }
119                         public void widgetSelected(SelectionEvent e) {
120                                 handleColorListSelection();
121                         }
122                 });
123
124                 Composite stylesComposite = new Composite(editorComposite, SWT.NONE);
125                 layout = new GridLayout();
126                 layout.marginHeight = 0;
127                 layout.marginWidth = 0;
128                 layout.numColumns = 2;
129                 stylesComposite.setLayout(layout);
130                 stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
131                 label = new Label(stylesComposite, SWT.LEFT);
132                 label.setText(getString("color")); //$NON-NLS-1$
133                 gridData = new GridData();
134                 gridData.horizontalAlignment = GridData.BEGINNING;
135                 label.setLayoutData(gridData);
136
137                 fColorSelector = new ColorSelector(stylesComposite);
138                 Button colorButton = fColorSelector.getButton();
139                 gridData = new GridData(GridData.FILL_HORIZONTAL);
140                 gridData.horizontalAlignment = GridData.BEGINNING;
141                 colorButton.setLayoutData(gridData);
142                 colorButton.addSelectionListener(new SelectionListener() {
143                         public void widgetDefaultSelected(SelectionEvent e) {
144                                 // do nothing
145                         }
146                         public void widgetSelected(SelectionEvent e) {
147                                 int i = fColorList.getSelectionIndex();
148                                 String key = fColorListModel[i][1];
149                                 PreferenceConverter.setValue(getPreferenceStore(), key,
150                                         fColorSelector.getColorValue());
151                         }
152                 });
153
154                 initialize();
155
156                 return composite;
157         }
158
159         // Event Handlers ----------------------------------------------------------
160
161         void handleColorListSelection() {
162                 int i = fColorList.getSelectionIndex();
163                 String key = fColorListModel[i][1];
164                 RGB rgb = PreferenceConverter.getColor(getPreferenceStore(), key);
165                 fColorSelector.setColorValue(rgb);
166         }
167
168         // Private Methods ---------------------------------------------------------
169
170         private static String getString(String key) {
171                 return CssUIMessages.getString(
172                         "CssEditorPreferencePage.contentAssist." + key); //$NON-NLS-1$
173         }
174
175         private void initialize() {
176                 for (int i = 0; i < fColorListModel.length; i++) {
177                         fColorList.add(fColorListModel[i][0]);
178                 }
179                 fColorList.getDisplay().asyncExec(new Runnable() {
180                         public void run() {
181                                 if ((fColorList != null)
182                                  && !fColorList.isDisposed()) {
183                                         fColorList.select(0);
184                                         handleColorListSelection();
185                                 }
186                         }
187                 });
188                 initializeFields();
189         }
190
191         private void updateAutoactivationControls() {
192                 boolean enabled = getPreferenceStore().getBoolean(
193                         CssUIPreferences.CONTENTASSIST_AUTOACTIVATION);
194                 fAutoActivationDelayField.setEnabled(enabled);
195                 getLabel(fAutoActivationDelayField).setEnabled(enabled);
196                 fAutoActivationTriggersField.setEnabled(enabled);
197                 getLabel(fAutoActivationTriggersField).setEnabled(enabled);
198         }
199
200 }