1 package org.enhydra.shark.swingclient; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.*; 6 7 import javax.swing.*; 8 9 17 public abstract class ActionPanel extends JPanel { 18 public static final String BUTTONS_SUFFIX="Buttons"; 19 public static final String TOOLBAR_SUFFIX="Toolbar"; 20 21 protected Hashtable commands; 22 23 26 protected Action[] defaultActions; 27 28 29 JToolBar toolbar; 30 33 protected JPanel buttonPanel; 34 35 36 protected JDialog myDialog; 37 38 protected JButton dialogOKButton; 39 protected JButton dialogCancelButton; 40 41 42 public ActionPanel () { 43 super(true); 44 } 45 46 47 protected void init () { 48 setBorder(BorderFactory.createEtchedBorder()); 49 setLayout(new BorderLayout()); 50 51 commands = new Hashtable(); 52 createActions(); 54 Action[] actions = getActions(); 55 if (actions!=null) { 56 57 for (int i = 0; i < actions.length; i++) { 58 Action a = actions[i]; 59 commands.put(a.getValue(Action.NAME), a); 60 } 61 62 toolbar = (JToolBar)BarFactory.createToolbar( 64 Utils.getUnqualifiedClassName(this.getClass())+TOOLBAR_SUFFIX,commands); 65 add(toolbar,BorderLayout.NORTH); 66 67 buttonPanel=(JPanel)BarFactory.createButtonPanel( 68 Utils.getUnqualifiedClassName(this.getClass())+BUTTONS_SUFFIX, 69 commands); 70 71 add(buttonPanel,BorderLayout.SOUTH); 72 73 } 74 add(createCenterComponent(),BorderLayout.CENTER); 75 } 76 77 78 protected void initDialog (Window parent,String title, 79 boolean hasButtons,boolean hasCancelButton) { 80 if (parent instanceof JDialog) { 81 myDialog=new JDialog((JDialog)parent,title,true); 82 } else { 83 myDialog=new JDialog((JFrame)parent,title,true); 84 } 85 86 JPanel btnPanel = new JPanel(); 87 dialogOKButton = (JButton)BarFactory.createButton("OK",null,true); 88 dialogOKButton.addActionListener(new ActionListener(){ 89 public void actionPerformed(ActionEvent evt){ 90 applyChanges(); 91 } 92 }); 93 btnPanel.add(dialogOKButton); 94 95 dialogCancelButton = (JButton)BarFactory.createButton("Cancel",null,true); 96 dialogCancelButton.addActionListener(new ActionListener(){ 97 public void actionPerformed(ActionEvent evt){ 98 cancelChanges(); 99 } 100 }); 101 if (hasCancelButton) { 102 btnPanel.add(dialogCancelButton); 103 } 104 105 Container cp=myDialog.getContentPane(); 106 cp.setLayout(new BorderLayout()); 107 cp.add(this,BorderLayout.CENTER); 108 if (hasButtons) { 109 cp.add(btnPanel,BorderLayout.SOUTH); 110 } 111 112 myDialog.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) 114 .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false),"Apply"); 115 myDialog.getRootPane().getActionMap().put("Apply", new AbstractAction() { 116 public void actionPerformed(ActionEvent e) { 117 applyChanges(); 118 } 119 }); 120 myDialog.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) 121 .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0,false),"Cancel"); 122 myDialog.getRootPane().getActionMap().put("Cancel", new AbstractAction() { 123 public void actionPerformed(ActionEvent e) { 124 myDialog.dispose(); 125 } 126 }); 127 128 myDialog.pack(); 129 130 Dimension screenSize = 131 GraphicsEnvironment 132 .getLocalGraphicsEnvironment() 133 .getDefaultScreenDevice() 134 .getDefaultConfiguration() 135 .getBounds() 136 .getSize(); 137 138 Dimension windowSize = myDialog.getPreferredSize(); 139 if (windowSize.width>screenSize.width-25) { 140 windowSize.width=screenSize.width-25; 141 } 142 if (windowSize.height>screenSize.height-25) { 143 windowSize.height=screenSize.height-25; 144 } 145 myDialog.setSize(windowSize); 146 myDialog.setLocation(screenSize.width/2 - (windowSize.width/2), 147 screenSize.height/2 - (windowSize.height/2)); 148 149 myDialog.setLocationRelativeTo(parent); 150 myDialog.setResizable(true); 151 } 152 153 154 public void showDialog () { 155 if (myDialog!=null) { 156 myDialog.setVisible(true); 157 } 158 } 159 160 163 public Action[] getActions() { 164 return defaultActions; 165 } 166 167 170 public Window getWindow () { 171 for (Container p = getParent(); p != null; p = p.getParent()) { 172 if (p instanceof Window) { 173 return (Window)p; 174 } 175 } 176 return null; 177 } 178 179 184 public Action getAction(String cmd) { 185 return (Action)commands.get(cmd); 186 } 187 188 189 protected abstract void createActions (); 190 191 195 protected abstract Component createCenterComponent(); 196 197 198 protected void applyChanges () {}; 199 200 201 protected void cancelChanges () {}; 202 203 } 204 | Popular Tags |