Java Code Examples for javax.swing.SwingUtilities#isEventDispatchThread()

The following examples show how to use javax.swing.SwingUtilities#isEventDispatchThread() . 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: Info.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public String getDisplayName() {
    final NbGradleProject nb = project.getLookup().lookup(NbGradleProject.class);
    if (SwingUtilities.isEventDispatchThread() && !nb.isGradleProjectLoaded()) {
        return project.getProjectDirectory().getNameExt();
    }

    GradleBaseProject prj = GradleBaseProject.get(project);
    String ret;
    if (GradleSettings.getDefault().isDisplayDesctiption()
            && (prj.getDescription() != null)
            && !prj.getDescription().isEmpty()) {
        ret = prj.getDescription();
    } else {
        // The current implementation of Gradle's displayName is kind of ugly
        // and cannot be configured.
        //ret = prj.getDisplayName() != null ? prj.getDisplayName() : getName();
        ret = getName();
    }
    return ret;
}
 
Example 2
Source File: OperationPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void setBody (final String msg, final Set<UpdateElement> updateElements) {
    final List<UpdateElement> elements = new ArrayList<UpdateElement> (updateElements);
    
    Collections.sort(elements, new Comparator<UpdateElement>() {

        @Override
            public int compare(UpdateElement o1, UpdateElement o2) {
                return Collator.getInstance().compare(o1.getDisplayName(), o2.getDisplayName());
            }
        });
    
    if (SwingUtilities.isEventDispatchThread ()) {
        setBodyInEQ (msg, elements);
    } else {
        SwingUtilities.invokeLater (new Runnable () {
            @Override
            public void run () {
                setBodyInEQ (msg, elements);
            }
        });
    }
}
 
Example 3
Source File: ClientInfo.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean checkSaasState( final WsdlSaas saas ) {
    if ( saas.getState() == State.READY || saas.getState() == State.RETRIEVED ){
        saasWsdl = saas.getLocalWsdlFile();
        if ( SwingUtilities.isEventDispatchThread() ){
            saasTextField.setText( FileUtil.toFile(saasWsdl).getAbsolutePath());
        }
        else {
            SwingUtilities.invokeLater( new Runnable( ) {
                
                @Override
                public void run() {
                    saasTextField.setText( FileUtil.toFile(saasWsdl).getAbsolutePath());
                }
            });
        }
        return true;
    }
    return false;
}
 
Example 4
Source File: MethodChooser.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static void requestFocus(final JEditorPane editorPane) {
    if (!SwingUtilities.isEventDispatchThread()) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                requestFocus(editorPane);
            }
        });
        return ;
    }
    Container p = editorPane;
    while ((p = p.getParent()) != null) {
        if (p instanceof TopComponent) {
            ((TopComponent) p).requestActive();
            break;
        }
    }
    editorPane.requestFocusInWindow();
}
 
Example 5
Source File: SwingWorkerWithProgressIndicatorPanel.java    From PyramidShader with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Enable or disable button to cancel the operation.
 *
 * Can be called from any thread.
 *
 * @param cancellable If true, the button is enabled.
 */
@Override
public void setCancellable(final boolean cancellable) {
    if (progressPanel != null) {
        if (SwingUtilities.isEventDispatchThread()) {
            progressPanel.setCancellable(cancellable);
        } else {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    progressPanel.setCancellable(cancellable);
                }
            });
        }
    }
}
 
Example 6
Source File: GTreeSelectionModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public void setSelectionPaths(TreePath[] paths, EventOrigin origin) {
	if (!SwingUtilities.isEventDispatchThread()) {
		// this code will not work as written (with flags) unless in the event thread
		throw new AssertException("Model must be used from within the event dispatch thread!");
	}

	currentEventOrigin = origin;
	setSelectionPaths(paths);
	currentEventOrigin = EventOrigin.USER_GENERATED; // reset the origin for future use
}
 
Example 7
Source File: SwingWorker.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void firePropertyChange(final PropertyChangeEvent evt) {
    if (SwingUtilities.isEventDispatchThread()) {
        super.firePropertyChange(evt);
    } else {
        doSubmit.add(
            new Runnable() {
                public void run() {
                    SwingWorkerPropertyChangeSupport.this
                        .firePropertyChange(evt);
                }
            });
    }
}
 
Example 8
Source File: LWComponentPeer.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Paints the peer. Delegate the actual painting to Swing components.
 */
