Subversion Repositories svn.mios

Rev

Rev 213 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 213 Rev 628
Line 17... Line 17...
17
 * along with this application; if not, write to the Free Software
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
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
19
 */
20
20
21
package org.midibox.sidlibr.gui;
21
package org.midibox.sidlibr.gui;
-
 
22
-
 
23
import java.awt.Component;
-
 
24
import java.awt.Toolkit;
-
 
25
import java.text.ParseException;
-
 
26
import java.util.EventObject;
22
27
23
import javax.swing.DefaultCellEditor;
28
import javax.swing.DefaultCellEditor;
24
import javax.swing.JFormattedTextField;
29
import javax.swing.JFormattedTextField;
25
import javax.swing.JOptionPane;
30
import javax.swing.JOptionPane;
26
import javax.swing.JTable;
31
import javax.swing.JTable;
27
import javax.swing.JTextField;
32
import javax.swing.JTextField;
28
import javax.swing.SwingUtilities;
33
import javax.swing.SwingUtilities;
29
import java.awt.Component;
-
 
30
import java.awt.Toolkit;
-
 
31
import java.text.ParseException;
34
import javax.swing.JFormattedTextField.AbstractFormatter;
32
import javax.swing.text.DefaultFormatterFactory;
35
import javax.swing.text.DefaultFormatterFactory;
33
import javax.swing.text.MaskFormatter;
36
import javax.swing.text.MaskFormatter;
34
import javax.swing.JFormattedTextField.AbstractFormatter;
-
 
35
import java.util.EventObject;
-
 
36
37
37
/**
38
/**
38
 * Implements a cell editor that uses a formatted text field
39
 * Implements a cell editor that uses a formatted text field to edit Patch Names
39
 * to edit Patch Names
-
 
40
 */
40
 */
41
41
42
public class PatchNameEditor extends DefaultCellEditor {
42
public class PatchNameEditor extends DefaultCellEditor {
43
    JFormattedTextField ftf;
43
    JFormattedTextField ftf;
44
   
44
Line 46... Line 46...
46
        super(new JFormattedTextField());
46
        super(new JFormattedTextField());
47
        ftf = (JFormattedTextField)getComponent();
47
        ftf = (JFormattedTextField) getComponent();
48
   
48
49
        try {
49
        try {
50
            MaskFormatter mf1 = new MaskFormatter("****************");
50
            MaskFormatter mf1 = new MaskFormatter("****************");
51
            ftf.setFormatterFactory(new DefaultFormatterFactory((AbstractFormatter)mf1));
51
            ftf.setFormatterFactory(new DefaultFormatterFactory(
-
 
52
                    (AbstractFormatter) mf1));
52
        } catch(ParseException ex){
53
        } catch (ParseException ex) {
53
            System.out.println("Parse exception!");
54
            System.out.println("Parse exception!");
54
        }
55
        }
55
       
56
56
        ftf.setHorizontalAlignment(JTextField.TRAILING);
57
        ftf.setHorizontalAlignment(JTextField.TRAILING);
57
        ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
58
        ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
58
       
59
59
    }
60
    }
60
61
61
    //Override to invoke setValue on the formatted text field.
62
    // Override to invoke setValue on the formatted text field.
62
    public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected,int row, int column) {
63
    public Component getTableCellEditorComponent(JTable table, Object value,
-
 
64
            boolean isSelected, int row, int column) {
-
 
65
        JFormattedTextField ftf = (JFormattedTextField) super
63
        JFormattedTextField ftf = (JFormattedTextField)super.getTableCellEditorComponent(table, value, isSelected, row, column);
66
                .getTableCellEditorComponent(table, value, isSelected, row,
-
 
67
                        column);
64
        ftf.setValue(value);
68
        ftf.setValue(value);
65
        return ftf;
69
        return ftf;
66
    }
70
    }
67
   
71
68
    public boolean isCellEditable(EventObject anEvent) {
72
    public boolean isCellEditable(EventObject anEvent) {
Line 81... Line 85...
81
    public boolean stopCellEditing() {
85
    public boolean stopCellEditing() {
82
        JFormattedTextField ftf = (JFormattedTextField)getComponent();
86
        JFormattedTextField ftf = (JFormattedTextField) getComponent();
83
        if (ftf.isEditValid()) {
87
        if (ftf.isEditValid()) {
84
            try {
88
            try {
85
                ftf.commitEdit();
89
                ftf.commitEdit();
86
            } catch (java.text.ParseException exc) { }
90
            } catch (java.text.ParseException exc) {
-
 
91
            }
87
       
92
88
        } else { //text is invalid
93
        } else { // text is invalid
89
            if (!userSaysRevert()) { //user wants to edit
94
            if (!userSaysRevert()) { // user wants to edit
90
            return false; //don't let the editor go away
95
                return false; // don't let the editor go away
91
        }
96
            }
92
        }
97
        }
93
        return super.stopCellEditing();
98
        return super.stopCellEditing();
94
    }
99
    }
95
100
96
    /**
101
    /**
97
     * Lets the user know that the text they entered is
102
     * Lets the user know that the text they entered is bad. Returns true if the
98
     * bad. Returns true if the user elects to revert to
-
 
99
     * the last good value.  Otherwise, returns false,
103
     * user elects to revert to the last good value. Otherwise, returns false,
100
     * indicating that the user wants to continue editing.
104
     * indicating that the user wants to continue editing.
101
     */
105
     */
102
    protected boolean userSaysRevert() {
106
    protected boolean userSaysRevert() {
103
        Toolkit.getDefaultToolkit().beep();
107
        Toolkit.getDefaultToolkit().beep();
104
        ftf.selectAll();
108
        ftf.selectAll();
105
        Object[] options = {"Edit",
109
        Object[] options = { "Edit", "Revert" };
106
                            "Revert"};
-
 
107
        int answer = JOptionPane.showOptionDialog(
110
        int answer = JOptionPane.showOptionDialog(SwingUtilities
108
            SwingUtilities.getWindowAncestor(ftf),
-
 
109
            "HOI",
-
 
110
            "Invalid Text Entered",
111
                .getWindowAncestor(ftf), "HOI", "Invalid Text Entered",
111
            JOptionPane.YES_NO_OPTION,
-
 
112
            JOptionPane.ERROR_MESSAGE,
112
                JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null,
113
            null,
-
 
114
            options,
-
 
115
            options[1]);
113
                options, options[1]);
116
       
114
117
        if (answer == 1) { //Revert!
115
        if (answer == 1) { // Revert!
118
            ftf.setValue(ftf.getValue());
116
            ftf.setValue(ftf.getValue());
119
        return true;
117
            return true;
120
        }
118
        }
121
    return false;
119
        return false;
122
    }
120
    }
123
 }
121
}
124
-