Class ProcessingThread

  • All Implemented Interfaces:
    EventManager.Processor, Disposable, java.awt.event.ActionListener, java.lang.Runnable, java.util.EventListener

    public class ProcessingThread
    extends java.lang.Object
    implements java.lang.Runnable, EventManager.Processor, java.awt.event.ActionListener, Disposable
    A subclass of Thread that is capable of dealing with synchronization issues. It will pause all event dispatching related to specified doors which will be locked during processing. It includes helper methods for updating a progress bar and displaying messages and exceptions.
    • Field Detail

      • name

        protected final java.lang.String name
      • progress

        protected volatile float progress
      • progressInvoked

        protected volatile boolean progressInvoked
      • exception

        protected volatile java.lang.Exception exception
      • returnCode

        protected int returnCode
      • sync

        protected final java.lang.Object sync
    • Constructor Detail

      • ProcessingThread

        public ProcessingThread​(ProcessingThread.Client client,
                                ProgressComponent pc,
                                java.lang.String procName)
        Creates a new ProcessingThread. To start the process and starts processing. synchronization: must be called in the event thread
        Parameters:
        client - Interface whose method runProcessing() is called inside the new thread when it's started.
        pc - Component responsible for displaying progress bar etc.
        procName - Name for the thread and the process monitoring
    • Method Detail

      • putClientArg

        public void putClientArg​(java.lang.Object key,
                                 java.lang.Object value)
      • getClientArg

        public java.lang.Object getClientArg​(java.lang.Object key)
      • getClientMap

        public java.util.Map getClientMap()
      • getName

        public java.lang.String getName()
      • start

        public void start()
        Starts processing. Call this method only once!
      • sync

        public void sync()
        Puts the calling thread in wait mode until the processing is finished. This should be called in the event thread only if the process is about to quit very fast since it will otherwise block GUI updates. An example for its use is the prior request to cancel the process. If the amount of time till the end of the process is unknown, the preferred method to wait for the thread is to register a listener!
        See Also:
        addListener( ProcessingThread.Listener )
      • sync

        public void sync​(int timeout)
      • cancel

        public void cancel​(boolean doSync)
        Forwards the cancel request to the client.
        Parameters:
        doSync - if true, wait for the client to abort, otherwise return immediately
      • shouldCancel

        public static boolean shouldCancel()
      • isRunning

        public boolean isRunning()
      • getReturnCode

        public int getReturnCode()
        Returns the cient's return code.
        Returns:
        the return code (Client.DONE etc.) or -1 if the client had not been started
      • addListener

        public void addListener​(ProcessingThread.Listener l)
        Registers a listener to be notified when the process starts and terminates.
        Parameters:
        l - the listener to register
      • removeListener

        public void removeListener​(ProcessingThread.Listener l)
        Removes a listener from being notified when the process starts and terminates.
        Parameters:
        l - the listener to unregister
      • run

        public void run()
        The is the main method of a thread; it invokes the client's processRun method.
        Specified by:
        run in interface java.lang.Runnable
      • setException

        public void setException​(java.lang.Exception e)
        If the client is capable of catching an exception in its execution block, it should pass it to the pt calling this method.
        Parameters:
        e - exception which was thrown in the client's run method. when the thread stops it will display this error to the user.
      • getException

        public java.lang.Exception getException()
        Queries the last exception thrown in the run method.
        Returns:
        the most recent exception or null if no exception was thrown
      • setNextProgStop

        public static void setNextProgStop​(float p)
      • flushProgression

        public static void flushProgression()
      • actionPerformed

        public void actionPerformed​(java.awt.event.ActionEvent e)
        Specified by:
        actionPerformed in interface java.awt.event.ActionListener
      • processEvent

        public void processEvent​(BasicEvent e)
        Description copied from interface: EventManager.Processor
        Processes the next event in the queue. This gets called in the event thread. Usually implementing classes should loop through all listeners by calling elm.countListeners() and elm.getListener(), and invoke specific dispatching methods on these listeners.
        Specified by:
        processEvent in interface EventManager.Processor
      • getComponent

        public java.awt.Component getComponent()
      • setProgression

        public void setProgression​(float p)
        Called by the client to update the progress bar.
        Parameters:
        p - new progression between zero and one
      • resetProgression

        public void resetProgression()