001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.ui.components.gpropertyeditor;
005    
006    
007    import graphlab.platform.StaticUtils;
008    import graphlab.ui.AttributeSetView;
009    import graphlab.ui.components.gpropertyeditor.editors.GStringEditor;
010    
011    import javax.swing.*;
012    import javax.swing.table.TableCellEditor;
013    import java.awt.*;
014    import java.util.HashMap;
015    import java.util.Map;
016    import java.util.Collections;
017    
018    /**
019     * 1- inplace editing and outplace editing
020     */
021    
022    /**
023     * @see graphlab.ui.components.gpropertyeditor.GPropertyEditor
024     *
025     * @author  Azin Azadi
026     */
027    public class GCellEditor extends AbstractCellEditor implements TableCellEditor, EditingFinishedListener {
028    
029        /**
030         *
031         */
032        private static final long serialVersionUID = -7943480654474872421L;
033        protected static HashMap<Class, GBasicCellEditor> knownEditors = new HashMap<Class, GBasicCellEditor>();
034        //current editor!
035        private GBasicCellEditor editor;
036        protected AttributeSetView atr;
037    
038        public static void registerEditor(Class clazz, GBasicCellEditor editor) {
039            knownEditors.put(clazz, editor);
040        }
041    
042        public GCellEditor() {
043        }
044    
045        public void cancelCellEditing() {
046            super.cancelCellEditing();
047            if (lastEditor != null)
048                lastEditor.cancelEditing();
049        }
050    
051        GBasicCellEditor lastEditor;
052    
053        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
054            String name = atr.getNameAt(row);
055            if (!atr.isEditable(name))
056                return null;
057            if (value == null) {
058                return null;
059            }
060    
061            editor = atr.getEditor(name);
062            if (editor == null) {
063                editor = getEditorFor(value);
064                if (editor == null)
065                    return null;
066            }
067    
068            editor.setEditingFinishedListener(this);
069            return editor.getEditorComponent(value);
070        }
071    
072        private static ObjectEditor oe = new ObjectEditor();
073    
074        /**
075         * gets an editor for the given object, the editor should be registered before,...
076         * several editors are registered as default
077         */
078        public static GBasicCellEditor getEditorFor(Object value) {
079            GBasicCellEditor editor = null;
080            Class c = value.getClass();
081            //search super classes
082            while (editor == null && c != Object.class) {
083                editor = knownEditors.get(c);
084                c = c.getSuperclass();
085            }
086            if (editor == null) {
087                //search implementing interfaces
088                Class cc[] = value.getClass().getInterfaces();
089                for (int i = 0; i < cc.length && editor == null; i++)
090                    editor = knownEditors.get(cc[i]);
091            }
092            if (editor == null) {
093                //no editor was defiend for this Class
094                //take the last chance
095                try {
096                    if (StaticUtils.fromString(value.getClass().getName(), value.toString()) != null) {
097                        return oe;   //return oe if it can be reconstructed from a string in StaticUtils
098                    }
099                } catch (Exception e) {
100                    return null;
101                }
102            }
103            return editor;
104        }
105    
106        public Object getCellEditorValue() {
107            return editor.getEditorValue();
108        }
109    
110        public void editingFinished(Object editorValue) {
111            stopCellEditing();
112        }
113    
114        void setAtrView(AttributeSetView attribute) {
115            this.atr = attribute;
116        }
117    
118        /**
119         * @return known editors as an unmodifiable map
120         */
121        public static Map<Class, GBasicCellEditor> getKnownEditors(){
122            return Collections.unmodifiableMap(knownEditors);
123        }
124    }
125    
126    class ObjectEditor extends GStringEditor {
127        Class clazz;
128    
129        public Component getEditorComponent(Object value) {
130            clazz = value.getClass();
131            return super.getEditorComponent(value);
132        }
133    
134        protected void finishEdit() {
135            jt.setText(getEditorValue() + "");
136            super.finishEdit();
137        }
138    
139        public Object getEditorValue() {
140            String s = super.getEditorValue() + "";
141    
142            Object o = null;
143            try {
144                o = StaticUtils.fromString(clazz.getName(), s);
145            } catch (Exception e) {
146                return StaticUtils.fromString(clazz.getName(), initVal);
147            }
148            return o;
149        }
150    }