protected final void paintPeer(final Graphics g) {
    final D delegate = getDelegate();
    if (delegate != null) {
        if (!SwingUtilities.isEventDispatchThread()) {
            throw new InternalError("Painting must be done on EDT");
        }
        synchronized (getDelegateLock()) {
            // JComponent.print() is guaranteed to not affect the double buffer
            getDelegate().print(g);
        }
    }
}
 
Example 9
Source File: ComponentView.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the parent for a child view.
 * The parent calls this on the child to tell it who its
 * parent is, giving the view access to things like
 * the hosting Container.  The superclass behavior is
 * executed, followed by a call to createComponent if
 * the parent view parameter is non-null and a component
 * has not yet been created. The embedded components parent
 * is then set to the value returned by <code>getContainer</code>.
 * If the parent view parameter is null, this view is being
 * cleaned up, thus the component is removed from its parent.
 * <p>
 * The changing of the component hierarchy will
 * touch the component lock, which is the one thing
 * that is not safe from the View hierarchy.  Therefore,
 * this functionality is executed immediately if on the
 * event thread, or is queued on the event queue if
 * called from another thread (notification of change
 * from an asynchronous update).
 *
 * @param p the parent
 */
public void setParent(View p) {
    super.setParent(p);
    if (SwingUtilities.isEventDispatchThread()) {
        setComponentParent();
    } else {
        Runnable callSetComponentParent = new Runnable() {
            public void run() {
                Document doc = getDocument();
                try {
                    if (doc instanceof AbstractDocument) {
                        ((AbstractDocument)doc).readLock();
                    }
                    setComponentParent();
                    Container host = getContainer();
                    if (host != null) {
                        preferenceChanged(null, true, true);
                        host.repaint();
                    }
                } finally {
                    if (doc instanceof AbstractDocument) {
                        ((AbstractDocument)doc).readUnlock();
                    }
                }
            }
        };
        SwingUtilities.invokeLater(callSetComponentParent);
    }
}
 
Example 10
Source File: FrameOcrSample.java    From java-ocr-api with GNU Affero General Public License v3.0 5 votes vote down vote up
void setSplitPaneDividerLocation(final int location) {
    if(! SwingUtilities.isEventDispatchThread()) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                setSplitPaneDividerLocation(location);
            }
        });
        return;
    }
    splitPane.setDividerLocation(location);
}
 
Example 11
Source File: LWComponentPeer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Paints the peer. Delegate the actual painting to Swing components.
 */
protected final void paintPeer(final Graphics g) {
    final D delegate = getDelegate();
    if (delegate != null) {
        if (!SwingUtilities.isEventDispatchThread()) {
            throw new InternalError("Painting must be done on EDT");
        }
        synchronized (getDelegateLock()) {
            // JComponent.print() is guaranteed to not affect the double buffer
            getDelegate().print(g);
        }
    }
}
 
Example 12
Source File: LogUtil.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * In the calling thread, start log annotation with stub ID.
 *
 * @param stub the sheet/stub related to processing
 */
public static void start (SheetStub stub)
{
    start(stub.getBook());

    if (!SwingUtilities.isEventDispatchThread()) {
        MDC.put(SHEET, stub.getNum());
    }
}
 
Example 13
Source File: CompletionJListOperator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void runInAWT(Runnable r) {
    if (SwingUtilities.isEventDispatchThread()) {
        r.run();
    } else {
        try{
            SwingUtilities.invokeAndWait(r);
        }catch(Exception exc){
            throw new JemmyException("INVOKATION FAILED", exc);
        }
    }
}
 
Example 14
Source File: MultiViewCloneableTopComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void updateName() {
    // ensure to trigger update name from AWT -> #44012 - will ultimately trigger winsys.
    if (peer != null) {
        if (SwingUtilities.isEventDispatchThread() ) {
            peer.updateName();
        } else {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    peer.updateName();
                }
            });
        }
    }
}
 
Example 15
Source File: KeyboardDnd.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static void start( WindowDnDManager dndManager, TopComponentDraggable draggable, WindowDnDManager.ViewAccessor viewAccessor ) {
    if( !SwingUtilities.isEventDispatchThread() ) {
        throw new IllegalStateException( "This method must be called from EDT."); //NOI18N
    }
    if( null != currentDnd ) {
        currentDnd.stop( false );
        currentDnd = null;
    }
    currentDnd = new KeyboardDnd( dndManager, draggable, viewAccessor );
    currentDnd.start();
}
 
