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