01: package net.sourceforge.squirrel_sql.fw.gui.action;
02:
03: /*
04: * Copyright (C) 2001-2004 Colin Bell
05: * colbell@users.sourceforge.net
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this library; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: */
21: import java.awt.event.ActionEvent;
22: import java.beans.PropertyChangeEvent;
23: import java.beans.PropertyChangeListener;
24:
25: import javax.swing.JInternalFrame;
26:
27: import net.sourceforge.squirrel_sql.fw.util.StringManager;
28: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
29:
30: public class SelectInternalFrameAction extends BaseAction {
31: /** Internationalized strings for this class. */
32: private static final StringManager s_stringMgr = StringManagerFactory
33: .getStringManager(SelectInternalFrameAction.class);
34:
35: private static final String FRAME_PTR = "FRAME_PTR";
36:
37: private static final int MAX_TITLE_LENGTH = 50;
38:
39: private MyPropertyChangeListener _myLis = null;
40:
41: public SelectInternalFrameAction(JInternalFrame child) {
42: super (getTitle(child));
43: putValue(FRAME_PTR, child);
44: putValue(SHORT_DESCRIPTION, s_stringMgr
45: .getString("SelectInternalFrameAction.description"));
46: // TODO: This listener should be removed.
47: _myLis = new MyPropertyChangeListener();
48: child.addPropertyChangeListener(JInternalFrame.TITLE_PROPERTY,
49: _myLis);
50: }
51:
52: public void actionPerformed(ActionEvent evt) {
53: final JInternalFrame fr = getInternalFrame();
54: if (fr != null) {
55: new SelectInternalFrameCommand(fr).execute();
56: }
57: }
58:
59: private JInternalFrame getInternalFrame()
60: throws IllegalStateException {
61: final JInternalFrame fr = (JInternalFrame) getValue(FRAME_PTR);
62: if (fr == null) {
63: throw new IllegalStateException(
64: "No JInternalFrame associated with SelectInternalFrameAction");
65: }
66: return fr;
67: }
68:
69: private static String getTitle(JInternalFrame child) {
70: if (child == null) {
71: throw new IllegalArgumentException("JInternalFrame == null");
72: }
73:
74: String myTitle = child.getTitle();
75: if (myTitle.length() > MAX_TITLE_LENGTH) {
76: myTitle = myTitle.substring(0, MAX_TITLE_LENGTH) + "...";
77: }
78:
79: return myTitle;
80: }
81:
82: // This class keeps the Action title in synch with the internal frame
83: // title.
84: private class MyPropertyChangeListener implements
85: PropertyChangeListener {
86: public void propertyChange(PropertyChangeEvent evt) {
87: putValue(BaseAction.NAME, getTitle(getInternalFrame()));
88: }
89: }
90: }
|