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
9 * Christopher Lenz - initial API and implementation
11 * $Id: AbstractConfigurationBlock.java,v 1.1 2004-09-02 18:11:50 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.preferences;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.List;
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;
41 abstract class AbstractConfigurationBlock {
43 // Instance Variables ------------------------------------------------------
45 private IPreferenceStore fPreferenceStore;
47 private Map fCheckBoxes = new HashMap();
48 private SelectionListener fCheckBoxListener = new SelectionListener() {
49 public void widgetDefaultSelected(SelectionEvent e) {
51 public void widgetSelected(SelectionEvent e) {
52 Button button = (Button) e.widget;
53 getPreferenceStore().setValue((String) fCheckBoxes.get(button),
54 button.getSelection());
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),
67 private List fNumberFields = new ArrayList();
68 private ModifyListener fNumberFieldListener = new ModifyListener() {
69 public void modifyText(ModifyEvent e) {
70 //numberFieldChanged((Text) e.widget);
74 private Map fLabels = new HashMap();
76 // Constructors ------------------------------------------------------------
78 public AbstractConfigurationBlock(IPreferenceStore store) {
79 this.fPreferenceStore = store;
82 // Public Methods ----------------------------------------------------------
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));
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));
97 // Protected Methods -------------------------------------------------------
99 protected final Button addBooleanField(Composite parent, String label,
100 String key, int indentation) {
102 Button checkBox = new Button(parent, SWT.CHECK);
103 checkBox.setText(label);
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);
111 fCheckBoxes.put(checkBox, key);
116 protected final Control addIntegerField(Composite composite, String label,
117 String key, int textLimit, int indentation) {
120 addStringField(composite, label, textLimit, indentation);
121 fTextFields.put(textControl, key);
122 fNumberFields.add(textControl);
123 textControl.addModifyListener(fNumberFieldListener);
128 protected final Control addTextField(Composite composite, String label,
129 String key, int textLimit, int indentation) {
132 addStringField(composite, label, textLimit, indentation);
133 fTextFields.put(textControl, key);
134 textControl.addModifyListener(fTextFieldListener);
139 protected final int convertHeightInCharsToPixels(Control control,
141 GC gc = new GC(control);
142 gc.setFont(control.getFont());
143 FontMetrics fontMetrics = gc.getFontMetrics();
145 if (fontMetrics == null) {
148 return Dialog.convertHeightInCharsToPixels(fontMetrics, chars);
151 protected final int convertWidthInCharsToPixels(Control control,
153 GC gc = new GC(control);
154 gc.setFont(control.getFont());
155 FontMetrics fontMetrics = gc.getFontMetrics();
157 if (fontMetrics == null) {
160 return Dialog.convertWidthInCharsToPixels(fontMetrics, chars);
163 protected final Control getLabel(Control field) {
164 return (Control) fLabels.get(field);
167 protected final IPreferenceStore getPreferenceStore() {
168 return fPreferenceStore;
171 // Private Methods ---------------------------------------------------------
173 private Text addStringField(Composite composite, String label,
174 int textLimit, int indentation) {
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);
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);
188 fLabels.put(textControl, labelControl);