SelectedTagEditor.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 4k
Category:

Windows Develop

Development Platform:

Java

  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.  *    SelectedTagEditor.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.gui;
  22. import weka.core.Tag;
  23. import weka.core.SelectedTag;
  24. import java.awt.BorderLayout;
  25. import java.awt.event.WindowAdapter;
  26. import java.awt.event.WindowEvent;
  27. import java.beans.PropertyEditor;
  28. import java.beans.PropertyEditorSupport;
  29. import javax.swing.JFrame;
  30. import javax.swing.JLabel;
  31. /** 
  32.  * A PropertyEditor that uses tags, where the tags are obtained from a
  33.  * weka.core.SelectedTag object.
  34.  *
  35.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  36.  * @version $Revision: 1.5 $
  37.  */
  38. public class SelectedTagEditor extends PropertyEditorSupport {
  39.   /**
  40.    * Returns a description of the property value as java source.
  41.    *
  42.    * @return a value of type 'String'
  43.    */
  44.   public String getJavaInitializationString() {
  45.     SelectedTag s = (SelectedTag)getValue();
  46.     Tag [] tags = s.getTags();
  47.     String result = "new SelectedTag("
  48.       + s.getSelectedTag().getID()
  49.       + ", {n";
  50.     for (int i = 0; i < tags.length; i++) {
  51.       result += "new Tag(" + tags[i].getID()
  52. + ","" + tags[i].getReadable()
  53. + "")";
  54.       if (i < tags.length - 1) {
  55. result += ',';
  56.       }
  57.       result += 'n';
  58.     }
  59.     return result + "})";
  60.   }
  61.   /**
  62.    * Gets the current value as text.
  63.    *
  64.    * @return a value of type 'String'
  65.    */
  66.   public String getAsText() {
  67.     SelectedTag s = (SelectedTag)getValue();
  68.     return s.getSelectedTag().getReadable();
  69.   }
  70.   /**
  71.    * Sets the current property value as text.
  72.    *
  73.    * @param text the text of the selected tag.
  74.    * @exception java.lang.IllegalArgumentException if an error occurs
  75.    */
  76.   public void setAsText(String text)
  77.     throws java.lang.IllegalArgumentException {
  78.     SelectedTag s = (SelectedTag)getValue();
  79.     Tag [] tags = s.getTags();
  80.     try {
  81.       for (int i = 0; i < tags.length; i++) {
  82. if (text.equals(tags[i].getReadable())) {
  83.   setValue(new SelectedTag(tags[i].getID(), tags));
  84.   return;
  85. }
  86.       }
  87.     } catch (Exception ex) {
  88.       throw new java.lang.IllegalArgumentException(text);
  89.     }
  90.   }
  91.   /**
  92.    * Gets the list of tags that can be selected from.
  93.    *
  94.    * @return an array of string tags.
  95.    */
  96.   public String[] getTags() {
  97.     SelectedTag s = (SelectedTag)getValue();
  98.     Tag [] tags = s.getTags();
  99.     String [] result = new String [tags.length];
  100.     for (int i = 0; i < tags.length; i++) {
  101.       result[i] = tags[i].getReadable();
  102.     }
  103.     return result;
  104.   }
  105.   
  106.   /**
  107.    * Tests out the selectedtag editor from the command line.
  108.    *
  109.    * @param args ignored
  110.    */
  111.   public static void main(String [] args) {
  112.     try {
  113.       System.err.println("---Registering Weka Editors---");
  114.       java.beans.PropertyEditorManager.registerEditor(SelectedTag.class,
  115.       SelectedTagEditor.class);
  116.       Tag [] tags =  {
  117. new Tag(1, "First option"),
  118. new Tag(2, "Second option"),
  119. new Tag(3, "Third option"),
  120. new Tag(4, "Fourth option"),
  121. new Tag(5, "Fifth option"),
  122.       };
  123.       SelectedTag initial = new SelectedTag(1, tags);
  124.       SelectedTagEditor ce = new SelectedTagEditor();
  125.       ce.setValue(initial);
  126.       PropertyValueSelector ps = new PropertyValueSelector(ce);
  127.       JFrame f = new JFrame(); 
  128.       f.addWindowListener(new WindowAdapter() {
  129. public void windowClosing(WindowEvent e) {
  130.   System.exit(0);
  131. }
  132.       });
  133.       f.getContentPane().setLayout(new BorderLayout());
  134.       f.getContentPane().add(ps, BorderLayout.CENTER);
  135.       f.pack();
  136.       f.setVisible(true);
  137.     } catch (Exception ex) {
  138.       ex.printStackTrace();
  139.       System.err.println(ex.getMessage());
  140.     }
  141.   }
  142. }