KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > AppsAction


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import java.net.*;
19 import javax.swing.*;
20
21 import org.compiere.util.*;
22 import org.compiere.swing.*;
23
24 /**
25  * Application Action.
26  * <br>
27  * Creates Action with MenuItem and Button
28  * The ActionCommand is translated for display
29  * If translated text contains &, the next character is the Mnemonic
30  *
31  * @author Jorg Janke
32  * @version $Id: AppsAction.java,v 1.11 2003/02/14 06:44:13 jjanke Exp $
33  */

34 public final class AppsAction extends AbstractAction
35 {
36     /**
37      * Application Action
38      *
39      * @param action base action command - used as AD_Message for Text and Icon name
40      * @param accelerator optional keystroke for accelerator
41      * @param toggle is toggle action (maintains state)
42      */

43     public AppsAction (String JavaDoc action, KeyStroke accelerator, boolean toggle)
44     {
45         super();
46         m_action = action;
47
48         // Data
49
String JavaDoc text = Msg.getMsg(Env.getCtx(), action);
50         int pos = text.indexOf("&");
51         if (pos != -1) // We have a nemonic
52
{
53             Character JavaDoc ch = new Character JavaDoc(text.toUpperCase().charAt(pos+1));
54             text = text.substring(0, pos) + text.substring(pos+1);
55             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(ch.hashCode()));
56         }
57         //
58
Icon small = getIcon(action, true);
59         Icon large = getIcon(action, false);
60         Icon smallPressed = null;
61         Icon largePressed = null;
62
63         m_toggle = toggle;
64         // ToggleIcons have the pressed name with X
65
if (toggle)
66         {
67             smallPressed = getIcon(action+"X", true);
68             if (smallPressed == null)
69                 smallPressed = small;
70             largePressed = getIcon(action+"X", false);
71             if (largePressed == null)
72                 largePressed = large;
73         }
74
75         // Attributes
76
putValue(Action.NAME, text); // Display
77
putValue(Action.SMALL_ICON, small); // Icon
78
putValue(Action.SHORT_DESCRIPTION, text); // Tooltip
79
putValue(Action.ACTION_COMMAND_KEY, m_action); // ActionCammand
80
putValue(Action.ACCELERATOR_KEY, accelerator); // KeyStroke
81
// putValue(Action.MNEMONIC_KEY, new Integer(0)); // Mnemonic
82
// putValue(Action.DEFAULT, text); // Not Used
83

84         // Create Button
85
if (toggle)
86         {
87             m_button = new CToggleButton(this);
88             m_button.setSelectedIcon(largePressed);
89         }
90         else
91             m_button = new CButton(this);
92         m_button.setName(action);
93         // Correcting Action items
94
if (large != null)
95         {
96             m_button.setIcon(large);
97             m_button.setText(null);
98         }
99         m_button.setActionCommand(m_action);
100         m_button.setMargin(BUTTON_INSETS);
101         m_button.setSize(BUTTON_SIZE);
102
103         // Create Menu
104
if (toggle)
105         {
106             m_menu = new JCheckBoxMenuItem(this);
107             m_menu.setSelectedIcon(smallPressed);
108         }
109         else
110             m_menu = new JMenuItem(this);
111         m_menu.setAccelerator(accelerator);
112         m_menu.setActionCommand(m_action);
113     } // Action
114

115     /** Button Size */
116     public static final Dimension BUTTON_SIZE = new Dimension(28,28);
117     /** Button Insets */
118     public static final Insets BUTTON_INSETS = new Insets(0, 0, 0, 0);
119     //
120
private AbstractButton m_button;
121     private JMenuItem m_menu;
122
123     private String JavaDoc m_action;
124     private ActionListener m_delegate = null;
125     private boolean m_toggle = false;
126     private boolean m_pressed = false;
127
128     /**
129      * Get Icon with name action
130      * @param name name
131      * @param small small
132      * @return Icon
133      */

134     private ImageIcon getIcon(String JavaDoc name, boolean small)
135     {
136         String JavaDoc fullName = name + (small ? "16.gif" : "24.gif");
137         return Env.getImageIcon(fullName);
138     } // getIcon
139

140     /**
141      * Get Name/ActionCommand
142      * @return ActionName
143      */

144     public String JavaDoc getName()
145     {
146         return m_action;
147     } // getName
148

149     /**
150      * Return Button
151      * @return Button
152      */

153     public AbstractButton getButton()
154     {
155         return m_button;
156     } // getButton
157

158     /**
159      * Return MenuItem
160      * @return MenuItem
161      */

162     public JMenuItem getMenuItem()
163     {
164         return m_menu;
165     } // getMenuItem
166

167     /**
168      * Set Delegate to receive the actionPerformed calls
169      * @param al listener
170      */

171     public void setDelegate(ActionListener al)
172     {
173         m_delegate = al;
174     } // setDelegate
175

176     /**
177      * Toggle
178      * @param pressed pressed
179      */

180     public void setPressed (boolean pressed)
181     {
182         if (!m_toggle)
183             return;
184         m_pressed = pressed;
185
186         // Set Button
187
m_button.setSelected(pressed);
188         // Set Menu
189
m_menu.setSelected(pressed);
190     } // setPressed
191

192     /**
193      * IsPressed
194      * @return true if pressed
195      */

196     public boolean isPressed()
197     {
198         return m_pressed;
199     } // isPressed
200

201     /**
202      * ActionListener
203      * @param e Event
204      */

205     public void actionPerformed(ActionEvent e)
206     {
207     // Log.trace(Log.l1_User, "AppsAction.actionPerformed", e.getActionCommand());
208
// Toggle Items
209
if (m_toggle)
210             setPressed(!m_pressed);
211         // Inform
212
if (m_delegate != null)
213             m_delegate.actionPerformed(e);
214     } // actionPerformed
215

216     /**
217      * Dispose
218      */

219     public void dispose()
220     {
221         m_button = null;
222         m_menu = null;
223     } // dispose
224

225     /**
226      * toString
227      * @return String Representation
228      */

229     public String JavaDoc toString()
230     {
231         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("AppsAction - ");
232         sb.append(m_action)
233             .append(" Acc=")
234             .append(getValue(Action.ACCELERATOR_KEY));
235         return sb.toString();
236     } // toString
237

238 } // AppsAction
239
Popular Tags