1   /*  BootStrapDialog.java
2    *  Copyright (c) 1998-2001, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Cristian URSU 05/03/2001
10   *
11   *  $Id: BootStrapDialog.java,v 1.9 2001/07/09 13:43:10 cursu Exp $
12   *
13   */
14  
15  package gate.gui;
16  
17  import gate.*;
18  import gate.annotation.*;
19  import gate.persist.*;
20  import gate.util.*;
21  import gate.creole.*;
22  
23  import javax.swing.*;
24  import java.awt.event.*;
25  import java.awt.*;
26  import java.util.*;
27  
28  /**
29    * This class is used to handle BootStrap wizard with the Gate GUI interface.
30    */
31  public class BootStrapDialog extends JDialog{
32  
33    MainFrame mainFrame = null;
34    BootStrapDialog thisBootStrapDialog = null;
35    BootStrap bootStrapWizard = null;
36    // Local data
37    String resourceName = null;
38    String packageName = null;
39    String resourceType = null;
40    Map    resourceTypes = null;
41    String className = null;
42    Set    resourceInterfaces = null;
43    String possibleInterfaces = null;
44    String pathNewProject = null;
45  
46    // GUI components
47    JLabel     resourceNameLabel = null;
48    JTextField resourceNameTextField = null;
49  
50    JLabel     packageNameLabel = null;
51    JTextField packageNameTextField = null;
52  
53    JLabel     resourceTypesLabel = null;
54    JComboBox  resourceTypesComboBox = null;
55  
56    JLabel     classNameLabel = null;
57    JTextField classNameTextField = null;
58  
59    JLabel     interfacesLabel = null;
60    JTextField interfacesTextField = null;
61  
62    JLabel     chooseFolderLabel = null;
63    JTextField chooseFolderTextField = null;
64    JButton    chooseFolderButton = null;
65  
66    JButton    createResourceButton = null;
67    JButton    cancelButton = null;
68  
69    JFileChooser fileChooser = null;
70  
71    public BootStrapDialog(MainFrame aMainFrame){
72      mainFrame = aMainFrame;
73      thisBootStrapDialog = this;
74      this.setTitle("BootStrap Wizard");
75      initLocalData();
76      initGuiComponents();
77      initListeners();
78  
79    }//BootStrapDialog
80  
81    private void doCreateResource(){
82      // Collect the  resourceName and signal ERROR if something goes wrong
83      resourceName = resourceNameTextField.getText();
84      if (resourceName == null || "".equals(resourceName)){
85        thisBootStrapDialog.setModal(false);
86        JOptionPane.showMessageDialog(mainFrame,
87                        "A name for the resource must be provided",
88                        "ERROR !",
89                        JOptionPane.ERROR_MESSAGE);
90        thisBootStrapDialog.setModal(true);
91        return;
92      }// End if
93  
94      // Collect the  packageName and signal ERROR if something goes wrong
95      packageName = packageNameTextField.getText();
96      if (packageName == null || "".equals(packageName)){
97        thisBootStrapDialog.setModal(false);
98        JOptionPane.showMessageDialog(mainFrame,
99                        "A package name must be provided",
100                       "ERROR !",
101                       JOptionPane.ERROR_MESSAGE);
102       thisBootStrapDialog.setModal(true);
103       return;
104     }// End if
105 
106     // Collect the  className and signal ERROR if something goes wrong
107     className = classNameTextField.getText();
108     if (className == null || "".equals(className)){
109       thisBootStrapDialog.setModal(false);
110       JOptionPane.showMessageDialog(mainFrame,
111                       "A name for the implementing class must be provided",
112                       "ERROR !",
113                       JOptionPane.ERROR_MESSAGE);
114       thisBootStrapDialog.setModal(true);
115       return;
116     }// End if
117 
118     // Collect the pathNewproject and signal ERROR if something goes wrong
119     pathNewProject = chooseFolderTextField.getText();
120     if (pathNewProject == null || "".equals(pathNewProject)){
121       thisBootStrapDialog.setModal(false);
122       JOptionPane.showMessageDialog(mainFrame,
123                       "A path to the creation folder must be provided",
124                       "ERROR !",
125                       JOptionPane.ERROR_MESSAGE);
126       thisBootStrapDialog.setModal(true);
127       return;
128     }// End if
129 
130     // Collect the  resourceType and signal ERROR if something goes wrong
131     resourceType = (String)resourceTypesComboBox.getSelectedItem();
132     resourceInterfaces = this.getSelectedInterfaces();
133 
134     Thread thread = new Thread(Thread.currentThread().getThreadGroup(),
135                                new CreateResourceRunner(),
136                                "BootstrapDialog1");
137     thread.setPriority(Thread.MIN_PRIORITY);
138     thread.start();
139   }//doCreateResource();
140 
141   /**Initialises the data (the loaded resources)*/
142   public void initLocalData(){
143     pathNewProject = new String(".");
144     resourceTypes = new HashMap();
145     resourceTypes.put("LanguageResource","gate.LanguageResource");
146     resourceTypes.put("VisualResource","gate.VisualResource");
147     resourceTypes.put("ProcessingResource","gate.ProcessingResource");
148 
149     possibleInterfaces = (String) resourceTypes.get("LanguageResource");
150     if (possibleInterfaces == null)
151       possibleInterfaces = new String();
152   }// initLocalData
153 
154   /**
155     * This method initializes the GUI components
156     */
157   public void initGuiComponents(){
158 
159     //Initialise GUI components
160     this.getContentPane().setLayout(
161         new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
162     this.setModal(true);
163     // Init resource name
164     resourceNameLabel = new JLabel("Resource name, e.g. myMorph");
165     resourceNameLabel.setToolTipText("The name of the resource" +
166                                      " you want to create");
167     resourceNameLabel.setOpaque(true);
168     resourceNameLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
169     resourceNameTextField = new JTextField();
170     resourceNameTextField.setAlignmentX(Component.LEFT_ALIGNMENT);
171     resourceNameTextField.setColumns(40);
172     Dimension dim = new Dimension(
173                               resourceNameTextField.getPreferredSize().width,
174                               resourceNameTextField.getPreferredSize().height);
175     resourceNameTextField.setPreferredSize(dim);
176     resourceNameTextField.setMinimumSize(dim);
177 
178     // Init package name
179     packageNameLabel =
180       new JLabel("Resource package, e.g. sheffield.creole.morph");
181     packageNameLabel.setToolTipText("The Java package of the resource" +
182                                      " you want to create");
183     packageNameLabel.setOpaque(true);
184     packageNameLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
185     packageNameTextField = new JTextField();
186     packageNameTextField.setAlignmentX(Component.LEFT_ALIGNMENT);
187     packageNameTextField.setColumns(40);
188     dim = new Dimension( packageNameTextField.getPreferredSize().width,
189                          packageNameTextField.getPreferredSize().height);
190     packageNameTextField.setPreferredSize(dim);
191     packageNameTextField.setMinimumSize(dim);
192 
193     // init resourceTypesComboBox
194     resourceTypesLabel = new JLabel("Resource type");
195     resourceTypesLabel.setToolTipText("Resources must be LRs, PRs or VRs");
196     resourceTypesLabel.setOpaque(true);
197     resourceTypesLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
198     Vector comboCont = new Vector(resourceTypes.keySet());
199     Collections.sort(comboCont);
200     resourceTypesComboBox = new JComboBox(comboCont);
201     resourceTypesComboBox.setEditable(false);
202     resourceTypesComboBox.setAlignmentX(Component.LEFT_ALIGNMENT);
203 
204     // init class name
205     classNameLabel = new JLabel("Implementing class name, e.g. Morpher");
206     classNameLabel.setToolTipText("The name of the class that " +
207                                   "impements this resource");
208     classNameLabel.setOpaque(true);
209     classNameLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
210     classNameTextField = new JTextField();
211     classNameTextField.setAlignmentX(Component.LEFT_ALIGNMENT);
212     classNameTextField.setColumns(40);
213     dim = new Dimension(classNameTextField.getPreferredSize().width,
214                         classNameTextField.getPreferredSize().height);
215 
216     classNameTextField.setPreferredSize(dim);
217     classNameTextField.setMinimumSize(dim);
218 //    classNameTextField.setMaximumSize(dim);
219 
220     // init interfaces
221     interfacesLabel = new JLabel("Interfaces implemented");
222     interfacesLabel.setToolTipText(
223       "Any additional interfaces implemented, separated by comma"
224     );
225     interfacesLabel.setOpaque(true);
226     interfacesLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
227     interfacesTextField = new JTextField(possibleInterfaces);
228     interfacesTextField.setAlignmentX(Component.LEFT_ALIGNMENT);
229     interfacesTextField.setColumns(40);
230     dim = new Dimension(interfacesTextField.getPreferredSize().width,
231                         interfacesTextField.getPreferredSize().height);
232 
233     interfacesTextField.setPreferredSize(dim);
234     interfacesTextField.setMinimumSize(dim);
235 //    interfacesTextField.setMaximumSize(dim);
236 
237     // init choose Folder
238     chooseFolderLabel = new JLabel("Create in folder ...");
239     chooseFolderLabel.setOpaque(true);
240     chooseFolderLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
241     chooseFolderLabel.setToolTipText("Select the name of the folder where" +
242                                   " you want the resource to be created.");
243     chooseFolderButton = new JButton("Browse");
244     chooseFolderButton.setAlignmentX(Component.LEFT_ALIGNMENT);
245     chooseFolderTextField = new JTextField();
246     chooseFolderTextField.setAlignmentX(Component.LEFT_ALIGNMENT);
247     chooseFolderTextField.setColumns(35);
248     dim = new Dimension(chooseFolderTextField.getPreferredSize().width,
249                         chooseFolderTextField.getPreferredSize().height);
250 
251     chooseFolderTextField.setPreferredSize(dim);
252     chooseFolderTextField.setMinimumSize(dim);
253 //    chooseFolderTextField.setMaximumSize(dim);
254 
255     // init createresource
256     createResourceButton = new JButton("Finish");
257     // init cancel
258     cancelButton = new JButton("Cancel");
259     fileChooser = new JFileChooser();
260 
261     // ARANGE the components
262     // Put all those components at their place
263     Box mainBox = new Box(BoxLayout.Y_AXIS);
264 
265     // resourceName
266     Box currentBox = new Box(BoxLayout.Y_AXIS);
267     currentBox.add(resourceNameLabel);
268     currentBox.add(resourceNameTextField);
269     mainBox.add(currentBox);
270 
271     mainBox.add(Box.createRigidArea(new Dimension(0,10)));
272 
273     // packageName
274     currentBox = new Box(BoxLayout.Y_AXIS);
275     currentBox.add(packageNameLabel);
276     currentBox.add(packageNameTextField);
277     mainBox.add(currentBox);
278 
279     mainBox.add(Box.createRigidArea(new Dimension(0,10)));
280 
281     // resourceTypes
282     currentBox = new Box(BoxLayout.Y_AXIS);
283     currentBox.add(resourceTypesLabel);
284     currentBox.add(resourceTypesComboBox);
285     mainBox.add(currentBox);
286 
287     mainBox.add(Box.createRigidArea(new Dimension(0,10)));
288 
289     // className
290     currentBox = new Box(BoxLayout.Y_AXIS);
291     currentBox.add(classNameLabel);
292     currentBox.add(classNameTextField);
293     mainBox.add(currentBox);
294 
295     mainBox.add(Box.createRigidArea(new Dimension(0,10)));
296 
297     // interfaces
298     currentBox = new Box(BoxLayout.Y_AXIS);
299     currentBox.add(interfacesLabel);
300     currentBox.add(interfacesTextField);
301     mainBox.add(currentBox);
302 
303     mainBox.add(Box.createRigidArea(new Dimension(0,10)));
304 
305     // folderName
306     currentBox = new Box(BoxLayout.Y_AXIS);
307     currentBox.add(chooseFolderLabel);
308     JPanel tmpBox = new JPanel();
309     tmpBox.setLayout(new BoxLayout(tmpBox,BoxLayout.X_AXIS));
310     tmpBox.setAlignmentX(Component.LEFT_ALIGNMENT);
311     tmpBox.add(chooseFolderTextField);
312     tmpBox.add(chooseFolderButton);
313     currentBox.add(tmpBox);
314     mainBox.add(currentBox);
315 
316     mainBox.add(Box.createRigidArea(new Dimension(0,20)));
317 
318     tmpBox = new JPanel();
319     tmpBox.setLayout(new BoxLayout(tmpBox,BoxLayout.X_AXIS));
320     tmpBox.setAlignmentX(Component.LEFT_ALIGNMENT);
321     tmpBox.add(Box.createHorizontalGlue());
322     tmpBox.add(createResourceButton);
323     tmpBox.add(Box.createRigidArea(new Dimension(25,0)));
324     tmpBox.add(cancelButton);
325     tmpBox.add(Box.createHorizontalGlue());
326     mainBox.add(tmpBox);
327 
328     // Add a space
329     this.getContentPane().add(Box.createVerticalGlue());
330     this.getContentPane().add(Box.createRigidArea(new Dimension(0,5)));
331     this.getContentPane().add(mainBox);
332     this.getContentPane().add(Box.createRigidArea(new Dimension(0,5)));
333     this.getContentPane().add(Box.createVerticalGlue());
334 
335     this.pack();
336     ////////////////////////////////
337     // Center it on screen
338     ///////////////////////////////
339     Dimension ownerSize;
340     Point ownerLocation;
341     if(getOwner() == null){
342       ownerSize = Toolkit.getDefaultToolkit().getScreenSize();
343       ownerLocation = new Point(0, 0);
344     }else{
345       ownerSize = getOwner().getSize();
346       ownerLocation = getOwner().getLocation();
347       if(ownerSize.height == 0 ||
348          ownerSize.width == 0 ||
349          !getOwner().isVisible()){
350         ownerSize = Toolkit.getDefaultToolkit().getScreenSize();
351         ownerLocation = new Point(0, 0);
352       }
353     }
354     //Center the window
355     Dimension frameSize = getSize();
356     if (frameSize.height > ownerSize.height)
357       frameSize.height = ownerSize.height;
358     if (frameSize.width > ownerSize.width)
359       frameSize.width = ownerSize.width;
360     setLocation(ownerLocation.x + (ownerSize.width - frameSize.width) / 2,
361                 ownerLocation.y + (ownerSize.height - frameSize.height) / 2);
362   }//initGuiComponents
363 
364   /**
365     * This one initializes the listeners fot the GUI components
366     */
367   public void initListeners(){
368 
369    createResourceButton.addActionListener(new java.awt.event.ActionListener(){
370       public void actionPerformed(ActionEvent e){
371         doCreateResource();
372       }//actionPerformed
373    });
374 
375    cancelButton.addActionListener(new java.awt.event.ActionListener(){
376       public void actionPerformed(ActionEvent e){
377         thisBootStrapDialog.hide();
378       }//actionPerformed
379    });
380 
381    resourceTypesComboBox.addActionListener(new ActionListener(){
382       public void actionPerformed(ActionEvent e){
383         String selectedItem =(String) resourceTypesComboBox.getSelectedItem();
384         possibleInterfaces = (String)resourceTypes.get(selectedItem);
385         interfacesTextField.setText(possibleInterfaces);
386       }// actionPerformed();
387    });
388 
389    chooseFolderButton.addActionListener(new java.awt.event.ActionListener(){
390       public void actionPerformed(ActionEvent e){
391         // choose folder code
392         fileChooser.setDialogTitle("Select the path for this resource");
393         fileChooser.setFileSelectionMode(fileChooser.DIRECTORIES_ONLY);
394         if(fileChooser.showOpenDialog(mainFrame) == fileChooser.APPROVE_OPTION){
395           pathNewProject = fileChooser.getSelectedFile().toString();
396           fileChooser.setCurrentDirectory(fileChooser.getCurrentDirectory());
397         }// End if
398         chooseFolderTextField.setText(pathNewProject);
399 
400       }//actionPerformed
401    });
402 
403   }//initListeners
404 
405 
406   /** It returns the interfaces the resource implements*/
407   public Set getSelectedInterfaces(){
408     String interfaces = interfacesTextField.getText();
409     resourceInterfaces = new HashSet();
410     if (interfaces == null || "".equals(interfaces))
411         return resourceInterfaces;
412     StringTokenizer tokenizer = new StringTokenizer(interfaces,",");
413     while (tokenizer.hasMoreElements()){
414       String token = tokenizer.nextToken();
415       resourceInterfaces.add(token);
416     }// end While
417     return resourceInterfaces;
418   }//getSelectedInterfaces
419 
420   /**Class used to run an annot. diff in a new thread*/
421   class CreateResourceRunner implements Runnable{
422 
423     public CreateResourceRunner(){
424     }// CreateResourceRunner()
425 
426     public void run(){
427 
428 
429       try{
430         bootStrapWizard = new BootStrap();
431         bootStrapWizard.createResource(resourceName,
432                                        packageName,
433                                        resourceType,
434                                        className,
435                                        resourceInterfaces,
436                                        pathNewProject);
437         thisBootStrapDialog.hide();
438         JOptionPane.showMessageDialog(mainFrame,
439                                       resourceName + " creation succeeded !\n" +
440                                       "Look for it in " + pathNewProject,
441                                       "DONE !",
442                                       JOptionPane.DEFAULT_OPTION);
443       }catch (Exception e){
444         thisBootStrapDialog.setModal(false);
445         e.printStackTrace(Err.getPrintWriter());
446         JOptionPane.showMessageDialog(mainFrame,
447                      e.getMessage() + "\n Resource creation stopped !",
448                      "BootStrap error !",
449                      JOptionPane.ERROR_MESSAGE);
450         thisBootStrapDialog.setModal(true);
451       }// End try
452     }// run();
453   }//CreateResourceRunner
454 
455 }//BootStrapDialog