001    /*
002     * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003     *
004     * This file is part of the Java Data Mining Package (JDMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * JDMP 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     * JDMP 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 JDMP; 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.jdmp.gui.util;
025    
026    import java.awt.GridBagConstraints;
027    import java.awt.GridBagLayout;
028    import java.awt.Insets;
029    import java.awt.event.KeyEvent;
030    import java.awt.event.KeyListener;
031    import java.awt.event.MouseEvent;
032    import java.awt.event.MouseListener;
033    import java.util.List;
034    import java.util.logging.Level;
035    import java.util.logging.Logger;
036    
037    import javax.swing.BorderFactory;
038    import javax.swing.JComponent;
039    import javax.swing.JPanel;
040    import javax.swing.JPopupMenu;
041    import javax.swing.JScrollPane;
042    import javax.swing.JTable;
043    import javax.swing.UIManager;
044    import javax.swing.border.TitledBorder;
045    import javax.swing.event.ListSelectionEvent;
046    import javax.swing.event.ListSelectionListener;
047    import javax.swing.event.TableModelEvent;
048    import javax.swing.event.TableModelListener;
049    import javax.swing.table.TableModel;
050    
051    import org.jdmp.core.CoreObject;
052    import org.jdmp.core.algorithm.Algorithm;
053    import org.jdmp.core.variable.Variable;
054    import org.jdmp.gui.algorithm.AlgorithmGUIObject;
055    import org.jdmp.gui.algorithm.actions.AlgorithmActions;
056    import org.jdmp.gui.dataset.DataSetGUIObject;
057    import org.jdmp.gui.dataset.actions.DataSetActions;
058    import org.jdmp.gui.module.ModuleGUIObject;
059    import org.jdmp.gui.module.actions.ModuleActions;
060    import org.jdmp.gui.sample.SampleGUIObject;
061    import org.jdmp.gui.sample.actions.SampleActions;
062    import org.jdmp.gui.variable.VariableGUIObject;
063    import org.jdmp.gui.variable.actions.VariableActions;
064    import org.ujmp.core.interfaces.GUIObject;
065    import org.ujmp.core.interfaces.HasToolTip;
066    import org.ujmp.gui.util.FrameManager;
067    
068    public abstract class AbstractListPanel extends JPanel implements MouseListener, KeyListener,
069                    ListSelectionListener, TableModelListener {
070            private static final long serialVersionUID = -8449938064172061258L;
071    
072            protected static final Logger logger = Logger.getLogger(AbstractListPanel.class.getName());
073    
074            protected JTable jTable = null;
075    
076            protected GUIObject object = null;
077    
078            public final int ICONWIDTH = UIManager.getInt("Table.iconWidth");
079    
080            protected TableModel dataModel = null;
081    
082            protected JScrollPane scrollPane = null;
083    
084            public AbstractListPanel() {
085                    this.setLayout(new GridBagLayout());
086    
087                    setBorder(BorderFactory.createTitledBorder("Object List"));
088    
089                    jTable = new JTable(dataModel) {
090                            private static final long serialVersionUID = -1349144990029853301L;
091    
092                            @Override
093                            public String getToolTipText(MouseEvent event) {
094                                    try {
095                                            int row = rowAtPoint(event.getPoint());
096                                            if (row < jTable.getRowCount()) {
097                                                    Object o = jTable.getValueAt(row, 0);
098                                                    if (o instanceof HasToolTip) {
099                                                            return ((HasToolTip) o).getToolTipText();
100                                                    }
101                                            }
102                                    } catch (Exception e) {
103                                    }
104                                    return null;
105                            }
106                    };
107    
108                    jTable.addMouseListener(this);
109                    jTable.addKeyListener(this);
110                    jTable.getSelectionModel().addListSelectionListener(this);
111                    jTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
112                    jTable.setAutoCreateRowSorter(true);
113    
114                    int rowHeight = UIManager.getInt("Table.rowHeight");
115                    if (rowHeight < 1)
116                            rowHeight = 32;
117    
118                    jTable.setRowHeight(rowHeight);
119    
120                    scrollPane = new JScrollPane(jTable);
121                    this.addMouseListener(this);
122    
123                    this.add(jTable.getTableHeader(), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
124                                    GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
125                    this.add(scrollPane, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.EAST,
126                                    GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
127    
128            }
129    
130            @Override
131            public TitledBorder getBorder() {
132                    return (TitledBorder) super.getBorder();
133            }
134    
135            public void mouseClicked(MouseEvent e) {
136                    if (e.getSource() == this)
137                            mouseClickedOnObjectList(e);
138                    else
139                            mouseClickedOnObject(e);
140            }
141    
142            public void mouseClickedOnObjectList(MouseEvent e) {
143                    switch (e.getButton()) {
144                    case MouseEvent.BUTTON1:
145                            break;
146                    case MouseEvent.BUTTON2:
147                            break;
148                    case MouseEvent.BUTTON3:
149                            List<JComponent> actions = null;
150                            // switch (type) {
151                            // case IVARIABLES:
152                            // actions = new VariableListActions(this, (HasVariables) object);
153                            // break;
154                            // case IALGORITHMS:
155                            // actions = new AlgorithmListActions(this, (HasAlgorithms) object);
156                            // break;
157                            // case IDATASETS:
158                            // actions = new DataSetListActions(this, (HasDataSets) object);
159                            // break;
160                            // case IMODULES:
161                            // actions = new ModuleListActions(this, (HasModuleList) object);
162                            // break;
163                            // case ISAMPLES:
164                            // actions = new SampleListActions(this, (HasSampleList) object);
165                            // break;
166                            // }
167                            if (actions != null) {
168                                    JPopupMenu popup = new JPopupMenu();
169                                    for (JComponent jc : actions) {
170                                            popup.add(jc);
171                                    }
172                                    popup.show(this, e.getX(), e.getY());
173                            }
174                            break;
175                    }
176            }
177    
178            public void mouseClickedOnObject(MouseEvent e) {
179    
180                    GUIObject otemp = null;
181    
182                    int selectedRow = jTable.getSelectedRow();
183                    if (selectedRow >= 0) {
184                            Object o = dataModel.getValueAt(selectedRow, 0);
185                            if (o instanceof CoreObject) {
186                                    otemp = ((CoreObject) o).getGUIObject();
187                            } else {
188                                    otemp = (GUIObject) o;
189                            }
190                    }
191    
192                    switch (e.getButton()) {
193                    case MouseEvent.BUTTON1:
194                            if (e.getClickCount() == 2) {
195                                    if (otemp != null) {
196                                            FrameManager.showFrame(otemp);
197                                    }
198                            }
199                            break;
200                    case MouseEvent.BUTTON2:
201                            break;
202                    case MouseEvent.BUTTON3:
203                            int row = jTable.rowAtPoint(e.getPoint());
204                            jTable.setRowSelectionInterval(row, row);
205                            selectedRow = jTable.getSelectedRow();
206                            Object obj = dataModel.getValueAt(selectedRow, 0);
207                            if (obj instanceof CoreObject) {
208                                    otemp = ((CoreObject) obj).getGUIObject();
209                            } else {
210                                    otemp = (GUIObject) obj;
211                            }
212                            if (otemp != null) {
213                                    // Reference ref = otemp.getReference();
214                                    GUIObject o = otemp;
215    
216                                    // switch (type) {
217                                    // case IVARIABLES:
218                                    // o = Module.getInstance().getVariableForReference(ref);
219                                    // break;
220                                    // case IALGORITHMS:
221                                    // o = Module.getInstance().getAlgorithmForReference(ref);
222                                    // break;
223                                    // case IDATASETS:
224                                    // o = Module.getInstance().getDataSetForReference(ref);
225                                    // break;
226                                    // case IMODULES:
227                                    // o = otemp;
228                                    // break;
229                                    // case ISAMPLES:
230                                    // o = ((HasSamples) object).getSample(selectedRow);
231                                    // break;
232                                    // }
233                                    if (o != null) {
234                                            JPopupMenu popup = new JPopupMenu();
235                                            List<JComponent> objectActions = null;
236    
237                                            if (o instanceof AlgorithmGUIObject) {
238                                                    objectActions = new AlgorithmActions(this, (AlgorithmGUIObject) o);
239                                            } else if (o instanceof ModuleGUIObject) {
240                                                    objectActions = new ModuleActions(this, (ModuleGUIObject) o);
241                                            } else if (o instanceof SampleGUIObject) {
242                                                    objectActions = new SampleActions(this, (SampleGUIObject) o);
243                                            } else if (o instanceof VariableGUIObject) {
244                                                    objectActions = new VariableActions(this, (VariableGUIObject) o);
245                                            } else if (o instanceof DataSetGUIObject) {
246                                                    objectActions = new DataSetActions(this, (DataSetGUIObject) o);
247                                            }
248    
249                                            for (JComponent jc : objectActions) {
250                                                    popup.add(jc);
251                                            }
252                                            popup.show(jTable, e.getX(), e.getY());
253                                    }
254                            }
255                            break;
256                    }
257            }
258    
259            public void mousePressed(MouseEvent e) {
260            }
261    
262            public void mouseReleased(MouseEvent e) {
263            }
264    
265            public void mouseEntered(MouseEvent e) {
266            }
267    
268            public void mouseExited(MouseEvent e) {
269            }
270    
271            public void keyTyped(KeyEvent e) {
272                    try {
273                            GUIObject o = null;
274                            int row = jTable.getSelectedRow();
275                            if (row >= 0 && row < dataModel.getRowCount()) {
276                                    o = (GUIObject) dataModel.getValueAt(row, 0);
277                                    if (o != null) {
278                                            // ref = o.getReference();
279                                    }
280                            }
281    
282                            if (e.getKeyChar() == 127) {
283                                    // if (ref != null) {
284                                    // int response = JOptionPane.showConfirmDialog(null, "Do you
285                                    // really want to delete this Object?");
286                                    // // if (response == JOptionPane.OK_OPTION)
287                                    // // getWorkspace().removeObject(ref);
288                                    //
289                                    // if (jTable.getRowCount() > 0) {
290                                    // row = (row == jTable.getRowCount() ? row - 1 : row);
291                                    // jTable.setRowSelectionInterval(row, row);
292                                    // }
293                                    // }
294                            } else if (e.getKeyChar() == 3) {
295                                    // System.out.println("copy:" + ref);
296                                    // if (ref != null) {
297                                    // // Workspace.getInstance().copyToClipboard(ref);
298                                    // }
299                            } else
300                                    System.out.println("char entered in ObjectListPanel: " + e.getKeyChar());
301    
302                            if (o instanceof VariableGUIObject) {
303                                    Variable v = ((VariableGUIObject) o).getVariable();
304                                    switch (e.getKeyChar()) {
305                                    case 'g':
306                                            // v.fillGaussian();
307                                            break;
308                                    case 'u':
309                                            // v.fillUniform();
310                                            break;
311                                    default:
312                                            break;
313                                    }
314                            } else if (o instanceof Algorithm) {
315                                    Algorithm a = (Algorithm) o;
316                                    switch (e.getKeyChar()) {
317                                    case 'c':
318                                            a.calculate();
319                                            break;
320                                    default:
321                                            break;
322                                    }
323                            }
324                    } catch (Exception ex) {
325                            logger.log(Level.WARNING, "error processing key event", e);
326                    }
327            }
328    
329            public void keyPressed(KeyEvent e) {
330            }
331    
332            public void keyReleased(KeyEvent e) {
333            }
334    
335            public void valueChanged(ListSelectionEvent e) {
336            }
337    
338            public abstract void updateTitle();
339    
340            public void tableChanged(TableModelEvent e) {
341                    updateTitle();
342            }
343    }