FileEditor.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 3k
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.  *    FileEditor.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.gui;
  22. import java.awt.FontMetrics;
  23. import java.awt.event.ActionListener;
  24. import java.awt.event.ActionEvent;
  25. import java.beans.PropertyEditor;
  26. import java.beans.PropertyEditorSupport;
  27. import java.io.File;
  28. import javax.swing.JFileChooser;
  29. /** 
  30.  * A PropertyEditor for File objects that lets the user select a file.
  31.  *
  32.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  33.  * @version $Revision: 1.6 $
  34.  */
  35. public class FileEditor extends PropertyEditorSupport {
  36.   /** The file chooser used for selecting files */
  37.   protected JFileChooser m_FileChooser;
  38.   
  39.   /**
  40.    * Returns a representation of the current property value as java source.
  41.    *
  42.    * @return a value of type 'String'
  43.    */
  44.   public String getJavaInitializationString() {
  45.     File f = (File) getValue();
  46.     if (f == null) {
  47.       return "null";
  48.     }
  49.     return "new File("" + f.getName() + "")";
  50.   }
  51.   /**
  52.    * Returns true because we do support a custom editor.
  53.    *
  54.    * @return true
  55.    */
  56.   public boolean supportsCustomEditor() {
  57.     return true;
  58.   }
  59.   
  60.   /**
  61.    * Gets the custom editor component.
  62.    *
  63.    * @return a value of type 'java.awt.Component'
  64.    */
  65.   public java.awt.Component getCustomEditor() {
  66.     if (m_FileChooser == null) {
  67.       m_FileChooser 
  68.         = new JFileChooser(new File(System.getProperty("user.dir")));
  69.       m_FileChooser.setApproveButtonText("Select");
  70.       m_FileChooser.setApproveButtonMnemonic('S');
  71.       m_FileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  72.       m_FileChooser.addActionListener(new ActionListener() {
  73. public void actionPerformed(ActionEvent e) {
  74.   String cmdString = e.getActionCommand();
  75.   if (cmdString.equals(JFileChooser.APPROVE_SELECTION)) {
  76.     File newVal = m_FileChooser.getSelectedFile();
  77.     setValue(newVal);
  78.   }
  79. }
  80.       });
  81.     }
  82.     return m_FileChooser;
  83.   }
  84.   /**
  85.    * Returns true since this editor is paintable.
  86.    *
  87.    * @return true.
  88.    */
  89.   public boolean isPaintable() {
  90.     return true;
  91.   }
  92.   /**
  93.    * Paints a representation of the current Object.
  94.    *
  95.    * @param gfx the graphics context to use
  96.    * @param box the area we are allowed to paint into
  97.    */
  98.   public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {
  99.     FontMetrics fm = gfx.getFontMetrics();
  100.     int vpad = (box.height - fm.getHeight()) / 2 ;
  101.     File f = (File) getValue();
  102.     String val = "No file";
  103.     if (f != null) {
  104.       val = f.getName();
  105.     }
  106.     gfx.drawString(val, 2, fm.getHeight() + vpad);
  107.   }  
  108. }