001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.gui.actions;
025    
026    import java.awt.event.ActionEvent;
027    import java.beans.PropertyChangeListener;
028    import java.io.Serializable;
029    import java.util.HashMap;
030    import java.util.concurrent.Callable;
031    import java.util.concurrent.Future;
032    
033    import javax.swing.Action;
034    import javax.swing.Icon;
035    import javax.swing.ImageIcon;
036    import javax.swing.JComponent;
037    import javax.swing.UIManager;
038    import javax.swing.event.SwingPropertyChangeSupport;
039    
040    import org.ujmp.core.Matrix;
041    import org.ujmp.core.interfaces.CoreObject;
042    import org.ujmp.core.interfaces.GUIObject;
043    import org.ujmp.gui.util.TaskQueue;
044    
045    public abstract class ObjectAction implements Action, Callable<Object>,
046                    Serializable {
047            private static final long serialVersionUID = -118767390543995981L;
048    
049            public static final int ROW = Matrix.ROW;
050    
051            public static final int COLUMN = Matrix.COLUMN;
052    
053            public static final int ALL = Matrix.ALL;
054    
055            private transient GUIObject object = null;
056    
057            private transient JComponent component = null;
058    
059            private transient Icon icon = null;
060    
061            private boolean enabled = true;
062    
063            protected transient SwingPropertyChangeSupport changeSupport;
064    
065            private transient final HashMap<String, Object> arrayTable = new HashMap<String, Object>();
066    
067            public ObjectAction(JComponent c, GUIObject o) {
068                    setGUIObject(o);
069                    this.component = c;
070                    icon = UIManager.getIcon("UJMP.icon." + getClass().getSimpleName());
071                    // putValue(Action.MNEMONIC_KEY, UIManager.get("UJMP.mnemonicKey." +
072                    // getClass().getName()));
073                    // putValue(Action.ACCELERATOR_KEY,
074                    // UIManager.get("UJMP.acceleratorKey." + getClass().getName()));
075            }
076    
077            public final void setComponent(JComponent component) {
078                    this.component = component;
079            }
080    
081            public final void setStatus(String status) {
082                    TaskQueue.setStatus(status);
083            }
084    
085            public final void setProgress(double progress) {
086                    TaskQueue.setProgress(progress);
087            }
088    
089            public final String toString() {
090                    return (String) getValue(Action.NAME) + " ("
091                                    + getValue(Action.SHORT_DESCRIPTION) + ")";
092            }
093    
094            public final GUIObject getGUIObject() {
095                    return object;
096            }
097    
098            public final CoreObject getCoreObject() {
099                    if (object == null) {
100                            return null;
101                    } else {
102                            return object.getCoreObject();
103                    }
104            }
105    
106            public final void setGUIObject(GUIObject o) {
107                    if (o != null) {
108                            this.object = o;
109                    }
110            }
111    
112            public final void actionPerformed(ActionEvent e) {
113                    try {
114                            call();
115                    } catch (Exception ex) {
116                            ex.printStackTrace();
117                    }
118            }
119    
120            public final Future<?> executeInBackground() {
121                    Future<?> f = TaskQueue.submit(this);
122                    return f;
123            }
124    
125            public abstract Object call();
126    
127            public final JComponent getComponent() {
128                    return component;
129            }
130    
131            public Object getValue(String key) {
132                    if (arrayTable == null) {
133                            return null;
134                    }
135    
136                    if ("enabled".equalsIgnoreCase(key)) {
137                            return enabled;
138                    } else if (key == Action.SMALL_ICON) {
139                            return icon;
140                    }
141    
142                    return arrayTable.get(key);
143            }
144    
145            public void setEnabled(boolean newValue) {
146                    this.enabled = newValue;
147            }
148    
149            public boolean isEnabled() {
150                    return enabled;
151            }
152    
153            protected void firePropertyChange(String propertyName, Object oldValue,
154                            Object newValue) {
155                    if (changeSupport == null
156                                    || (oldValue != null && newValue != null && oldValue
157                                                    .equals(newValue))) {
158                            return;
159                    }
160                    changeSupport.firePropertyChange(propertyName, oldValue, newValue);
161            }
162    
163            public void putValue(String key, Object newValue) {
164                    Object oldValue = null;
165                    if (key == "enabled") {
166                            if (newValue == null || !(newValue instanceof Boolean)) {
167                                    newValue = false;
168                            }
169                            oldValue = enabled;
170                            enabled = (Boolean) newValue;
171                    } else if (key == Action.SMALL_ICON) {
172                            oldValue = icon;
173                            icon = (ImageIcon) newValue;
174                    } else {
175                            if (arrayTable.containsKey(key))
176                                    oldValue = arrayTable.get(key);
177                            if (newValue == null) {
178                                    arrayTable.remove(key);
179                            } else {
180                                    arrayTable.put(key, newValue);
181                            }
182                    }
183                    firePropertyChange(key, oldValue, newValue);
184            }
185    
186            public synchronized void addPropertyChangeListener(
187                            PropertyChangeListener listener) {
188                    if (changeSupport == null) {
189                            changeSupport = new SwingPropertyChangeSupport(this);
190                    }
191                    changeSupport.addPropertyChangeListener(listener);
192            }
193    
194            public synchronized void removePropertyChangeListener(
195                            PropertyChangeListener listener) {
196                    if (changeSupport == null) {
197                            return;
198                    }
199                    changeSupport.removePropertyChangeListener(listener);
200            }
201    
202    }