1 /*
2 * Copyright (c) 1998-2004 The Jgroup Team.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19 package jgroup.experiment.gui.views.helpers;
20
21 import java.awt.AWTEvent;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.awt.event.FocusAdapter;
25 import java.awt.event.FocusEvent;
26
27 import javax.swing.JFrame;
28 import javax.swing.JLabel;
29 import javax.swing.JPanel;
30 import javax.swing.text.JTextComponent;
31
32 /**
33 * @author Bjarte Svaeren
34 */
35 public class ChangeableActionListener extends FocusAdapter
36 implements ActionListener
37 {
38 ////////////////////////////////////////////////////////////////////////////////////////////
39 // Fields
40 ////////////////////////////////////////////////////////////////////////////////////////////
41
42 private JLabel connectedLabel;
43 private JPanel connectedPanel;
44 private String oldValue;
45
46 ////////////////////////////////////////////////////////////////////////////////////////////
47 // Constructor
48 ////////////////////////////////////////////////////////////////////////////////////////////
49
50 public ChangeableActionListener(JLabel label, JPanel panel)
51 {
52 connectedLabel = label;
53 connectedPanel = panel;
54 }
55
56
57 ////////////////////////////////////////////////////////////////////////////////////////////
58 // Implemented interface methods
59 ////////////////////////////////////////////////////////////////////////////////////////////
60
61 /* (non-Javadoc)
62 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
63 */
64 public void actionPerformed(ActionEvent e)
65 {
66 change(e);
67 }
68
69
70 /* (non-Javadoc)
71 * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
72 */
73 public void focusLost(FocusEvent e)
74 {
75 super.focusLost(e);
76 change(e);
77 }
78
79 /* (non-Javadoc)
80 * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
81 */
82 public void focusGained(FocusEvent e)
83 {
84 super.focusGained(e);
85 // Get the contents of the textarea/textfield,
86 // so we can see if it has changed when it loses focus.
87 oldValue = ((JTextComponent) e.getSource()).getText();
88 }
89
90
91 ////////////////////////////////////////////////////////////////////////////////////////////
92 // Private methods
93 ////////////////////////////////////////////////////////////////////////////////////////////
94
95 // If the value of the textfield/textarea has changed,
96 // use ChangeTracker to mark the component and its parent tab.
97 private void change(AWTEvent e)
98 {
99 String newValue = ((JTextComponent) e.getSource()).getText();
100 if( newValue.equals(oldValue) == false )
101 {
102 ChangeTracker.performedChange(connectedLabel, connectedPanel);
103 JTextComponent component = (JTextComponent) e.getSource();
104 JFrame topFrame = (JFrame) component.getTopLevelAncestor();
105 topFrame.getRootPane().revalidate();
106 }
107 }
108 }