| 1 | /* |
|---|
| 2 | * This program is free software; you can redistribute it and/or modify |
|---|
| 3 | * it under the terms of the GNU General Public License as published by |
|---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 5 | * (at your option) any later version. |
|---|
| 6 | * |
|---|
| 7 | * This program is distributed in the hope that it will be useful, |
|---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 10 | * GNU General Public License for more details. |
|---|
| 11 | * |
|---|
| 12 | * You should have received a copy of the GNU General Public License |
|---|
| 13 | * along with this program; if not, write to the Free Software |
|---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /* |
|---|
| 18 | * SelectedTagEditor.java |
|---|
| 19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | package weka.gui; |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | import weka.core.Tag; |
|---|
| 28 | import weka.core.SelectedTag; |
|---|
| 29 | |
|---|
| 30 | import java.awt.BorderLayout; |
|---|
| 31 | import java.awt.event.WindowAdapter; |
|---|
| 32 | import java.awt.event.WindowEvent; |
|---|
| 33 | import java.beans.PropertyEditor; |
|---|
| 34 | import java.beans.PropertyEditorSupport; |
|---|
| 35 | import javax.swing.JFrame; |
|---|
| 36 | import javax.swing.JLabel; |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * A PropertyEditor that uses tags, where the tags are obtained from a |
|---|
| 40 | * weka.core.SelectedTag object. |
|---|
| 41 | * |
|---|
| 42 | * @author Len Trigg (trigg@cs.waikato.ac.nz) |
|---|
| 43 | * @version $Revision: 1.8 $ |
|---|
| 44 | */ |
|---|
| 45 | public class SelectedTagEditor extends PropertyEditorSupport { |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Returns a description of the property value as java source. |
|---|
| 49 | * |
|---|
| 50 | * @return a value of type 'String' |
|---|
| 51 | */ |
|---|
| 52 | public String getJavaInitializationString() { |
|---|
| 53 | |
|---|
| 54 | SelectedTag s = (SelectedTag)getValue(); |
|---|
| 55 | Tag [] tags = s.getTags(); |
|---|
| 56 | String result = "new SelectedTag(" |
|---|
| 57 | + s.getSelectedTag().getID() |
|---|
| 58 | + ", {\n"; |
|---|
| 59 | for (int i = 0; i < tags.length; i++) { |
|---|
| 60 | result += "new Tag(" + tags[i].getID() |
|---|
| 61 | + ",\"" + tags[i].getReadable() |
|---|
| 62 | + "\")"; |
|---|
| 63 | if (i < tags.length - 1) { |
|---|
| 64 | result += ','; |
|---|
| 65 | } |
|---|
| 66 | result += '\n'; |
|---|
| 67 | } |
|---|
| 68 | return result + "})"; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * Gets the current value as text. |
|---|
| 73 | * |
|---|
| 74 | * @return a value of type 'String' |
|---|
| 75 | */ |
|---|
| 76 | public String getAsText() { |
|---|
| 77 | |
|---|
| 78 | SelectedTag s = (SelectedTag)getValue(); |
|---|
| 79 | return s.getSelectedTag().getReadable(); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Sets the current property value as text. |
|---|
| 84 | * |
|---|
| 85 | * @param text the text of the selected tag. |
|---|
| 86 | * @exception java.lang.IllegalArgumentException if an error occurs |
|---|
| 87 | */ |
|---|
| 88 | public void setAsText(String text) |
|---|
| 89 | { |
|---|
| 90 | |
|---|
| 91 | SelectedTag s = (SelectedTag)getValue(); |
|---|
| 92 | Tag [] tags = s.getTags(); |
|---|
| 93 | try { |
|---|
| 94 | for (int i = 0; i < tags.length; i++) { |
|---|
| 95 | if (text.equals(tags[i].getReadable())) { |
|---|
| 96 | setValue(new SelectedTag(tags[i].getID(), tags)); |
|---|
| 97 | return; |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | } catch (Exception ex) { |
|---|
| 101 | throw new java.lang.IllegalArgumentException(text); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * Gets the list of tags that can be selected from. |
|---|
| 107 | * |
|---|
| 108 | * @return an array of string tags. |
|---|
| 109 | */ |
|---|
| 110 | public String[] getTags() { |
|---|
| 111 | |
|---|
| 112 | SelectedTag s = (SelectedTag)getValue(); |
|---|
| 113 | Tag [] tags = s.getTags(); |
|---|
| 114 | String [] result = new String [tags.length]; |
|---|
| 115 | for (int i = 0; i < tags.length; i++) { |
|---|
| 116 | result[i] = tags[i].getReadable(); |
|---|
| 117 | } |
|---|
| 118 | return result; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | * Tests out the selectedtag editor from the command line. |
|---|
| 123 | * |
|---|
| 124 | * @param args ignored |
|---|
| 125 | */ |
|---|
| 126 | public static void main(String [] args) { |
|---|
| 127 | |
|---|
| 128 | try { |
|---|
| 129 | GenericObjectEditor.registerEditors(); |
|---|
| 130 | Tag [] tags = { |
|---|
| 131 | new Tag(1, "First option"), |
|---|
| 132 | new Tag(2, "Second option"), |
|---|
| 133 | new Tag(3, "Third option"), |
|---|
| 134 | new Tag(4, "Fourth option"), |
|---|
| 135 | new Tag(5, "Fifth option"), |
|---|
| 136 | }; |
|---|
| 137 | SelectedTag initial = new SelectedTag(1, tags); |
|---|
| 138 | SelectedTagEditor ce = new SelectedTagEditor(); |
|---|
| 139 | ce.setValue(initial); |
|---|
| 140 | PropertyValueSelector ps = new PropertyValueSelector(ce); |
|---|
| 141 | JFrame f = new JFrame(); |
|---|
| 142 | f.addWindowListener(new WindowAdapter() { |
|---|
| 143 | public void windowClosing(WindowEvent e) { |
|---|
| 144 | System.exit(0); |
|---|
| 145 | } |
|---|
| 146 | }); |
|---|
| 147 | f.getContentPane().setLayout(new BorderLayout()); |
|---|
| 148 | f.getContentPane().add(ps, BorderLayout.CENTER); |
|---|
| 149 | f.pack(); |
|---|
| 150 | f.setVisible(true); |
|---|
| 151 | } catch (Exception ex) { |
|---|
| 152 | ex.printStackTrace(); |
|---|
| 153 | System.err.println(ex.getMessage()); |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|