1   /*
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    *  Valentin Tablan 23/01/2001
10   *
11   *  $Id: NewResourceDialog.java,v 1.56 2005/09/19 14:07:02 valyt Exp $
12   *
13   */
14  
15  package gate.gui;
16  
17  import java.awt.Dimension;
18  import java.awt.Frame;
19  import java.awt.event.ActionEvent;
20  import java.awt.event.ActionListener;
21  import java.text.NumberFormat;
22  
23  import javax.swing.*;
24  import javax.swing.table.TableCellEditor;
25  
26  import gate.*;
27  import gate.creole.ResourceData;
28  import gate.creole.ResourceInstantiationException;
29  import gate.util.Err;
30  
31  public class NewResourceDialog extends JDialog {
32  
33    public NewResourceDialog(Frame frame, String title, boolean modal) {
34      super(frame, title, modal);
35      MainFrame.getGuiRoots().add(this);
36      initLocalData();
37      initGuiComponents();
38      initListeners();
39      setLocationRelativeTo(frame);
40    }// public NewResourceDialog(Frame frame, String title, boolean modal)
41  
42    public void dispose(){
43      MainFrame.getGuiRoots().remove(this);
44      super.dispose();
45    }
46  
47    protected void initLocalData(){
48    }// protected void initLocalData()
49  
50    protected void initGuiComponents(){
51      this.getContentPane().setLayout(new BoxLayout(this.getContentPane(),
52                                                    BoxLayout.Y_AXIS));
53  
54      //name field
55      Box nameBox = Box.createHorizontalBox();
56      nameBox.add(Box.createHorizontalStrut(5));
57      nameBox.add(new JLabel("Name: "));
58      nameBox.add(Box.createHorizontalStrut(5));
59      nameField = new JTextField(30);
60      nameField.setMaximumSize(
61          new Dimension(Integer.MAX_VALUE, nameField.getPreferredSize().height));
62      nameField.setRequestFocusEnabled(true);
63      nameField.setVerifyInputWhenFocusTarget(false);
64      nameBox.add(nameField);
65      nameBox.add(Box.createHorizontalStrut(5));
66      nameBox.add(Box.createHorizontalGlue());
67      this.getContentPane().add(nameBox);
68      this.getContentPane().add(Box.createVerticalStrut(5));
69  
70      //parameters table
71      parametersEditor = new ResourceParametersEditor();
72      tableScroll = new JScrollPane(parametersEditor);
73      this.getContentPane().add(tableScroll);
74      this.getContentPane().add(Box.createVerticalStrut(5));
75      this.getContentPane().add(Box.createVerticalGlue());
76  
77      //buttons box
78      JPanel buttonsBox = new JPanel();
79      buttonsBox.setLayout(new BoxLayout(buttonsBox, BoxLayout.X_AXIS));
80      //buttonsBox.setAlignmentX(Component.CENTER_ALIGNMENT);
81      buttonsBox.add(Box.createHorizontalStrut(10));
82      buttonsBox.add(okBtn = new JButton("OK"));
83      buttonsBox.add(Box.createHorizontalStrut(10));
84      buttonsBox.add(cancelBtn = new JButton("Cancel"));
85      buttonsBox.add(Box.createHorizontalStrut(10));
86      this.getContentPane().add(buttonsBox);
87      this.getContentPane().add(Box.createVerticalStrut(5));
88      setSize(400, 300);
89  
90      getRootPane().setDefaultButton(okBtn);
91      getRootPane().setWindowDecorationStyle(JRootPane.QUESTION_DIALOG);
92    }// protected void initGuiComponents()
93  
94  
95    protected void initListeners(){
96      okBtn.addActionListener(new ActionListener() {
97        public void actionPerformed(ActionEvent e) {
98          userCanceled = false;
99          TableCellEditor cellEditor = parametersEditor.getCellEditor();
100         if(cellEditor != null){
101           cellEditor.stopCellEditing();
102         }
103         setVisible(false);
104       }//public void actionPerformed(ActionEvent e)
105     });
106 
107     cancelBtn.addActionListener(new ActionListener() {
108       public void actionPerformed(ActionEvent e) {
109         userCanceled = true;
110         setVisible(false);
111       }//public void actionPerformed(ActionEvent e)
112     });
113   }//protected void initListeners()
114 
115   JButton okBtn, cancelBtn;
116   JTextField nameField;
117   ResourceParametersEditor parametersEditor;
118   JScrollPane tableScroll;
119   ResourceData resourceData;
120   Resource resource;
121 
122   boolean userCanceled;
123 
124   /** This method is intended to be used in conjunction with
125     * getSelectedParameters(). The method will not instantiate the resource
126     * like {@link #show(ResourceData)} but it is intended to colect the params
127     * required to instantiate a resource. Returns true if the user pressed Ok
128     * and false otherwise.
129     */
130   public synchronized boolean show(ResourceData rData, String aTitle) {
131     this.resourceData = rData;
132     if (aTitle != null) setTitle(aTitle);
133     nameField.setText("");
134     parametersEditor.init(null,
135                           rData.getParameterList().getInitimeParameters());
136 
137     validate();
138     pack();
139     nameField.requestFocusInWindow();
140     userCanceled = true;
141     setModal(true);
142     super.setVisible(true);
143     setLocationRelativeTo(getParent());
144     if(userCanceled) return false;
145     else return true;
146   }//show();
147 
148   /** Returns the selected params for the resource or null if none was selected
149     * or the user pressed cancel
150     */
151   public FeatureMap getSelectedParameters(){
152     if (parametersEditor != null)
153       return parametersEditor.getParameterValues();
154     else
155       return null;
156   }// getSelectedParameters()
157 
158   public synchronized void show(ResourceData rData) {
159     this.resourceData = rData;
160     nameField.setText("");
161     parametersEditor.init(null,
162                           rData.getParameterList().getInitimeParameters());
163 
164     validate();
165     pack();
166 
167     requestFocus();
168     nameField.requestFocus();
169     userCanceled = true;
170 //    setModal(true);
171     super.setVisible(true);
172     setLocationRelativeTo(getParent());
173     if(userCanceled) return;
174     else{
175       Runnable runnable = new Runnable(){
176         public void run(){
177           //create the new resource
178           FeatureMap params = parametersEditor.getParameterValues();
179 
180           Resource res;
181           gate.event.StatusListener sListener =
182             (gate.event.StatusListener)MainFrame.getListeners().
183                                        get("gate.event.StatusListener");
184           if(sListener != null) sListener.statusChanged("Loading " +
185                                                         nameField.getText() +
186                                                         "...");
187 
188           gate.event.ProgressListener pListener =
189             (gate.event.ProgressListener)MainFrame.getListeners().
190                                          get("gate.event.ProgressListener");
191           if(pListener != null){
192             pListener.progressChanged(0);
193           }
194 
195           try {
196             long startTime = System.currentTimeMillis();
197             FeatureMap features = Factory.newFeatureMap();
198             String name = nameField.getText();
199             if(name == null || name.length() == 0) name = null;
200             res = Factory.createResource(resourceData.getClassName(), params,
201                                          features, name);
202             long endTime = System.currentTimeMillis();
203             if(sListener != null) sListener.statusChanged(
204                 nameField.getText() + " loaded in " +
205                 NumberFormat.getInstance().format(
206                 (double)(endTime - startTime) / 1000) + " seconds");
207             if(pListener != null) pListener.processFinished();
208           } catch(ResourceInstantiationException rie){
209             JOptionPane.showMessageDialog(getOwner(),
210                                           "Resource could not be created!\n" +
211                                           rie.toString(),
212                                           "GATE", JOptionPane.ERROR_MESSAGE);
213             rie.printStackTrace(Err.getPrintWriter());
214             res = null;
215             if(sListener != null) sListener.statusChanged("Error loading " +
216                                                           nameField.getText() +
217                                                           "!");
218             if(pListener != null) pListener.processFinished();
219           }catch(Throwable thr){
220             JOptionPane.showMessageDialog(getOwner(),
221                     "Unhandled error!\n" +
222                     thr.toString(),
223                     "GATE", JOptionPane.ERROR_MESSAGE);
224             thr.printStackTrace(Err.getPrintWriter());
225             res = null;
226             if(sListener != null) sListener.statusChanged("Error loading " +
227                                                 nameField.getText() +
228                                                 "!");
229             if(pListener != null) pListener.processFinished();
230           }
231         }//public void run()
232       };
233       Thread thread = new Thread(runnable, "");
234       thread.setPriority(Thread.MIN_PRIORITY);
235       thread.start();
236     }
237   }// public synchronized Resource show(ResourceData rData)
238 
239 }//class NewResourceDialog