Debian Codegraph @

src/dr/app/gui/table/TableEditorStopper.java

raw symbols SCIP
1 /*
2 * TableEditorStopper.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.gui.table;
27
28 import java.awt.Component;
29 import java.awt.event.FocusAdapter;
30 import java.awt.event.FocusEvent;
31 import java.beans.PropertyChangeEvent;
32 import java.beans.PropertyChangeListener;
33 import javax.swing.JTable;
34 import javax.swing.table.TableCellEditor;
35
36 /**
37 * This class is a workaround for a bug in Java (4503845)
38 * see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4503845
39 * Although the state appears as fixed, it does not yet work on 1.5
40 * (see the comments at the end on the previous page)
41 *
42 * The bug refers to tables with editing cells. When the user interacts
43 * with other UI element -or other cell in the table-, the editor component
44 * should receive a stopCellEditing call.
45 * This does not happen if the user selects a UI element outside the table.
46 *
47 * For example:
48 * - A dialog contains a table and a Cancel button that, before closing,
49 * verifies whether there are changes to save.
50 * -The user is modifying one cell and then presses the Cancel button
51 * -Then the cell editor does not receive an stopCellEditing call, so
52 * there seems to be no changes to save!
53 *
54 * A basic solution for this last example would be to call to
55 * table.getCellEditor().stopCellEditing() when reacting to events on the
56 * Cancel button, but this call should be made on every event handler for
57 * each UI element!
58 *
59 * This class works by listening to focus events when a cell editor is selected.
60 * If the focus is transfered to any place outside the table, it calls to
61 * stopCellEditing()
62 * Why is it not enough to check the focus on just the editor component?
63 * => Because the TableCellEditor could be composed of multiple Components
64 *
65 */
66 public class TableEditorStopper extends FocusAdapter implements PropertyChangeListener
67 {
68 public static void ensureEditingStopWhenTableLosesFocus(JTable table)
69 {
70 new TableEditorStopper(table);
71 }
72 private TableEditorStopper(JTable table)
73 {
74 this.table=table;
75 table.addPropertyChangeListener("tableCellEditor", this);
76 }
77 public void propertyChange(PropertyChangeEvent evt)
78 {
79 if (focused!=null)
80 {
81 focused.removeFocusListener(this);
82 }
83 focused = table.getEditorComponent();
84 if (focused!=null)
85 {
86 focused.addFocusListener(this);
87 }
88 }
89 public void focusLost(FocusEvent e)
90 {
91 if (focused!=null)
92 {
93 focused.removeFocusListener(this);
94 focused = e.getOppositeComponent();
95 if (table==focused || table.isAncestorOf(focused))
96 {
97 focused.addFocusListener(this);
98 }
99 else
100 {
101 focused=null;
102 TableCellEditor editor = table.getCellEditor();
103 if (editor!=null)
104 {
105 editor.stopCellEditing();
106 }
107 }
108 }
109 }
110 private Component focused;
111 private JTable table;
112 }