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