Example 16
Source File: SwingWorkerWithProgressIndicatorPanel.java    From PyramidShader with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set progress GUI status to indeterminate, i.e. the duration of the task
 * is unknown and calls to progress() will not update the progress
 * indicator.
 *
 * Can be called from any thread.
 *
 * @param indeterminate
 */
@Override
public void setIndeterminate(final boolean indeterminate) {
    if (SwingUtilities.isEventDispatchThread()) {
        progressPanel.setIndeterminate(indeterminate);
    } else {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                progressPanel.setIndeterminate(indeterminate);
            }
        });
    }
}
 
Example 17
Source File: IOFunctions.java    From SPIM_Registration with GNU General Public License v2.0 5 votes vote down vote up
public static void printlnSafe( final String string )
{
	if ( printIJLog )
	{
		if ( SwingUtilities.isEventDispatchThread() )
			IJ.log( string );
		else
			SwingUtilities.invokeLater( () -> IJ.log( string ) );
	}
	else
		System.out.println( string );
}
 
Example 18
Source File: CloneableEditorInitializer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("fallthrough")
public void run() {
    long now = System.currentTimeMillis();
    boolean success = false; // determine if phase ended with success
    try {
        switch (phase) {
            case DOCUMENT_OPEN:
                success = initDocument();
                break;
            case HANDLE_USER_QUESTION_EXCEPTION:
                success = handleUserQuestionExceptionInEDT();
                break;
            case ACTION_MAP:
                success = initActionMapInEDT();
                break;
            case INIT_KIT:
                success = initKit();
                break;
            case KIT_AND_DOCUMENT_TO_PANE:
                success = setKitAndDocumentToPaneInEDT();
                break;
            case CUSTOM_EDITOR_AND_DECORATIONS:
                success = initCustomEditorAndDecorationsInEDT();
                break;
            case FIRE_PANE_READY:
                success = firePaneReadyInEDT();
                break;
            case ANNOTATIONS:
                // Initialization of annotations should not affect the opening process success
                initAnnotations();
                success = true;
                break;
                
            default:
                throw new IllegalStateException("Wrong state: " + phase + " for " + ces);
        }

    } catch (RuntimeException ex) {
        Exceptions.printStackTrace(ex);
        // Re-throw the exception. The EDT clients may recieve the exception
        // if the current phase runs in EDT. If this would be a problem rethrowing
        // may be abandoned and replaced with 'return' only.
        throw ex;
    } finally {
        if (!success) {
            cancelInitialization(); // Cancel init - noitify possible EDT waiter(s)
            return;
        }
    }

    success = false;
    try {
        long howLong = System.currentTimeMillis() - now;
        if (TIMER.isLoggable(Level.FINE)) {
            String thread = SwingUtilities.isEventDispatchThread() ? "EDT" : "RP"; // NOI18N
            Document d = doc;
            Object who = d == null ? null : d.getProperty(Document.StreamDescriptionProperty);
            if (who == null) {
                who = ces.messageName();
            }
            TIMER.log(Level.FINE,
                    "Open Editor, phase " + phase + ", " + thread + " [ms]",
                    new Object[]{who, howLong});
        }
        success = true;
    } finally {
        if (!success) {
            cancelInitialization();
        }
    }

    success = false;
    try {
        nextPhase();
        success = true;
    } finally {
        if (!success) {
            cancelInitialization();
        }
    }
    // Note: finishInitialization() called as part of CUSTOM_EDITOR_AND_DECORATIONS phase
}
 
Example 19
Source File: MainFrame.java    From beast-mcmc with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void enableTaxaPanel() {

        final int index = Utils.getTabbedPaneComponentIndex(tabbedPane, TAXA_TAB_NAME);

        if (SwingUtilities.isEventDispatchThread()) {

            tabbedPane.setEnabledAt(index, true);

        } else {

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {

                    tabbedPane.setEnabledAt(index, true);

                }
            });
        }// END: edt check

    }
 
Example 20
Source File: StyleContext.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns a set no longer needed by the MutableAttributeSet implementation.
 * This is useful for operation under 1.1 where there are no weak
 * references.  This would typically be called by the finalize method
 * of the MutableAttributeSet implementation.
 * <p>
 * This method is thread safe, although most Swing methods
 * are not. Please see
 * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 * in Swing</A> for more information.
 *
 * @param a the set to reclaim
 */
public void reclaim(AttributeSet a) {
    if (SwingUtilities.isEventDispatchThread()) {
        attributesPool.size(); // force WeakHashMap to expunge stale entries
    }
    // if current thread is not event dispatching thread
    // do not bother with expunging stale entries.
}