Source Code Cross Referenced for SwingThread.java in  » Database-Client » ViennaSQL » uk » co » whisperingwind » framework » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » ViennaSQL » uk.co.whisperingwind.framework 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         ** $Id: SwingThread.java,v 1.1 2000/10/26 08:34:34 mrw Exp $
003:         **
004:         ** Mike Wilson, October 2000, mrw@whisperingwind.co.uk
005:         **
006:         ** (C) Copyright 2000, Mike Wilson, Reading, Berkshire, UK
007:         **
008:         ** This program is free software; you can redistribute it and/or modify
009:         ** it under the terms of the GNU General Public License as published by
010:         ** the Free Software Foundation; either version 2 of the License, or
011:         ** (at your option) any later version.
012:         ** 
013:         ** This program is distributed in the hope that it will be useful,
014:         ** but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016:         ** GNU General Public License for more details.
017:         ** 
018:         ** You should have received a copy of the GNU Library General
019:         ** Public License along with this library; if not, write to the
020:         ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
021:         ** Boston, MA  02111-1307  USA.
022:         */
023:
024:        package uk.co.whisperingwind.framework;
025:
026:        import javax.swing.SwingUtilities;
027:
028:        /**
029:         ** Inspired by, but <B>not</B> to identical to SwingWorker on Sun's
030:         ** web site. Runs one part, <CODE>construct</CODE> in a separate
031:         ** thread, then <CODE>finished</CODE> in the event dispatch thread.
032:         ** The <CODE>construct</CODE> method should not modify any Swing
033:         ** components as these are not thread safe. <CODE>finished</CODE>
034:         ** <B>can</B> safely modify Swing components as it is run in the event
035:         ** thread. <CODE>finished</CODE> does not run until
036:         ** <CODE>construct</CODE> is complete.
037:         **
038:         ** When they say Swing is not Thread safe, they <B>really</B> mean it.
039:         ** For example, poping up a dialog using JOptionPane from some thread
040:         ** other than event dispatch can hang the application. Modifying the
041:         ** TableModel in use for a visible JTable is really asking for it.
042:         */
043:
044:        public abstract class SwingThread {
045:            protected boolean stopped = false;
046:            private HelperThread helper;
047:
048:            /**
049:             ** The sub class must implement this method. It is run in a
050:             ** separate thread and must not modify any Swing component.
051:             */
052:
053:            public abstract void construct();
054:
055:            /**
056:             ** The sub class must implement this method. It is run in the
057:             ** event dispatch thread when <CODE>construct</CODE> is complete.
058:             */
059:
060:            public abstract void finished();
061:
062:            /**
063:             ** Create a SwingThread. Call <CODE>start</CODE> to start the
064:             ** thread.
065:             */
066:
067:            public SwingThread() {
068:                /**
069:                 ** This will be executed in the event dispatch thread when
070:                 ** doConstruct has completed.
071:                 */
072:
073:                final Runnable doFinished = new Runnable() {
074:                    public void run() {
075:                        finished();
076:                    }
077:                };
078:
079:                /**
080:                 ** This is executed in a separate thread. It calls the
081:                 ** abstract method <CODE>construct</CODE> which should not
082:                 ** modify any Swing components.
083:                 */
084:
085:                Runnable doConstruct = new Runnable() {
086:                    public void run() {
087:                        try {
088:                            construct();
089:                        } finally {
090:                            helper.clear();
091:                        }
092:
093:                        SwingUtilities.invokeLater(doFinished);
094:                    }
095:                };
096:
097:                helper = new HelperThread(new Thread(doConstruct));
098:            }
099:
100:            /**
101:             ** Returns true if the thread has been interrupted. The thread may
102:             ** not have yet completed.
103:             */
104:
105:            public synchronized boolean isInterrupted() {
106:                return stopped;
107:            }
108:
109:            /**
110:             ** This doesn't actually interrupt the thread. This class is used
111:             ** for methods which communicate with the database server. Some
112:             ** JDBC drivers (eg Oracle) don't like being interrupted and will
113:             ** fail in subsequent calls if they are. This relies on construct
114:             ** taking notice of the value of "stopped". Will not return
115:             ** until the helper thread finishes.
116:             */
117:
118:            public void interrupt() {
119:                Thread thread = helper.get();
120:
121:                if (thread != null) {
122:                    synchronized (this ) {
123:                        stopped = true;
124:                    }
125:
126:                    try {
127:                        thread.join();
128:                    } catch (InterruptedException ex) {
129:                        // Do nothing
130:                    } finally {
131:                        helper.clear();
132:                    }
133:                }
134:            }
135:
136:            /**
137:             ** Start the helper thread. This will call <CODE>construct</CODE>
138:             ** and, when the thread completes, <CODE>finished</CODE>.
139:             */
140:
141:            public void start() {
142:                Thread thread = helper.get();
143:
144:                if (thread != null)
145:                    thread.start();
146:            }
147:
148:            /**
149:             ** Wrapper class for the helper thread which executes the
150:             ** <CODE>construct</CODE> method.
151:             */
152:
153:            private static class HelperThread {
154:                private Thread thread;
155:
156:                public HelperThread(Thread thread) {
157:                    this .thread = thread;
158:                }
159:
160:                public synchronized void clear() {
161:                    thread = null;
162:                }
163:
164:                public Thread get() {
165:                    return thread;
166:                }
167:            }
168:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.