1 package net.sourceforge.phpdt.tidy.preferences;
3 import java.util.ArrayList;
4 import java.util.StringTokenizer;
6 import org.eclipse.jface.dialogs.IDialogConstants;
7 import org.eclipse.jface.preference.FieldEditor;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.SelectionAdapter;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.layout.GridData;
12 import org.eclipse.swt.layout.GridLayout;
13 import org.eclipse.swt.widgets.Button;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Label;
16 import org.eclipse.swt.widgets.List;
17 import org.eclipse.swt.widgets.Text;
20 * A field editor for displaying and storing a list of strings.
21 * Buttons are provided for adding items to the list and removing
22 * items from the list.
24 public class AddRemoveListFieldEditor extends FieldEditor {
25 private static final String DEFAULT_ADD_LABEL = "Add";
26 private static final String DEFAULT_REMOVE_LABEL = "Remove selection";
27 private static final String DEFAULT_SEPERATOR = ";";
29 private static final int VERTICAL_DIALOG_UNITS_PER_CHAR = 8;
30 private static final int HORIZONTAL_DIALOG_UNITS_PER_CHAR = 4;
31 private static final int LIST_HEIGHT_IN_CHARS = 10;
32 private static final int LIST_HEIGHT_IN_DLUS =
33 LIST_HEIGHT_IN_CHARS * VERTICAL_DIALOG_UNITS_PER_CHAR;
35 // The top-level control for the field editor.
36 private Composite top;
39 // The text field for inputting new tags.
40 private Text textField;
41 // The button for adding the contents of
42 // the text field to the list.
44 // The button for removing the currently-selected list item.
45 private Button remove;
46 // The string used to seperate list items
47 // in a single String representation.
48 private String seperator = DEFAULT_SEPERATOR;
50 public AddRemoveListFieldEditor(
54 super(name, labelText, parent);
57 public AddRemoveListFieldEditor(
61 String removeButtonText,
63 super(name, labelText, parent);
64 setAddButtonText(addButtonText);
65 setRemoveButtonText(removeButtonText);
69 * @see org.eclipse.jface.preference.FieldEditor#adjustForNumColumns(int)
71 protected void adjustForNumColumns(int numColumns) {
72 ((GridData)top.getLayoutData()).horizontalSpan = numColumns;
76 * @see org.eclipse.jface.preference.FieldEditor#doFillIntoGrid
79 protected void doFillIntoGrid(Composite parent, int numColumns) {
82 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
83 gd.horizontalSpan = numColumns;
84 top.setLayoutData(gd);
86 Label label = getLabelControl(top);
87 GridData labelData = new GridData();
88 labelData.horizontalSpan = numColumns;
89 label.setLayoutData(labelData);
91 list = new List(top, SWT.BORDER);
93 // Create a grid data that takes up the extra
94 // space in the dialog and spans both columns.
95 GridData listData = new GridData(GridData.FILL_HORIZONTAL);
97 convertVerticalDLUsToPixels(list, LIST_HEIGHT_IN_DLUS);
98 listData.horizontalSpan = numColumns;
100 list.setLayoutData(listData);
101 list.addSelectionListener(new SelectionAdapter() {
102 public void widgetSelected(SelectionEvent e) {
107 // Create a composite for the add and remove
108 // buttons and the input text field.
109 Composite addRemoveGroup = new Composite(top, SWT.NONE);
111 GridData addRemoveData = new GridData(GridData.FILL_HORIZONTAL);
112 addRemoveData.horizontalSpan = numColumns;
113 addRemoveGroup.setLayoutData(addRemoveData);
115 GridLayout addRemoveLayout = new GridLayout();
116 addRemoveLayout.numColumns = numColumns;
117 addRemoveLayout.marginHeight = 0;
118 addRemoveLayout.marginWidth = 0;
119 addRemoveGroup.setLayout(addRemoveLayout);
121 // Create a composite for the add and remove buttons.
122 Composite buttonGroup = new Composite(addRemoveGroup, SWT.NONE);
123 buttonGroup.setLayoutData(new GridData());
125 GridLayout buttonLayout = new GridLayout();
126 buttonLayout.marginHeight = 0;
127 buttonLayout.marginWidth = 0;
128 buttonGroup.setLayout(buttonLayout);
130 // Create the add button.
131 add = new Button(buttonGroup, SWT.NONE);
132 add.setText(DEFAULT_ADD_LABEL);
133 add.addSelectionListener(new SelectionAdapter() {
134 public void widgetSelected(SelectionEvent e) {
138 GridData addData = new GridData(GridData.FILL_HORIZONTAL);
139 addData.heightHint = convertVerticalDLUsToPixels(
141 IDialogConstants.BUTTON_HEIGHT);
142 addData.widthHint = convertHorizontalDLUsToPixels(
144 IDialogConstants.BUTTON_WIDTH);
145 add.setLayoutData(addData);
147 // Create the remove button.
148 remove = new Button(buttonGroup, SWT.NONE);
149 remove.setEnabled(false);
150 remove.setText(DEFAULT_REMOVE_LABEL);
151 remove.addSelectionListener(new SelectionAdapter() {
152 public void widgetSelected(SelectionEvent e) {
153 list.remove(list.getSelectionIndex());
157 GridData removeData = new GridData(GridData.FILL_HORIZONTAL);
158 removeData.heightHint = convertVerticalDLUsToPixels(
160 IDialogConstants.BUTTON_HEIGHT);
161 removeData.widthHint = convertHorizontalDLUsToPixels(
163 IDialogConstants.BUTTON_WIDTH);
164 remove.setLayoutData(removeData);
166 // Create the text field.
167 textField = new Text(addRemoveGroup, SWT.BORDER);
169 GridData textData = new GridData(GridData.FILL_HORIZONTAL);
170 textData.horizontalSpan = numColumns - 1;
171 textData.verticalAlignment = GridData.BEGINNING;
172 textField.setLayoutData(textData);
176 * @see org.eclipse.jface.preference.FieldEditor#doLoad()
178 protected void doLoad() {
179 String items = getPreferenceStore().getString(getPreferenceName());
184 * @see org.eclipse.jface.preference.FieldEditor#doLoadDefault()
186 protected void doLoadDefault() {
187 String items = getPreferenceStore().getDefaultString(getPreferenceName());
191 // Parses the string into seperate list items and adds them to the list.
192 private void setList(String items) {
193 String[] itemArray = parseString(items);
194 list.setItems(itemArray);
198 * @see org.eclipse.jface.preference.FieldEditor#doStore()
200 protected void doStore() {
201 String s = createListString(list.getItems());
203 getPreferenceStore().setValue(getPreferenceName(), s);
207 * @see org.eclipse.jface.preference.FieldEditor#getNumberOfControls()
209 public int getNumberOfControls() {
210 // The button composite and the text field.
214 // Adds the string in the text field to the list.
216 String tag = textField.getText();
217 if (tag != null && tag.length() > 0)
219 textField.setText("");
223 * Sets the label for the button that adds
224 * the contents of the text field to the list.
226 public void setAddButtonText(String text) {
231 * Sets the label for the button that removes
232 * the selected item from the list.
234 public void setRemoveButtonText(String text) {
235 remove.setText(text);
239 * Sets the string that seperates items in the list when the
240 * list is stored as a single String in the preference store.
242 public void setSeperator(String seperator) {
243 this.seperator = seperator;
247 * Creates the single String representation of the list
248 * that is stored in the preference store.
250 private String createListString(String[] items) {
251 StringBuffer path = new StringBuffer("");//$NON-NLS-1$
253 for (int i = 0; i < items.length; i++) {
254 path.append(items[i]);
255 path.append(seperator);
257 return path.toString();
261 * Parses the single String representation of the list
262 * into an array of list items.
264 private String[] parseString(String stringList) {
266 new StringTokenizer(stringList, seperator); //$NON-NLS-1$
267 ArrayList v = new ArrayList();
268 while (st.hasMoreElements()) {
269 v.add(st.nextElement());
271 return (String[])v.toArray(new String[v.size()]);
274 // Sets the enablement of the remove button depending
275 // on the selection in the list.
276 private void selectionChanged() {
277 int index = list.getSelectionIndex();
278 remove.setEnabled(index >= 0);