intial version
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.jtidy / src / net / sourceforge / phpdt / tidy / preferences / AddRemoveListFieldEditor.java
1 package net.sourceforge.phpdt.tidy.preferences;
2
3 import java.util.ArrayList;
4 import java.util.StringTokenizer;
5
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;
18
19 /**
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.
23  */
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 = ";";
28
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;
34
35         // The top-level control for the field editor.
36         private Composite top;
37         // The list of tags.
38         private List list;
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.
43         private Button add;
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;
49         
50         public AddRemoveListFieldEditor(
51                 String name,
52                 String labelText,
53                 Composite parent) {
54                         super(name, labelText, parent);         
55         }
56         
57         public AddRemoveListFieldEditor(
58                 String name,
59                 String labelText,
60                 String addButtonText,
61                 String removeButtonText,
62                 Composite parent) {
63                         super(name, labelText, parent);
64                         setAddButtonText(addButtonText);
65                         setRemoveButtonText(removeButtonText);          
66         }
67         
68         /**
69          * @see org.eclipse.jface.preference.FieldEditor#adjustForNumColumns(int)
70          */
71         protected void adjustForNumColumns(int numColumns) {
72                 ((GridData)top.getLayoutData()).horizontalSpan = numColumns;
73         }
74
75         /**
76          * @see org.eclipse.jface.preference.FieldEditor#doFillIntoGrid
77          * (Composite, int)
78          */
79         protected void doFillIntoGrid(Composite parent, int numColumns) {
80                 top = parent;
81         
82                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
83                 gd.horizontalSpan = numColumns;
84                 top.setLayoutData(gd);
85         
86                 Label label = getLabelControl(top);
87                 GridData labelData = new GridData();
88                 labelData.horizontalSpan = numColumns;
89                 label.setLayoutData(labelData);
90         
91                 list = new List(top, SWT.BORDER);
92         
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);
96                 listData.heightHint = 
97                         convertVerticalDLUsToPixels(list, LIST_HEIGHT_IN_DLUS);
98                 listData.horizontalSpan = numColumns;
99                 
100                 list.setLayoutData(listData);
101                 list.addSelectionListener(new SelectionAdapter() {
102                         public void widgetSelected(SelectionEvent e) {
103                                 selectionChanged();
104                         }
105                 });
106                 
107                 // Create a composite for the add and remove 
108                 // buttons and the input text field.
109                 Composite addRemoveGroup = new Composite(top, SWT.NONE);
110         
111                 GridData addRemoveData = new GridData(GridData.FILL_HORIZONTAL);
112                 addRemoveData.horizontalSpan = numColumns;
113                 addRemoveGroup.setLayoutData(addRemoveData);
114         
115                 GridLayout addRemoveLayout = new GridLayout();
116                 addRemoveLayout.numColumns = numColumns;
117                 addRemoveLayout.marginHeight = 0;
118                 addRemoveLayout.marginWidth = 0;
119                 addRemoveGroup.setLayout(addRemoveLayout);
120                 
121                 // Create a composite for the add and remove buttons.
122                 Composite buttonGroup = new Composite(addRemoveGroup, SWT.NONE);
123                 buttonGroup.setLayoutData(new GridData());
124         
125                 GridLayout buttonLayout = new GridLayout();
126                 buttonLayout.marginHeight = 0;
127                 buttonLayout.marginWidth = 0;
128                 buttonGroup.setLayout(buttonLayout);
129         
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) {  
135                                 add();
136                         }       
137                 });             
138                 GridData addData = new GridData(GridData.FILL_HORIZONTAL);
139                 addData.heightHint = convertVerticalDLUsToPixels(
140                         add,
141                         IDialogConstants.BUTTON_HEIGHT);
142                 addData.widthHint = convertHorizontalDLUsToPixels(
143                         add,
144                         IDialogConstants.BUTTON_WIDTH); 
145                 add.setLayoutData(addData);     
146                 
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());
154                                 selectionChanged();
155                         }
156                 });
157                 GridData removeData = new GridData(GridData.FILL_HORIZONTAL);
158                 removeData.heightHint = convertVerticalDLUsToPixels(
159                         remove,
160                         IDialogConstants.BUTTON_HEIGHT);
161                 removeData.widthHint = convertHorizontalDLUsToPixels(
162                         remove,
163                         IDialogConstants.BUTTON_WIDTH);
164                 remove.setLayoutData(removeData);       
165                                 
166                 // Create the text field.
167                 textField = new Text(addRemoveGroup, SWT.BORDER);
168                 
169                 GridData textData = new GridData(GridData.FILL_HORIZONTAL);
170                 textData.horizontalSpan = numColumns - 1;       
171                 textData.verticalAlignment = GridData.BEGINNING;
172                 textField.setLayoutData(textData);      
173         }
174
175         /**
176          * @see org.eclipse.jface.preference.FieldEditor#doLoad()
177          */
178         protected void doLoad() {
179                 String items = getPreferenceStore().getString(getPreferenceName());
180                 setList(items);
181         }
182         
183         /**
184          * @see org.eclipse.jface.preference.FieldEditor#doLoadDefault()
185          */
186         protected void doLoadDefault() {
187                 String items = getPreferenceStore().getDefaultString(getPreferenceName());
188                 setList(items);
189         }
190         
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);
195         }
196         
197         /**
198          * @see org.eclipse.jface.preference.FieldEditor#doStore()
199          */
200         protected void doStore() {
201                 String s = createListString(list.getItems());
202                 if (s != null)
203                         getPreferenceStore().setValue(getPreferenceName(), s);
204         }
205
206         /**
207          * @see org.eclipse.jface.preference.FieldEditor#getNumberOfControls()
208          */
209         public int getNumberOfControls() {
210                 // The button composite and the text field.
211                 return 2;
212         }
213
214         // Adds the string in the text field to the list.
215         private void add() {
216                 String tag = textField.getText();
217                 if (tag != null && tag.length() > 0)
218                         list.add(tag);
219                 textField.setText("");
220         }
221
222         /**
223          *  Sets the label for the button that adds
224          * the contents of the text field to the list.
225          */
226         public void setAddButtonText(String text) {
227                 add.setText(text);
228         }
229         
230         /**
231          *  Sets the label for the button that removes
232          * the selected item from the list.
233          */
234         public void setRemoveButtonText(String text) {
235                 remove.setText(text);
236         }
237          
238         /**
239          * Sets the string that seperates items in the list when the
240          * list is stored as a single String in the preference store.
241          */
242         public void setSeperator(String seperator) {
243                 this.seperator = seperator;     
244         }
245         
246         /**
247          *  Creates the single String representation of the list
248          * that is stored in the preference store.
249          */
250         private String createListString(String[] items) {
251                 StringBuffer path = new StringBuffer("");//$NON-NLS-1$
252         
253                 for (int i = 0; i < items.length; i++) {
254                         path.append(items[i]);
255                         path.append(seperator);
256                 }
257                 return path.toString();
258         }
259         
260         /**
261          *  Parses the single String representation of the list
262          * into an array of list items.
263          */
264         private String[] parseString(String stringList) {
265                 StringTokenizer st = 
266                         new StringTokenizer(stringList, seperator); //$NON-NLS-1$
267                 ArrayList v = new ArrayList();
268                 while (st.hasMoreElements()) {
269                         v.add(st.nextElement());
270                 }
271                 return (String[])v.toArray(new String[v.size()]);
272         }
273         
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);          
279         }
280 }