1 package org.enhydra.shark.corbaclient; 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 myDialog.pack(); 128 129 Dimension screenSize = 130 GraphicsEnvironment 131 .getLocalGraphicsEnvironment() 132 .getDefaultScreenDevice() 133 .getDefaultConfiguration() 134 .getBounds() 135 .getSize(); 136 137 Dimension windowSize = myDialog.getPreferredSize(); 138 if (windowSize.width>screenSize.width-25) { 139 windowSize.width=screenSize.width-25; 140 } 141 if (windowSize.height>screenSize.height-25) { 142 windowSize.height=screenSize.height-25; 143 } 144 myDialog.setSize(windowSize); 145 myDialog.setLocation(screenSize.width/2 - (windowSize.width/2), 146 screenSize.height/2 - (windowSize.height/2)); 147 148 myDialog.setLocationRelativeTo(parent); 149 myDialog.setResizable(true); 150 } 151 152 153 public void showDialog () { 154 if (myDialog!=null) { 155 myDialog.setVisible(true); 156 } 157 } 158 159 162 public Action[] getActions() { 163 return defaultActions; 164 } 165 166 169 public Window getWindow () { 170 for (Container p = getParent(); p != null; p = p.getParent()) { 171 if (p instanceof Window) { 172 return (Window)p; 173 } 174 } 175 return null; 176 } 177 178 183 public Action getAction(String cmd) { 184 return (Action)commands.get(cmd); 185 } 186 187 188 protected abstract void createActions (); 189 190 194 protected abstract Component createCenterComponent(); 195 196 197 protected void applyChanges () {}; 198 199 200 protected void cancelChanges () {}; 201 202 } 203 | Popular Tags |