source: src/main/java/weka/gui/FileEditor.java @ 24

Last change on this file since 24 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 3.7 KB
Line 
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 *    FileEditor.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23
24package weka.gui;
25
26import java.awt.Container;
27import java.awt.Dialog;
28import java.awt.FontMetrics;
29import java.awt.event.ActionEvent;
30import java.awt.event.ActionListener;
31import java.beans.PropertyEditorSupport;
32import java.io.File;
33
34import javax.swing.JFileChooser;
35
36
37/**
38 * A PropertyEditor for File objects that lets the user select a file.
39 *
40 * @author Len Trigg (trigg@cs.waikato.ac.nz)
41 * @version $Revision: 5204 $
42 */
43public class FileEditor extends PropertyEditorSupport {
44
45  /** The file chooser used for selecting files. */
46  protected JFileChooser m_FileChooser;
47 
48  /**
49   * Returns a representation of the current property value as java source.
50   *
51   * @return a value of type 'String'
52   */
53  public String getJavaInitializationString() {
54
55    File f = (File) getValue();
56    if (f == null) {
57      return "null";
58    }
59    return "new File(\"" + f.getName() + "\")";
60  }
61
62  /**
63   * Returns true because we do support a custom editor.
64   *
65   * @return true
66   */
67  public boolean supportsCustomEditor() {
68    return true;
69  }
70 
71  /**
72   * Gets the custom editor component.
73   *
74   * @return a value of type 'java.awt.Component'
75   */
76  public java.awt.Component getCustomEditor() {
77
78    if (m_FileChooser == null) {
79      File currentFile = (File) getValue();
80      if (currentFile != null) {
81        m_FileChooser
82          = new JFileChooser();
83        m_FileChooser.setSelectedFile(currentFile);
84      } else {
85        m_FileChooser
86          = new JFileChooser(new File(System.getProperty("user.dir")));
87      }
88      m_FileChooser.setApproveButtonText("Select");
89      m_FileChooser.setApproveButtonMnemonic('S');
90      m_FileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
91      m_FileChooser.addActionListener(new ActionListener() {
92        public void actionPerformed(ActionEvent e) {
93          String cmdString = e.getActionCommand();
94          if (cmdString.equals(JFileChooser.APPROVE_SELECTION)) {
95            File newVal = m_FileChooser.getSelectedFile();
96            setValue(newVal);
97          }
98          closeDialog();
99        }
100      });
101    }
102    return m_FileChooser;
103  }
104
105  /**
106   * Returns true since this editor is paintable.
107   *
108   * @return true.
109   */
110  public boolean isPaintable() {
111    return true;
112  }
113
114  /**
115   * Paints a representation of the current Object.
116   *
117   * @param gfx the graphics context to use
118   * @param box the area we are allowed to paint into
119   */
120  public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {
121
122    FontMetrics fm = gfx.getFontMetrics();
123    int vpad = (box.height - fm.getHeight()) / 2 ;
124    File f = (File) getValue();
125    String val = "No file";
126    if (f != null) {
127      val = f.getName();
128    }
129    gfx.drawString(val, 2, fm.getHeight() + vpad);
130  } 
131 
132  /**
133   * Closes the dialog.
134   */
135  protected void closeDialog() {
136    if (m_FileChooser instanceof Container) {
137      Dialog dlg = PropertyDialog.getParentDialog((Container) m_FileChooser);
138      if (dlg != null)
139        dlg.setVisible(false);
140    }
141  }
142}
143
Note: See TracBrowser for help on using the repository browser.