KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
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
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    /** Show dialog for editing panel. */
154    public void showDialog () {
155       if (myDialog!=null) {
156          myDialog.setVisible(true);
157       }
158    }
159
160    /**
161     * Fetch the list of actions supported by this panel.
162     */

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

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

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

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