KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > actions > TargetableActionTest


1 /*
2  * $Id: TargetableActionTest.java,v 1.2 2004/09/30 00:05:53 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.actions;
9
10 import java.awt.*;
11 import java.awt.event.*;
12
13 import java.util.List JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import javax.swing.*;
17 import javax.swing.event.*;
18 import javax.swing.table.*;
19
20 import org.jdesktop.swing.JXEditorPane;
21 import org.jdesktop.swing.JXTable;
22 import org.jdesktop.swing.JXTree;
23 import org.jdesktop.swing.JXTreeTable;
24
25 import junit.framework.TestCase;
26
27 /**
28  * A unit test for targetable actions.
29  */

30 public class TargetableActionTest extends TestCase {
31
32     public void testDummy() {
33     }
34
35     public static void main(String JavaDoc[] args) {
36         showActionMaps();
37         showUI();
38     }
39
40     /**
41      * These are cover methods for the same methods in ActionFactory.
42      */

43     public static TargetableAction createTargetableAction(String JavaDoc id, String JavaDoc name) {
44         return createTargetableAction(id, name, null);
45     }
46
47     public static TargetableAction createTargetableAction(String JavaDoc id, String JavaDoc name,
48                                                           String JavaDoc mnemonic) {
49         return createTargetableAction(id, name, mnemonic, false);
50     }
51
52     public static TargetableAction createTargetableAction(String JavaDoc id, String JavaDoc name,
53                                                           String JavaDoc mnemonic, boolean toggle) {
54         return ActionFactory.createTargetableAction(id, name, mnemonic, toggle, null);
55     }
56
57     public static TargetableAction createTargetableAction(String JavaDoc id, String JavaDoc name,
58                                                           String JavaDoc mnemonic, boolean toggle,
59                                                           String JavaDoc group) {
60         return ActionFactory.createTargetableAction(id, name, mnemonic, toggle, group);
61     }
62
63     public static void showUI() {
64         ActionManager manager = new ActionManager();
65         ActionManager.setInstance(manager);
66
67         // The action id must map to the action-ids in the component.
68
manager.addAction(createTargetableAction("cut-to-clipboard", "Cut", "C"));
69         manager.addAction(createTargetableAction("copy-to-clipboard", "Copy", "P"));
70         manager.addAction(createTargetableAction("paste-from-clipboard", "Paste", "T"));
71         manager.addAction(createTargetableAction("print", "Print", "P"));
72         manager.addAction(createTargetableAction("find", "Find", "F"));
73         manager.addAction(createTargetableAction("collapse-all", "Collapse", "l"));
74         manager.addAction(createTargetableAction("expand-all", "Expand", "x"));
75         // More toggle actions for a group
76
manager.addAction(createTargetableAction("left-justify", "Left", "L", true,
77                                                  "position-group"));
78         manager.addAction(createTargetableAction("center-justify", "Center", "C", true,
79                                                  "position-group"));
80         manager.addAction(createTargetableAction("right-justify", "Right", "R", true,
81                                                  "position-group"));
82
83         List JavaDoc list = new ArrayList JavaDoc();
84         list.add("cut-to-clipboard");
85         list.add("copy-to-clipboard");
86         list.add("paste-from-clipboard");
87         list.add(null);
88         list.add("left-justify");
89         list.add("center-justify");
90         list.add("right-justify");
91         list.add(null);
92         list.add("print");
93         list.add("find");
94         list.add("collapse-all");
95         list.add("expand-all");
96
97         // Use the factory to build components from lists.
98
ActionContainerFactory factory = manager.getFactory();
99         JToolBar toolbar = factory.createToolBar(list);
100
101         JPanel panel = new JPanel(new GridLayout(2,2, 5, 5));
102         JXEditorPane editor = new JXEditorPane("text/html", "This is an example of some text");
103         panel.add(editor);
104         panel.add(new JXEditorPane());
105         panel.add(createTabbedPane());
106         panel.add(createTree());
107
108         JFrame frame = new JFrame();
109         frame.getContentPane().add(toolbar, BorderLayout.NORTH);
110         frame.getContentPane().add(panel);
111         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
112         frame.pack();
113         frame.setVisible(true);
114     }
115
116     public static JComponent createTabbedPane() {
117         JTabbedPane pane = new JTabbedPane();
118         pane.add(createTable());
119         pane.add(createTable());
120         pane.add(createTable());
121         pane.add(createTable());
122
123
124         // XXX - this is a way to support setting a target manager.
125
// There are some bugs with this approach. Namely, there is no
126
// reliable way for the TargetManager to be reset when th
127
pane.addFocusListener(new FocusListener() {
128                 public void focusGained(FocusEvent evt) {
129                     // System.out.println("FocusGained: " + evt);
130
JTabbedPane pane = (JTabbedPane)evt.getSource();
131                     Component comp = pane.getSelectedComponent();
132                     if (comp instanceof Targetable) {
133                         TargetManager.getInstance().setTarget((Targetable)comp);
134                     }
135                 }
136
137                 public void focusLost(FocusEvent evt) {
138                     //System.out.println("FocusLost: " + evt);
139
if (!evt.isTemporary()) {
140                         TargetManager.getInstance().setTarget(null);
141                     }
142                 }
143             });
144
145         pane.addChangeListener(new ChangeListener() {
146                 public void stateChanged(ChangeEvent evt) {
147                     JTabbedPane pane = (JTabbedPane)evt.getSource();
148                     Component comp = pane.getSelectedComponent();
149                     if (comp instanceof Targetable) {
150                         TargetManager.getInstance().setTarget((Targetable)comp);
151                     }
152                 }
153             });
154
155
156         return pane;
157     }
158
159     public static int tableNum = 0;
160
161     public static JComponent createTable() {
162         TableModel dataModel = new AbstractTableModel() {
163                 public int getColumnCount() { return 4; }
164                 public int getRowCount() { return 4;}
165                 public Object JavaDoc getValueAt(int row, int col) { return new Integer JavaDoc(row*col); }
166                 public boolean isCellEditable(int row, int col) { return true; }
167             };
168         JXTable table = new JXTable(dataModel);
169         table.setName("Table: " + tableNum++);
170
171         return table;
172     }
173
174     public static JComponent createTree() {
175         JTree jtree = new JTree();
176         JXTree tree = new JXTree(jtree.getModel());
177         tree.setEditable(true);
178
179         // Stuff the action map.
180
ActionMap map = tree.getActionMap();
181         Action action = new MyAction("print", "Print on JTree");
182         map.put(action.getValue(Action.NAME), action);
183         action = new MyAction("find", "Find on JTree");
184         map.put(action.getValue(Action.NAME), action);
185
186         return tree;
187     }
188
189     /**
190      * Simple action which displays a string when invoked.
191      */

192     public static class MyAction extends AbstractAction {
193
194         private String JavaDoc description;
195
196         public MyAction(String JavaDoc name, String JavaDoc desc) {
197             super(name);
198             this.description = desc;
199         }
200
201         public void actionPerformed(ActionEvent evt) {
202             System.out.println(getValue(Action.NAME) + " has been invoked: " + description);
203         }
204     }
205
206
207     public static void showActionMaps() {
208         System.out.println("\nActionMap keys for JXTable\n===================");
209         showActionMap(new JXTable());
210
211         System.out.println("\nActionMap keys for JXEditorPane\n==================");
212         showActionMap(new JXEditorPane());
213
214         System.out.println("\nActionMap keys for an HTML JXEditorPane\n==================");
215         showActionMap(new JXEditorPane("text/html", ""));
216
217         System.out.println("\nActionMap keys for JXTree\n====================");
218         showActionMap(new JXTree());
219
220         System.out.println("\nActionMap keys for JXTreeTable\n====================");
221         showActionMap(new JXTreeTable());
222     }
223
224     public static void showActionMap(JComponent comp) {
225         ActionMap map = comp.getActionMap();
226         Object JavaDoc[] keys = map.allKeys();
227
228         if (keys != null) {
229             for (int i = 0; i < keys.length; i++) {
230                 System.out.println(keys[i]);
231             }
232         }
233     }
234 }
235
Popular Tags