|
NewResourceDialog |
|
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.46 2002/03/08 18:00:30 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 getRootPane().setDefaultButton(okBtn); 96 }// protected void initGuiComponents() 97 98 99 protected void initListeners(){ 100 okBtn.addActionListener(new ActionListener() { 101 public void actionPerformed(ActionEvent e) { 102 userCanceled = false; 103 TableCellEditor cellEditor = parametersEditor.getCellEditor(); 104 if(cellEditor != null){ 105 cellEditor.stopCellEditing(); 106 } 107 hide(); 108 }//public void actionPerformed(ActionEvent e) 109 }); 110 111 cancelBtn.addActionListener(new ActionListener() { 112 public void actionPerformed(ActionEvent e) { 113 userCanceled = true; 114 hide(); 115 }//public void actionPerformed(ActionEvent e) 116 }); 117 }//protected void initListeners() 118 119 JButton okBtn, cancelBtn; 120 JTextField nameField; 121 ResourceParametersEditor parametersEditor; 122 JScrollPane tableScroll; 123 ResourceData resourceData; 124 Resource resource; 125 126 boolean userCanceled; 127 128 /** This method is intended to be used in conjunction with 129 * getSelectedParameters(). The method will not instantiate the resource 130 * like {@link show(ResourceData)} but it is intended to colect the params 131 * required to instantiate a resource. Returns true if the user pressed Ok 132 * and false otherwise. 133 */ 134 public synchronized boolean show(ResourceData rData, String aTitle) { 135 this.resourceData = rData; 136 if (aTitle != null) setTitle(aTitle); 137 setLocationRelativeTo(getParent()); 138 nameField.setText(""); 139 parametersEditor.init(null, 140 rData.getParameterList().getInitimeParameters()); 141 142 validate(); 143 pack(); 144 requestFocus(); 145 nameField.requestFocus(); 146 userCanceled = true; 147 setModal(true); 148 super.show(); 149 if(userCanceled) return false; 150 else return true; 151 }//show(); 152 153 /** Returns the selected params for the resource or null if none was selected 154 * or the user pressed cancel 155 */ 156 public FeatureMap getSelectedParameters(){ 157 if (parametersEditor != null) 158 return parametersEditor.getParameterValues(); 159 else 160 return null; 161 }// getSelectedParameters() 162 163 public synchronized void show(ResourceData rData) { 164 this.resourceData = rData; 165 setLocationRelativeTo(getParent()); 166 nameField.setText(""); 167 parametersEditor.init(null, 168 rData.getParameterList().getInitimeParameters()); 169 170 validate(); 171 pack(); 172 173 requestFocus(); 174 nameField.requestFocus(); 175 userCanceled = true; 176 // setModal(true); 177 super.show(); 178 if(userCanceled) return; 179 else{ 180 Runnable runnable = new Runnable(){ 181 public void run(){ 182 //create the new resource 183 FeatureMap params = parametersEditor.getParameterValues(); 184 185 Resource res; 186 gate.event.StatusListener sListener = 187 (gate.event.StatusListener)MainFrame.getListeners(). 188 get("gate.event.StatusListener"); 189 if(sListener != null) sListener.statusChanged("Loading " + 190 nameField.getText() + 191 "..."); 192 193 gate.event.ProgressListener pListener = 194 (gate.event.ProgressListener)MainFrame.getListeners(). 195 get("gate.event.ProgressListener"); 196 if(pListener != null){ 197 pListener.progressChanged(0); 198 } 199 200 try { 201 long startTime = System.currentTimeMillis(); 202 FeatureMap features = Factory.newFeatureMap(); 203 String name = nameField.getText(); 204 if(name == null || name.length() == 0) name = null; 205 res = Factory.createResource(resourceData.getClassName(), params, 206 features, name); 207 long endTime = System.currentTimeMillis(); 208 if(sListener != null) sListener.statusChanged( 209 nameField.getText() + " loaded in " + 210 NumberFormat.getInstance().format( 211 (double)(endTime - startTime) / 1000) + " seconds"); 212 if(pListener != null) pListener.processFinished(); 213 } catch(ResourceInstantiationException rie){ 214 JOptionPane.showMessageDialog(getOwner(), 215 "Resource could not be created!\n" + 216 rie.toString(), 217 "Gate", JOptionPane.ERROR_MESSAGE); 218 rie.printStackTrace(Err.getPrintWriter()); 219 res = null; 220 if(sListener != null) sListener.statusChanged("Error loading " + 221 nameField.getText() + 222 "!"); 223 if(pListener != null) pListener.processFinished(); 224 } 225 }//public void run() 226 }; 227 Thread thread = new Thread(runnable, ""); 228 thread.setPriority(Thread.MIN_PRIORITY); 229 thread.start(); 230 } 231 }// public synchronized Resource show(ResourceData rData) 232 233 }//class NewResourceDialog
|
NewResourceDialog |
|