Subversion Repositories svn.mios

Rev

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

/*
 * @(#)SIDV2librarian.java  beta1   2008/01/21
 *
 * Copyright (C) 2008    Rutger Vlek (rutgervlek@hotmail.com)
 *
 * This application is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This application is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this application; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


package org.midibox.sidlibr.gui;

import java.awt.Component;
import java.awt.Toolkit;
import java.text.ParseException;
import java.util.EventObject;

import javax.swing.DefaultCellEditor;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

/**
 * Implements a cell editor that uses a formatted text field to edit Patch Names
 */


public class PatchNameEditor extends DefaultCellEditor {
    JFormattedTextField ftf;

    public PatchNameEditor() {
        super(new JFormattedTextField());
        ftf = (JFormattedTextField) getComponent();

        try {
            MaskFormatter mf1 = new MaskFormatter("****************");
            ftf.setFormatterFactory(new DefaultFormatterFactory(
                    (AbstractFormatter) mf1));
        } catch (ParseException ex) {
            System.out.println("Parse exception!");
        }

        ftf.setHorizontalAlignment(JTextField.TRAILING);
        ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);

    }

    // Override to invoke setValue on the formatted text field.
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
        JFormattedTextField ftf = (JFormattedTextField) super
                .getTableCellEditorComponent(table, value, isSelected, row,
                        column);
        ftf.setValue(value);
        return ftf;
    }

    public boolean isCellEditable(EventObject anEvent) {
        Boolean b = false;
        if (anEvent.getSource().getClass() == BankTable.class) {
            b = true;
        }
        return b;
    }

    // Override to check whether the edit is valid,
    // setting the value if it is and complaining if
    // it isn't. If it's OK for the editor to go
    // away, we need to invoke the superclass's version
    // of this method so that everything gets cleaned up.
    public boolean stopCellEditing() {
        JFormattedTextField ftf = (JFormattedTextField) getComponent();
        if (ftf.isEditValid()) {
            try {
                ftf.commitEdit();
            } catch (java.text.ParseException exc) {
            }

        } else { // text is invalid
            if (!userSaysRevert()) { // user wants to edit
                return false; // don't let the editor go away
            }
        }
        return super.stopCellEditing();
    }

    /**
     * Lets the user know that the text they entered is bad. Returns true if the
     * user elects to revert to the last good value. Otherwise, returns false,
     * indicating that the user wants to continue editing.
     */

    protected boolean userSaysRevert() {
        Toolkit.getDefaultToolkit().beep();
        ftf.selectAll();
        Object[] options = { "Edit", "Revert" };
        int answer = JOptionPane.showOptionDialog(SwingUtilities
                .getWindowAncestor(ftf), "HOI", "Invalid Text Entered",
                JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null,
                options, options[1]);

        if (answer == 1) { // Revert!
            ftf.setValue(ftf.getValue());
            return true;
        }
        return false;
    }
}