Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jpicedt/ui/util/PEProgressBar.java


1   /*  jPicEdt version 1.3.2, a picture editor for LaTeX.
2       Copyright (C) 1999-2002  Sylvain Reynal
3   
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8   
9       This program is distributed in the hope that it will be useful,
10      but WITHOUT ANY WARRANTY; without even the implied warranty of
11      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12      GNU General Public License for more details.
13  
14      You should have received a copy of the GNU General Public License
15      along with this program; if not, write to the Free Software
16      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  
18      Sylvain Reynal
19      Département de Physique
20      Ecole Nationale Supérieure de l'Electronique et de ses Applications (ENSEA)
21      6, avenue du Ponceau
22      95014 CERGY CEDEX
23      FRANCE
24  
25      Tel : 00 +33 130 736 245
26      Fax : 00 +33 130 736 667
27      e-mail : reynal@ensea.fr
28      jPicEdt web page : http://www.jpicedt.org
29  */
30  
31  package jpicedt.ui.util;
32  
33  
34  import javax.swing.*;
35  import javax.swing.border.*;
36  import java.awt.*;
37  import java.awt.geom.*;
38  import java.awt.event.*;
39  
40  
41  /**
42   * a progress bar that is (currently) used only during GUI initialization on start-up (hence messages are predefined so that 
43   * it makes sense during a GUI init, but probably not elsewhere).
44   */
45  public class PEProgressBar extends JWindow {
46  
47    private int maximum, progressValue;
48    private String progressMsg;
49    private String title;
50    private LogoPanel logoPanel;
51  
52    private static final String TITLE = jpicedt.Version.getVersion() + "-" + jpicedt.Version.getBuildDate();
53    private static final String INITIAL_MSG = "Loading, please wait..."; // [pending] i18n
54    
55    private static final String LOGO_URL = "/jpicedt/images/Logo-jpicedt.gif";
56    private static final int LOGO_WIDTH = 600;
57    private static final int LOGO_HEIGHT = 124;
58    private static final float MSG_X = 0.55f * LOGO_WIDTH;
59    private static final float MSG_Y = 0.5f * LOGO_HEIGHT;
60    private static final Color MSG_COLOUR = Color.red;
61    private static final float TITLE_X = 0.55f * LOGO_WIDTH;
62    private static final float TITLE_Y = 0.3f * LOGO_HEIGHT;
63    private static final Color TITLE_COLOUR = Color.black;
64  
65    /**
66     *
67     */
68    public PEProgressBar(int maximum){
69      this(maximum,null,null);
70    }
71    
72    /**
73     * @param maximum max nb of steps
74     * @param title not used anymore
75     * @param initMsg initial message
76     */
77    public PEProgressBar(int maximum, String title, String initMsg){
78  
79      this.maximum = maximum;
80      this.progressMsg = initMsg;
81      if (progressMsg==null) progressMsg = INITIAL_MSG;
82      this.title = title;
83      if (this.title==null) this.title = TITLE;
84      this.progressValue = 0;
85      setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
86      getContentPane().add(logoPanel=new LogoPanel(), BorderLayout.CENTER);
87      pack();
88      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
89      setLocation(d.width/2 - this.getWidth()/2, d.height/2 - this.getHeight()/2);
90      show();
91      try {SwingUtilities.invokeAndWait(logoPanel);}
92      catch (Exception ex){ex.printStackTrace();}
93    }
94  
95    /**
96     *
97     */
98    private class LogoPanel extends JPanel implements Runnable {
99      
100     Line2D.Double redLine;
101     ImageIcon icon;
102     
103     LogoPanel(){
104       this.setBorder(new EmptyBorder(10, 10, 10, 10));
105       this.setBackground(Color.white);
106       // Logo-jpicedt.gif : size = LOGO_WIDTH x LOGO_HEIGHT :
107       java.net.URL u = this.getClass().getResource(LOGO_URL);
108       if (u != null) icon = new ImageIcon(u);
109       this.setPreferredSize(new Dimension(LOGO_WIDTH,LOGO_HEIGHT));
110       redLine =  new Line2D.Double();
111       redLine.y1 = redLine.y2 = LOGO_HEIGHT * 0.6;
112       redLine.x1 = redLine.x2 = LOGO_WIDTH * 0.1;
113     }
114     
115     public void paintComponent(Graphics g){
116       super.paintComponent(g);
117       Graphics2D g2 = (Graphics2D)g;
118       // 1) background logo :
119       icon.paintIcon(this,g2,0,0);
120       // 2) title :
121       g2.setPaint(TITLE_COLOUR);
122       g2.drawString(title, TITLE_X, TITLE_Y);
123       // 3) message :
124       g2.setPaint(MSG_COLOUR);
125       g2.drawString(progressMsg, MSG_X, MSG_Y);
126       // 4) red horizontal line (aka progress bar) :
127       double ratio = ((double)progressValue)/((double)maximum);
128       GradientPaint gradient = new GradientPaint((float)redLine.x1,0,Color.white,LOGO_WIDTH,0,Color.red);
129       g2.setPaint(gradient);
130       g2.setStroke(new BasicStroke(5.0f));
131       redLine.x2 = redLine.x1 + 0.93 * ratio * (LOGO_WIDTH - redLine.x1);
132       //System.out.println("repaint : progressValue = " + progressValue + ", max = " + maximum);
133       if (ratio >0) g2.draw(redLine);
134     }
135     public void run(){
136       this.repaint();
137     }
138   }
139   
140   /**
141    * destroy this progress bar, disposing the hosting frame
142    */
143   public void destroy(){
144     //System.out.println("destroy...");
145     dispose();
146   }
147 
148   /**
149    * increment the state of this progress bar by one, displaying the given message at the same time
150    * @param message if null, left unchanged.
151    */
152   public void increment(String message){
153     if (message != null) this.progressMsg = message;
154     progressValue++;
155     if (progressValue > maximum) progressValue = maximum;
156     //System.out.println("increment : msg = " + message + ", val = " + progressValue + ", max = " + maximum); 
157     try {SwingUtilities.invokeAndWait(logoPanel);}
158     catch (Exception ex){ex.printStackTrace();}
159   }
160 
161   /**
162    * increment the state of this progress bar by one, leaving the message unchanged
163    */
164   public void increment(){
165     increment(null);
166   }
167   
168   /**
169    * open a dialog box with the given information message
170    */
171   public void confirmMsg(String message){
172     JOptionPane.showMessageDialog(this, message, "Information", JOptionPane.INFORMATION_MESSAGE); // [todo] to localize
173 
174   }
175 
176   /**
177    * open a dialog box with the given error message
178    */
179   public void recoverableError(String message){
180     JOptionPane.showMessageDialog(this, message, "Recoverable error", JOptionPane.ERROR_MESSAGE); // [todo] to localize
181   }
182 
183   /**
184    * open a modal dialog box with the given error message, wait for the user to close the dialog, then exit the application
185    */
186   public void fatalError(String message){
187     JOptionPane.showMessageDialog(this, message, "Fatal error", JOptionPane.ERROR_MESSAGE); // [todo] to localize
188     System.exit(0);
189   }
190 
191   ///////////////////////////////////////////////////////////
192   /**
193    * test
194    */
195   public static void main(String args[]){
196     Test test = new Test();
197   }
198   /**
199    * test class
200    */
201   public static class Test implements ActionListener {
202     PEProgressBar progressBar;
203     public Test(){
204       int numberOfProgressSteps = 20;
205       progressBar = new PEProgressBar(numberOfProgressSteps);
206       javax.swing.Timer timer = new javax.swing.Timer(400, this);
207       timer.start();
208     }
209     
210     public void actionPerformed(ActionEvent e){
211       progressBar.increment(Double.toString(Math.random()));
212     }
213     
214   }
215 }