KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.*;
17 import javax.swing.*;
18 import java.awt.*;
19
20 import org.compiere.util.*;
21 import org.compiere.plaf.*;
22
23 /**
24  * Glass Pane to display "waiting" and capture events while processing.
25  *
26  * @author Jorg Janke
27  * @version $Id: AGlassPane.java,v 1.5 2003/02/14 06:44:13 jjanke Exp $
28  */

29 public class AGlassPane extends JPanel implements MouseListener, ActionListener
30 {
31     /**
32      * Constructor
33      */

34     public AGlassPane()
35     {
36         this.setOpaque(false);
37         this.setVisible(false);
38         this.addMouseListener(this);
39     } // AGlassPane
40

41     /** The Image */
42     public static Image s_image = Env.getImage("C10030.gif");
43     /** The Message Font */
44     public static Font s_font = new Font("Dialog", 3, 14);
45     /** The Message Color */
46     public static Color s_color = CompierePLAF.getTextColor_OK();
47
48     /** Gap between components */
49     private static final int GAP = 4;
50
51     /** The Message */
52     private String JavaDoc m_message = Msg.getMsg(Env.getCtx(), "Processing");
53
54     /** Timer */
55     private Timer m_timer;
56     /** Value of timer */
57     private int m_timervalue = 0;
58     private int m_timermax = 0;
59
60     /**
61      * Set Message
62      * @param AD_Message to be translated - null resets to default message
63      */

64     public void setMessage(String JavaDoc AD_Message)
65     {
66         if (AD_Message == null)
67             m_message = Msg.getMsg(Env.getCtx(), "Processing");
68         else if (AD_Message.length() == 0)
69             m_message = AD_Message;
70         else
71             m_message = Msg.getMsg(Env.getCtx(), AD_Message);
72         if (isVisible())
73             repaint();
74     } // setMessage
75

76     /**
77      * Get Message
78      * @return displayed message
79      */

80     public String JavaDoc getMessage()
81     {
82         return m_message;
83     } // getMessage
84

85     /*************************************************************************/
86
87     /**
88      * Set and start Busy Counter if over 2 seconds
89      * @param time in seconds
90      */

91     public void setBusyTimer (int time)
92     {
93         Log.trace(Log.l4_Data, "AGlassPane.setBusyTimer - " + time);
94         // should we display a progress bar?
95
if (time < 2)
96         {
97             m_timermax = 0;
98             if (isVisible())
99                 repaint();
100             return;
101         }
102
103         m_timermax = time;
104         m_timervalue = 0;
105
106         // Start Timer
107
m_timer = new Timer (1000, this); // every second
108
m_timer.start();
109
110         if (!isVisible())
111             setVisible(true);
112         repaint();
113     } // setBusyTimer
114

115     /**
116      * ActionListener
117      * @param e
118      */

119     public void actionPerformed (ActionEvent e)
120     {
121         if (m_timermax > 0)
122         {
123             m_timervalue++;
124             if (m_timervalue > m_timermax)
125                 m_timervalue = 0;
126             repaint();
127         }
128     } // actionPerformed
129

130     /*************************************************************************/
131
132     /**
133      * Paint Component.
134      * <pre>
135      * image
136      * message
137      * progressBar
138      * </pre>
139      * @param g
140      */

141     public void paintComponent (Graphics g)
142     {
143         Dimension panelSize = getSize();
144         g.setColor(new Color(1f,1f,1f, 0.4f)); // .5 is a bit too light
145
g.fillRect(0,0, panelSize.width, panelSize.height);
146         //
147
g.setFont(s_font);
148         g.setColor(s_color);
149         FontMetrics fm = g.getFontMetrics();
150         Dimension messageSize = new Dimension (fm.stringWidth(m_message), fm.getAscent() + fm.getDescent());
151         Dimension imageSize = new Dimension (s_image.getWidth(this), s_image.getHeight(this));
152         Dimension progressSize = new Dimension(150, 15);
153     // System.out.println("Panel=" + panelSize + " - Message=" + messageSize + " - Image=" + imageSize + " - Progress=" + progressSize);
154

155         // Horizontal layout
156
int height = imageSize.height + GAP + messageSize.height + GAP + progressSize.height;
157         if (height > panelSize.height)
158         {
159             Log.error("AGlassPane.paintComponent - Panel too small - height=" + panelSize.height);
160             return;
161         }
162         int yImage = (panelSize.height/2) - (height/2);
163         int yMessage = yImage + imageSize.height + GAP + fm.getAscent();
164         int yProgress = yMessage + fm.getDescent() + GAP;
165     // System.out.println("yImage=" + yImage + " - yMessage=" + yMessage);
166

167         // Vertical layout
168
if (imageSize.width > panelSize.width || messageSize.width > panelSize.width)
169         {
170             Log.error("AGlassPane.paintComponent - Panel too small - width=" + panelSize.width);
171             return;
172         }
173         int xImage = (panelSize.width/2) - (imageSize.width/2);
174         int xMessage = (panelSize.width/2) - (messageSize.width/2);
175         int xProgress = (panelSize.width/2) - (progressSize.width/2);
176
177         g.drawImage(s_image, xImage, yImage, this);
178         g.drawString(m_message, xMessage, yMessage);
179         if (m_timermax > 0)
180         {
181             int pWidth = progressSize.width / m_timermax * m_timervalue;
182             g.setColor(CompierePLAF.getPrimary3());
183             g.fill3DRect(xProgress, yProgress, pWidth, progressSize.height, true);
184             g.setColor(CompierePLAF.getPrimary2());
185             g.draw3DRect(xProgress, yProgress, progressSize.width, progressSize.height, true);
186         }
187     } // paintComponent
188

189
190     /*************************************************************************/
191
192     /**
193      * Mouse Listener
194      * @param e
195      */

196     public void mouseClicked(MouseEvent e)
197     {
198         if (isVisible())
199             e.consume();
200     }
201     /**
202      * Mouse Listener
203      * @param e
204      */

205     public void mousePressed(MouseEvent e)
206     {
207         if (isVisible())
208             e.consume();
209     }
210     /**
211      * Mouse Listener
212      * @param e
213      */

214     public void mouseReleased(MouseEvent e)
215     {
216         if (isVisible())
217             e.consume();
218     }
219     /**
220      * Mouse Listener
221      * @param e
222      */

223     public void mouseEntered(MouseEvent e)
224     {
225         if (isVisible())
226             e.consume();
227     }
228     /**
229      * Mouse Listener
230      * @param e
231      */

232     public void mouseExited(MouseEvent e)
233     {
234         if (isVisible())
235             e.consume();
236     }
237
238 } // AGlassPane
239
Popular Tags