| 1 | //  Copyright 2004-2007 Jean-Francois Poilpret | 
| 2 | // | 
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
| 4 | // you may not use this file except in compliance with the License. | 
| 5 | // You may obtain a copy of the License at | 
| 6 | // | 
| 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
| 8 | // | 
| 9 | // Unless required by applicable law or agreed to in writing, software | 
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 12 | // See the License for the specific language governing permissions and | 
| 13 | // limitations under the License. | 
| 14 |   | 
| 15 | package net.sourceforge.hiveboard.util; | 
| 16 |   | 
| 17 | import java.awt.EventQueue; | 
| 18 | import java.awt.Frame; | 
| 19 |   | 
| 20 | /** | 
| 21 |  * Utility class that avoids calling <code>System.exit()</code> from the AWT | 
| 22 |  * Event Dispatch Thread (not good practice, and generates problems when using | 
| 23 |  * abbot tools for GUI testing). | 
| 24 |  * @author Jean-Francois Poilpret | 
| 25 |  */ | 
| 26 | final public class ExitManager | 
| 27 | { | 
| 28 |         static private boolean                _exitCalled = false; | 
| 29 |   | 
| 30 |         private ExitManager() | 
| 31 |         { | 
| 32 |         } | 
| 33 |          | 
| 34 |         static public void        exit(int code) | 
| 35 |         { | 
| 36 |                 exit(code, false); | 
| 37 |         } | 
| 38 |          | 
| 39 |         static public void        exit(final int code, boolean silent) | 
| 40 |         { | 
| 41 |                 if (_exitCalled) | 
| 42 |                 { | 
| 43 |                         return; | 
| 44 |                 } | 
| 45 |                 _exitCalled = true; | 
| 46 |                 if (EventQueue.isDispatchThread()) | 
| 47 |                 { | 
| 48 |                         disposeFrames(); | 
| 49 |                 } | 
| 50 |                 else | 
| 51 |                 { | 
| 52 |                         // CSOFF: EmptyBlockCheck | 
| 53 |                         // CSOFF: IllegalCatchCheck | 
| 54 |                         try | 
| 55 |                         { | 
| 56 |                                 EventQueue.invokeAndWait(new Runnable() | 
| 57 |                                 { | 
| 58 |                                         public void        run() | 
| 59 |                                         { | 
| 60 |                                                 disposeFrames(); | 
| 61 |                                         } | 
| 62 |                                 }); | 
| 63 |                         } | 
| 64 |                         catch (Exception e) | 
| 65 |                         { | 
| 66 |                                 // Don't do anything | 
| 67 |                         } | 
| 68 |                         // CSON: IllegalCatchCheck | 
| 69 |                         // CSON: EmptyBlockCheck | 
| 70 |                 } | 
| 71 |                 if (!silent) | 
| 72 |                 { | 
| 73 |                         throw new ExitException(code); | 
| 74 |                 } | 
| 75 |         } | 
| 76 |   | 
| 77 |         static private void        disposeFrames() | 
| 78 |         { | 
| 79 |                 Frame[] frames = Frame.getFrames(); | 
| 80 |                 for (int i = 0; i < frames.length; i++) | 
| 81 |                 { | 
| 82 |                         if (frames[i].isDisplayable()) | 
| 83 |                         { | 
| 84 |                                 frames[i].dispose(); | 
| 85 |                         } | 
| 86 |                 } | 
| 87 |         } | 
| 88 | } |