KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > GUI > AwtGUI


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch.GUI;
25
26 import java.io.*;
27 import java.lang.reflect.*;
28
29 import javax.swing.*;
30 import java.awt.*;
31 import java.util.*;
32
33 import net.fenyo.gnetwatch.*;
34 import net.fenyo.gnetwatch.activities.*;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 /**
40  * This class creates JFrame instances outside of SWT, to allow use of Java2D API.
41  * @author Alexandre Fenyo
42  * @version $Id: AwtGUI.java,v 1.12 2007/03/03 00:38:19 fenyo Exp $
43  */

44
45 public class AwtGUI implements Runnable JavaDoc {
46   private static Log log = LogFactory.getLog(AwtGUI.class);
47
48   private final Config config;
49
50   private Thread JavaDoc repaint_thread;
51   private java.util.List JavaDoc<JFrame> frame_list = new ArrayList<JFrame>();
52
53   /**
54    * Constructor.
55    * @param config configuration.
56    */

57   // GUI thread
58
public AwtGUI(final Config config) {
59     this.config = config;
60   }
61
62   /**
63    * Removes an AWT frame.
64    * @param frame frame to remove.
65    * @return void.
66    */

67   // GUI thread
68
public void dropFrame(final JFrame frame) {
69     synchronized (frame_list) {
70       frame_list.remove(frame);
71     }
72     frame.dispose();
73   }
74
75   /**
76    * Creates a thread that will repaint each frame regularly.
77    * @param none.
78    * @return void.
79    */

80   // GUI thread
81
public void createAwtGUI() {
82     JFrame.setDefaultLookAndFeelDecorated(false);
83     createRepaintThread();
84   }
85
86   /**
87    * Creates a thread that will repaint each frame regularly.
88    * @param none.
89    * @return void.
90    */

91   // GUI thread
92
private void createRepaintThread() {
93     repaint_thread = new Thread JavaDoc(this, "Repaint Thread");
94     repaint_thread.start();
95   }
96
97   /**
98    * Adds a component to a frame and displays the frame.
99    * This method must be run from the AWT thread.
100    * @param frame frame.
101    * @param component component to add.
102    */

103   // AWT thread
104
private void _createFrame(final JFrame frame, final BasicComponent component) {
105     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
106     frame.getContentPane().add(component);
107
108     //Display the window.
109
frame.pack();
110     frame.setVisible(true);
111     frame.toFront();
112   }
113
114   /**
115    * Adds a component to a frame and displays the frame.
116    * This method can be run from any thread, and particularly the SWT thread.
117    * @param frame frame.
118    * @param component component to add.
119    */

120   // GUI thread
121
public JFrame createFrame(final String JavaDoc name, final BasicComponent component) throws InterruptedException JavaDoc, InvocationTargetException {
122     final JFrame frame = new JFrame(name);
123     frame.addWindowListener(component);
124     javax.swing.SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
125       public void run() {
126         _createFrame(frame, component);
127       }
128     });
129     synchronized (frame_list) {
130       frame_list.add(frame);
131     }
132     return frame;
133   }
134
135   /**
136    * Refreshes the frames regularly.
137    * @param none.
138    * @return void.
139    */

140   // repaint thread
141
public void run() {
142     while (!config.isEnd())
143       try {
144         Thread.sleep(20);
145         synchronized (frame_list) {
146           for (final Frame frame : frame_list) frame.repaint();
147         }
148       } catch (final InterruptedException JavaDoc ex) {
149         // this thread is interrupted when the application is terminated
150
}
151   }
152
153   /**
154    * Terminates the repaint thread and closes any frame.
155    * @param none.
156    * @return void.
157    * @throws InterruptedException exception.
158    */

159   // main thread
160
public void end() throws InterruptedException JavaDoc {
161     // terminate the repaint thread
162
repaint_thread.interrupt();
163     repaint_thread.join();
164
165     synchronized (frame_list) {
166       for (final Frame frame : frame_list) frame.dispose();
167     }
168   }
169 }
170
Popular Tags