View Javadoc

1   /*
2    * $Id: OWLLibraryBuilderPageZero.java,v 1.4 2005/05/31 21:57:12 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.ui.wizards;
10  
11  import java.util.regex.Pattern;
12  
13  import org.eclipse.jdt.ui.ISharedImages;
14  import org.eclipse.jdt.ui.JavaUI;
15  import org.eclipse.jface.wizard.WizardPage;
16  import org.eclipse.swt.SWT;
17  import org.eclipse.swt.events.ModifyEvent;
18  import org.eclipse.swt.events.ModifyListener;
19  import org.eclipse.swt.events.VerifyEvent;
20  import org.eclipse.swt.events.VerifyListener;
21  import org.eclipse.swt.layout.GridData;
22  import org.eclipse.swt.layout.GridLayout;
23  import org.eclipse.swt.widgets.Composite;
24  import org.eclipse.swt.widgets.Group;
25  import org.eclipse.swt.widgets.Label;
26  import org.eclipse.swt.widgets.Text;
27  
28  import com.bbn.swede.core.libraries.Libraries;
29  
30  /***
31   * The first page for the OWL Library creation and edit wizards.
32   * @author aperezlo
33   */
34  public class OWLLibraryBuilderPageZero extends WizardPage
35  {
36  
37     private Pattern _validInputPattern = Pattern.compile("[a-zA-Z0-9//-_//.]*?");
38     
39     /***
40      * The name of the library that is being created.
41      */
42     protected String _libraryName = "";
43     /***
44      * The widget for entering library name text.
45      */
46     protected Text _nameText;
47     private ModifyListener _modifyListener = new ModifyListener()
48     {
49        public void modifyText(ModifyEvent e)
50        {
51           _libraryName = ((Text) e.getSource()).getText();
52  //         _nameText.removeModifyListener(_modifyListener);
53           updatePageCompletion();
54  //         _nameText.addModifyListener(_modifyListener);
55        }
56     };
57     
58     /***
59      * Checks whether the page is complete and updates the wizard internally
60      * to reflect the result.
61      */
62     protected void updatePageCompletion()
63     {
64        setPageComplete((_libraryName.length() > 0) && (Libraries.getLibrary(_libraryName) == null));
65     }
66     
67     /***
68      * Default constructor.
69      */
70     public OWLLibraryBuilderPageZero()
71     {
72        super("page-zero", "Create a new library", JavaUI.getSharedImages()
73           .getImageDescriptor(ISharedImages.IMG_OBJS_LIBRARY));
74        setMessage("Enter a name for the new library.");
75        setImageDescriptor(JavaUI.getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_LIBRARY));
76        setPageComplete(false);
77     }
78     
79     /*
80      *  (non-Javadoc)
81      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
82      */
83     public void createControl(Composite parent)
84     {
85        Composite mainPanel = new Composite(parent, parent.getStyle());
86        mainPanel.setFont(parent.getFont());
87        mainPanel.setLayout(new GridLayout());
88        
89        Group nameGroup = new Group(mainPanel, mainPanel.getStyle());
90        nameGroup.setFont(mainPanel.getFont());
91        nameGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER
92           | GridData.VERTICAL_ALIGN_CENTER | GridData.FILL_HORIZONTAL
93           | GridData.GRAB_HORIZONTAL));
94        nameGroup.setLayout(new GridLayout(2, false));
95        nameGroup.setText("Name");
96        
97        Label nameLabel = new Label(nameGroup, nameGroup.getStyle());
98        nameLabel.setFont(nameGroup.getFont());
99        nameLabel.setText("Enter a name for  the new library:");
100 
101       _nameText = new Text(nameGroup, nameGroup.getStyle() | SWT.BORDER);
102       _nameText.setFont(nameGroup.getFont());
103       _nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
104       if(_libraryName != null)
105       {
106          _nameText.setText(_libraryName);
107       }
108       _nameText.addModifyListener(_modifyListener);
109       _nameText.addVerifyListener(new VerifyListener()
110       {
111          public void verifyText(VerifyEvent e)
112          {
113             e.doit = (_validInputPattern.matcher(e.text).matches());
114          }
115       });
116       
117       updatePageCompletion();
118       setControl(mainPanel);
119    }
120 
121    /* (non-Javadoc)
122     * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
123     */
124    public boolean canFlipToNextPage()
125    {
126       return isPageComplete();
127    }
128    
129    /***
130     * Programmatically sets the name of the library.
131     * @param newName The name to set
132     */
133    public void setLibraryName(String newName)
134    {
135       _libraryName = newName;
136       if(!(_nameText == null))
137       {
138          _nameText.removeModifyListener(_modifyListener);
139          _nameText.setText(_libraryName);
140          _nameText.addModifyListener(_modifyListener);
141       }
142    }
143    
144    /***
145     * Retrieves the name of the library.
146     * @return The library name
147     */
148    public String getLibraryName()
149    {
150       return _libraryName;
151    }
152    
153 }