BootStrapDialog.java
001 /*  BootStrapDialog.java
002  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
003  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004  *
005  *  This file is part of GATE (see http://gate.ac.uk/), and is free
006  *  software, licenced under the GNU Library General Public License,
007  *  Version 2, June 1991 (in the distribution as file licence.html,
008  *  and also available at http://gate.ac.uk/gate/licence.html).
009  *
010  *  Cristian URSU 05/03/2001
011  *
012  *  $Id: BootStrapDialog.java 12152 2010-01-11 15:28:47Z thomas_heitz $
013  *
014  */
015 
016 package gate.gui;
017 
018 import java.awt.*;
019 import java.awt.event.ActionEvent;
020 import java.awt.event.ActionListener;
021 import java.util.*;
022 
023 import javax.swing.*;
024 
025 import gate.creole.BootStrap;
026 import gate.util.Err;
027 
028 /**
029   * This class is used to handle BootStrap wizard with the Gate GUI interface.
030   */
031 public class BootStrapDialog extends JDialog{
032 
033   MainFrame mainFrame = null;
034   BootStrapDialog thisBootStrapDialog = null;
035   BootStrap bootStrapWizard = null;
036   // Local data
037   String resourceName = null;
038   String packageName = null;
039   String resourceType = null;
040   Map    resourceTypes = null;
041   String className = null;
042   Set    resourceInterfaces = null;
043   String possibleInterfaces = null;
044   String pathNewProject = null;
045 
046   // GUI components
047   JLabel     resourceNameLabel = null;
048   JTextField resourceNameTextField = null;
049 
050   JLabel     packageNameLabel = null;
051   JTextField packageNameTextField = null;
052 
053   JLabel     resourceTypesLabel = null;
054   JComboBox  resourceTypesComboBox = null;
055 
056   JLabel     classNameLabel = null;
057   JTextField classNameTextField = null;
058 
059   JLabel     interfacesLabel = null;
060   JTextField interfacesTextField = null;
061 
062   JLabel     chooseFolderLabel = null;
063   JTextField chooseFolderTextField = null;
064   JButton    chooseFolderButton = null;
065 
066   JButton    createResourceButton = null;
067   JButton    cancelButton = null;
068   JButton    helpButton = null;
069 
070   JFileChooser fileChooser = null;
071 
072   public BootStrapDialog(MainFrame aMainFrame){
073     mainFrame = aMainFrame;
074     thisBootStrapDialog = this;
075     this.setTitle("BootStrap Wizard");
076     this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
077     initLocalData();
078     initGuiComponents();
079     initListeners();
080 
081   }//BootStrapDialog
082 
083   private void doCreateResource(){
084     // Collect the  resourceName and signal ERROR if something goes wrong
085     resourceName = resourceNameTextField.getText();
086     if (resourceName == null || "".equals(resourceName)){
087       thisBootStrapDialog.setModal(false);
088       JOptionPane.showMessageDialog(mainFrame,
089                       "A name for the resource must be provided",
090                       "ERROR !",
091                       JOptionPane.ERROR_MESSAGE);
092       thisBootStrapDialog.setModal(true);
093       return;
094     }// End if
095 
096     // Collect the  packageName and signal ERROR if something goes wrong
097     packageName = packageNameTextField.getText();
098     if (packageName == null || "".equals(packageName)){
099       thisBootStrapDialog.setModal(false);
100       JOptionPane.showMessageDialog(mainFrame,
101                       "A package name must be provided",
102                       "ERROR !",
103                       JOptionPane.ERROR_MESSAGE);
104       thisBootStrapDialog.setModal(true);
105       return;
106     }// End if
107 
108     // Collect the  className and signal ERROR if something goes wrong
109     className = classNameTextField.getText();
110     if (className == null || "".equals(className)){
111       thisBootStrapDialog.setModal(false);
112       JOptionPane.showMessageDialog(mainFrame,
113                       "A name for the implementing class must be provided",
114                       "ERROR !",
115                       JOptionPane.ERROR_MESSAGE);
116       thisBootStrapDialog.setModal(true);
117       return;
118     }// End if
119 
120     // Collect the pathNewproject and signal ERROR if something goes wrong
121     pathNewProject = chooseFolderTextField.getText();
122     if (pathNewProject == null || "".equals(pathNewProject)){
123       thisBootStrapDialog.setModal(false);
124       JOptionPane.showMessageDialog(mainFrame,
125                       "A path to the creation folder must be provided",
126                       "ERROR !",
127                       JOptionPane.ERROR_MESSAGE);
128       thisBootStrapDialog.setModal(true);
129       return;
130     }// End if
131 
132     // Collect the  resourceType and signal ERROR if something goes wrong
133     resourceType = (String)resourceTypesComboBox.getSelectedItem();
134     resourceInterfaces = this.getSelectedInterfaces();
135 
136     Thread thread = new Thread(Thread.currentThread().getThreadGroup(),
137                                new CreateResourceRunner(),
138                                "BootstrapDialog1");
139     thread.setPriority(Thread.MIN_PRIORITY);
140     thread.start();
141   }//doCreateResource();
142 
143   /**Initialises the data (the loaded resources)*/
144   public void initLocalData(){
145     pathNewProject = new String(".");
146     resourceTypes = new HashMap();
147     resourceTypes.put("LanguageResource","gate.LanguageResource");
148     resourceTypes.put("VisualResource","gate.VisualResource");
149     resourceTypes.put("ProcessingResource","gate.ProcessingResource");
150 
151     possibleInterfaces = (StringresourceTypes.get("LanguageResource");
152     if (possibleInterfaces == null)
153       possibleInterfaces = new String();
154   }// initLocalData
155 
156   /**
157     * This method initializes the GUI components
158     */
159   public void initGuiComponents(){
160 
161     //Initialise GUI components
162     this.getContentPane().setLayout(
163         new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
164     this.setModal(true);
165     // Init resource name
166     resourceNameLabel = new JLabel("Resource name, e.g. myMorph");
167     resourceNameLabel.setToolTipText("The name of the resource" +
168                                      " you want to create");
169     resourceNameLabel.setOpaque(true);
170     resourceNameLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
171     resourceNameTextField = new JTextField();
172     resourceNameTextField.setAlignmentX(Component.LEFT_ALIGNMENT);
173     resourceNameTextField.setColumns(40);
174     Dimension dim = new Dimension(
175                               resourceNameTextField.getPreferredSize().width,
176                               resourceNameTextField.getPreferredSize().height);
177     resourceNameTextField.setPreferredSize(dim);
178     resourceNameTextField.setMinimumSize(dim);
179 
180     // Init package name
181     packageNameLabel =
182       new JLabel("Resource package, e.g. sheffield.creole.morph");
183     packageNameLabel.setToolTipText("The Java package of the resource" +
184                                      " you want to create");
185     packageNameLabel.setOpaque(true);
186     packageNameLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
187     packageNameTextField = new JTextField();
188     packageNameTextField.setAlignmentX(Component.LEFT_ALIGNMENT);
189     packageNameTextField.setColumns(40);
190     dim = new DimensionpackageNameTextField.getPreferredSize().width,
191                          packageNameTextField.getPreferredSize().height);
192     packageNameTextField.setPreferredSize(dim);
193     packageNameTextField.setMinimumSize(dim);
194 
195     // init resourceTypesComboBox
196     resourceTypesLabel = new JLabel("Resource type");
197     resourceTypesLabel.setToolTipText("Resources must be LRs, PRs or VRs");
198     resourceTypesLabel.setOpaque(true);
199     resourceTypesLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
200     Vector comboCont = new Vector(resourceTypes.keySet());
201     Collections.sort(comboCont);
202     resourceTypesComboBox = new JComboBox(comboCont);
203     resourceTypesComboBox.setEditable(false);
204     resourceTypesComboBox.setAlignmentX(Component.LEFT_ALIGNMENT);
205 
206     // init class name
207     classNameLabel = new JLabel("Implementing class name, e.g. Morpher");
208     classNameLabel.setToolTipText("The name of the class that " +
209                                   "impements this resource");
210     classNameLabel.setOpaque(true);
211     classNameLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
212     classNameTextField = new JTextField();
213     classNameTextField.setAlignmentX(Component.LEFT_ALIGNMENT);
214     classNameTextField.setColumns(40);
215     dim = new Dimension(classNameTextField.getPreferredSize().width,
216                         classNameTextField.getPreferredSize().height);
217 
218     classNameTextField.setPreferredSize(dim);
219     classNameTextField.setMinimumSize(dim);
220 //    classNameTextField.setMaximumSize(dim);
221 
222     // init interfaces
223     interfacesLabel = new JLabel("Interfaces implemented");
224     interfacesLabel.setToolTipText(
225       "Any additional interfaces implemented, separated by comma"
226     );
227     interfacesLabel.setOpaque(true);
228     interfacesLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
229     interfacesTextField = new JTextField(possibleInterfaces);
230     interfacesTextField.setAlignmentX(Component.LEFT_ALIGNMENT);
231     interfacesTextField.setColumns(40);
232     dim = new Dimension(interfacesTextField.getPreferredSize().width,
233                         interfacesTextField.getPreferredSize().height);
234 
235     interfacesTextField.setPreferredSize(dim);
236     interfacesTextField.setMinimumSize(dim);
237 //    interfacesTextField.setMaximumSize(dim);
238 
239     // init choose Folder
240     chooseFolderLabel = new JLabel("Create in folder ...");
241     chooseFolderLabel.setOpaque(true);
242     chooseFolderLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
243     chooseFolderLabel.setToolTipText("Select the name of the folder where" +
244                                   " you want the resource to be created.");
245     chooseFolderButton = new JButton("Browse");
246     chooseFolderButton.setAlignmentX(Component.LEFT_ALIGNMENT);
247     chooseFolderTextField = new JTextField();
248     chooseFolderTextField.setAlignmentX(Component.LEFT_ALIGNMENT);
249     chooseFolderTextField.setColumns(35);
250     dim = new Dimension(chooseFolderTextField.getPreferredSize().width,
251                         chooseFolderTextField.getPreferredSize().height);
252 
253     chooseFolderTextField.setPreferredSize(dim);
254     chooseFolderTextField.setMinimumSize(dim);
255 //    chooseFolderTextField.setMaximumSize(dim);
256 
257     // init createresource
258     createResourceButton = new JButton("Finish");
259     getRootPane().setDefaultButton(createResourceButton);
260     // init cancel
261     cancelButton = new JButton("Cancel");
262     helpButton = new JButton("Help");
263     fileChooser = new JFileChooser();
264 
265     // Arrange the components
266     // Put all those components at their place
267     Box mainBox = new Box(BoxLayout.Y_AXIS);
268 
269     // resourceName
270     Box currentBox = new Box(BoxLayout.Y_AXIS);
271     currentBox.add(resourceNameLabel);
272     currentBox.add(resourceNameTextField);
273     mainBox.add(currentBox);
274 
275     mainBox.add(Box.createRigidArea(new Dimension(0,10)));
276 
277     // packageName
278     currentBox = new Box(BoxLayout.Y_AXIS);
279     currentBox.add(packageNameLabel);
280     currentBox.add(packageNameTextField);
281     mainBox.add(currentBox);
282 
283     mainBox.add(Box.createRigidArea(new Dimension(0,10)));
284 
285     // resourceTypes
286     currentBox = new Box(BoxLayout.Y_AXIS);
287     currentBox.add(resourceTypesLabel);
288     currentBox.add(resourceTypesComboBox);
289     mainBox.add(currentBox);
290 
291     mainBox.add(Box.createRigidArea(new Dimension(0,10)));
292 
293     // className
294     currentBox = new Box(BoxLayout.Y_AXIS);
295     currentBox.add(classNameLabel);
296     currentBox.add(classNameTextField);
297     mainBox.add(currentBox);
298 
299     mainBox.add(Box.createRigidArea(new Dimension(0,10)));
300 
301     // interfaces
302     currentBox = new Box(BoxLayout.Y_AXIS);
303     currentBox.add(interfacesLabel);
304     currentBox.add(interfacesTextField);
305     mainBox.add(currentBox);
306 
307     mainBox.add(Box.createRigidArea(new Dimension(0,10)));
308 
309     // folderName
310     currentBox = new Box(BoxLayout.Y_AXIS);
311     currentBox.add(chooseFolderLabel);
312     JPanel tmpBox = new JPanel();
313     tmpBox.setLayout(new BoxLayout(tmpBox,BoxLayout.X_AXIS));
314     tmpBox.setAlignmentX(Component.LEFT_ALIGNMENT);
315     tmpBox.add(chooseFolderTextField);
316     tmpBox.add(chooseFolderButton);
317     currentBox.add(tmpBox);
318     mainBox.add(currentBox);
319 
320     mainBox.add(Box.createRigidArea(new Dimension(0,20)));
321 
322     tmpBox = new JPanel();
323     tmpBox.setLayout(new BoxLayout(tmpBox,BoxLayout.X_AXIS));
324     tmpBox.setAlignmentX(Component.LEFT_ALIGNMENT);
325     tmpBox.add(Box.createHorizontalGlue());
326     tmpBox.add(createResourceButton);
327     tmpBox.add(Box.createRigidArea(new Dimension(25,0)));
328     tmpBox.add(cancelButton);
329     tmpBox.add(Box.createRigidArea(new Dimension(25,0)));
330     tmpBox.add(helpButton);
331     tmpBox.add(Box.createHorizontalGlue());
332     mainBox.add(tmpBox);
333 
334     // Add a space
335     this.getContentPane().add(Box.createVerticalGlue());
336     this.getContentPane().add(Box.createRigidArea(new Dimension(0,5)));
337     this.getContentPane().add(mainBox);
338     this.getContentPane().add(Box.createRigidArea(new Dimension(0,5)));
339     this.getContentPane().add(Box.createVerticalGlue());
340 
341     this.pack();
342     ////////////////////////////////
343     // Center it on screen
344     ///////////////////////////////
345     Dimension ownerSize;
346     Point ownerLocation;
347     if(getOwner() == null){
348       ownerSize = Toolkit.getDefaultToolkit().getScreenSize();
349       ownerLocation = new Point(00);
350     }else{
351       ownerSize = getOwner().getSize();
352       ownerLocation = getOwner().getLocation();
353       if(ownerSize.height == ||
354          ownerSize.width == ||
355          !getOwner().isVisible()){
356         ownerSize = Toolkit.getDefaultToolkit().getScreenSize();
357         ownerLocation = new Point(00);
358       }
359     }
360     //Center the window
361     Dimension frameSize = getSize();
362     if (frameSize.height > ownerSize.height)
363       frameSize.height = ownerSize.height;
364     if (frameSize.width > ownerSize.width)
365       frameSize.width = ownerSize.width;
366     setLocation(ownerLocation.x + (ownerSize.width - frameSize.width2,
367                 ownerLocation.y + (ownerSize.height - frameSize.height2);
368   }//initGuiComponents
369 
370   /**
371     * This one initializes the listeners fot the GUI components
372     */
373   public void initListeners(){
374 
375    createResourceButton.addActionListener(new java.awt.event.ActionListener(){
376       public void actionPerformed(ActionEvent e){
377         doCreateResource();
378       }
379    });
380 
381    cancelButton.addActionListener(new java.awt.event.ActionListener(){
382       public void actionPerformed(ActionEvent e){
383         thisBootStrapDialog.setVisible(false);
384         BootStrapDialog.this.dispose();
385       }
386    });
387 
388    helpButton.addActionListener(new java.awt.event.ActionListener(){
389       public void actionPerformed(ActionEvent e){
390         MainFrame.getInstance().showHelpFrame(
391           "http://gate.ac.uk/userguide/sec:api:bootstrap",
392           "gate.gui.BootStrapDialog");
393       }
394    });
395 
396    resourceTypesComboBox.addActionListener(new ActionListener(){
397       public void actionPerformed(ActionEvent e){
398         String selectedItem =(StringresourceTypesComboBox.getSelectedItem();
399         possibleInterfaces = (String)resourceTypes.get(selectedItem);
400         interfacesTextField.setText(possibleInterfaces);
401       }
402    });
403 
404    chooseFolderButton.addActionListener(new java.awt.event.ActionListener(){
405       public void actionPerformed(ActionEvent e){
406         // choose folder code
407         fileChooser.setDialogTitle("Select the path for this resource");
408         fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
409         if(fileChooser.showOpenDialog(mainFrame== JFileChooser.APPROVE_OPTION){
410           pathNewProject = fileChooser.getSelectedFile().toString();
411           fileChooser.setCurrentDirectory(fileChooser.getCurrentDirectory());
412         }// End if
413         chooseFolderTextField.setText(pathNewProject);
414 
415       }//actionPerformed
416    });
417 
418     // define keystrokes action bindings at the level of the main window
419     InputMap inputMap = ((JComponent)this.getContentPane()).
420       getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
421     ActionMap actionMap = ((JComponent)this.getContentPane()).getActionMap();
422     inputMap.put(KeyStroke.getKeyStroke("ENTER")"Apply");
423     actionMap.put("Apply"new AbstractAction() {
424       public void actionPerformed(ActionEvent e) {
425         createResourceButton.doClick();
426       }
427     });
428     inputMap.put(KeyStroke.getKeyStroke("ESCAPE")"Cancel");
429     actionMap.put("Cancel"new AbstractAction() {
430       public void actionPerformed(ActionEvent e) {
431         cancelButton.doClick();
432       }
433     });
434     inputMap.put(KeyStroke.getKeyStroke("F1")"Help");
435     actionMap.put("Help"new AbstractAction() {
436       public void actionPerformed(ActionEvent e) {
437         helpButton.doClick();
438       }
439     });
440   }//initListeners
441 
442 
443   /** It returns the interfaces the resource implements*/
444   public Set getSelectedInterfaces(){
445     String interfaces = interfacesTextField.getText();
446     resourceInterfaces = new HashSet();
447     if (interfaces == null || "".equals(interfaces))
448         return resourceInterfaces;
449     StringTokenizer tokenizer = new StringTokenizer(interfaces,",");
450     while (tokenizer.hasMoreElements()){
451       String token = tokenizer.nextToken();
452       resourceInterfaces.add(token);
453     }// end While
454     return resourceInterfaces;
455   }//getSelectedInterfaces
456 
457   /**Class used to run an annot. diff in a new thread*/
458   class CreateResourceRunner implements Runnable{
459 
460     public CreateResourceRunner(){
461     }// CreateResourceRunner()
462 
463     public void run(){
464 
465 
466       try{
467         bootStrapWizard = new BootStrap();
468         bootStrapWizard.createResource(resourceName,
469                                        packageName,
470                                        resourceType,
471                                        className,
472                                        resourceInterfaces,
473                                        pathNewProject);
474         thisBootStrapDialog.setVisible(false);
475         thisBootStrapDialog.dispose();
476         JOptionPane.showMessageDialog(mainFrame,
477                                       resourceName + " creation succeeded !\n" +
478                                       "Look for it in " + pathNewProject,
479                                       "DONE !",
480                                       JOptionPane.DEFAULT_OPTION);
481       }catch (Exception e){
482         thisBootStrapDialog.setModal(false);
483         e.printStackTrace(Err.getPrintWriter());
484         JOptionPane.showMessageDialog(mainFrame,
485                      e.getMessage() "\n Resource creation stopped !",
486                      "BootStrap error !",
487                      JOptionPane.ERROR_MESSAGE);
488         thisBootStrapDialog.setModal(true);
489       //End try
490     }// run();
491   }//CreateResourceRunner
492 
493 }//BootStrapDialog