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 / AbstractConfigurationBlock.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: AbstractConfigurationBlock.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.HashMap;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.ModifyEvent;
26 import org.eclipse.swt.events.ModifyListener;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.events.SelectionListener;
29 import org.eclipse.swt.graphics.FontMetrics;
30 import org.eclipse.swt.graphics.GC;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Text;
37
38 /**
39  * 
40  */
41 abstract class AbstractConfigurationBlock {
42
43         // Instance Variables ------------------------------------------------------
44
45         private IPreferenceStore fPreferenceStore;
46
47         private Map fCheckBoxes = new HashMap();
48         private SelectionListener fCheckBoxListener = new SelectionListener() {
49                 public void widgetDefaultSelected(SelectionEvent e) {
50                 }
51                 public void widgetSelected(SelectionEvent e) {
52                         Button button = (Button) e.widget;
53                         getPreferenceStore().setValue((String) fCheckBoxes.get(button),
54                                 button.getSelection());
55                 }
56         };
57
58         private Map fTextFields = new HashMap();
59         private ModifyListener fTextFieldListener = new ModifyListener() {
60                 public void modifyText(ModifyEvent e) {
61                         Text text = (Text) e.widget;
62                         getPreferenceStore().setValue((String) fTextFields.get(text),
63                                 text.getText());
64                 }
65         };
66
67         private List fNumberFields = new ArrayList();
68         private ModifyListener fNumberFieldListener = new ModifyListener() {
69                 public void modifyText(ModifyEvent e) {
70                         //numberFieldChanged((Text) e.widget);
71                 }
72         };
73
74         private Map fLabels = new HashMap();
75
76         // Constructors ------------------------------------------------------------
77
78         public AbstractConfigurationBlock(IPreferenceStore store) {
79                 this.fPreferenceStore = store;    
80         }
81
82         // Public Methods ----------------------------------------------------------
83
84         public void initializeFields() {
85                 for (Iterator i = fCheckBoxes.keySet().iterator(); i.hasNext(); ) {
86                         Button button = (Button) i.next();
87                         String key = (String) fCheckBoxes.get(button);
88                         button.setSelection(getPreferenceStore().getBoolean(key));
89                 }
90                 for (Iterator i = fTextFields.keySet().iterator(); i.hasNext(); ) {
91                         Text text = (Text) i.next();
92                         String key = (String) fTextFields.get(text);
93                         text.setText(getPreferenceStore().getString(key));
94                 }
95         }
96
97         // Protected Methods -------------------------------------------------------
98
99         protected final Button addBooleanField(Composite parent, String label,
100                 String key, int indentation) {
101
102                 Button checkBox = new Button(parent, SWT.CHECK);
103                 checkBox.setText(label);
104
105                 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
106                 gd.horizontalIndent = indentation;
107                 gd.horizontalSpan = 2;
108                 checkBox.setLayoutData(gd);
109                 checkBox.addSelectionListener(fCheckBoxListener);
110
111                 fCheckBoxes.put(checkBox, key);
112
113                 return checkBox;
114         }
115
116         protected final Control addIntegerField(Composite composite, String label,
117                 String key, int textLimit, int indentation) {
118
119                 Text textControl =
120                         addStringField(composite, label, textLimit, indentation);
121                 fTextFields.put(textControl, key);
122                 fNumberFields.add(textControl);
123                 textControl.addModifyListener(fNumberFieldListener);
124
125                 return textControl;
126         }
127
128         protected final Control addTextField(Composite composite, String label,
129                 String key, int textLimit, int indentation) {
130
131                 Text textControl =
132                         addStringField(composite, label, textLimit, indentation);
133                 fTextFields.put(textControl, key);
134                 textControl.addModifyListener(fTextFieldListener);
135
136                 return textControl;
137         }
138
139         protected final int convertHeightInCharsToPixels(Control control,
140                 int chars) {
141                 GC gc = new GC(control);
142                 gc.setFont(control.getFont());
143                 FontMetrics fontMetrics = gc.getFontMetrics();
144                 gc.dispose();
145                 if (fontMetrics == null) {
146                         return 0;
147                 }
148                 return Dialog.convertHeightInCharsToPixels(fontMetrics, chars);
149         }
150
151         protected final int convertWidthInCharsToPixels(Control control,
152                 int chars) {
153                 GC gc = new GC(control);
154                 gc.setFont(control.getFont());
155                 FontMetrics fontMetrics = gc.getFontMetrics();
156                 gc.dispose();
157                 if (fontMetrics == null) {
158                         return 0;
159                 }
160                 return Dialog.convertWidthInCharsToPixels(fontMetrics, chars);
161         }
162
163         protected final Control getLabel(Control field) {
164                 return (Control) fLabels.get(field);
165         }
166
167         protected final IPreferenceStore getPreferenceStore() {
168                 return fPreferenceStore;
169         }
170
171         // Private Methods ---------------------------------------------------------
172
173         private Text addStringField(Composite composite, String label,
174                 int textLimit, int indentation) {
175
176                 Label labelControl = new Label(composite, SWT.NONE);
177                 labelControl.setText(label);
178                 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
179                 gd.horizontalIndent = indentation;
180                 labelControl.setLayoutData(gd);
181
182                 Text textControl = new Text(composite, SWT.BORDER | SWT.SINGLE);
183                 gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
184                 gd.widthHint = convertWidthInCharsToPixels(textControl, textLimit + 1);
185                 textControl.setLayoutData(gd);
186                 textControl.setTextLimit(textLimit);
187
188                 fLabels.put(textControl, labelControl);
189
190                 return textControl;
191         }
192
193 }