|
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.15 2002/03/06 17:15:46 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 cleanup(){ 112 super.cleanup(); 113 corpus = null; 114 115 } 116 117 public void setTarget(Object target){ 118 if(!(target instanceof Corpus)){ 119 throw new IllegalArgumentException( 120 "The GATE corpus editor can only be used with a GATE corpus!\n" + 121 target.getClass().toString() + " is not a GATE corpus!"); 122 } 123 this.corpus = (Corpus)target; 124 corpus.addCorpusListener(this); 125 126 docListModel.clear(); 127 java.util.List docNamesList = corpus.getDocumentNames(); 128 Iterator namesIter = docNamesList.iterator(); 129 while(namesIter.hasNext()){ 130 String docName = (String) namesIter.next(); 131 docListModel.addElement(docName); 132 } 133 134 if(!docListModel.isEmpty()) 135 SwingUtilities.invokeLater(new Runnable(){ 136 public void run(){ 137 documentsList.setSelectedIndex(0); 138 } 139 }); 140 } 141 142 public void documentAdded(final CorpusEvent e) { 143 SwingUtilities.invokeLater(new Runnable(){ 144 public void run(){ 145 //a new document has been added to the corpus 146 Document doc = e.getDocument(); 147 docListModel.addElement(doc.getName()); 148 } 149 }); 150 } 151 152 public void documentRemoved(final CorpusEvent e) { 153 SwingUtilities.invokeLater(new Runnable(){ 154 public void run(){ 155 docListModel.removeElementAt(e.getDocumentIndex()); 156 } 157 }); 158 } 159 160 161 class DocumentListCellRenderer extends DefaultListCellRenderer{ 162 public Component getListCellRendererComponent(JList list, 163 Object value, 164 int index, 165 boolean isSelected, 166 boolean cellHasFocus){ 167 //prepare the renderer 168 String docName = (String)value; 169 super.getListCellRendererComponent(list, docName, index, 170 isSelected, cellHasFocus); 171 setIcon(MainFrame.getIcon("lr.gif")); 172 return this; 173 } 174 } 175 176 177 class NewDocumentAction extends AbstractAction{ 178 public NewDocumentAction(){ 179 super("Add document", MainFrame.getIcon("add.gif")); 180 putValue(SHORT_DESCRIPTION, "Add a new document to this corpus"); 181 } 182 183 public void actionPerformed(ActionEvent e){ 184 try{ 185 //get all the documents loaded in the system 186 java.util.List loadedDocuments = Gate.getCreoleRegister(). 187 getAllInstances("gate.Document"); 188 if(loadedDocuments == null || loadedDocuments.isEmpty()){ 189 JOptionPane.showMessageDialog( 190 CorpusEditor.this, 191 "There are no documents available in the system!\n" + 192 "Please load some and try again!" , 193 "Gate", JOptionPane.ERROR_MESSAGE); 194 return; 195 } 196 197 Vector docNames = new Vector(loadedDocuments.size()); 198 for (int i = 0; i< loadedDocuments.size(); i++) { 199 docNames.add(((Document)loadedDocuments.get(i)).getName()); 200 } 201 JList docList = new JList(docNames); 202 docList.setCellRenderer(listRenderer); 203 204 JOptionPane dialog = new JOptionPane(new JScrollPane(docList), 205 JOptionPane.QUESTION_MESSAGE, 206 JOptionPane.OK_CANCEL_OPTION); 207 dialog.createDialog(CorpusEditor.this, 208 "Add document(s) to corpus").show(); 209 210 if(((Integer)dialog.getValue()).intValue() == dialog.OK_OPTION){ 211 int[] selection = docList.getSelectedIndices(); 212 for (int i = 0; i< selection.length ; i++) { 213 corpus.add(loadedDocuments.get(selection[i])); 214 } 215 } 216 }catch(GateException ge){ 217 //gate.Document is not registered in creole.xml....what is!? 218 throw new GateRuntimeException( 219 "gate.Document is not registered in the creole register!\n" + 220 "Something must be terribly wrong...take a vacation!"); 221 } 222 } 223 }//class NewDocumentAction extends AbstractAction 224 225 class RemoveDocumentsAction extends AbstractAction{ 226 public RemoveDocumentsAction(){ 227 super("Remove documents", MainFrame.getIcon("remove.gif")); 228 putValue(SHORT_DESCRIPTION, "Removes selected documents from this corpus"); 229 } 230 231 public void actionPerformed(ActionEvent e){ 232 int[] selectedIndexes = documentsList.getSelectedIndices(); 233 for(int i = selectedIndexes.length-1; i >= 0; i--){ 234 corpus.remove(selectedIndexes[i]); 235 } 236 documentsList.clearSelection(); 237 } 238 }//class RemoveDocumentsAction extends AbstractAction 239 240 241 JList documentsList; 242 DocumentListCellRenderer listRenderer; 243 FeaturesEditor featuresEditor; 244 JToolBar toolbar; 245 Corpus corpus; 246 DefaultListModel docListModel; 247 } 248
|
CorpusEditor |
|