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.matrix;
025
026 import java.awt.GridBagConstraints;
027 import java.awt.GridBagLayout;
028 import java.awt.Insets;
029 import java.awt.Rectangle;
030 import java.awt.event.KeyEvent;
031 import java.awt.event.MouseEvent;
032 import java.awt.event.MouseListener;
033
034 import javax.swing.Action;
035 import javax.swing.BorderFactory;
036 import javax.swing.JPanel;
037 import javax.swing.JPopupMenu;
038 import javax.swing.JScrollPane;
039 import javax.swing.JTable;
040 import javax.swing.JViewport;
041 import javax.swing.KeyStroke;
042 import javax.swing.ListSelectionModel;
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
050 import org.jdmp.core.variable.Variable;
051 import org.ujmp.core.Matrix;
052 import org.ujmp.core.exceptions.MatrixException;
053 import org.ujmp.core.interfaces.HasToolTip;
054 import org.ujmp.gui.MatrixGUIObject;
055 import org.ujmp.gui.frame.MatrixFrame;
056
057 public class MatrixListPanel extends JPanel implements MouseListener, ListSelectionListener,
058 TableModelListener {
059 private static final long serialVersionUID = -6599031713168986357L;
060
061 private JTable jTable = null;
062
063 private Variable variable = null;
064
065 private MatrixListTableModel dataModel = null;
066
067 private boolean scroll = true;
068
069 public MatrixListPanel(Variable variable) {
070 this.variable = variable;
071 this.setLayout(new GridBagLayout());
072
073 dataModel = new MatrixListTableModel(variable);
074
075 setBorder(BorderFactory.createTitledBorder("Matrices (" + dataModel.getRowCount() + ")"));
076
077 jTable = new JTable(dataModel) {
078 private static final long serialVersionUID = -1349144990029853301L;
079
080 @Override
081 public String getToolTipText(MouseEvent event) {
082 int row = rowAtPoint(event.getPoint());
083 if (row < jTable.getRowCount()) {
084 Object o = jTable.getValueAt(row, 0);
085 if (o instanceof HasToolTip) {
086 return ((HasToolTip) o).getToolTipText();
087 }
088 }
089 return null;
090 }
091 };
092
093 // jTable.setSelectionModel(dataModel.getRowSelectionModel());
094 // jTable.getSelectionModel().addListSelectionListener(this);
095 jTable.addMouseListener(this);
096 addMouseListener(this);
097 // jTable.addKeyListener(this);
098 jTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
099
100 dataModel.addTableModelListener(this);
101
102 int rowHeight = UIManager.getInt("Table.rowHeight");
103 if (rowHeight < 1)
104 rowHeight = 32;
105
106 jTable.setRowHeight(rowHeight);
107
108 jTable.getColumnModel().getColumn(0).setMinWidth(45);
109 jTable.getColumnModel().getColumn(0).setPreferredWidth(45);
110 jTable.setDefaultRenderer(MatrixGUIObject.class, new MatrixTableCellRenderer());
111
112 this.add(jTable.getTableHeader(), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
113 GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
114 this.add(new JScrollPane(jTable), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
115 GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
116
117 registerKeyboardActions();
118
119 }
120
121 private void registerKeyboardActions() {
122 // for (JComponent c : new MatrixActions(this, null, dataModel)) {
123 // if (c instanceof JMenuItem) {
124 // registerKeyboardAction(((JMenuItem) c).getAction());
125 // }
126 // }
127 }
128
129 private void registerKeyboardAction(Action a) {
130 KeyStroke keyStroke = (KeyStroke) a.getValue(Action.ACCELERATOR_KEY);
131 getActionMap().put(a.getValue(Action.NAME), a);
132 getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke, a.getValue(Action.NAME));
133 }
134
135 public void mouseClicked(MouseEvent e) {
136 try {
137 if (e.getSource() == this)
138 mouseClickedOnObjectList(e);
139 else
140 mouseClickedOnObject(e);
141 } catch (Exception e1) {
142 e1.printStackTrace();
143 }
144 }
145
146 public void mouseClickedOnObjectList(MouseEvent e) {
147 switch (e.getButton()) {
148 case MouseEvent.BUTTON1:
149 break;
150 case MouseEvent.BUTTON2:
151 break;
152 case MouseEvent.BUTTON3:
153 // JPopupMenu popup = null;
154 // popup = variable.getActions();
155 // if (popup != null)
156 // popup.show(this, e.getX(), e.getY());
157 // break;
158 }
159 }
160
161 public void mouseClickedOnObject(MouseEvent e) throws MatrixException {
162 switch (e.getButton()) {
163 case MouseEvent.BUTTON1:
164 if (e.getClickCount() == 2) {
165 int row = jTable.rowAtPoint(e.getPoint());
166 jTable.setRowSelectionInterval(row, row);
167 MatrixGUIObject m = (MatrixGUIObject) ((Matrix) dataModel.getValueAt(jTable
168 .getSelectedRow(), 0)).getGUIObject();
169 MatrixFrame mf = new MatrixFrame(m);
170 mf.setVisible(true);
171 }
172 break;
173 case MouseEvent.BUTTON2:
174 break;
175 case MouseEvent.BUTTON3:
176 int row = jTable.rowAtPoint(e.getPoint());
177 jTable.setRowSelectionInterval(row, row);
178 MatrixGUIObject m = (MatrixGUIObject) ((Matrix) dataModel.getValueAt(jTable
179 .getSelectedRow(), 0));
180 JPopupMenu popup = new JPopupMenu();
181
182 if (variable instanceof Variable) {
183 // for (JComponent c : new MatrixActions(this, m, variable)) {
184 // popup.add(c);
185 // }
186 } else {
187 // for (JComponent c : new MatrixActions(this, m, variable)) {
188 // popup.add(c);
189 // }
190 }
191
192 popup.show(jTable, e.getX(), e.getY());
193 break;
194 }
195 }
196
197 public void mousePressed(MouseEvent e) {
198 scroll = false;
199 }
200
201 public void mouseReleased(MouseEvent e) {
202 scroll = true;
203 }
204
205 public void mouseEntered(MouseEvent e) {
206 }
207
208 public void mouseExited(MouseEvent e) {
209 }
210
211 public void keyTyped(KeyEvent e) {
212 try {
213 MatrixGUIObject m = null;
214 int row = jTable.getSelectedRow();
215 if (row >= 0 && row < dataModel.getRowCount()) {
216 m = (MatrixGUIObject) dataModel.getValueAt(row, 0);
217 }
218
219 if (e.getKeyChar() == 127) {
220 // if (ref != null) {
221 // int response = JOptionPane.showConfirmDialog(null, "Do you
222 // really
223 // want to delete this Object?");
224 // // if (response == JOptionPane.OK_OPTION)
225 // // getWorkspace().removeObject(ref);
226 //
227 // if (jTable.getRowCount() > 0) {
228 // row = (row == jTable.getRowCount() ? row - 1 : row);
229 // jTable.setRowSelectionInterval(row, row);
230 // }
231 // }
232 } else if (e.getKeyChar() == 3) {
233 // System.out.println("copy:" + ref);
234 // if (ref != null) {
235 // Workspace.getInstance().copyToClipboard(ref);
236 // }
237 } else {
238 System.out.println("char entered in MatrixListPanel: " + e.getKeyChar());
239 }
240
241 if (variable instanceof Variable) {
242 if (m != null) {
243 switch (e.getKeyChar()) {
244 }
245 }
246 }
247 } catch (Exception ex) {
248 ex.printStackTrace();
249 }
250
251 }
252
253 public void valueChanged(ListSelectionEvent e) {
254 if (scroll && e.getValueIsAdjusting() == false) {
255 int minRow = jTable.getSelectionModel().getMinSelectionIndex();
256 int maxRow = jTable.getSelectionModel().getMaxSelectionIndex();
257 if (minRow == maxRow) {
258 JViewport viewport = (JViewport) jTable.getParent();
259 Rectangle rect = jTable.getCellRect(minRow, 0, true);
260 Rectangle viewRect = viewport.getViewRect();
261 rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);
262 int centerX = (viewRect.width - rect.width) / 2;
263 int centerY = (viewRect.height - rect.height) / 2;
264 if (rect.x < centerX) {
265 centerX = -centerX;
266 }
267 if (rect.y < centerY) {
268 centerY = -centerY;
269 }
270 rect.translate(centerX, centerY);
271 viewport.scrollRectToVisible(rect);
272 }
273 }
274 fireValueChanged(e); // for MatrixPaintPanel
275 }
276
277 public void fireValueChanged(ListSelectionEvent e) {
278 for (Object o : listenerList.getListenerList()) {
279 if (o instanceof ListSelectionListener) {
280 ((ListSelectionListener) o).valueChanged(e);
281 }
282 }
283 }
284
285 public void addListSelectionListener(ListSelectionListener l) {
286 listenerList.add(ListSelectionListener.class, l);
287 }
288
289 public void removeListSelectionListener(ListSelectionListener l) {
290 listenerList.add(ListSelectionListener.class, l);
291 }
292
293 public MatrixGUIObject getSelectedMatrix() {
294 ListSelectionModel lsm = jTable.getSelectionModel();
295 if (lsm.isSelectionEmpty() || lsm.getValueIsAdjusting())
296 return null;
297 int selectedRow = lsm.getMinSelectionIndex();
298 return (MatrixGUIObject) jTable.getModel().getValueAt(selectedRow,
299 MatrixListTableModel.MATRIXCOLUMN);
300 }
301
302 public void updateTitle() {
303 getBorder().setTitle("Matrices (" + jTable.getRowCount() + ")");
304 repaint(1000);
305 }
306
307 public void tableChanged(TableModelEvent e) {
308 updateTitle();
309 }
310
311 @Override
312 public TitledBorder getBorder() {
313 return (TitledBorder) super.getBorder();
314 }
315
316 }