View Javadoc

1   /*
2    * $Id: NewOWLPropertyWizardPage.java,v 1.2 2005/04/07 21:44:42 jlerner Exp $
3    * Created on May 19, 2004
4    *
5    * Copyright (c) 1999-2004, BBN Technologies, LLC.
6    * All rights reserved.
7    * http://www.daml.org/legal/opensource/bbn_license.html
8    */
9   
10  /***
11   * @author tself
12   */
13  
14  package com.bbn.swede.ui.wizards;
15  
16  import org.eclipse.jface.dialogs.IDialogConstants;
17  import org.eclipse.jface.wizard.WizardPage;
18  import org.eclipse.swt.SWT;
19  import org.eclipse.swt.events.ModifyEvent;
20  import org.eclipse.swt.events.ModifyListener;
21  import org.eclipse.swt.layout.GridData;
22  import org.eclipse.swt.layout.GridLayout;
23  import org.eclipse.swt.widgets.Button;
24  import org.eclipse.swt.widgets.Composite;
25  import org.eclipse.swt.widgets.Label;
26  import org.eclipse.swt.widgets.Text;
27  
28  /***
29   * Wizard page for naming and labeling a new OWL object or datatype property.
30   * @author tself
31   */
32  public class NewOWLPropertyWizardPage extends WizardPage
33  {
34     private String _sPropertyName;
35     private String _sComments;
36     private String _sLabel;
37     private boolean _bIsObjectProperty = true;
38  
39     private Text _tPropertyName;
40     private Text _tComments;
41     private Text _tLabel;
42  
43     private Button _btnObjectProperty;
44     private Button _btnDatatypeProperty;
45  
46     /***
47      * Default constructor.
48      */
49     public NewOWLPropertyWizardPage()
50     {
51        super("New OWL Property");
52        setDescription("Create a new OWL Property within an OWL document");
53        setTitle("New OWL Property");
54     }
55  
56     /* (non-Javadoc)
57      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
58      */
59     public void createControl(Composite parent)
60     {
61        ModifyListener listener = new ModifyListener()
62        {
63           public void modifyText(ModifyEvent e)
64           {
65              validate();
66           }
67        };
68        Composite container = new Composite(parent, SWT.NONE);
69        GridLayout mainLayout = new GridLayout();
70        mainLayout.verticalSpacing = 0;
71        mainLayout.horizontalSpacing = 0;
72        mainLayout.marginWidth = 0;
73        container.setLayout(mainLayout);
74        GridData mainData =
75           new GridData(
76              GridData.VERTICAL_ALIGN_FILL
77                 | GridData.HORIZONTAL_ALIGN_FILL
78                 | GridData.GRAB_VERTICAL);
79        container.setLayoutData(mainData);
80        container.setFont(parent.getFont());
81  
82        // Property type
83        createRadioButtons(container);
84  
85        // ID
86        Composite comp = new Composite(container, SWT.NONE);
87        GridLayout gl = new GridLayout();
88        comp.setLayoutData(
89           new GridData(
90              GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
91        gl.marginHeight = 2;
92        gl.marginWidth = 2;
93        gl.numColumns = 2;
94        comp.setLayout(gl);
95        _tPropertyName = createTextWithLabel(comp, "Property ID");
96        _tPropertyName.addModifyListener(listener);
97        _tLabel = createTextWithLabel(comp, "Label");
98        _tLabel.addModifyListener(listener);
99        _tPropertyName.addModifyListener(new ModifyListener()
100       {
101          public void modifyText(ModifyEvent e)
102          {
103             suggestLabel();
104          }
105       });
106 
107       // comments
108       comp = new Composite(container, SWT.NONE);
109       gl = new GridLayout();
110       comp.setLayoutData(
111          new GridData(
112             GridData.HORIZONTAL_ALIGN_FILL
113                | GridData.GRAB_HORIZONTAL
114                | GridData.VERTICAL_ALIGN_FILL
115                | GridData.GRAB_VERTICAL));
116       gl.marginHeight = 2;
117       gl.marginWidth = 2;
118       comp.setLayout(gl);
119       Label lComments = new Label(comp, SWT.NONE);
120       lComments.setText("Comments");
121       GridData gd =
122          new GridData(
123             GridData.HORIZONTAL_ALIGN_FILL
124                | GridData.GRAB_HORIZONTAL
125                | GridData.VERTICAL_ALIGN_FILL
126                | GridData.GRAB_VERTICAL);
127       _tComments = new Text(comp, SWT.BORDER | SWT.MULTI | SWT.WRAP);
128       _tComments.setLayoutData(gd);
129       _tComments.addModifyListener(listener);
130 
131       setControl(parent);
132       validate();
133    }
134    private Text createTextWithLabel(Composite parent, String label)
135    {
136       Composite comp = new Composite(parent, SWT.NONE);
137       GridData gd = new GridData();
138       gd.grabExcessHorizontalSpace = true;
139       gd.horizontalAlignment = GridData.HORIZONTAL_ALIGN_FILL;
140       GridLayout layout = new GridLayout();
141       layout.numColumns = 2;
142       layout.marginWidth = 0;
143       comp.setLayout(layout);
144       comp.setLayoutData(
145          new GridData(
146             GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
147       comp.setFont(parent.getFont());
148 
149       Label l = new Label(comp, SWT.NONE);
150       l.setText(label);
151       l.setFont(parent.getFont());
152 
153       Text t = new Text(comp, SWT.BORDER);
154       GridData data =
155          new GridData(
156             GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
157       data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
158       data.widthHint = 100;
159       t.setLayoutData(data);
160       t.setFont(parent.getFont());
161       return t;
162    }
163    private void createRadioButtons(Composite parent)
164    {
165       Composite comp = new Composite(parent, SWT.NONE);
166       GridData gd = new GridData();
167       gd.grabExcessHorizontalSpace = true;
168       gd.horizontalAlignment = GridData.HORIZONTAL_ALIGN_FILL;
169       GridLayout layout = new GridLayout();
170       layout.numColumns = 2;
171       layout.marginWidth = 0;
172       comp.setLayout(layout);
173       comp.setLayoutData(
174          new GridData(
175             GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
176       comp.setFont(parent.getFont());
177 
178       _btnObjectProperty = new Button(comp, SWT.RADIO);
179       _btnObjectProperty.setText("Object Property");
180       _btnObjectProperty.setFont(parent.getFont());
181 
182       _btnDatatypeProperty = new Button(comp, SWT.RADIO);
183       _btnDatatypeProperty.setText("Datatype Property");
184 
185       GridData data =
186          new GridData(
187             GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
188       data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
189       data.widthHint = 100;
190    }
191    private Composite createSplitRow(
192       Composite parent,
193       Composite left,
194       Composite right)
195    {
196       Composite comp = new Composite(parent, SWT.NONE);
197       GridLayout gl = new GridLayout();
198       gl.numColumns = 2;
199       comp.setLayout(gl);
200       return comp;
201    }
202 
203    private void validate()
204    {
205       boolean valid = true;
206       boolean warning = false;
207 
208       /*
209        * Validation checks should be performed in order of
210        * highest priority. Each validation section should only
211        * start if valid==true.
212        */
213 
214       // ID validation
215       if (_tPropertyName.getText().length() < 1)
216       {
217          setErrorMessage("Property ID must be specified");
218          _sPropertyName = null;
219          valid = false;
220       }
221       else
222       {
223          if (!(valid = isValidID(_tPropertyName.getText())))
224          {
225             setErrorMessage("Invalid ID");
226             _sPropertyName = null;
227          }
228       }
229 
230       // if we're invalid, go ahead and return
231       if (!valid)
232       {
233          setPageComplete(valid);
234          return;
235       }
236       // if we're here then everything is valid
237       setErrorMessage(null);
238       if (!warning)
239       {
240          setMessage(null);
241       }
242 
243       _sPropertyName = _tPropertyName.getText();
244       _sLabel = _tLabel.getText();
245       _sComments = _tComments.getText();
246       _bIsObjectProperty = _btnObjectProperty.getSelection();
247       setPageComplete(valid);
248    }
249    private void suggestLabel()
250    {
251       _tLabel.setText(_tPropertyName.getText());
252       validate();
253    }
254    private boolean isValidID(String name)
255    {
256       char c = '1';
257       if (name == null || name.length() < 1)
258       {
259          return false;
260       }
261       c = name.charAt(0);
262       if (!isValidIdentifierStart(name.charAt(0)))
263       {
264          return false;
265       }
266       for (int i = 1; i < name.length(); i++)
267       {
268          c = name.charAt(i);
269          if (!isValidIdentifierPart(c))
270          {
271             return false;
272          }
273       }
274       return true;
275    }
276    private boolean isValidIdentifierStart(char c)
277    {
278       return (Character.isJavaIdentifierStart(c) && c != '$');
279    }
280    private boolean isValidIdentifierPart(char c)
281    {
282       return (Character.isJavaIdentifierPart(c) && c != '$');
283    }
284 
285    /***
286     * Retrieves the user-defined comments for the OWL property.
287     * @return The contents of the Comments text box.
288     */
289    public String getComments()
290    {
291       return _sComments.length() > 0 ? _sComments : null;
292    }
293 
294    /***
295     * Retrieves the user-defined name for the OWL property.
296     * @return The contents of the ID text box.
297     */
298    public String getPropertyName()
299    {
300       return _sPropertyName.length() > 0 ? _sPropertyName : null;
301    }
302    
303    /***
304     * Retrieves the user-defined label for the OWL property.
305     * @return The contents of the Label text box.
306     */
307    public String getLabel()
308    {
309       return _sLabel.length() > 0 ? _sLabel : null;
310    }
311 
312    /***
313     * Indicates whether the user has asked to create an object or datatype
314     * property.
315     * @return <code>true</code> if the Object Property radio button is selected,
316     *         <code>false</code> if the Datatype Property radio button is selected.
317     */
318    public boolean isObjectProperty()
319    {
320       return _bIsObjectProperty;
321    }
322 }