001/**
002 *  '$Author: crawl $'
003 *  '$Date: 2015-11-02 18:37:28 +0000 (Mon, 02 Nov 2015) $'
004 *  '$Revision: 34179 $'
005 *
006 *  For Details:
007 *  http://www.kepler-project.org
008 *
009 *  Copyright (c) 2009-2010 The Regents of the
010 *  University of California. All rights reserved. Permission is hereby granted,
011 *  without written agreement and without license or royalty fees, to use, copy,
012 *  modify, and distribute this software and its documentation for any purpose,
013 *  provided that the above copyright notice and the following two paragraphs
014 *  appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF
015 *  CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
016 *  OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
017 *  DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
018 *  POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY
019 *  DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
020 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
021 *  SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 *  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 *  ENHANCEMENTS, OR MODIFICATIONS.
024 */
025
026package org.kepler.workflowrunmanager.gui;
027
028import java.awt.Component;
029import java.awt.KeyEventDispatcher;
030import java.awt.KeyboardFocusManager;
031import java.awt.event.KeyEvent;
032
033import javax.swing.DefaultCellEditor;
034import javax.swing.InputVerifier;
035import javax.swing.JComponent;
036import javax.swing.JTable;
037import javax.swing.JTextField;
038
039import org.kepler.workflowrunmanager.ExecIdQueryParse;
040import org.kepler.workflowrunmanager.WRMDefaults;
041
042public class ExecIdCellEditor extends DefaultCellEditor {
043
044        private final JTextField tf;
045        private ExecIdVerifier execIdVerifier = new ExecIdVerifier();
046        private WRMDefaults wrmdefaults = WRMDefaults.getInstance();
047        private KeyEventDispatcherClass dispatcher = null;
048
049        class KeyEventDispatcherClass implements KeyEventDispatcher{
050                @Override
051        public boolean dispatchKeyEvent(KeyEvent e) {
052                        // TODO find out why we get two tabs here:
053            if (e.getKeyCode() == KeyEvent.VK_TAB ||
054                    e.getKeyCode() == KeyEvent.VK_ESCAPE) {
055                                stopCellEditing();
056                        }
057                        return false;
058                }
059        }
060        
061        public ExecIdCellEditor(JTextField jTextField) {
062                super(jTextField);
063                tf = (JTextField) getComponent();
064                tf.setInputVerifier(execIdVerifier);
065
066                dispatcher =  new KeyEventDispatcherClass();
067                
068                // TODO likely be able to remove this when focus stuff reworked:
069                KeyboardFocusManager.getCurrentKeyboardFocusManager()
070                                .addKeyEventDispatcher(dispatcher);
071
072        }
073
074        @Override
075    public Component getTableCellEditorComponent(JTable table, Object value,
076                        boolean isSelected, int row, int column) {
077                tf.setText(value.toString());
078                return tf;
079        }
080
081        @Override
082    public Object getCellEditorValue() {
083                return tf.getText();
084        }
085
086        /**
087         * Forwards the message from the <code>CellEditor</code> to the
088         * <code>delegate</code>.
089         * 
090         * @see EditorDelegate#stopCellEditing
091         */
092        @Override
093    public boolean stopCellEditing() {
094
095                // if text invalid, just reset to default - friendlier than trapping
096                // user in the cell.
097                if (!execIdVerifier.verify(tf)) {
098                        tf.setText(wrmdefaults.getDefault(WRMDefaults.EXEC_ID));
099                        // setForeground(Color.LIGHT_GRAY);
100                } else {
101                        ExecIdQueryParse execIdQueryParse = new ExecIdQueryParse(tf
102                                        .getText());
103                        String execIdText = execIdQueryParse.getCompleteText();
104                        if (isValid(execIdText)) {
105                                tf.setText(execIdText);
106                        }
107                }
108
109                return super.stopCellEditing();
110        }
111
112        // sanity check - verify strings we try to set in code too.
113        private boolean isValid(String test) {
114                JTextField testField = new JTextField(test);
115                if (!execIdVerifier.verify(testField)) {
116                        System.out.println("ERROR trying to set execId text");
117                        return false;
118                } else {
119                        return true;
120                }
121        }
122
123        static class ExecIdVerifier extends InputVerifier {
124
125                @Override
126        public boolean verify(JComponent input) {
127                        JTextField tf = (JTextField) input;
128
129                        ExecIdQueryParse execIdQueryParse = new ExecIdQueryParse(tf
130                                        .getText());
131                        if (execIdQueryParse.isValid()) {
132                                return true;
133                        }
134                        return false;
135                }
136
137                // not necessary but probably good to have.
138                @Override
139        public boolean shouldYieldFocus(JComponent input) {
140                        boolean inputOK = verify(input);
141
142                        if (inputOK) {
143                                return true;
144                        } else {
145                                return false;
146                        }
147                }
148
149        }
150        
151        public void dispose(){
152                if (dispatcher != null){
153                        KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);
154                }
155        }
156
157}