Debian Codegraph @

src/java/org/astrogrid/samp/gui/SampThread.java

raw
1 package org.astrogrid.samp.gui;
2
3 import java.awt.Component;
4 import java.util.EventObject;
5 import javax.swing.SwingUtilities;
6 import org.astrogrid.samp.client.SampException;
7
8 /**
9 * Helper class for performing a SAMP operation from within the
10 * Event Dispatch Thread.
11 * You must implement the {@link #runSamp} method to do the work;
12 * any resulting SampException will be suitably displayed to the user.
13 *
14 * @author Mark Taylor
15 * @since 27 Jul 2011
16 */
17 abstract class SampThread extends Thread {
18
19 private final Component parent_;
20 private final String errTitle_;
21 private final String errText_;
22
23 /**
24 * Constructs a SampThread given a parent component.
25 * Arguments are required for posting an error if one occurs.
26 *
27 * @param parent parent component
28 * @param errTitle title of error window if one is needed
29 * @param errText text of error messsage if one is needed
30 */
31 public SampThread( Component parent, String errTitle, String errText ) {
32 super( "SAMP call" );
33 parent_ = parent;
34 errTitle_ = errTitle;
35 errText_ = errText;
36 }
37
38 /**
39 * Constructs a SampThread given an event object with a source which
40 * presumably corresponds to a parent component.
41 *
42 * @param evt triggering event
43 * @param errTitle title of error window if one is needed
44 * @param errText text of error messsage if one is needed
45 */
46 public SampThread( EventObject evt, String errTitle, String errText ) {
47 this( evt.getSource() instanceof Component ? (Component) evt.getSource()
48 : null,
49 errTitle, errText );
50 }
51
52 /**
53 * Called from the {@link #run} method.
54 */
55 protected abstract void sampRun() throws SampException;
56
57 public void run() {
58 try {
59 sampRun();
60 }
61 catch ( final SampException e ) {
62 SwingUtilities.invokeLater( new Runnable() {
63 public void run() {
64 ErrorDialog.showError( parent_, errTitle_, errText_, e );
65 }
66 } );
67 }
68 }
69 }