|
ChooseSynsetPanel |
|
1 /* 2 * ChooseSynsetPanel.java 3 * 4 * Copyright (c) 1998-2001, The University of Sheffield. 5 * 6 * This file is part of GATE (see http://gate.ac.uk/), and is free 7 * software, licenced under the GNU Library General Public License, 8 * Version 2, June 1991 (in the distribution as file licence.html, 9 * and also available at http://gate.ac.uk/gate/licence.html). 10 * 11 * Kalina Bontcheva, 20/02/2003 12 * 13 * $Id: ChooseSynsetPanel.java,v 1.5 2003/03/13 20:48:37 kalina Exp $ 14 */ 15 16 package gate.gui.lexicon; 17 18 import javax.swing.*; 19 import java.awt.*; 20 import gate.lexicon.*; 21 import java.awt.event.*; 22 import gate.util.GateRuntimeException; 23 import gate.gui.OkCancelDialog; 24 import java.util.Vector; 25 import javax.swing.event.*; 26 27 public class ChooseSynsetPanel extends JPanel { 28 29 public ChooseSynsetPanel(LexicalKnowledgeBase theLex, boolean isEdit) { 30 if (theLex == null) 31 throw new GateRuntimeException("To view/edit synsets please provide a valid lexicon"); 32 isEditable = isEdit; 33 lexKB = theLex; 34 initLocalData(); 35 initGuiComponents(); 36 initListeners(); 37 } 38 39 public ChooseSynsetPanel(LexicalKnowledgeBase theLex) { 40 if (theLex == null) 41 throw new GateRuntimeException("To view/edit synsets please provide a valid lexicon"); 42 isEditable = true; 43 lexKB = theLex; 44 initLocalData(); 45 initGuiComponents(); 46 initListeners(); 47 } 48 49 public LexKBSynset getSelectedSynset() { 50 return (LexKBSynset) synsetList.getSelectedValue(); 51 } 52 53 public void addSynsetSelectionListener(ListSelectionListener l) { 54 if (l != null) 55 synsetList.addListSelectionListener(l); 56 } 57 58 public void removeSynsetSelectionListener(ListSelectionListener l) { 59 if (l != null) 60 synsetList.removeListSelectionListener(l); 61 } 62 63 protected void initLocalData(){ 64 this.addSynsetAction = new AddSynsetAction(); 65 this.removeSynsetAction = new RemoveSynsetAction(); 66 } 67 68 protected void initGuiComponents(){ 69 SynsetTextLabel.setText("Synset Entries"); 70 synsetListModel = new DefaultListModel(); 71 synsetList = new JList(synsetListModel); 72 synsetList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 73 74 this.posComboBox = new JComboBox(lexKB.POS_TYPES); 75 posComboBox.setMaximumSize(new Dimension(Integer.MAX_VALUE, 76 posComboBox.getPreferredSize().height)); 77 mainBox = Box.createHorizontalBox(); 78 leftBox = Box.createVerticalBox(); 79 rightBox = Box.createVerticalBox(); 80 81 definitionTextArea.setText(""); 82 definitionTextArea.setWrapStyleWord(true); 83 definitionTextArea.setEditable(false); 84 definitionTextLabel.setText("Definition"); 85 86 this.setLayout(gridLayout1); 87 this.setAlignmentX((float) 0.5); 88 this.setDebugGraphicsOptions(0); 89 90 POSTextLabel.setText("Part of Speech"); 91 92 if (isEditable) { 93 addSynsetButton = new JButton(addSynsetAction); 94 addSynsetButton.setText("Add"); 95 96 removeSynsetButton = new JButton(removeSynsetAction); 97 removeSynsetButton.setText("Remove"); 98 } 99 100 101 this.add(mainBox, null); 102 mainBox.add(leftBox, null); 103 104 mainBox.add(rightBox, null); 105 106 leftBox.add(POSTextLabel, null); 107 leftBox.add(posComboBox, null); 108 109 leftBox.add(SynsetTextLabel, null); 110 leftBox.add(synsetScrollPane, null); 111 synsetScrollPane.getViewport().add(synsetList, null); 112 113 rightBox.add(definitionTextLabel, null); 114 JScrollPane definitionScroller = new JScrollPane(); 115 definitionScroller.getViewport().add(definitionTextArea); 116 definitionScroller.setPreferredSize(new Dimension(300, 150)); 117 definitionScroller.setMinimumSize(new Dimension(300, 150)); 118 rightBox.add(definitionScroller, null); 119 120 if (isEditable) { 121 Box buttonBox = Box.createHorizontalBox(); 122 buttonBox.add(addSynsetButton, null); 123 buttonBox.add(Box.createHorizontalStrut(20)); 124 buttonBox.add(removeSynsetButton, null); 125 rightBox.add(buttonBox); 126 } 127 128 mainBox.add(Box.createVerticalGlue()); 129 130 updateGUI(null); 131 } 132 133 protected void initListeners(){ 134 posComboBox.addItemListener(new ItemListener(){ 135 public void itemStateChanged(ItemEvent event){ 136 int state = event.getStateChange(); 137 Object item = event.getItem(); 138 if (state == ItemEvent.SELECTED) { 139 updateGUI(null); 140 }//new POS selected 141 } 142 }); 143 144 synsetList.addListSelectionListener(new ListSelectionListener(){ 145 public void valueChanged(ListSelectionEvent e) { 146 LexKBSynset selectedSynset = (LexKBSynset)synsetList.getSelectedValue(); 147 if (selectedSynset != null) 148 definitionTextArea.setText(selectedSynset.getDefinition()); 149 } 150 }); 151 } 152 153 protected void updateGUI(LexKBSynset theSynset) { 154 synsetListModel.clear(); 155 definitionTextArea.setText(""); 156 157 if (posComboBox.getSelectedItem() != null) { 158 java.util.Iterator iter = lexKB.getSynsets(posComboBox.getSelectedItem()); 159 int selectedIndex = 0; 160 while (iter.hasNext()) { 161 LexKBSynset nextSynset = (LexKBSynset) iter.next(); 162 synsetListModel.addElement(nextSynset); 163 if (nextSynset.equals(theSynset)) 164 selectedIndex = synsetListModel.size() - 1; 165 }//while 166 if (synsetList.getModel().getSize() == 0) 167 return; 168 synsetList.setSelectedIndex(selectedIndex); 169 LexKBSynset selectedSynset = (LexKBSynset) synsetList.getSelectedValue(); 170 if (selectedSynset != null) 171 definitionTextArea.setText(selectedSynset.getDefinition()); 172 } 173 } 174 175 protected void showInputDialog(MutableLexKBSynset theSynset) { 176 JPanel inputPanel = new JPanel(); 177 inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS)); 178 JLabel defLabel = new JLabel("Definition"); 179 JTextArea newDefField = new JTextArea(); 180 newDefField.setWrapStyleWord(true); 181 JScrollPane newDefScroller = new JScrollPane(newDefField); 182 newDefScroller.setPreferredSize(new Dimension(400, 150)); 183 newDefScroller.setMinimumSize(new Dimension(300, 150)); 184 185 JLabel defPOS = new JLabel("Part of speech"); 186 final JComboBox newPOSCombo = new JComboBox(lexKB.getPOSTypes()); 187 newPOSCombo.setEditable(true); 188 newPOSCombo.getEditor().addActionListener(new ActionListener() { 189 public void actionPerformed(ActionEvent e) { 190 Object editedItem = newPOSCombo.getEditor().getItem(); 191 if (! (lexKB instanceof MutableLexicalKnowledgeBase)) 192 return; 193 newPOSCombo.addItem(editedItem); 194 newPOSCombo.setSelectedItem(editedItem); 195 ((MutableLexicalKnowledgeBase) lexKB).addPOSType(editedItem); 196 } 197 }); 198 inputPanel.add(defLabel); 199 inputPanel.add(newDefScroller); 200 inputPanel.add(defPOS); 201 inputPanel.add(newPOSCombo); 202 203 boolean okPressed = 204 OkCancelDialog.showDialog(this, inputPanel, 205 "Please provide definition and POS of the new synset"); 206 207 if (! okPressed) 208 return; 209 210 theSynset.setDefinition(newDefField.getText()); 211 theSynset.setPOS(newPOSCombo.getSelectedItem()); 212 if (newPOSCombo.getModel().getSize() != posComboBox.getModel().getSize()) { 213 posComboBox.removeAllItems(); 214 Object[] posTypes = lexKB.getPOSTypes(); 215 for (int i=0; i< posTypes.length; i++) 216 posComboBox.addItem(posTypes[i]); 217 } 218 219 } 220 221 222 /** 223 * Adds a synset 224 */ 225 protected class AddSynsetAction extends AbstractAction{ 226 AddSynsetAction(){ 227 super("AddSynset"); 228 putValue(SHORT_DESCRIPTION, "Add a synset to the lexicon"); 229 } 230 public void actionPerformed(ActionEvent e){ 231 if (lexKB == null || 232 !(lexKB instanceof MutableLexicalKnowledgeBase)) 233 return; 234 MutableLexicalKnowledgeBase theKB = (MutableLexicalKnowledgeBase) lexKB; 235 MutableLexKBSynset newSynset = theKB.addSynset(); 236 237 showInputDialog(newSynset); 238 if (newSynset.getPOS() != null) { 239 posComboBox.setSelectedItem(newSynset.getPOS()); 240 updateGUI(newSynset); 241 } 242 }//actionPerformed 243 } 244 245 /** 246 * Removes a synset 247 */ 248 protected class RemoveSynsetAction extends AbstractAction{ 249 RemoveSynsetAction(){ 250 super("RemoveSynset"); 251 putValue(SHORT_DESCRIPTION, "Removes a synset from the lexicon"); 252 } 253 public void actionPerformed(ActionEvent e){ 254 int result = JOptionPane.showConfirmDialog(ChooseSynsetPanel.this, 255 "Deleting the synset will also delete all word senses it contains. Are you sure?", 256 "Warning", 257 JOptionPane.YES_NO_OPTION); 258 if (result == JOptionPane.NO_OPTION) 259 return; 260 if (lexKB == null || 261 !(lexKB instanceof MutableLexicalKnowledgeBase)) 262 return; 263 MutableLexicalKnowledgeBase theKB = (MutableLexicalKnowledgeBase) lexKB; 264 MutableLexKBSynset synset = (MutableLexKBSynset) synsetList.getSelectedValue(); 265 theKB.removeSynset(synset); 266 267 updateGUI(null); 268 }//actionPerformed 269 } 270 271 protected LexicalKnowledgeBase lexKB; 272 273 protected GridLayout gridLayout1 = new GridLayout(); 274 protected Box mainBox; 275 protected Box leftBox; 276 protected Box rightBox; 277 278 protected JLabel definitionTextLabel = new JLabel(); 279 protected JTextArea definitionTextArea = new JTextArea(); 280 281 protected JLabel POSTextLabel = new JLabel(); 282 protected JComboBox posComboBox; 283 284 protected JLabel SynsetTextLabel = new JLabel(); 285 protected JScrollPane synsetScrollPane = new JScrollPane(); 286 protected JList synsetList; 287 protected DefaultListModel synsetListModel; 288 protected JButton addSynsetButton; 289 /** 290 * An action that adds a new synset to the lexicon 291 */ 292 protected Action addSynsetAction; 293 294 protected JButton removeSynsetButton; 295 /** 296 * An action that removes a synset from the lexicon 297 */ 298 protected Action removeSynsetAction; 299 300 protected boolean isEditable = true; 301 302 public static void main(String[] args) { 303 304 JFrame frame = new JFrame(); 305 306 frame.setSize(250, 200); 307 308 frame.setLocation(200, 300); 309 frame.getContentPane().add(new ChooseSynsetPanel(new NLGLexiconImpl(), false)); 310 frame.pack(); 311 312 frame.setVisible(true); 313 314 }//main 315 316 }
|
ChooseSynsetPanel |
|