KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.print.*;
19 import javax.swing.*;
20 import java.util.*;
21
22 import org.compiere.*;
23 import org.compiere.util.*;
24 import org.compiere.plaf.*;
25 import org.compiere.swing.*;
26 import org.compiere.db.*;
27
28 /**
29  * Dialog Windows
30  *
31  * @author Jorg Janke
32  * @version $Id: ADialogDialog.java,v 1.19 2003/11/01 02:32:21 jjanke Exp $
33  */

34 public final class ADialogDialog extends CDialog implements ActionListener
35 {
36     /**
37      * Create Dialog Window for Frame
38      *
39      * @param frame
40      * @param title
41      * @param message
42      * @param messageType
43      */

44     public ADialogDialog(Frame frame, String JavaDoc title, String JavaDoc message, int messageType)
45     {
46         super (frame, title, frame != null);
47         common (message, messageType);
48         AEnv.showCenterWindow(frame, this);
49     } // ADialogDialog
50

51     /**
52      * Create Dialog Window for Dialog
53      *
54      * @param dialog
55      * @param title
56      * @param message
57      * @param messageType
58      */

59     public ADialogDialog(Dialog dialog, String JavaDoc title, String JavaDoc message, int messageType)
60     {
61         super (dialog, title, dialog != null);
62         common (message, messageType);
63         AEnv.showCenterWindow(dialog, this);
64     } // ADialogDialog
65

66     /**
67      * Common Init
68      * @param message
69      * @param messageType
70      */

71     private void common (String JavaDoc message, int messageType)
72     {
73     // Log.trace(Log.l3_Util, "ADialogDialog");
74
CompiereColor.setBackground(this);
75         try
76         {
77             setInfoMessage (message);
78             jbInit();
79             setInfoIcon (messageType);
80         }
81         catch(Exception JavaDoc ex)
82         {
83             Log.error("ADialogDialog.common - " + ex.getMessage());
84         }
85         // Default Button
86
this.getRootPane().setDefaultButton(confirmPanel.getOKButton());
87     } // common
88

89     /**
90      * Window Events - requestFocus
91      * @param e
92      */

93     protected void processWindowEvent(WindowEvent e)
94     {
95         super.processWindowEvent(e);
96         if (e.getID() == WindowEvent.WINDOW_OPENED)
97             confirmPanel.getOKButton().requestFocusInWindow();
98     } // processWindowEvent
99

100     /** Answer OK (0) */
101     public static int A_OK = 0;
102     /** Answer Cancel (1) */
103     public static int A_CANCEL = 1;
104
105     private int m_returnCode = A_CANCEL;
106
107     private static Icon i_inform = Env.getImageIcon("Inform32.gif");
108     private static Icon i_warn = Env.getImageIcon("Warn32.gif");
109     private static Icon i_question = Env.getImageIcon("Question32.gif");
110     private static Icon i_error = Env.getImageIcon("Error32.gif");
111
112     private JMenuBar menuBar = new JMenuBar();
113     private JMenu mFile = AEnv.getMenu("File");
114     private JMenuItem mEMail = new JMenuItem();
115     private JMenuItem mPrintScreen = new JMenuItem();
116     private JMenuItem mScreenShot = new JMenuItem();
117     private JMenuItem mEnd = new JMenuItem();
118     private JMenuItem mPreference = new JMenuItem();
119     private ConfirmPanel confirmPanel = new ConfirmPanel(true);
120     private CPanel westPanel = new CPanel();
121     private CLabel iconLabel = new CLabel();
122     private GridBagLayout westLayout = new GridBagLayout();
123     private CText info = new CText (3, 40);
124     private GridBagLayout infoLayout = new GridBagLayout();
125     private CPanel infoPanel = new CPanel();
126
127     /**
128      * Static Constructor
129      * @throws Exception
130      */

131     private void jbInit() throws Exception JavaDoc
132     {
133         this.setJMenuBar(menuBar);
134         //
135
mEMail.setIcon(Env.getImageIcon("SendMail16.gif"));
136         mEMail.setText(Msg.getMsg(Env.getCtx(), "EMail"));
137         mEMail.addActionListener(this);
138         mPrintScreen.setIcon(Env.getImageIcon("PrintScreen16.gif"));
139         mPrintScreen.setText(Msg.getMsg(Env.getCtx(), "PrintScreen"));
140         mPrintScreen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PRINTSCREEN, 0));
141         mPrintScreen.addActionListener(this);
142         mScreenShot.setIcon(Env.getImageIcon("ScreenShot16.gif"));
143         mScreenShot.setText(Msg.getMsg(Env.getCtx(), "ScreenShot"));
144         mScreenShot.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PRINTSCREEN, Event.SHIFT_MASK));
145         mScreenShot.addActionListener(this);
146         mPreference.setIcon(Env.getImageIcon("Preference16.gif"));
147         mPreference.setText(Msg.getMsg(Env.getCtx(), "Preference"));
148         mPreference.addActionListener(this);
149         mEnd.setIcon(Env.getImageIcon("End16.gif"));
150         mEnd.setText(Msg.getMsg(Env.getCtx(), "End"));
151         mEnd.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.ALT_MASK));
152         mEnd.addActionListener(this);
153         //
154
westPanel.setLayout(westLayout);
155         westPanel.setName("westPanel");
156         westPanel.setRequestFocusEnabled(false);
157         infoPanel.setLayout(infoLayout);
158         infoPanel.setName("infoPanel");
159         infoPanel.setRequestFocusEnabled(false);
160         this.getContentPane().add(confirmPanel, BorderLayout.SOUTH);
161         this.getContentPane().add(westPanel, BorderLayout.WEST);
162         westPanel.add(iconLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
163             ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 10, 10, 10), 0, 0));
164         this.getContentPane().add(infoPanel, BorderLayout.CENTER);
165         infoPanel.add(info, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0
166             ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 10, 10, 10), 0, 0));
167         //
168
menuBar.add(mFile);
169         mFile.add(mPrintScreen);
170         mFile.add(mScreenShot);
171         mFile.addSeparator();
172         mFile.add(mEMail);
173         mFile.add(mPreference);
174         mFile.addSeparator();
175         mFile.add(mEnd);
176         //
177
confirmPanel.addActionListener(this);
178     } // jbinit
179

