1 7 8 package org.jdesktop.swing.actions; 9 10 import java.awt.*; 11 import java.awt.event.*; 12 13 import java.util.List ; 14 import java.util.ArrayList ; 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 30 public class TargetableActionTest extends TestCase { 31 32 public void testDummy() { 33 } 34 35 public static void main(String [] args) { 36 showActionMaps(); 37 showUI(); 38 } 39 40 43 public static TargetableAction createTargetableAction(String id, String name) { 44 return createTargetableAction(id, name, null); 45 } 46 47 public static TargetableAction createTargetableAction(String id, String name, 48 String mnemonic) { 49 return createTargetableAction(id, name, mnemonic, false); 50 } 51 52 public static TargetableAction createTargetableAction(String id, String name, 53 String mnemonic, boolean toggle) { 54 return ActionFactory.createTargetableAction(id, name, mnemonic, toggle, null); 55 } 56 57 public static TargetableAction createTargetableAction(String id, String name, 58 String mnemonic, boolean toggle, 59 String 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 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 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 list = new ArrayList (); 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 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 pane.addFocusListener(new FocusListener() { 128 public void focusGained(FocusEvent evt) { 129 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 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 getValueAt(int row, int col) { return new Integer (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 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 192 public static class MyAction extends AbstractAction { 193 194 private String description; 195 196 public MyAction(String name, String 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 [] 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 |