| 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.43 2001/11/16 15:15:28 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      nameField.setNextFocusableComponent(parametersEditor);
    96      parametersEditor.setNextFocusableComponent(okBtn);
    97      okBtn.setNextFocusableComponent(cancelBtn);
    98      cancelBtn.setNextFocusableComponent(nameField);
    99      getRootPane().setDefaultButton(okBtn);
    100   }// protected void initGuiComponents()
    101
    102
    103   protected void initListeners(){
    104     okBtn.addActionListener(new ActionListener() {
    105       public void actionPerformed(ActionEvent e) {
    106         userCanceled = false;
    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 the
    131     * other show() method but it is intended to colect the params required to
    132     * instantiate a resource. Returns true if the user pressed Ok and false
    133     * if the used pressed Cancel;
    134     */
    135   public synchronized boolean show(ResourceData rData, String aTitle) {
    136     this.resourceData = rData;
    137     if (aTitle != null) setTitle(aTitle);
    138     setLocationRelativeTo(getParent());
    139     nameField.setText("");
    140     parametersEditor.init(null,
    141                           rData.getParameterList().getInitimeParameters());
    142
    143     validate();
    144     pack();
    145     requestFocus();
    146     nameField.requestFocus();
    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

| NewResourceDialog |