180     /**
181      * Calculate Size
182      * @param message
183      */

184     private void setInfoMessage(String JavaDoc message)
185     {
186         int rows = 0;
187         int cols = 0;
188
189         StringTokenizer st = new StringTokenizer(message, "\n", false);
190         while (st.hasMoreTokens())
191         {
192             rows++;
193             int length = st.nextToken().length();
194             cols = Math.max(cols, length);
195             while (length > 35)
196             {
197                 rows++;
198                 length -= 35;
199             }
200         }
201
202         rows = Math.max(2, rows);
203         rows = Math.min(10, rows);
204         cols = Math.max(30, cols);
205         cols = Math.min(40, cols);
206
207         info.setText(message);
208         info.setRows(rows);
209         info.setColumns(cols);
210         info.setRequestFocusEnabled(false);
211         info.setReadWrite(false);
212         info.setOpaque(false);
213         info.setLineWrap(true);
214         info.setWrapStyleWord(true);
215         info.setBorder(null);
216         //
217
info.setCaretPosition(0);
218     // Log.clear("Rows=" + info.getRows() + ", Cols=" + info.getColumns());
219
} // calculateSize
220

221     /*************************************************************************/
222
223     /**
224      * Set Info
225      * @param messageType
226      */

227     private void setInfoIcon (int messageType)
228     {
229         confirmPanel.getCancelButton().setVisible(false);
230         //
231
switch (messageType)
232         {
233             case JOptionPane.ERROR_MESSAGE:
234                 iconLabel.setIcon(i_error);
235                 break;
236             case JOptionPane.INFORMATION_MESSAGE:
237                 iconLabel.setIcon(i_inform);
238                 break;
239             case JOptionPane.QUESTION_MESSAGE:
240                 confirmPanel.getCancelButton().setVisible(true);
241                 iconLabel.setIcon(i_question);
242                 break;
243             case JOptionPane.WARNING_MESSAGE:
244                 iconLabel.setIcon(i_warn);
245                 break;
246
247             case JOptionPane.PLAIN_MESSAGE:
248             default:
249                 break;
250         } // switch
251
} // setInfo
252

253     /*************************************************************************/
254
255     /**
256      * ActionListener
257      * @param e
258      */

259     public void actionPerformed (ActionEvent e)
260     {
261         if (e.getActionCommand().equals(ConfirmPanel.A_OK))
262         {
263             m_returnCode = A_OK;
264             dispose();
265         }
266         else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL) || e.getSource() == mEnd)
267         {
268             m_returnCode = A_CANCEL;
269             dispose();
270         }
271         else if (e.getSource() == mPrintScreen)
272             printScreen();
273         else if (e.getSource() == mEMail)
274         {
275             String JavaDoc title = getTitle();
276             String JavaDoc text = info.getText();
277             dispose(); // otherwise locking
278
ADialog.createSupportEMail(this, title, text);
279         }
280         else if (e.getSource() == mPreference)
281         {
282             Preference p = new Preference(null, 0, true);
283             p.show();
284         }
285     } // actionPerformed
286

287     /**
288      * Get Return Code
289      * @return return code
290      */

291     public int getReturnCode()
292     {
293         return m_returnCode;
294     } // getReturnCode
295

296     /**
297      * PrintScreen
298      */

299     private void printScreen()
300     {
301         PrintScreenPainter.printScreen(this);
302     } // printScreen
303

304 } // ADialogDialog
305
Popular Tags