View Javadoc

1   /*
2    * $Id: PropertyEditor.java,v 1.5 2005/06/01 17:47:43 aperezlo Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8    
9   package com.bbn.swede.ui.wizards;
10  
11  import java.util.ArrayList;
12  import java.util.Arrays;
13  import java.util.Iterator;
14  import java.util.List;
15  import java.util.Properties;
16  
17  import org.eclipse.jface.viewers.CheckboxTableViewer;
18  import org.eclipse.jface.viewers.ICellModifier;
19  import org.eclipse.jface.viewers.IDoubleClickListener;
20  import org.eclipse.jface.viewers.IOpenListener;
21  import org.eclipse.jface.viewers.ISelection;
22  import org.eclipse.jface.viewers.ISelectionChangedListener;
23  import org.eclipse.jface.viewers.ITableLabelProvider;
24  import org.eclipse.jface.viewers.LabelProvider;
25  import org.eclipse.jface.viewers.TextCellEditor;
26  import org.eclipse.jface.viewers.ViewerFilter;
27  import org.eclipse.swt.SWT;
28  import org.eclipse.swt.custom.TableEditor;
29  import org.eclipse.swt.events.HelpListener;
30  import org.eclipse.swt.events.SelectionAdapter;
31  import org.eclipse.swt.events.SelectionEvent;
32  import org.eclipse.swt.graphics.Image;
33  import org.eclipse.swt.layout.GridData;
34  import org.eclipse.swt.layout.GridLayout;
35  import org.eclipse.swt.widgets.Button;
36  import org.eclipse.swt.widgets.Composite;
37  import org.eclipse.swt.widgets.Control;
38  import org.eclipse.swt.widgets.Table;
39  import org.eclipse.swt.widgets.TableColumn;
40  import org.eclipse.swt.widgets.TableItem;
41  import org.eclipse.ui.model.BaseWorkbenchContentProvider;
42  
43  /***
44   * A widget used to populate Properties objects.  It consists of 
45   * a checkbox table with two buttons - add metadata and remove metadata 
46   * as well as associated cell editors.  It also supports read-only 
47   * properties, which are not editable.
48   * 
49   * @author aperezlo
50   */
51  public class PropertyEditor
52  {
53     /***
54      * Label for the name column.
55      */
56     protected static final String NAME_COLUMN_STR = "Name";
57     /***
58      * Label for the value column.
59      */
60     protected static final String VALUE_COLUMN_STR = "Value";
61  
62     private Properties _readOnlyProps;
63     
64     private Properties _props;
65     private CheckboxTableViewer _tableViewer;   
66     private Table _tableControl;
67     private Composite _control;
68     private TableColumn _namesColumn;
69     private TableColumn _valuesColumn; 
70     private TableEditor _editor;
71     
72     private List _keyList;
73     
74     private Button _newPropertyButton;
75     private Button _removePropertyButton;
76  
77     //private List 
78     
79     /***
80      * The index of the name column.
81      */
82     protected static final int NAME_COLUMN_INDEX = 0;
83     /***
84      * The index of the value column.
85      */
86     protected static final int VALUE_COLUMN_INDEX = 1;
87     
88     /***
89      * Constructor.
90      * 
91      * @param props the properties object to edit
92      */
93     public PropertyEditor(Properties props)
94     {
95        _props = props;
96        _keyList = new ArrayList();
97        _keyList.addAll(_props.keySet());
98        _readOnlyProps = new Properties();
99     }
100 
101    /***
102     * Mutator.
103     * 
104     * Configure a particular property to be read only (or not).
105     * 
106     * @param property name of the property
107     * @param isReadOnly true if it should be read-only, false otherwise
108     */
109    public void setReadOnly(String property, boolean isReadOnly)
110    {
111       _readOnlyProps.setProperty(property, Boolean.toString(isReadOnly));
112    }
113    
114    /***
115     * Creates a GUI control and configures it.
116     * 
117     * @param parent the parent component
118     * @param style the default style informationds
119     */
120    public void createControl(Composite parent, int style)
121    {
122       String[] columnNames = 
123       {
124          NAME_COLUMN_STR, 
125          VALUE_COLUMN_STR,
126       };
127 
128       _control = new Composite(parent, parent.getStyle());
129       _control.setFont(parent.getFont());
130       _control.setLayout(new GridLayout());
131       _control.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
132 
133       
134       _tableViewer = CheckboxTableViewer.newCheckList(_control, style
135          | SWT.BORDER | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
136       _tableControl = _tableViewer.getTable();
137       _tableControl.setFont(_control.getFont());
138       _tableControl.setHeaderVisible(true);
139       _tableControl.setLinesVisible(true);
140 
141       _namesColumn = new TableColumn(_tableControl, _tableControl.getStyle(), NAME_COLUMN_INDEX);
142       _valuesColumn = new TableColumn(_tableControl, _tableControl.getStyle(), VALUE_COLUMN_INDEX);
143       
144       _namesColumn.setWidth(200);
145       _valuesColumn.setWidth(200);
146      
147       _namesColumn.setText(columnNames[NAME_COLUMN_INDEX]);
148       _valuesColumn.setText(columnNames[VALUE_COLUMN_INDEX]);
149 
150       TextCellEditor[] cellEditors = new TextCellEditor[2];
151       cellEditors[0] = new TextCellEditor(_tableControl, SWT.NORMAL);
152       cellEditors[1] = new TextCellEditor(_tableControl, SWT.NORMAL);
153 
154       _tableViewer.setUseHashlookup(true);
155       _tableViewer.setColumnProperties(columnNames);
156       _tableViewer.setContentProvider(new InternalContentProvider());
157       _tableViewer.setLabelProvider(new InternalLabelProvider());
158       _tableViewer.setCellEditors(cellEditors);
159       _tableViewer.setCellModifier(new InternalCellModifier());
160       
161       _tableViewer.setInput(_keyList);
162       
163       Composite buttonPanel = new Composite(_control, _control.getStyle());
164       GridLayout buttonPanelLayout = new GridLayout(2, false);
165       
166       buttonPanel.setFont(_control.getFont());
167       buttonPanel.setLayout(buttonPanelLayout);
168       buttonPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
169 
170       _newPropertyButton = new Button(buttonPanel, buttonPanel.getStyle() | SWT.PUSH);
171       _removePropertyButton = new Button(buttonPanel, buttonPanel.getStyle() | SWT.PUSH);
172       
173       _newPropertyButton.addSelectionListener(new InternalSelectionListener());
174       _removePropertyButton.addSelectionListener(new InternalSelectionListener());
175       
176       _newPropertyButton.setFont(buttonPanel.getFont());
177       _removePropertyButton.setFont(buttonPanel.getFont());
178       
179       _newPropertyButton.setText("Add Metadata");
180       _removePropertyButton.setText("Remove Metadata");
181    }
182    
183    /***
184     * Accessor. 
185     * 
186     * @return the table control used by this PropertyEditor object
187     */
188    public Control getControl()
189    {
190       return _tableControl;
191    }
192    
193    /***
194     * Accessor.
195     * 
196     * @return true if this component is enabled, false otherwise
197     */
198    public boolean getEnabled()
199    {
200       return _control.getEnabled();
201    }
202    /***
203     * Mutator.
204     * 
205     * Sets the enabled status of this control.
206     * 
207     * @param enabled true if this component should be enabled, false otherwise
208     */
209    public void setEnabled(boolean enabled)
210    {
211       _newPropertyButton.setEnabled(enabled);
212       _removePropertyButton.setEnabled(enabled);
213       _tableControl.setEnabled(enabled);
214    }
215    
216    /***
217     * Mutator.
218     * 
219     * Set the properties object to be edited by this 'Editor.
220     * 
221     * @param newProps the java.util.Properties object to be edited
222     */
223    public void setProperties(Properties newProps)
224    {
225       if(newProps != null)
226       {
227          _newPropertyButton.setEnabled(true);
228          _removePropertyButton.setEnabled(true);
229          _tableControl.setEnabled(true);
230       }
231       else
232       {
233          _newPropertyButton.setEnabled(false);
234          _removePropertyButton.setEnabled(false);
235          _tableControl.setEnabled(false);
236       }
237       _props = newProps;
238       _keyList.clear();
239       _keyList.addAll(_props.keySet());
240       _tableViewer.setInput(_keyList);
241       _tableViewer.refresh();
242    }
243   
244    /***
245     * Accessor.
246     * 
247     * @return the Properties object being edited by this 'Editor
248     */
249    public Properties getProperties()
250    {
251       return _props;
252    }
253    
254    /***
255     * @author aperezlo
256     */
257    protected class InternalCellModifier implements ICellModifier
258    {
259       /* (non-Javadoc)
260        * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
261        */
262       public boolean canModify(Object element, String property)
263       {
264          boolean toReturn = true;
265          String key = (String) element; 
266          if(_readOnlyProps.getProperty(key) != null)
267          {
268             toReturn = Boolean.valueOf(_readOnlyProps.getProperty(key)).booleanValue();
269          }
270          return toReturn;
271       }
272       /* (non-Javadoc)
273        * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
274        */
275       public Object getValue(Object element, String property)
276       {
277          String toReturn = "";
278          toReturn = (String)(property.equals(NAME_COLUMN_STR) ? (String)element
279             : _props.getProperty((String)element));
280          return toReturn;
281       }
282       /* (non-Javadoc)
283        * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
284        */
285       public void modify(Object element, String property, Object value)
286       {
287          TableItem ti = (TableItem) element;
288          String oldKey = "";
289          String oldValue = "";
290          int index = property.equals(NAME_COLUMN_STR) ? NAME_COLUMN_INDEX : VALUE_COLUMN_INDEX;
291          
292          oldKey = ti.getText(NAME_COLUMN_INDEX);
293          oldValue = ti.getText(VALUE_COLUMN_INDEX);
294          
295          ti.setText(index, (String) value);
296          int listIndex = 0;
297          if(property.equals(NAME_COLUMN_STR))
298          {
299             listIndex = _keyList.indexOf(oldKey);
300             _keyList.remove(oldKey);
301             _props.remove(oldKey);
302             _keyList.add(listIndex, value);
303             _props.setProperty((String) value, oldValue);
304          }
305          else if(property.equals(VALUE_COLUMN_STR))
306          {
307             _props.setProperty(oldKey, (String) value);
308          }
309          _tableViewer.setInput(_keyList);
310       }
311    }
312    
313    /***
314     * @author aperezlo
315     */
316    protected class InternalSelectionListener extends SelectionAdapter
317    {
318       /* (non-Javadoc)
319        * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
320        */
321       public void widgetSelected(SelectionEvent e)
322       {
323          Button source = (Button) e.getSource();
324          if(source == _newPropertyButton)
325          {
326             String newKey = "New metadata name";
327             String newValue = "New value";
328             if(_keyList.indexOf(newKey) < 0)
329             {
330                _keyList.add(newKey);
331                _props.setProperty(newKey, newValue);
332                _tableViewer.refresh();
333                _tableViewer.editElement(_tableViewer.getTable().getItem(
334                   _keyList.size() - 1).getData(), NAME_COLUMN_INDEX);
335             }
336          }
337          else if(source == _removePropertyButton)
338          {
339             Iterator i = Arrays.asList(_tableViewer.getCheckedElements()).iterator(); 
340             String obj = null;
341             while(i.hasNext())
342             {
343                obj = (String) i.next();
344                _keyList.remove(obj);
345                _props.remove(obj);
346                _tableViewer.refresh();
347             }
348          }
349       }
350    }
351    
352    /***
353     * @author aperezlo
354     */
355    protected class InternalContentProvider extends BaseWorkbenchContentProvider
356    {
357       /* (non-Javadoc)
358        * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
359        */
360       public Object[] getElements(Object element)
361       {
362          Object[] toReturn = new Object[0];
363          if(element instanceof List)
364          {
365             toReturn = ((List) element).toArray();
366          }
367          return toReturn; 
368       }
369    }
370    
371    protected class InternalLabelProvider extends LabelProvider implements ITableLabelProvider
372    {
373       
374       /* (non-Javadoc)
375        * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
376        */
377       public Image getColumnImage(Object element, int columnIndex)
378       {
379          return null;
380       }
381 
382       /* (non-Javadoc)
383        * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
384        */
385       public String getColumnText(Object element, int columnIndex)
386       {
387          String toReturn = "";
388          if(element instanceof String)
389          {
390             toReturn = (columnIndex == NAME_COLUMN_INDEX) ? (String) element 
391                : (columnIndex == VALUE_COLUMN_INDEX) ? _props.getProperty((String) element) 
392                : "";
393          }
394          return toReturn;
395       }
396    }
397   
398 
399    /***
400     * Delegate method.
401     * 
402     * @param listener
403     */
404    public void addDoubleClickListener(IDoubleClickListener listener)
405    {
406       _tableViewer.addDoubleClickListener(listener);
407    }
408    /***
409     * Delegate method.
410     * @param filter
411     */
412    public void addFilter(ViewerFilter filter)
413    {
414       _tableViewer.addFilter(filter);
415    }
416    /***
417     * Delegate method.
418     * @param listener
419     */
420    public void addHelpListener(HelpListener listener)
421    {
422       _tableViewer.addHelpListener(listener);
423    }
424    /***
425     * Delegate method.
426     * @param listener
427     */
428    public void addOpenListener(IOpenListener listener)
429    {
430       _tableViewer.addOpenListener(listener);
431    }
432    /***
433     * Delegate method.
434     * @param listener
435     */
436    public void addPostSelectionChangedListener(
437       ISelectionChangedListener listener)
438    {
439       _tableViewer.addPostSelectionChangedListener(listener);
440    }
441    /***
442     * Delegate method.
443     * @param listener
444     */
445    public void addSelectionChangedListener(ISelectionChangedListener listener)
446    {
447       _tableViewer.addSelectionChangedListener(listener);
448    }
449    /***
450     * Delegate method.
451     * @return filters
452     */
453    public ViewerFilter[] getFilters()
454    {
455       return _tableViewer.getFilters();
456    }
457 
458    /***
459     * Delegate method.
460     * @param listener
461     */
462    public void removeDoubleClickListener(IDoubleClickListener listener)
463    {
464       _tableViewer.removeDoubleClickListener(listener);
465    }
466    /***
467     * Delegate method.
468     * @param filter
469     */
470    public void removeFilter(ViewerFilter filter)
471    {
472       _tableViewer.removeFilter(filter);
473    }
474    /***
475     * Delegate method.
476     * @param listener
477     */
478    public void removeHelpListener(HelpListener listener)
479    {
480       _tableViewer.removeHelpListener(listener);
481    }
482    /***
483     * Delegate method.
484     * @param listener
485     */
486    public void removeOpenListener(IOpenListener listener)
487    {
488       _tableViewer.removeOpenListener(listener);
489    }
490    /***
491     * Delegate method.
492     * @param listener
493     */
494    public void removePostSelectionChangedListener(
495       ISelectionChangedListener listener)
496    {
497       _tableViewer.removePostSelectionChangedListener(listener);
498    }
499    /***
500     * Delegate method.
501     * @param listener
502     */
503    public void removeSelectionChangedListener(ISelectionChangedListener listener)
504    {
505       _tableViewer.removeSelectionChangedListener(listener);
506    }
507    /***
508     * Delegate method.
509     */
510    public void resetFilters()
511    {
512       _tableViewer.resetFilters();
513    }
514    /***
515     * Delegate method.
516     * @param selection
517     */
518    public void setSelection(ISelection selection)
519    {
520       _tableViewer.setSelection(selection);
521    }
522    /***
523     * Delegate method.
524     * @param selection
525     * @param reveal
526     */
527    public void setSelection(ISelection selection, boolean reveal)
528    {
529       _tableViewer.setSelection(selection, reveal);
530    }
531 }