source: src/main/java/weka/gui/SimpleDateFormatEditor.java @ 6

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

Import di weka.

File size: 8.0 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 * SimpleDateFormatEditor.java
19 * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui;
24
25import java.awt.Component;
26import java.awt.FlowLayout;
27import java.awt.Graphics;
28import java.awt.Rectangle;
29import java.awt.event.ActionEvent;
30import java.awt.event.ActionListener;
31import java.beans.PropertyChangeListener;
32import java.beans.PropertyChangeSupport;
33import java.beans.PropertyEditor;
34import java.text.SimpleDateFormat;
35
36import javax.swing.JButton;
37import javax.swing.JLabel;
38import javax.swing.JPanel;
39import javax.swing.JTextField;
40import javax.swing.event.DocumentEvent;
41import javax.swing.event.DocumentListener;
42
43/**
44 * Class for editing SimpleDateFormat strings.
45 *
46 * @author FracPete (fracpete at waikato dot ac dot nz)
47 * @version $Revision: 1.2 $
48 * @see SimpleDateFormat
49 */
50public class SimpleDateFormatEditor 
51  implements PropertyEditor {
52
53  /** the default format */
54  public final static String DEFAULT_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
55 
56  /** The date format being edited */
57  private SimpleDateFormat m_Format;
58
59  /** A helper class for notifying listeners */
60  private PropertyChangeSupport m_propSupport;
61
62  /** An instance of the custom editor */
63  private CustomEditor m_customEditor;
64
65  /**
66   * This class presents a GUI for editing the cost matrix, and saving and
67   * loading from files.
68   */
69  private class CustomEditor 
70    extends JPanel
71    implements ActionListener, DocumentListener {
72
73    /** for serialization */
74    private static final long serialVersionUID = -4018834274636309987L;
75   
76    /** The text field for the format */
77    private JTextField m_FormatText;
78   
79    /** The button for setting a default date format */
80    private JButton m_DefaultButton;
81
82    /** The button for applying the format */
83    private JButton m_ApplyButton;
84
85    /**
86     * Constructs a new CustomEditor.
87     */
88    public CustomEditor() {
89      m_FormatText    = new JTextField(20);
90      m_DefaultButton = new JButton("Default");
91      m_ApplyButton   = new JButton("Apply");
92
93      m_DefaultButton.setMnemonic('D');
94      m_ApplyButton.setMnemonic('A');
95
96      m_FormatText.getDocument().addDocumentListener(this);
97      m_DefaultButton.addActionListener(this);
98      m_ApplyButton.addActionListener(this);
99
100      setLayout(new FlowLayout());
101      add(new JLabel("ISO 8601 Date format"));
102      add(m_FormatText);
103      add(m_DefaultButton);
104      add(m_ApplyButton);
105    }
106
107    /**
108     * Responds to the user perfoming an action.
109     *
110     * @param e the action event that occured
111     */
112    public void actionPerformed(ActionEvent e) {
113      if (e.getSource() == m_DefaultButton)
114        defaultFormat();
115      else if (e.getSource() == m_ApplyButton)
116        applyFormat();
117    }
118
119    /**
120     * sets the format to default
121     */
122    public void defaultFormat() {
123      m_FormatText.setText(DEFAULT_FORMAT);
124      formatChanged();
125    }
126
127    /**
128     * returns true if the current format is a valid format
129     */
130    protected boolean isValidFormat() {
131      boolean   result;
132     
133      result = false;
134     
135      try {
136        new SimpleDateFormat(m_FormatText.getText());
137        result = true;
138      }
139      catch (Exception e) {
140        // we can ignore this exception
141      }
142     
143      return result;
144    }
145   
146    /**
147     * sets the format, but only if it's a valid one
148     */
149    public void applyFormat() {
150      if (isValidFormat()) {
151        m_Format = new SimpleDateFormat(m_FormatText.getText());
152        m_propSupport.firePropertyChange(null, null, null);
153      }
154      else {
155        throw new IllegalArgumentException(
156            "Date format '" 
157            + m_FormatText.getText() 
158            + "' is invalid! Cannot execute applyFormat!");
159      }
160    }
161   
162    /**
163     * Responds to a change of the text field.
164     */
165    public void formatChanged() {
166      m_FormatText.setText(m_Format.toPattern());
167      m_propSupport.firePropertyChange(null, null, null);
168    }
169   
170    /**
171     * Gives notification that an attribute or set of attributes changed.
172     */
173    public void changedUpdate(DocumentEvent e) {
174      m_ApplyButton.setEnabled(isValidFormat());
175    }
176   
177    /**
178     * Gives notification that there was an insert into the document.
179     */
180    public void insertUpdate(DocumentEvent e) {
181      m_ApplyButton.setEnabled(isValidFormat());
182    }
183   
184    /**
185     * Gives notification that a portion of the document has been removed.
186     */
187    public void removeUpdate(DocumentEvent e) {
188      m_ApplyButton.setEnabled(isValidFormat());
189    }
190  }
191
192  /**
193   * Constructs a new SimpleDateFormatEditor.
194   *
195   */
196  public SimpleDateFormatEditor() {
197    m_propSupport = new PropertyChangeSupport(this);
198    m_customEditor = new CustomEditor();
199  }
200
201  /**
202   * Sets the value of the date format to be edited.
203   *
204   * @param value a SimpleDateFormat object to be edited
205   */
206  public void setValue(Object value) {
207    m_Format = (SimpleDateFormat) value;
208    m_customEditor.formatChanged();
209  }
210
211  /**
212   * Gets the date format that is being edited.
213   *
214   * @return the edited SimpleDateFormat object
215   */ 
216  public Object getValue() {
217    return m_Format;
218  }
219
220  /**
221   * Indicates whether the object can be represented graphically. In this case
222   * it can.
223   *
224   * @return true
225   */ 
226  public boolean isPaintable() {
227    return true;
228  }
229
230  /**
231   * Paints a graphical representation of the object. It just prints the
232   * format.
233   *
234   * @param gfx the graphics context to draw the representation to
235   * @param box the bounds within which the representation should fit.
236   */   
237  public void paintValue(Graphics gfx,
238                         Rectangle box) {
239    gfx.drawString(m_Format.toPattern(), box.x, box.y + box.height);
240  }
241
242  /**
243   * Returns the Java code that generates an object the same as the one being edited.
244   *
245   * @return the initialization string
246   */   
247  public String getJavaInitializationString() {
248    return ("new SimpleDateFormat(" + m_Format.toPattern() + ")");
249  }
250
251  /**
252   * Returns the date format string.
253   *
254   * @return the date format string
255   */   
256  public String getAsText() {
257    return m_Format.toPattern();
258  }
259
260  /**
261   * Sets the date format string.
262   *
263   * @param text the date format string
264   */   
265  public void setAsText(String text) {
266    m_Format = new SimpleDateFormat(text);
267  }
268
269  /**
270   * Some objects can return tags, but a date format cannot.
271   *
272   * @return null
273   */ 
274  public String[] getTags() {
275    return null;
276  }
277
278  /**
279   * Gets a GUI component with which the user can edit the date format.
280   *
281   * @return an editor GUI component
282   */   
283  public Component getCustomEditor() {
284    return m_customEditor;
285  }
286
287  /**
288   * Indicates whether the date format can be edited in a GUI, which it can.
289   *
290   * @return true
291   */     
292  public boolean supportsCustomEditor() {
293    return true;
294  }
295
296  /**
297   * Adds an object to the list of those that wish to be informed when the
298   * date format changes.
299   *
300   * @param listener a new listener to add to the list
301   */   
302  public void addPropertyChangeListener(PropertyChangeListener listener) {
303    m_propSupport.addPropertyChangeListener(listener);
304  }
305
306  /**
307   * Removes an object from the list of those that wish to be informed when the
308   * date format changes.
309   *
310   * @param listener the listener to remove from the list
311   */ 
312  public void removePropertyChangeListener(PropertyChangeListener listener) {
313    m_propSupport.removePropertyChangeListener(listener);
314  }
315}
Note: See TracBrowser for help on using the repository browser.