| CorpusEditor |

    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 12/07/2001
    10   *
    11   *  $Id: CorpusEditor.java,v 1.12 2001/12/03 14:04:04 kalina Exp $
    12   *
    13   */
    14  package gate.gui;
    15
    16  import gate.creole.AbstractVisualResource;
    17  import gate.*;
    18  import gate.util.*;
    19
    20
    21  import java.awt.*;
    22  import java.awt.event.*;
    23  import javax.swing.*;
    24  import javax.swing.event.*;
    25  import java.util.*;
    26  import gate.event.*;
    27
    28  /**
    29   * A simple viewer/editor for corpora. It will allow the visualisation of the
    30   * list of documents inside a corpus along withe their features.
    31   * It will also allow addition and removal of documents.
    32   */
    33  public class CorpusEditor extends AbstractVisualResource implements CorpusListener {
    34
    35    public Resource init(){
    36      initLocalData();
    37      initGuiComponents();
    38      initListeners();
    39      return this;
    40    }
    41
    42
    43    protected void initLocalData(){
    44      docListModel = new DefaultListModel();
    45    }
    46
    47    protected void initGuiComponents(){
    48      setLayout(new BorderLayout());
    49
    50      documentsList = new JList(docListModel);
    51      documentsList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    52      listRenderer = new DocumentListCellRenderer();
    53      documentsList.setCellRenderer(listRenderer);
    54      JScrollPane listScroll = new JScrollPane(documentsList);
    55
    56  //    featuresEditor = new FeaturesEditor();
    57  //    JScrollPane fEdScroll = new JScrollPane(featuresEditor);
    58  //
    59  //    JSplitPane mainSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
    60  //                                          listScroll, fEdScroll);
    61  //    mainSplit.setDividerLocation(0.30);
    62  //    add(mainSplit, BorderLayout.CENTER);
    63
    64      add(listScroll, BorderLayout.CENTER);
    65
    66      toolbar = new JToolBar();
    67      toolbar.setFloatable(false);
    68      toolbar.add(new NewDocumentAction());
    69      toolbar.add(new RemoveDocumentsAction());
    70
    71      add(toolbar, BorderLayout.NORTH);
    72    }
    73
    74    protected void initListeners(){
    75  /*
    76  //kalina: I commented it, because we want the corpus viewer to show only the
    77  //document names and not add the documents to memory
    78      documentsList.addListSelectionListener(new ListSelectionListener(){
    79        public void valueChanged(ListSelectionEvent e){
    80          featuresEditor.setTarget(
    81            (docListModel.isEmpty() || documentsList.getSelectedIndex() == -1) ?
    82            null : docListModel.get(documentsList.getSelectedIndex())
    83          );
    84        }
    85      });
    86  */
    87      documentsList.addMouseListener(new MouseAdapter() {
    88        public void mouseClicked(MouseEvent e) {
    89          if(SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2){
    90            int row = documentsList.locationToIndex(e.getPoint());
    91            if(row != -1){
    92              Document doc = (Document) corpus.get(row);
    93            }
    94          }
    95        }
    96
    97        public void mousePressed(MouseEvent e) {
    98        }
    99
    100       public void mouseReleased(MouseEvent e) {
    101       }
    102
    103       public void mouseEntered(MouseEvent e) {
    104       }
    105
    106       public void mouseExited(MouseEvent e) {
    107       }
    108     });
    109   }
    110
    111   public void setTarget(Object target){
    112     if(!(target instanceof Corpus)){
    113       throw new IllegalArgumentException(
    114         "The GATE corpus editor can only be used with a GATE corpus!\n" +
    115         target.getClass().toString() + " is not a GATE corpus!");
    116     }
    117     this.corpus = (Corpus)target;
    118     corpus.addCorpusListener(this);
    119
    120     docListModel.clear();
    121     java.util.List docNamesList = corpus.getDocumentNames();
    122     Iterator namesIter = docNamesList.iterator();
    123     while(namesIter.hasNext()){
    124       String docName = (String) namesIter.next();
    125       docListModel.addElement(docName);
    126     }
    127
    128     if(!docListModel.isEmpty())
    129       SwingUtilities.invokeLater(new Runnable(){
    130         public void run(){
    131           documentsList.setSelectedIndex(0);
    132         }
    133       });
    134   }
    135
    136   public void documentAdded(final CorpusEvent e) {
    137     SwingUtilities.invokeLater(new Runnable(){
    138       public void run(){
    139         //a new document has been added to the corpus
    140         Document doc = e.getDocument();
    141         docListModel.addElement(doc.getName());
    142       }
    143     });
    144   }
    145
    146   public void documentRemoved(final CorpusEvent e) {
    147     SwingUtilities.invokeLater(new Runnable(){
    148       public void run(){
    149         docListModel.removeElementAt(e.getDocumentIndex());
    150       }
    151     });
    152   }
    153
    154
    155   class DocumentListCellRenderer extends DefaultListCellRenderer{
    156     public Component getListCellRendererComponent(JList list,
    157                                               Object value,
    158                                               int index,
    159                                               boolean isSelected,
    160                                               boolean cellHasFocus){
    161       //prepare the renderer
    162       String docName = (String)value;
    163       super.getListCellRendererComponent(list, docName, index,
    164                                          isSelected, cellHasFocus);
    165       setIcon(MainFrame.getIcon("lr.gif"));
    166       return this;
    167     }
    168   }
    169
    170
    171   class NewDocumentAction extends AbstractAction{
    172     public NewDocumentAction(){
    173       super("Add document", MainFrame.getIcon("add.gif"));
    174       putValue(SHORT_DESCRIPTION, "Add a new document to this corpus");
    175     }
    176
    177     public void actionPerformed(ActionEvent e){
    178       try{
    179         //get all the documents loaded in the system
    180         java.util.List loadedDocuments = Gate.getCreoleRegister().
    181                                getAllInstances("gate.Document");
    182         if(loadedDocuments == null || loadedDocuments.isEmpty()){
    183           JOptionPane.showMessageDialog(
    184               CorpusEditor.this,
    185               "There are no documents available in the system!\n" +
    186               "Please load some and try again!" ,
    187               "Gate", JOptionPane.ERROR_MESSAGE);
    188           return;
    189         }
    190
    191         Vector docNames = new Vector(loadedDocuments.size());
    192         for (int i = 0; i< loadedDocuments.size(); i++) {
    193           docNames.add(((Document)loadedDocuments.get(i)).getName());
    194         }
    195         JList docList = new JList(docNames);
    196         docList.setCellRenderer(listRenderer);
    197
    198         JOptionPane dialog = new JOptionPane(new JScrollPane(docList),
    199                                              JOptionPane.QUESTION_MESSAGE,
    200                                              JOptionPane.OK_CANCEL_OPTION);
    201         dialog.createDialog(CorpusEditor.this,
    202                             "Add document(s) to corpus").show();
    203
    204         if(((Integer)dialog.getValue()).intValue() == dialog.OK_OPTION){
    205           int[] selection = docList.getSelectedIndices();
    206           for (int i = 0; i< selection.length ; i++) {
    207             corpus.add(loadedDocuments.get(selection[i]));
    208           }
    209         }
    210       }catch(GateException ge){
    211         //gate.Document is not registered in creole.xml....what is!?
    212         throw new GateRuntimeException(
    213           "gate.Document is not registered in the creole register!\n" +
    214           "Something must be terribly wrong...take a vacation!");
    215       }
    216     }
    217   }//class NewDocumentAction extends AbstractAction
    218
    219   class RemoveDocumentsAction extends AbstractAction{
    220     public RemoveDocumentsAction(){
    221       super("Rmove documents", MainFrame.getIcon("remove.gif"));
    222       putValue(SHORT_DESCRIPTION, "Removes selected documents from this corpus");
    223     }
    224
    225     public void actionPerformed(ActionEvent e){
    226       int[] selectedIndexes = documentsList.getSelectedIndices();
    227       for(int i = selectedIndexes.length-1; i >= 0; i--){
    228         corpus.remove(selectedIndexes[i]);
    229       }
    230       documentsList.clearSelection();
    231     }
    232   }//class RemoveDocumentsAction extends AbstractAction
    233
    234
    235   JList documentsList;
    236   DocumentListCellRenderer listRenderer;
    237   FeaturesEditor featuresEditor;
    238   JToolBar toolbar;
    239   Corpus corpus;
    240   DefaultListModel docListModel;
    241 }
    242

| CorpusEditor |