Java Code Examples for org.openide.util.RequestProcessor#postRequest()

The following examples show how to use org.openide.util.RequestProcessor#postRequest() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestProcessorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Ensure that it is safe to call schedule() while the task is running
 * (should finish the task and run it again).
 */
public void testScheduleWhileRunning() throws Exception {
    class X implements Runnable {
        public synchronized void run() {
            try {
                if (cnt == 0) {
                    this.notify(); // #1
                    this.wait(9999); // #2
                    cnt++;
                } else {
                    cnt++;
                    this.notify(); // #3
                }
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
        }
        public int cnt = 0;
    }
    X x = new X();
    synchronized (x) {
        RequestProcessor.Task task = RequestProcessor.postRequest(x);
        x.wait(9999); // #1
        assertEquals(0, x.cnt);
        task.schedule(0);
        x.notify(); // #2
        x.wait(9999); // #3
        assertEquals(2, x.cnt);
    }
}
 
Example 2
Source File: RequestProcessorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Make sure it is safe to call waitFinished() on a task from within
 * a task listener.
 */
public void testWaitFinishedFromNotification() throws Exception {
    class X implements Runnable {
        private Task task;
        private int cnt;
        public synchronized Task start() {
            if (task == null) {
                task = RequestProcessor.postRequest(this);
            }
            return task;
        }
        public void run() {
            cnt++;
        }
        public int getCount() {
            return cnt;
        }
        public void block() {
            start().waitFinished();
        }
    }
    final X x = new X();
    final Object lock = "wait for task to finish";
    final boolean[] finished = new boolean[1];
    x.start().addTaskListener(new TaskListener() {
        public void taskFinished(Task t) {
            x.block();
            finished[0] = true;
            synchronized (lock) {
                lock.notify();
            }
        }
    });
    synchronized (lock) {
        lock.wait(5000);
    }
    assertTrue(finished[0]);
    assertEquals(1, x.getCount());
}
 
Example 3
Source File: RequestProcessorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Make sure that successfully canceled task is not performed.
 */
public void testCancel() throws Exception {
    class X implements Runnable {
        public boolean performed = false;
        public void run() {
            performed = true;
        }
    }
    
    X x = new X();
    final boolean[] finished = new boolean[1];
    finished[0] = false;
    
    // post task with some delay
    RequestProcessor.Task task = RequestProcessor.postRequest(x, 1000);
    task.addTaskListener(new TaskListener() {
        @Override
        public void taskFinished(Task t) {
            finished[0] = true;
        }
    });

    boolean canceled = task.cancel();
    assertTrue("Task is canceled now", canceled);
    assertTrue("Cancelling actually means finished", finished[0]);
    Thread.sleep(1500); // wait longer than task delay
    assertFalse("Task should not be performed", x.performed);
}
 
Example 4
Source File: TransactionView.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
    * Open the transaction nodes (i.e. first level children of the root).
    */
   void openTransactionNodes() {

// Post the request for later in case there are timing issues
// going on here. 

OpenTransactionNodesRequest req = new
    OpenTransactionNodesRequest();

RequestProcessor.Task t = 
    RequestProcessor.postRequest(req, 500); // wait a sec...
   }
 
Example 5
Source File: TreeEditorCookieImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Fire tree and status change. */
private void fireTreeAndStatus() {
    final int fireStatus = status;
    final int fireOldStatus = oldStatus;
    final Object fireTree = getDocumentRoot();

    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Firing tree status transition: " + oldStatus + "=>" + status); // NOI18N
    
    RequestProcessor.postRequest( new Runnable() {
        public void run() {
            pchs.firePropertyChange (PROP_STATUS, fireOldStatus, fireStatus);
            pchs.firePropertyChange (PROP_DOCUMENT_ROOT, null, fireTree);        
        }
    });
}
 
Example 6
Source File: CheckAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Check all selected nodes. */
protected void performAction (Node[] nodes) {

    if (nodes == null) return;
    
    RequestProcessor.postRequest(
           new CheckAction.RunAction (nodes));
}
 
Example 7
Source File: RequestProcessorTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** A test to check that objects are executed in the right order.
     */
    public void testOrder () throws Exception {
        final int[] count = new int[1];
        final String[] fail = new String[1];
        
        class X extends Object 
        implements Runnable, Comparable {
            public int order;
            
            public void run () {
                if (order != count[0]++) {
                    if (fail[0] == null) {
                        fail[0] = "Executing task " + order + " instead of " + count[0];
                    }
                }
            }
            
            public int compareTo (Object o) {
                X x = (X)o;
                
                return System.identityHashCode (x) - System.identityHashCode (this);
            }
            
            @Override
            public String toString () {
                return "O: " + order;
            }
        }
        
        // prepare the tasks 
        X[] arr = new X[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new X ();
        }
        
        // sort it
//        Arrays.sort (arr);
        
        for (int i = 0; i < arr.length; i++) {
            arr[i].order = i;
        }
        
        // execute the task as quickly as possible (only those with the same time
        // can have wrong order
        RequestProcessor.Task[] wait = new RequestProcessor.Task[arr.length];
        for (int i = 0; i < arr.length; i++) {
            wait[i] = RequestProcessor.postRequest (arr[i]);
        }
        
        // wait to all tasks to finish
        for (int i = 0; i < arr.length; i++) {
            wait[i].waitFinished ();
        }
        
        if (fail[0] != null) {
            fail (fail[0]);
        }
            
    }
 
Example 8
Source File: CheckDTDAction.java    From netbeans with Apache License 2.0 3 votes vote down vote up
/** Check all selected nodes. */
protected void performAction (Node[] nodes) {

    if (nodes == null) return;
    
    RequestProcessor.postRequest(
           new CheckDTDAction.RunAction (nodes));

}