View Javadoc

1   /*
2    * $Id: CombinationDialogCellEditor.java,v 1.7 2005/05/31 19:37:20 jlerner 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.editor.visualeditors.restriction;
10  
11  import java.util.List;
12  import java.util.regex.Pattern;
13  
14  import org.eclipse.jface.dialogs.Dialog;
15  import org.eclipse.jface.dialogs.IInputValidator;
16  import org.eclipse.jface.dialogs.InputDialog;
17  import org.eclipse.jface.viewers.DialogCellEditor;
18  import org.eclipse.jface.viewers.ISelectionChangedListener;
19  import org.eclipse.jface.viewers.IStructuredSelection;
20  import org.eclipse.jface.viewers.ListViewer;
21  import org.eclipse.jface.viewers.SelectionChangedEvent;
22  import org.eclipse.jface.viewers.ViewerSorter;
23  import org.eclipse.swt.SWT;
24  import org.eclipse.swt.events.SelectionAdapter;
25  import org.eclipse.swt.events.SelectionEvent;
26  import org.eclipse.swt.events.SelectionListener;
27  import org.eclipse.swt.layout.GridData;
28  import org.eclipse.swt.layout.GridLayout;
29  import org.eclipse.swt.widgets.Button;
30  import org.eclipse.swt.widgets.Composite;
31  import org.eclipse.swt.widgets.Control;
32  import org.eclipse.swt.widgets.Shell;
33  import org.eclipse.ui.dialogs.ListDialog;
34  
35  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
36  import com.bbn.swede.core.dom.owl.Restriction;
37  
38  /***
39   * A dialog that accepts a IInputValidator and can be configured for 
40   * integers, classes or instances, appropriately for cardinality, 
41   * allValuesFrom and someValuesFrom, and hasValue restrictions, 
42   * respectively. 
43   * 
44   * @author aperezlo
45   */
46  public class CombinationDialogCellEditor extends DialogCellEditor implements
47     ISelectionChangedListener
48  {
49     private static final int NO_MODE = 0;
50     private static final int INDIVIDUAL_MODE = 1;
51     private static final int DATA_VALUE_MODE = 2;
52     private static final int DATA_RANGE_MODE = 4;
53     private static final int CLASS_DESCRIPTION_MODE = 8;
54     private static final int INTEGER_MODE = 16;
55     
56     
57     private int _mode;
58     
59     private RestrictionEditorInspector _detective;
60     
61     private Restriction _currentRestriction;
62     private IOWLAbstractSyntaxTree _oast;
63     
64     
65     /***
66      * Creates a CombinationDialogCellEditor.
67      * @param parent the control which will host this editor
68      * @param detective the inspector used to populate this editor
69      * @param oast the OAST for the current document
70      */
71     public CombinationDialogCellEditor(Composite parent,
72        RestrictionEditorInspector detective, IOWLAbstractSyntaxTree oast)
73     {
74        super(parent);
75        _detective = detective;
76        _oast = oast;
77     }
78     
79     /*
80      *  (non-Javadoc)
81      * @see org.eclipse.jface.viewers.DialogCellEditor#openDialogBox(org.eclipse.swt.widgets.Control)
82      */
83     protected Object openDialogBox(Control cellEditorWindow)
84     {
85        Object toReturn = null;
86        int response = -1;
87        Object[] result = null;
88        CombinationDialogCellEditorDialog dialog = new CombinationDialogCellEditorDialog(
89           cellEditorWindow.getShell(), "0", null);
90        dialog.setBlockOnOpen(true);
91        dialog.setMode(_mode);
92        if((_mode & INTEGER_MODE) != 0)
93        {
94           dialog.setInput(null);
95           dialog.setValidator(new IInputValidator()
96           {
97              private final Pattern INTEGER_PATTERN = Pattern.compile("[//d]+"); 
98              public String isValid(String newText)
99              {
100                String toReturn = INTEGER_PATTERN.matcher(newText).matches() 
101                   ? null 
102                   : (newText.length() == 0) 
103                   ? "Please enter an integer." 
104                   : "'" + newText + "' is not a valid integer."; 
105                return toReturn;
106             }  
107          });
108          response = dialog.open();
109          if(response == Dialog.OK)
110          {
111             toReturn = dialog.getValue();
112          }
113       }
114       else if((_mode & INDIVIDUAL_MODE) != 0)
115       {
116          dialog.setInput(_detective.getAllInstances());
117          response = dialog.open();
118          if(response == Dialog.OK)
119          {
120             toReturn = dialog.getValue();
121             if(dialog.isURI())
122             {
123                java.net.URI tempURI = null;
124                try
125                {
126                   tempURI = new java.net.URI((String) toReturn);
127                   toReturn = tempURI;
128                } 
129                catch(Exception e)
130                {
131                   e.printStackTrace();
132                }
133             }
134          }
135       } 
136       else if((_mode & CLASS_DESCRIPTION_MODE) != 0)
137       {
138          dialog.setInput(_detective.getAllClasses());
139          response = dialog.open();
140          if(response == Dialog.OK)
141          {
142             toReturn = dialog.getValue();
143             if(dialog.isURI())
144             {
145                java.net.URI tempURI = null;
146                try
147                {
148                   tempURI = new java.net.URI((String) toReturn);
149                   toReturn = tempURI;
150                } 
151                catch(Exception e)
152                {
153                   e.printStackTrace();
154                }
155             }
156          }
157       }
158       
159       return toReturn;
160    }
161 
162    /***
163     * This method is to determine which restriction is selected.  Knowing the 
164     * currently selected restriction allows this cell editor to configure itself 
165     * based upon the type of restriction (cardinality restriction, value restriction, 
166     * etc.).
167     * 
168     * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(
169     *       org.eclipse.jface.viewers.SelectionChangedEvent)
170     */
171    public void selectionChanged(SelectionChangedEvent event)
172    {
173       IStructuredSelection iss = (IStructuredSelection) event.getSelection();
174       Object obj = null;
175       Restriction restriction = null;
176       if(!iss.isEmpty())
177       {
178          obj = iss.getFirstElement();
179          if(obj instanceof Restriction)
180          {
181             restriction = (Restriction) obj;
182             _currentRestriction = restriction;
183             RestrictionWrapper wrapper = new RestrictionWrapper(restriction);
184             String type = wrapper.getRestrictionType();
185             
186             _mode = NO_MODE;
187             if(type != null)
188             {
189                if(type.endsWith(RestrictionWrapper.ALLVALUES)
190                   || type.endsWith(RestrictionWrapper.SOMEVALUES))
191                {
192                   _mode |= CLASS_DESCRIPTION_MODE | DATA_RANGE_MODE;
193                } 
194                else if(type.endsWith(RestrictionWrapper.HASVALUE))
195                {
196                   _mode |= INDIVIDUAL_MODE | DATA_VALUE_MODE;
197                }
198                else if(type.endsWith(RestrictionWrapper.CARDINALITY)
199                   || type.endsWith(RestrictionWrapper.MIN)
200                   || type.endsWith(RestrictionWrapper.MAX))
201                {
202                   _mode |= INTEGER_MODE;
203                }
204             }
205          }
206       }
207       
208    }
209    
210    private class CombinationDialogCellEditorDialog extends InputDialog implements SelectionListener
211    {
212       protected List _selections;
213       protected ListViewer _listViewer;
214       protected int _mode = -1;
215       protected Object _inputCache;
216       protected boolean _isURI;
217       protected Button _isURIButton;
218       protected IInputValidator _validator;
219       
220       public CombinationDialogCellEditorDialog(Shell parentShell, String initialValue, IInputValidator iiv)
221       {
222          super(parentShell, "Restriction Value", "Please set the value of this restriction", initialValue, iiv);
223          _validator = getValidator();
224       }
225       
226       protected Control createDialogArea(Composite parent)
227       {
228          Composite superPanel = (Composite) super.createDialogArea(parent);
229          Composite selectionPanel = new Composite(superPanel, superPanel.getStyle());
230          selectionPanel.setFont(superPanel.getFont());
231          selectionPanel.setLayoutData(new GridData());
232          selectionPanel.setLayout(new GridLayout(2, false));
233 
234          Button chooseButton = new Button(selectionPanel, SWT.PUSH);
235          chooseButton.setFont(selectionPanel.getFont());
236          chooseButton.setText("Select a value");
237          chooseButton.addSelectionListener(this);
238          _isURIButton = new Button(selectionPanel, SWT.CHECK);
239          _isURIButton.setText("This is a URI");
240          _isURIButton.addSelectionListener(new SelectionAdapter()
241          {
242             public void widgetSelected(SelectionEvent e)
243             {
244                _isURI = _isURIButton.getSelection();
245             }
246          });
247 //         if(_inputCache != null)
248 //         {
249 //            _listViewer = new ListViewer(selectionPanel, SWT.SINGLE | SWT.V_SCROLL);
250 //            _listViewer.setContentProvider(RestrictionEditorProviderFactory.getFactory().getContentProvider(_detective, _oast));
251 //            _listViewer.setLabelProvider(RestrictionEditorProviderFactory.getFactory().getLabelProvider(_detective, _oast));
252 //            _listViewer.addSelectionChangedListener(this);
253 ////            _listViewer.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
254 //            _listViewer.setInput(_inputCache);
255 //         }
256          
257          return selectionPanel;
258       }
259       
260       protected void validateInput()
261       {
262          IInputValidator validator = getValidator();
263          String result = null;
264          if(validator == null)
265          {
266             validator = _validator;
267          }
268          if(validator != null)
269          {
270             result = validator.isValid(getText().getText());
271             setErrorMessage(result);
272          }
273       }
274 
275       public void setValidator(IInputValidator valid)
276       {
277          _validator = valid;
278       }
279       
280       public void setMode(int iMode)
281       {
282          this._mode = iMode;
283       }
284       
285       public boolean isURI()
286       {
287          return _isURI;
288       }
289       
290       public void setInput(Object input)
291       {
292          _inputCache = input;
293       }
294       
295       
296       public void widgetSelected(SelectionEvent e)
297       {
298          ListDialog dlg = new ListDialog(getShell())
299          {
300             protected Control createDialogArea(Composite container)
301             {
302                Control toReturn = super.createDialogArea(container);
303                super.getTableViewer().setSorter(new ViewerSorter());
304                if((_mode & INDIVIDUAL_MODE) != 0)
305                {
306                   super.getTableViewer().addFilter(NewRestrictionWizard.THE_LANGUAGE_NAMESPACE_FILTER);
307                   super.getTableViewer().addFilter(NewRestrictionWizard.THE_XSD_NAMESPACE_FILTER);
308                }
309                return toReturn;
310             }
311          };
312          dlg.setContentProvider(RestrictionEditorProviderFactory.getFactory().getContentProvider(_detective, _oast)); 
313          dlg.setLabelProvider(RestrictionEditorProviderFactory.getFactory().getLabelProvider(_detective, _oast)); 
314          dlg.setInput(_inputCache);
315          dlg.setTitle("Restriction Value Selection");
316          dlg.open();
317          Object[] obj = dlg.getResult(); 
318          String toReturn = null;
319          if(obj != null)
320          {
321             if(obj.length > 0)
322             {
323                toReturn = ((java.net.URI) obj[0]).toString();
324                getText().setText(toReturn);
325                _isURIButton.setSelection(true);
326                _isURI = true;
327             }
328          }
329       }
330       public void widgetDefaultSelected(SelectionEvent e)
331       {
332       }
333       
334    }
335    
336 
337 }