KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > corbaclient > ActionPanel


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 /**
10  * The base abstract class for creating panel with menubar toolbar and central
11  * component, and for assigning actions to toolbar buttons. Also, this panel
12  * has it's dialog for editing.
13  *
14  * @author Sasa Bojanic
15  * @version 1.0
16  */

17 public abstract class ActionPanel extends JPanel {
18    public static final String JavaDoc BUTTONS_SUFFIX="Buttons";
19    public static final String JavaDoc TOOLBAR_SUFFIX="Toolbar";
20
21    protected Hashtable commands;
22
23    /**
24     * Actions defined.
25     */

26    protected Action[] defaultActions;
27
28    /** Toolbar. */
29    JToolBar toolbar;
30    /**
31    * Buttton panel.
32    */

33    protected JPanel buttonPanel;
34
35    /** The dialog to edit panel elements. */
36    protected JDialog myDialog;
37
38    protected JButton dialogOKButton;
39    protected JButton dialogCancelButton;
40
41    /** Creates instance of class. */
42    public ActionPanel () {
43       super(true);
44    }
45
46    /** Creates actions, toolbar and button panel. */
47    protected void init () {
48       setBorder(BorderFactory.createEtchedBorder());
49       setLayout(new BorderLayout());
50
51       commands = new Hashtable();
52       // fill actions
53
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          // create toolbar
63
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    /** Inits the dialog for editing the panel. */
78    protected void initDialog (Window parent,String JavaDoc 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       // assigning default actions for ESCAPE and ENTER keys
113
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    /** Show dialog for editing panel. */
153    public void showDialog () {
154       if (myDialog!=null) {
155          myDialog.setVisible(true);
156       }
157    }
158
159    /**
160     * Fetch the list of actions supported by this panel.
161     */

162    public Action[] getActions() {
163       return defaultActions;
164    }
165
166    /**
167    * Find the hosting window.
168    */

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    /**
179     * Method to get action corresponding to the given string.
180     * @param cmd String representation of editor's action.
181     * @return action specified by the string cmd.
182     **/

183    public Action getAction(String JavaDoc cmd) {
184       return (Action)commands.get(cmd);
185    }
186
187    /** Derived classes must override this method to put all needed actions. */
188    protected abstract void createActions ();
189
190    /**
191    * Derived classes must override this method to create the
192    * central componenet of action panel.
193    */

194    protected abstract Component createCenterComponent();
195
196    /** Called when OK button of dialog is pressed. */
197    protected void applyChanges () {};
198
199    /** Called when Cancel button of dialog is pressed. */
200    protected void cancelChanges () {};
201
202 }
203
Popular Tags