Subversion Repositories svn.mios

Rev

Go to most recent revision | Details | 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
 
23
import javax.swing.DefaultCellEditor;
24
import javax.swing.JFormattedTextField;
25
import javax.swing.JOptionPane;
26
import javax.swing.JTable;
27
import javax.swing.JTextField;
28
import javax.swing.SwingUtilities;
29
import java.awt.Component;
30
import java.awt.Toolkit;
31
import java.text.ParseException;
32
import javax.swing.text.DefaultFormatterFactory;
33
import javax.swing.text.MaskFormatter;
34
import javax.swing.JFormattedTextField.AbstractFormatter;
35
import java.util.EventObject;
36
 
37
/**
38
 * Implements a cell editor that uses a formatted text field
39
 * to edit Patch Names
40
 */
41
 
42
public class PatchNameEditor extends DefaultCellEditor {
43
    JFormattedTextField ftf;
44
 
45
    public PatchNameEditor() {
46
        super(new JFormattedTextField());
47
        ftf = (JFormattedTextField)getComponent();
48
 
49
        try {
50
            MaskFormatter mf1 = new MaskFormatter("****************");
51
            ftf.setFormatterFactory(new DefaultFormatterFactory((AbstractFormatter)mf1));
52
        } catch(ParseException ex){
53
            System.out.println("Parse exception!");
54
        }
55
 
56
        ftf.setHorizontalAlignment(JTextField.TRAILING);
57
        ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
58
 
59
    }
60
 
61
    //Override to invoke setValue on the formatted text field.
62
    public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected,int row, int column) {
63
        JFormattedTextField ftf = (JFormattedTextField)super.getTableCellEditorComponent(table, value, isSelected, row, column);
64
        ftf.setValue(value);
65
        return ftf;
66
    }
67
 
68
    public boolean isCellEditable(EventObject anEvent) {
69
        Boolean b = false;     
70
        if (anEvent.getSource().getClass() == BankTable.class) {
71
            b = true;
72
        }      
73
        return b;
74
    }
75
 
76
    //Override to check whether the edit is valid,
77
    //setting the value if it is and complaining if
78
    //it isn't.  If it's OK for the editor to go
79
    //away, we need to invoke the superclass's version 
80
    //of this method so that everything gets cleaned up.
81
    public boolean stopCellEditing() {
82
        JFormattedTextField ftf = (JFormattedTextField)getComponent();
83
        if (ftf.isEditValid()) {
84
            try {
85
                ftf.commitEdit();
86
            } catch (java.text.ParseException exc) { }
87
 
88
        } else { //text is invalid
89
            if (!userSaysRevert()) { //user wants to edit
90
            return false; //don't let the editor go away
91
        }
92
        }
93
        return super.stopCellEditing();
94
    }
95
 
96
    /**
97
     * Lets the user know that the text they entered is
98
     * bad. Returns true if the user elects to revert to
99
     * the last good value.  Otherwise, returns false,
100
     * indicating that the user wants to continue editing.
101
     */
102
    protected boolean userSaysRevert() {
103
        Toolkit.getDefaultToolkit().beep();
104
        ftf.selectAll();
105
        Object[] options = {"Edit",
106
                            "Revert"};
107
        int answer = JOptionPane.showOptionDialog(
108
            SwingUtilities.getWindowAncestor(ftf),
109
            "HOI",
110
            "Invalid Text Entered",
111
            JOptionPane.YES_NO_OPTION,
112
            JOptionPane.ERROR_MESSAGE,
113
            null,
114
            options,
115
            options[1]);
116
 
117
        if (answer == 1) { //Revert!
118
            ftf.setValue(ftf.getValue());
119
        return true;
120
        }
121
    return false;
122
    }
123
 }
124