1   /*
2    *  WaitDialog.java
3    *
4    *  Copyright (c) 1998-2005, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Valentin Tablan, 12/07/2000
12   *
13   *  $Id: WaitDialog.java,v 1.8 2005/01/11 13:51:37 ian Exp $
14   */
15  
16  package gate.swing;
17  
18  import java.awt.*;
19  
20  import javax.swing.*;
21  
22  /**
23   * A small window used to show messages to the user during processing.
24   * This component is intended as a nicer alternative
25   * to a status bar/progress bar.
26   * The window has its own thread for updating the animated pictures displayed.
27   *
28   */
29  public class WaitDialog extends JWindow implements Runnable {
30  
31    /** Debug flag
32     */
33    private static final boolean DEBUG = false;
34  
35    /**    *
36     */
37    Box centerBox;
38  
39    /**    */
40    public WaitDialog(Frame frame, String title) {
41      super(frame);
42      //this.icon = new ImageIcon(ClassLoader.getSystemResource(
43      //            "gate/resources/img/working.gif"));
44      // use the cached version from MainFrame
45      this.icon = gate.gui.MainFrame.getIcon("working.gif");
46      this.frame = frame;
47      try  {
48        jbInit();
49        pack();
50      }
51      catch(Exception ex) {
52        ex.printStackTrace();
53      }
54    }
55  
56    /**
57     * Shows the window containing labels for the texts provided as attributes.
58     *
59     * @param texts
60     */
61    public synchronized void showDialog(String[] texts) {
62      centerBox.removeAll();
63  
64      for(int i =0; i < texts.length; i++){
65        centerBox.add(new JLabel(texts[i]));
66      }
67  
68      centerBox.validate();
69      pack();
70  /*
71      Point loc = frame.getLocation();
72      loc.move(frame.getSize().width - getSize().width / 2 ,
73               frame.getSize().height - getSize().height /2 );
74      setLocation(loc);
75  */
76      stop = false;
77      Thread thread = new Thread(Thread.currentThread().getThreadGroup(),
78                                 this,
79                                 "WaitDialog1");
80      thread.setPriority(Thread.MAX_PRIORITY);
81      thread.start();
82      setVisible(true);
83    }
84  
85    /**
86     * Shows the window containing the components provided as attributes.
87     *
88     * @param components
89     */
90    public synchronized void showDialog(Component[] components) {
91      centerBox.removeAll();
92      for(int i =0; i < components.length; i++){
93        centerBox.add(components[i]);
94      }
95      centerBox.validate();
96      pack();
97  /*
98      Point loc = frame.getLocation();
99      setLocation(loc.x + (frame.getSize().width - getSize().width) / 2 ,
100                 loc.y + (frame.getSize().height - getSize().height) /2);
101 */
102     stop = false;
103     Thread thread = new Thread(Thread.currentThread().getThreadGroup(),
104                                this,
105                                "WaitDialog2");
106     thread.setPriority(Thread.MAX_PRIORITY);
107     thread.start();
108     setVisible(true);
109   }
110 
111   /**    */
112   void jbInit() throws Exception {
113     JPanel centerPanel = new JPanel();
114     Container content = getContentPane();
115     centerBox = Box.createVerticalBox();
116     centerPanel.setLayout(borderLayout1);
117     //centerPanel.setBorder(new LineBorder(Color.darkGray, 2));
118 //    centerPanel.setBackground(Color.white);
119 //    centerBox.setBackground(Color.white);
120     picture = new JLabel(icon);
121     picture.setOpaque(false);
122     centerPanel.add(centerBox, BorderLayout.CENTER);
123     centerPanel.add(picture, BorderLayout.WEST);
124     centerPanel.add(Box.createVerticalStrut(5), BorderLayout.NORTH);
125     centerPanel.add(Box.createVerticalStrut(5), BorderLayout.SOUTH);
126     centerPanel.add(Box.createHorizontalStrut(8), BorderLayout.EAST);
127     getContentPane().add(centerPanel, BorderLayout.CENTER);
128     centerPanel.setOpaque(false);
129   }
130 
131   /**
132    * Hides the window.
133    *
134    */
135   public void goAway() {
136     stop = true;
137   }
138 
139   /**    *
140    */
141   public void run() {
142     while(!stop){
143       try{
144         Thread.sleep(300);
145         centerBox.validate();
146         pack();
147         /*
148         Point loc = frame.getLocation();
149         setLocation(loc.x + (frame.getSize().width - getSize().width) / 2 ,
150                     loc.y + (frame.getSize().height - getSize().height) /2);
151         */
152         picture.paintImmediately(picture.getVisibleRect());
153       }catch(InterruptedException ie){}
154     }
155     this.setVisible(false);
156   }
157 
158 
159   boolean stop = false;
160 
161   BorderLayout borderLayout1 = new BorderLayout();
162 
163   Frame frame;
164 
165   JLabel picture;
166 
167   Icon icon;
168 
169 } // class WaitDialog
170