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    import graphlab.platform.attribute.AttributeListener;
007    import graphlab.platform.attribute.NotifiableAttributeSet;
008    import graphlab.ui.AttributeSetView;
009    import graphlab.ui.NotifiableAttributeSetView;
010    
011    import javax.swing.event.TableModelEvent;
012    import javax.swing.event.TableModelListener;
013    import java.util.HashMap;
014    
015    /**
016     * Author: azin azadi
017     */
018    public class ProEditor2NotifiableAttributeSetConnector implements AttributeListener, TableModelListener {
019        private GPropertyTableModel model;
020        GPropertyEditor editor;
021        private NotifiableAttributeSet atr;
022    
023        public ProEditor2NotifiableAttributeSetConnector(GPropertyEditor editor) {
024            this.model = editor.model;
025            this.editor = editor;
026            model.addTableModelListener(this);
027        }
028    
029        public void connect(NotifiableAttributeSet atr) {
030    //        if (!atr.equals(this.atr)) {
031            this.atr = atr;
032            load();
033    //        }
034        }
035    
036        private void load() {
037            if (this.atr != null)
038                this.atr.removeAttributeListener(this);
039            this.load(atr);
040            atr.addAttributeListener(this);
041            iChangedTheAtr = false;
042    
043        }
044    
045        public void attributeUpdated(String name, Object oldVal, Object newVal) {
046            //System.out.println(newVal);
047            if (newVal != null && !newVal.equals(oldVal)) {
048                iChangedTheAtr = true;
049                model.setValue(name, newVal);
050                editor.updateUI();
051            }
052        }
053    
054        boolean iChangedTheAtr = false;
055    
056        public void tableChanged(TableModelEvent e) {
057            if (atr != null && !iChangedTheAtr)
058                if (e.getType() == TableModelEvent.UPDATE) {
059                    int i = e.getFirstRow();
060                    if (i != -1) {
061                        String name = keyByRow.get(i);
062                        Object value = model.getValueAt(i, 1);
063                        atr.put(name, value);
064                    }
065                }
066            iChangedTheAtr = false;
067        }
068    
069        HashMap<Integer, String> keyByRow = new HashMap<Integer, String>();
070    
071        public void load(NotifiableAttributeSet x) {
072            if (x instanceof NotifiableAttributeSetView)
073                editor.target = (NotifiableAttributeSetView) x;
074            else {
075                editor.def.getView().setAttribute(x);
076                editor.target = editor.def;
077            }
078            reset();
079            AttributeSetView attributes = editor.target.getView();
080            editor.editor.setAtrView(attributes);
081            editor.renderer.setAtrView(attributes);
082            load(attributes);
083            editor.validate();
084            editor.updateUI();
085        }
086    
087        private void reset() {
088            editor.model.clear();
089            editor.editor.cancelCellEditing();
090            keyByRow.clear();
091        }
092    
093        private void load(AttributeSetView xatr) {
094            String[] _ = xatr.getNames();
095            int i = 0;
096            for (String key : _) {
097                if (xatr.isVisible(key)) {
098                    iChangedTheAtr = true;
099                    keyByRow.put(i++, key);
100                    editor.model.addData(xatr.getDisplayName(key), xatr.getAttribute().get(key));
101                }
102    
103            }
104        }
105    }