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