NewResourceDialog.java
001 /*
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  *  Valentin Tablan 23/01/2001
011  *
012  *  $Id: NewResourceDialog.java 12950 2010-08-11 18:58:56Z bensonmargulies $
013  *
014  */
015 
016 package gate.gui;
017 
018 import gate.*;
019 import gate.creole.ResourceData;
020 import gate.creole.ResourceInstantiationException;
021 import gate.util.Err;
022 import java.awt.Dimension;
023 import java.awt.Frame;
024 import java.awt.event.ActionEvent;
025 import java.awt.event.KeyEvent;
026 import java.text.NumberFormat;
027 import javax.swing.*;
028 import javax.swing.table.TableCellEditor;
029 
030 public class NewResourceDialog extends JDialog {
031 
032   public NewResourceDialog(Frame frame, String title, boolean modal) {
033     super(frame, title, modal/*, frame.getGraphicsConfiguration()*/);
034     MainFrame.getGuiRoots().add(this);
035     initLocalData();
036     initGuiComponents();
037     initListeners();
038   }
039 
040 
041   protected void initLocalData(){
042     // nothing
043   }
044 
045   protected void initGuiComponents(){
046     this.getContentPane().setLayout(new BoxLayout(this.getContentPane(),
047                                                   BoxLayout.Y_AXIS));
048 
049     //name field
050     Box nameBox = Box.createHorizontalBox();
051     nameBox.add(Box.createHorizontalStrut(5));
052     nameBox.add(new JLabel("Name: "));
053     nameBox.add(Box.createHorizontalStrut(5));
054     nameField = new JTextField(30);
055     nameField.setMaximumSize(
056         new Dimension(Integer.MAX_VALUE, nameField.getPreferredSize().height));
057     nameField.setRequestFocusEnabled(true);
058     nameField.setVerifyInputWhenFocusTarget(false);
059     nameBox.add(nameField);
060     nameField.setToolTipText("Enter a name for the resource");
061     
062     nameBox.add(Box.createHorizontalStrut(5));
063     nameBox.add(Box.createHorizontalGlue());
064     this.getContentPane().add(nameBox);
065     this.getContentPane().add(Box.createVerticalStrut(5));
066 
067     //parameters table
068     parametersEditor = new ResourceParametersEditor();
069     tableScroll = new JScrollPane(parametersEditor);
070     this.getContentPane().add(tableScroll);
071     this.getContentPane().add(Box.createVerticalStrut(5));
072     this.getContentPane().add(Box.createVerticalGlue());
073     //buttons box
074     JPanel buttonsBox = new JPanel();
075     buttonsBox.setLayout(new BoxLayout(buttonsBox, BoxLayout.X_AXIS));
076     buttonsBox.add(Box.createHorizontalStrut(10));
077     buttonsBox.add(okBtn = new JButton("OK"));
078     buttonsBox.add(Box.createHorizontalStrut(10));
079     buttonsBox.add(cancelBtn = new JButton("Cancel"));
080     buttonsBox.add(Box.createHorizontalStrut(10));
081     buttonsBox.add(helpBtn = new JButton("Help"));
082     buttonsBox.add(Box.createHorizontalStrut(10));
083     this.getContentPane().add(buttonsBox);
084     this.getContentPane().add(Box.createVerticalStrut(5));
085     setSize(400300);
086 
087     getRootPane().setDefaultButton(okBtn);
088   }// protected void initGuiComponents()
089 
090 
091   protected void initListeners(){
092     Action applyAction = new AbstractAction() {
093       public void actionPerformed(ActionEvent e) {
094         userCanceled = false;
095         TableCellEditor cellEditor = parametersEditor.getCellEditor();
096         if(cellEditor != null){
097           cellEditor.stopCellEditing();
098         }
099         setVisible(false);
100       }
101     };
102     Action helpAction = new AbstractAction() {
103       public void actionPerformed(ActionEvent e) {
104         MainFrame.getInstance().showHelpFrame(resourceData.getHelpURL(),
105           resourceData.getClassName());
106       }
107     };
108     Action cancelAction = new AbstractAction() {
109       public void actionPerformed(ActionEvent e) {
110         userCanceled = true;
111         setVisible(false);
112       }
113     };
114 
115     okBtn.addActionListener(applyAction);
116     helpBtn.addActionListener(helpAction);
117     cancelBtn.addActionListener(cancelAction);
118 
119     // disable Enter key in the table so this key will confirm the dialog
120     InputMap im = parametersEditor.getInputMap(
121       JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
122     KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
123     im.put(enter, "none");
124 
125     // define keystrokes action bindings at the level of the main window
126     InputMap inputMap = ((JComponent)this.getContentPane()).
127       getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
128     ActionMap actionMap =
129       ((JComponent)this.getContentPane()).getActionMap();
130     inputMap.put(KeyStroke.getKeyStroke("ENTER")"Apply");
131     actionMap.put("Apply", applyAction);
132     inputMap.put(KeyStroke.getKeyStroke("F1")"Help");
133     actionMap.put("Help", helpAction);
134     inputMap.put(KeyStroke.getKeyStroke("ESCAPE")"Cancel");
135     actionMap.put("Cancel", cancelAction);
136   }
137 
138   JButton okBtn, helpBtn, cancelBtn;
139   JTextField nameField;
140   ResourceParametersEditor parametersEditor;
141   JScrollPane tableScroll;
142   ResourceData resourceData;
143   Resource resource;
144 
145   boolean userCanceled;
146 
147   /** This method is intended to be used in conjunction with
148     * getSelectedParameters(). The method will not instantiate the resource
149     * like {@link #show(ResourceData)} but it is intended to collect the params
150     * required to instantiate a resource. Returns true if the user pressed OK
151     * and false otherwise.
152     */
153   public synchronized boolean show(ResourceData rData, String aTitle) {
154     this.resourceData = rData;
155     if (aTitle != nullsetTitle(aTitle);
156     nameField.setText("");
157     parametersEditor.init(null,
158                           rData.getParameterList().getInitimeParameters());
159     pack();
160     nameField.requestFocusInWindow();
161     userCanceled = true;
162     setModal(true);
163     setLocationRelativeTo(getOwner());
164     super.setVisible(true);
165     dispose();
166     if(userCanceledreturn false;
167     else return true;
168   }//show();
169 
170   /** Returns the selected params for the resource or null if none was selected
171     * or the user pressed cancel
172     */
173   public FeatureMap getSelectedParameters(){
174     if (parametersEditor != null)
175       return parametersEditor.getParameterValues();
176     else
177       return null;
178   }// getSelectedParameters()
179 
180   public synchronized void show(ResourceData rData) {
181     this.resourceData = rData;
182     nameField.setText("");
183     parametersEditor.init(null,
184                           rData.getParameterList().getInitimeParameters());
185     pack();
186     setLocationRelativeTo(getOwner());
187     //default case when the dialog just gets closed 
188     userCanceled = true;
189     //show the dialog
190     setVisible(true);
191     //release resources
192     dispose();
193     if(userCanceledreturn;
194     else{
195       Runnable runnable = new Runnable(){
196         public void run(){
197           //create the new resource
198           FeatureMap params = parametersEditor.getParameterValues();
199 
200           Resource res;
201           gate.event.StatusListener sListener =
202             (gate.event.StatusListener)Gate.getListeners().
203                                        get("gate.event.StatusListener");
204           if(sListener != nullsListener.statusChanged("Loading " +
205                                                         nameField.getText() +
206                                                         "...");
207 
208           gate.event.ProgressListener pListener =
209             (gate.event.ProgressListener)Gate.getListeners().
210                                          get("gate.event.ProgressListener");
211           if(pListener != null){
212             pListener.progressChanged(0);
213           }
214 
215           try {
216             long startTime = System.currentTimeMillis();
217             FeatureMap features = Factory.newFeatureMap();
218             String name = nameField.getText();
219             if(name == null || name.length() == 0name = null;
220             res = Factory.createResource(resourceData.getClassName(), params,
221                                          features, name);
222             long endTime = System.currentTimeMillis();
223             if(sListener != nullsListener.statusChanged(
224                 nameField.getText() " loaded in " +
225                 NumberFormat.getInstance().format(
226                 (double)(endTime - startTime1000" seconds");
227             if(pListener != nullpListener.processFinished();
228           catch(ResourceInstantiationException rie){
229             JOptionPane.showMessageDialog(getOwner(),
230                                           "Resource could not be created!\n" +
231                                           rie.toString(),
232                                           "GATE", JOptionPane.ERROR_MESSAGE);
233             rie.printStackTrace(Err.getPrintWriter());
234             res = null;
235             if(sListener != nullsListener.statusChanged("Error loading " +
236                                                           nameField.getText() +
237                                                           "!");
238             if(pListener != nullpListener.processFinished();
239           }catch(Throwable thr){
240             JOptionPane.showMessageDialog(getOwner(),
241                     "Unhandled error!\n" +
242                     thr.toString(),
243                     "GATE", JOptionPane.ERROR_MESSAGE);
244             thr.printStackTrace(Err.getPrintWriter());
245             res = null;
246             if(sListener != nullsListener.statusChanged("Error loading " +
247                                                 nameField.getText() +
248                                                 "!");
249             if(pListener != nullpListener.processFinished();
250           }
251         }//public void run()
252       };
253       Thread thread = new Thread(runnable, "");
254       thread.setPriority(Thread.MIN_PRIORITY);
255       thread.start();
256     }
257   }// public synchronized Resource show(ResourceData rData)
258 
259 }//class NewResourceDialog