View Javadoc

1   /*
2    * $Id: NewOWLClassWizardPage.java,v 1.3 2005/05/31 21:57:12 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  package com.bbn.swede.ui.wizards;
11  
12  import org.eclipse.jface.dialogs.IDialogConstants;
13  import org.eclipse.jface.wizard.WizardPage;
14  import org.eclipse.swt.SWT;
15  import org.eclipse.swt.events.ModifyEvent;
16  import org.eclipse.swt.events.ModifyListener;
17  import org.eclipse.swt.layout.GridData;
18  import org.eclipse.swt.layout.GridLayout;
19  import org.eclipse.swt.widgets.Composite;
20  import org.eclipse.swt.widgets.Label;
21  import org.eclipse.swt.widgets.Text;
22  
23  /***
24   * Wizard page for naming and labeling a new OWL Class.
25   * @author tself
26   */
27  public class NewOWLClassWizardPage extends WizardPage
28  {
29     /***
30      * Pixel width hint used for calculating exact button sizes.
31      */
32     private static final int DEFAULT_WIDTH_HINT = 100;
33     private String _className;
34     private String _comments;
35     private String _label;
36  
37     private Text _tClassName;
38     private Text _tComments;
39     private Text _tLabel;
40  
41     /***
42      * Default constructor.
43      */
44     public NewOWLClassWizardPage()
45     {
46        super("New OWL Class");
47        setDescription("Create a new OWL Class within an OWL document");
48        setTitle("New OWL Class");
49     }
50  
51     /*
52      *  (non-Javadoc)
53      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
54      */
55     public void createControl(Composite parent)
56     {
57        ModifyListener listener = new ModifyListener()
58        {
59           public void modifyText(ModifyEvent e)
60           {
61              validate();
62           }
63        };
64        Composite container = new Composite(parent, SWT.NONE);
65        GridLayout mainLayout = new GridLayout();
66        mainLayout.verticalSpacing = 0;
67        mainLayout.horizontalSpacing = 0;
68        mainLayout.marginWidth = 0;
69        container.setLayout(mainLayout);
70        GridData mainData =
71           new GridData(
72              GridData.VERTICAL_ALIGN_FILL
73                 | GridData.HORIZONTAL_ALIGN_FILL
74                 | GridData.GRAB_VERTICAL);
75        container.setLayoutData(mainData);
76        container.setFont(parent.getFont());
77  
78        // ID
79        Composite comp = new Composite(container, SWT.NONE);
80        GridLayout gl = new GridLayout();
81        comp.setLayoutData(
82           new GridData(
83              GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
84        gl.marginHeight = 2;
85        gl.marginWidth = 2;
86        gl.numColumns = 2;
87        comp.setLayout(gl);
88        _tClassName = createTextWithLabel(comp, "Class ID");
89        _tClassName.addModifyListener(listener);
90        _tLabel = createTextWithLabel(comp, "Label");
91        _tLabel.addModifyListener(listener);
92        _tClassName.addModifyListener(new ModifyListener()
93        {
94           public void modifyText(ModifyEvent e)
95           {
96              suggestLabel();
97           }
98        });
99  
100       // comments
101       comp = new Composite(container, SWT.NONE);
102       gl = new GridLayout();
103       comp.setLayoutData(
104          new GridData(
105             GridData.HORIZONTAL_ALIGN_FILL
106                | GridData.GRAB_HORIZONTAL
107                | GridData.VERTICAL_ALIGN_FILL
108                | GridData.GRAB_VERTICAL));
109       gl.marginHeight = 2;
110       gl.marginWidth = 2;
111       comp.setLayout(gl);
112       Label lComments = new Label(comp, SWT.NONE);
113       lComments.setText("Comments");
114       GridData gd =
115          new GridData(
116             GridData.HORIZONTAL_ALIGN_FILL
117                | GridData.GRAB_HORIZONTAL
118                | GridData.VERTICAL_ALIGN_FILL
119                | GridData.GRAB_VERTICAL);
120       _tComments = new Text(comp, SWT.BORDER | SWT.MULTI | SWT.WRAP);
121       _tComments.setLayoutData(gd);
122       _tComments.addModifyListener(listener);
123 
124       setControl(parent);
125       validate();
126    }
127    private Text createTextWithLabel(Composite parent, String label)
128    {
129       Composite comp = new Composite(parent, SWT.NONE);
130       GridData gd = new GridData();
131       gd.grabExcessHorizontalSpace = true;
132       gd.horizontalAlignment = GridData.HORIZONTAL_ALIGN_FILL;
133       GridLayout layout = new GridLayout();
134       layout.numColumns = 2;
135       layout.marginWidth = 0;
136       comp.setLayout(layout);
137       comp.setLayoutData(
138          new GridData(
139             GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
140       comp.setFont(parent.getFont());
141 
142       Label l = new Label(comp, SWT.NONE);
143       l.setText(label);
144       l.setFont(parent.getFont());
145 
146       Text t = new Text(comp, SWT.BORDER);
147       GridData data =
148          new GridData(
149             GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
150       data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
151       data.widthHint = DEFAULT_WIDTH_HINT;
152       t.setLayoutData(data);
153       t.setFont(parent.getFont());
154       return t;
155    }
156    private Composite createSplitRow(
157       Composite parent,
158       Composite left,
159       Composite right)
160    {
161       Composite comp = new Composite(parent, SWT.NONE);
162       GridLayout gl = new GridLayout();
163       gl.numColumns = 2;
164       comp.setLayout(gl);
165       return comp;
166    }
167 
168    private void validate()
169    {
170       boolean valid = true;
171       boolean warning = false;
172 
173       /*
174        * Validation checks should be performed in order of
175        * highest priority. Each validation section should only
176        * start if valid==true.
177        */
178 
179       // ID validation
180       if (_tClassName.getText().length() < 1)
181       {
182          setErrorMessage("Class ID must be specified");
183          _className = null;
184          valid = false;
185       }
186       else
187       {
188          if (!(valid = isValidID(_tClassName.getText())))
189          {
190             setErrorMessage("Invalid ID");
191             _className = null;
192          }
193       }
194 
195       // if we're invalid, go ahead and return
196       if (!valid)
197       {
198          setPageComplete(valid);
199          return;
200       }
201       // if we're here then everything is valid
202       setErrorMessage(null);
203       if (!warning)
204       {
205          setMessage(null);
206       }
207 
208       _className = _tClassName.getText();
209       _label = _tLabel.getText();
210       _comments = _tComments.getText();
211       setPageComplete(valid);
212    }
213    private void suggestLabel()
214    {
215       _tLabel.setText(_tClassName.getText());
216       validate();
217    }
218    private boolean isValidID(String name)
219    {
220       char c = '1';
221       if (name == null || name.length() < 1)
222       {
223          return false;
224       }
225       c = name.charAt(0);
226       if (!isValidIdentifierStart(name.charAt(0)))
227       {
228          return false;
229       }
230       for (int i = 1; i < name.length(); i++)
231       {
232          c = name.charAt(i);
233          if (!isValidIdentifierPart(c))
234          {
235             return false;
236          }
237       }
238       return true;
239    }
240    private boolean isValidIdentifierStart(char c)
241    {
242       return (Character.isJavaIdentifierStart(c) && c != '$');
243    }
244    private boolean isValidIdentifierPart(char c)
245    {
246       return (Character.isJavaIdentifierPart(c) && c != '$');
247    }
248 
249    /***
250     * Retrieves the user-specified comments for the new OWL class.
251     * @return The contents of the Comments text box.
252     */
253    public String getComments()
254    {
255       return _comments.length() > 0 ? _comments : null;
256    }
257    /***
258     * Retrieves the user-specified ID for the new OWL class.
259     * @return The contents of the ID text box.
260     */
261    public String getClassName()
262    {
263       return _className.length() > 0 ? _className : null;
264    }
265    
266    /***
267     * Retrieves the user-specified label for the new OWL class.
268     * @return The contents of the Label text box.
269     */
270    public String getLabel()
271    {
272       return _label.length() > 0 ? _label : null;
273    }
274 
275 }