Subversion Repositories svn.mios

Rev

Rev 213 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
213 tk 1
/*
2
 * @(#)SIDV2librarian.java  beta1   2008/01/21
3
 *
4
 * Copyright (C) 2008    Rutger Vlek (rutgervlek@hotmail.com)
5
 *
6
 * This application is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this application; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
package org.midibox.sidlibr.gui;
22
 
628 adamjking 23
import java.awt.Component;
24
import java.awt.Toolkit;
25
import java.text.ParseException;
26
import java.util.EventObject;
27
 
213 tk 28
import javax.swing.DefaultCellEditor;
29
import javax.swing.JFormattedTextField;
30
import javax.swing.JOptionPane;
31
import javax.swing.JTable;
32
import javax.swing.JTextField;
33
import javax.swing.SwingUtilities;
628 adamjking 34
import javax.swing.JFormattedTextField.AbstractFormatter;
213 tk 35
import javax.swing.text.DefaultFormatterFactory;
36
import javax.swing.text.MaskFormatter;
37
 
38
/**
628 adamjking 39
 * Implements a cell editor that uses a formatted text field to edit Patch Names
213 tk 40
 */
41
 
42
public class PatchNameEditor extends DefaultCellEditor {
628 adamjking 43
    JFormattedTextField ftf;
213 tk 44
 
628 adamjking 45
    public PatchNameEditor() {
46
        super(new JFormattedTextField());
47
        ftf = (JFormattedTextField) getComponent();
213 tk 48
 
628 adamjking 49
        try {
50
            MaskFormatter mf1 = new MaskFormatter("****************");
51
            ftf.setFormatterFactory(new DefaultFormatterFactory(
52
                    (AbstractFormatter) mf1));
53
        } catch (ParseException ex) {
54
            System.out.println("Parse exception!");
55
        }
213 tk 56
 
628 adamjking 57
        ftf.setHorizontalAlignment(JTextField.TRAILING);
58
        ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
59
 
60
    }
61
 
62
    // Override to invoke setValue on the formatted text field.
63
    public Component getTableCellEditorComponent(JTable table, Object value,
64
            boolean isSelected, int row, int column) {
65
        JFormattedTextField ftf = (JFormattedTextField) super
66
                .getTableCellEditorComponent(table, value, isSelected, row,
67
                        column);
68
        ftf.setValue(value);
69
        return ftf;
70
    }
71
 
72
    public boolean isCellEditable(EventObject anEvent) {
73
        Boolean b = false;
74
        if (anEvent.getSource().getClass() == BankTable.class) {
75
            b = true;
76
        }
77
        return b;
78
    }
79
 
80
    // Override to check whether the edit is valid,
81
    // setting the value if it is and complaining if
82
    // it isn't. If it's OK for the editor to go
83
    // away, we need to invoke the superclass's version
84
    // of this method so that everything gets cleaned up.
85
    public boolean stopCellEditing() {
86
        JFormattedTextField ftf = (JFormattedTextField) getComponent();
87
        if (ftf.isEditValid()) {
88
            try {
89
                ftf.commitEdit();
90
            } catch (java.text.ParseException exc) {
91
            }
92
 
93
        } else { // text is invalid
94
            if (!userSaysRevert()) { // user wants to edit
95
                return false; // don't let the editor go away
96
            }
97
        }
98
        return super.stopCellEditing();
99
    }
100
 
101
    /**
102
     * Lets the user know that the text they entered is bad. Returns true if the
103
     * user elects to revert to the last good value. Otherwise, returns false,
104
     * indicating that the user wants to continue editing.
105
     */
106
    protected boolean userSaysRevert() {
107
        Toolkit.getDefaultToolkit().beep();
108
        ftf.selectAll();
109
        Object[] options = { "Edit", "Revert" };
110
        int answer = JOptionPane.showOptionDialog(SwingUtilities
111
                .getWindowAncestor(ftf), "HOI", "Invalid Text Entered",
112
                JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null,
113
                options, options[1]);
114
 
115
        if (answer == 1) { // Revert!
116
            ftf.setValue(ftf.getValue());
117
            return true;
118
        }
119
        return false;
120
    }
121
}