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