001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Niraj Aswani, 2007
011 *
012 * $Id$
013 *
014 */
015
016 package gate.gui.ontology;
017
018 import gate.gui.MainFrame;
019
020 import java.awt.FlowLayout;
021 import java.awt.event.ActionEvent;
022 import java.awt.event.ActionListener;
023 import java.awt.event.KeyAdapter;
024 import java.awt.event.KeyEvent;
025 import java.util.ArrayList;
026 import java.util.Arrays;
027 import java.util.Collections;
028 import javax.swing.*;
029 import javax.swing.text.JTextComponent;
030
031 public class ValuesSelectionAction {
032 public ValuesSelectionAction() {
033 list = null;
034 domainBox = new JComboBox();
035 domainBox.setEditable(true);
036 list = new JList(new DefaultComboBoxModel());
037 list.setVisibleRowCount(7);
038 add = new JButton("Add");
039 remove = new JButton("Remove");
040 panel = new JPanel();
041 BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
042 panel.setLayout(boxlayout);
043 panel.add(domainBox);
044 domainBox.setEditable(true);
045 domainBox.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
046 public void keyReleased(KeyEvent keyevent) {
047 String s = ((JTextComponent)domainBox.getEditor().getEditorComponent())
048 .getText();
049 if(s != null) {
050 if(keyevent.getKeyCode() != KeyEvent.VK_ENTER) {
051 ArrayList<String> arraylist = new ArrayList<String>();
052 for(int i = 0; i < ontologyClasses.length; i++) {
053 String s1 = ontologyClasses[i];
054 if(s1.toLowerCase().startsWith(s.toLowerCase())) {
055 arraylist.add(s1);
056 }
057 }
058 Collections.sort(arraylist);
059 DefaultComboBoxModel model =
060 new DefaultComboBoxModel(arraylist.toArray());
061 domainBox.setModel(model);
062 if(!arraylist.isEmpty()) domainBox.showPopup();
063 }
064 ((JTextComponent)domainBox.getEditor().getEditorComponent())
065 .setText(s);
066 }
067 }
068 });
069 JPanel jpanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
070 jpanel.add(add);
071 jpanel.add(remove);
072 panel.add(jpanel);
073 panel.add(new JScrollPane(list));
074 add.addActionListener(new ActionListener() {
075 public void actionPerformed(ActionEvent actionevent) {
076 String s = (String)domainBox.getSelectedItem();
077 if(!allowValueOutsideDropDownList) {
078 if(!Arrays.asList(ontologyClasses).contains(s)) {
079 JOptionPane.showMessageDialog(MainFrame.getInstance(),
080 "The value \"" + s + "\" is not in the drop down list!");
081 return;
082 }
083 }
084 if(((DefaultComboBoxModel)list.getModel()).getIndexOf(s) != -1) {
085 JOptionPane.showMessageDialog(MainFrame.getInstance(),
086 "Already added!");
087 }
088 else {
089 ((DefaultComboBoxModel)list.getModel()).addElement(s);
090 }
091 }
092 });
093 remove.addActionListener(new ActionListener() {
094 public void actionPerformed(ActionEvent actionevent) {
095 Object aobj[] = list.getSelectedValues();
096 if(aobj != null && aobj.length > 0) {
097 for(int i = 0; i < aobj.length; i++)
098 ((DefaultComboBoxModel)list.getModel()).removeElement(aobj[i]);
099 }
100 }
101 });
102 }
103
104 /**
105 * Dialogue that list possible choices to choose from.
106 * @param windowTitle title of the window
107 * @param inDropDownList list of choices
108 * @param alreadySelected initial selection
109 * @param allowValueOutsideDropDownList true if allowed
110 * @param icon message dialogue icon
111 * @return {@link JOptionPane#CLOSED_OPTION},
112 * {@link JOptionPane#UNINITIALIZED_VALUE}, {@link JOptionPane#OK_OPTION},
113 * {@link JOptionPane#CANCEL_OPTION}.
114 */
115 public int showGUI(String windowTitle, String[] inDropDownList,
116 String[] alreadySelected, boolean allowValueOutsideDropDownList,
117 Icon icon) {
118 this.ontologyClasses = inDropDownList;
119 this.allowValueOutsideDropDownList = allowValueOutsideDropDownList;
120 domainBox.setModel(new DefaultComboBoxModel(inDropDownList));
121 list.setModel(new DefaultComboBoxModel(alreadySelected));
122 JOptionPane pane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE,
123 JOptionPane.OK_CANCEL_OPTION, icon) {
124 public void selectInitialValue() {
125 domainBox.requestFocusInWindow();
126 domainBox.getEditor().selectAll();
127 }
128 };
129 pane.createDialog(MainFrame.getInstance(), windowTitle).setVisible(true);
130 return pane.getValue() == null ?
131 JOptionPane.CLOSED_OPTION : (Integer) pane.getValue();
132 }
133
134 public String[] getSelectedValues() {
135 DefaultComboBoxModel model = (DefaultComboBoxModel) list.getModel();
136 String as[] = new String[model.getSize()];
137 for(int i = 0; i < as.length; i++)
138 as[i] = (String) model.getElementAt(i);
139 return as;
140 }
141
142 protected JComboBox domainBox;
143 protected JList list;
144 protected JButton add;
145 protected JButton remove;
146 protected JPanel panel;
147 protected String[] ontologyClasses;
148 protected boolean allowValueOutsideDropDownList = true;
149 }
|