Example usage for javax.swing SwingUtilities isEventDispatchThread

List of usage examples for javax.swing SwingUtilities isEventDispatchThread

Introduction

In this page you can find the example usage for javax.swing SwingUtilities isEventDispatchThread.

Prototype

public static boolean isEventDispatchThread() 

Source Link

Document

Returns true if the current thread is an AWT event dispatching thread.

Usage

From source file:org.notebook.gui.widget.LookAndFeelSelector.java

/**
 * Sets the look and feel./*from  w w w  . ja va  2  s. c o  m*/
 * 
 * @param theme
 *            the new look and feel
 */
public void setLookAndFeel(String theme) {
    try {
        log.info("Updating skin '" + theme + "'");
        if (skins.containsKey(theme)) {
            UIManager.setLookAndFeel(skins.get(theme));
        } else {
            log.error("Not found skin '" + theme + "'");
            return;
        }

        /** fix font bug start */
        if (SwingUtilities.isEventDispatchThread()) {
            fixFontBug();
        } else {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    fixFontBug();
                }
            });
        }
    } catch (Exception e) {
        log.error(e.toString(), e);
    }

    /**
     * ??????
     * tiptool
     */
    if (!isDefaultLookAndFeel(theme)) {
        // Get border color
        try {
            Color c = SubstanceLookAndFeel.getCurrentSkin().getMainActiveColorScheme().getMidColor();
            UIManager.put("ToolTip.border", BorderFactory.createLineBorder(c));
            UIManager.put("ToolTip.background", new ColorUIResource(Color.WHITE));
            UIManager.put("ToolTip.foreground", new ColorUIResource(Color.BLACK));

            SubstanceSkin skin = SubstanceLookAndFeel.getCurrentSkin();
            SubstanceColorScheme scheme = skin.getMainActiveColorScheme();

            GuiUtils.putLookAndFeelColor("borderColor", scheme.getMidColor());
            GuiUtils.putLookAndFeelColor("lightColor", scheme.getLightColor());
            GuiUtils.putLookAndFeelColor("lightBackgroundFillColor", scheme.getLightBackgroundFillColor());
            GuiUtils.putLookAndFeelColor("darkColor", scheme.getDarkColor());
            GuiUtils.putLookAndFeelColor("backgroundFillColor", scheme.getBackgroundFillColor());
            GuiUtils.putLookAndFeelColor("lineColor", scheme.getLineColor());
            GuiUtils.putLookAndFeelColor("selectionForegroundColor", scheme.getSelectionForegroundColor());
            GuiUtils.putLookAndFeelColor("selectionBackgroundColor", scheme.getSelectionBackgroundColor());
            GuiUtils.putLookAndFeelColor("foregroundColor", scheme.getForegroundColor());
            GuiUtils.putLookAndFeelColor("focusRingColor", scheme.getFocusRingColor());

        } catch (Exception e) {
            log.info("This is not a SubstanceLookAndFeel skin.");
        }

        UIManager.put(LafWidget.ANIMATION_KIND, LafConstants.AnimationKind.NONE);
        UIManager.put(SubstanceLookAndFeel.TABBED_PANE_CONTENT_BORDER_KIND,
                SubstanceConstants.TabContentPaneBorderKind.SINGLE_FULL);

        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
    }
}

From source file:org.omegat.gui.scripting.ScriptRunner.java

private static void invokeGuiScript(Invocable engine) throws ScriptException {
    Runnable invoke = () -> {/*  www . ja v  a  2  s .c o  m*/
        try {
            engine.invokeFunction(SCRIPT_GUI_FUNCTION_NAME);
        } catch (NoSuchMethodException e) {
            // No GUI invocation defined
        } catch (ScriptException e) {
            throw new RuntimeException(e);
        }
    };
    if (SwingUtilities.isEventDispatchThread()) {
        invoke.run();
    } else {
        try {
            SwingUtilities.invokeAndWait(invoke);
        } catch (InvocationTargetException e) {
            // The original cause is double-wrapped at this point
            if (e.getCause().getCause() instanceof ScriptException) {
                throw (ScriptException) e.getCause().getCause();
            } else {
                Log.log(e);
            }
        } catch (InterruptedException e) {
            Log.log(e);
        }
    }
}

From source file:org.openstreetmap.josm.plugins.mapillary.utils.MapillaryUtils.java

/**
 * Zooms to fit all the given {@link MapillaryAbstractImage} objects.
 *
 * @param images The images your are zooming to.
 * @param select Whether the added images must be selected or not.
 *///  www  .j ava2s  . c o  m
public static void showPictures(final Set<MapillaryAbstractImage> images, final boolean select) {
    if (!SwingUtilities.isEventDispatchThread()) {
        SwingUtilities.invokeLater(() -> showPictures(images, select));
    } else {
        Bounds zoomBounds;
        if (images.isEmpty()) {
            zoomBounds = new Bounds(new LatLon(0, 0));
        } else {
            zoomBounds = new Bounds(images.iterator().next().getMovingLatLon());
            for (MapillaryAbstractImage img : images) {
                zoomBounds.extend(img.getMovingLatLon());
            }
        }

        // The zoom rectangle must have a minimum size.
        double latExtent = Math.max(zoomBounds.getMaxLat() - zoomBounds.getMinLat(), MIN_ZOOM_SQUARE_SIDE);
        double lonExtent = Math.max(zoomBounds.getMaxLon() - zoomBounds.getMinLon(), MIN_ZOOM_SQUARE_SIDE);
        zoomBounds = new Bounds(zoomBounds.getCenter(), latExtent, lonExtent);

        Main.map.mapView.zoomTo(zoomBounds);
        MapillaryLayer.getInstance().getData().setSelectedImage(null);
        if (select)
            MapillaryLayer.getInstance().getData().addMultiSelectedImage(images);
        if (Main.main != null)
            MapillaryData.dataUpdated();
    }

}

From source file:org.pentaho.reporting.engine.classic.core.util.ComponentDrawable.java

/**
 * Returns the preferred size of the drawable. If the drawable is aspect ratio aware, these bounds should be used to
 * compute the preferred aspect ratio for this drawable.
 * <p/>//w  w w  .  ja v a  2  s.  c om
 * This calls {@link java.awt.Component#getPreferredSize()} on the given component.
 *
 * @return the preferred size.
 */
public Dimension getPreferredSize() {
    if (component == null) {
        return new Dimension(0, 0);
    }
    if (SwingUtilities.isEventDispatchThread() || paintSynchronized == false) {
        preferredSizeRunnable.run();
        return preferredSizeRunnable.getRetval();
    }

    try {
        SwingUtilities.invokeAndWait(preferredSizeRunnable);
        return preferredSizeRunnable.getRetval();
    } catch (Exception e) {
        ComponentDrawable.logger.warn("Failed to compute the preferred size.");
    }
    return new Dimension(0, 0);
}

From source file:org.pentaho.reporting.engine.classic.core.util.ComponentDrawable.java

/**
 * Returns the declared size of the drawable. If the drawable is aspect ratio aware, these bounds should be used to
 * compute the declared aspect ratio for this drawable.
 * <p/>/*from   www  .j  av a 2  s  . c  o m*/
 * This calls {@link java.awt.Component#getSize()} on the given component.
 *
 * @return the preferred size.
 */
public Dimension getSize() {
    if (component == null) {
        return new Dimension(0, 0);
    }
    if (SwingUtilities.isEventDispatchThread() || paintSynchronized == false) {
        sizeRunnable.run();
        return sizeRunnable.getRetval();
    }

    try {
        SwingUtilities.invokeAndWait(sizeRunnable);
        return sizeRunnable.getRetval();
    } catch (Exception e) {
        ComponentDrawable.logger.warn("Failed to compute the defined size.");
    }
    return new Dimension(0, 0);
}

From source file:org.pentaho.reporting.engine.classic.core.util.ComponentDrawable.java

/**
 * Draws the component./*  w  ww  .  j  av a  2s.com*/
 *
 * @param g2
 *          the graphics device.
 * @param area
 *          the area inside which the object should be drawn.
 */
public void draw(final Graphics2D g2, final Rectangle2D area) {
    if (component == null) {
        return;
    }

    runnable.setArea(area);
    runnable.setGraphics(g2);

    if (SwingUtilities.isEventDispatchThread() || paintSynchronized == false) {
        runnable.run();
    } else {
        try {
            SwingUtilities.invokeAndWait(runnable);
        } catch (Exception e) {
            ComponentDrawable.logger.warn("Failed to redraw the component.");
        }
    }
}

From source file:org.rdv.ui.ControlPanel.java

/**
 * Update the boundaries of the time sliders based on the subscribed channel
 * bounds and the event marker bounds./*from ww  w  .  j ava  2 s .  co m*/
 */
private void updateTimeBoundaries() {
    // We haven't got the metadata channel tree yet
    if (ctree == null) {
        return;
    }

    if (!SwingUtilities.isEventDispatchThread()) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    updateTimeBoundaries();
                }
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return;
    }

    double startTime = -1;
    double endTime = -1;

    boolean hasSubscribedChannels = rbnbController.hasSubscribedChannels();

    // get the time bounds for all channels
    Iterator<?> it = ctree.iterator();
    while (it.hasNext()) {
        ChannelTree.Node node = (ChannelTree.Node) it.next();
        ChannelTree.NodeTypeEnum type = node.getType();
        if (type != ChannelTree.CHANNEL) {
            continue;
        }

        String channelName = node.getFullName();
        if (rbnbController.isSubscribed(channelName) || !hasSubscribedChannels) {
            double channelStart = node.getStart();
            double channelDuration = node.getDuration();
            double channelEnd = channelStart + channelDuration;
            if (startTime == -1 || channelStart < startTime) {
                startTime = channelStart;
            }
            if (endTime == -1 || channelEnd > endTime) {
                endTime = channelEnd;
            }
        }
    }

    if (hideEmptyTime) {
        List<TimeRange> timeRanges = new ArrayList<TimeRange>();

        double markerStartTime = -1;

        // get the time bounds for all markers
        List<EventMarker> markers = rbnbController.getMarkerManager().getMarkers();
        for (EventMarker marker : markers) {
            double markerTime = Double.parseDouble(marker.getProperty("timestamp"));
            if (startTime == -1 || markerTime < startTime) {
                startTime = markerTime;
            }
            if (endTime == -1 || markerTime > endTime) {
                endTime = markerTime;
            }

            String type = marker.getProperty("type");
            if (type.compareToIgnoreCase("start") == 0 && markerStartTime == -1) {
                markerStartTime = markerTime;
            } else if (type.compareToIgnoreCase("stop") == 0 && markerStartTime != -1) {
                timeRanges.add(new TimeRange(markerStartTime, markerTime));
                markerStartTime = -1;
            }
        }

        // add time range for ongoing event
        if (markerStartTime != -1) {
            timeRanges.add(new TimeRange(markerStartTime, Double.MAX_VALUE));
        }

        zoomTimeSlider.setTimeRanges(timeRanges);
        globalTimeSlider.setTimeRanges(timeRanges);
    }

    if (startTime == -1) {
        return;
    }

    double state = rbnbController.getState();
    double location = rbnbController.getLocation();
    if (state == Player.STATE_MONITORING && location > endTime) {
        endTime = location;
    }

    globalTimeSlider.setValues(startTime, endTime);

    // reset the selected time range if it gets stuck together
    double start = globalTimeSlider.getStart();
    double end = globalTimeSlider.getEnd();
    if (start == end) {
        if (start == globalTimeSlider.getMinimum()) {
            globalTimeSlider.setEnd(globalTimeSlider.getMaximum());
        } else if (start == globalTimeSlider.getMaximum()) {
            globalTimeSlider.setStart(globalTimeSlider.getMinimum());
        }
    }
}

From source file:org.rdv.ui.ControlPanel.java

/**
 * Called when the time changes. This updates the slider and display
 * components in the UI. It will also adjust the bounds and range if needed.
 * /* w w w  .ja v a  2s .  c o  m*/
 * @param time  the new time
 */
public void postTime(final double time) {
    if (!SwingUtilities.isEventDispatchThread()) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    postTime(time);
                }
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return;
    }

    if (time < globalTimeSlider.getMinimum()) {
        globalTimeSlider.setMinimum(time);
    } else if (time > globalTimeSlider.getMaximum()) {
        globalTimeSlider.setMaximum(time);
    }

    if (time < globalTimeSlider.getStart()) {
        globalTimeSlider.setStart(time);
    } else if (time > globalTimeSlider.getEnd()) {
        globalTimeSlider.setEnd(time);
    }

    if (!zoomTimeSlider.isValueAdjusting()) {
        zoomTimeSlider.removeTimeAdjustmentListener(this);
        zoomTimeSlider.setValue(time);
        zoomTimeSlider.addTimeAdjustmentListener(this);
    }

    if (rbnbController.getState() == Player.STATE_PLAYING && !globalTimeSlider.isTimeValid(time)) {
        double newTime = globalTimeSlider.getNextValidTime(time);
        if (newTime == -1) {
            newTime = globalTimeSlider.getActualMaximum();
        }
        zoomTimeSlider.setValue(newTime);
        if (newTime > time) {
            rbnbController.play();
        }
    }

    globalTimeSlider.setValue(time);

    locationButton.setText(DataViewer.formatDate(time));
}

From source file:org.trianacode.gui.main.imp.TrianaTask.java

/**
 * Called when the core properties of a task change i.e. its name, whether it is running continuously etc.
 *//*from   w  ww .j  ava2s.c o  m*/
public void taskPropertyUpdate(TaskPropertyEvent event) {
    if (SwingUtilities.isEventDispatchThread()) {
        handleTaskPropertyUpdated(event);
    } else {
        final TaskPropertyEvent evt = event;

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                handleTaskPropertyUpdated(evt);
            }
        });
    }
}

From source file:org.trianacode.gui.main.imp.TrianaTask.java

/**
 * Called when a data input node is added.
 *///from  w ww .  j  a v a  2  s .c  om
public void nodeAdded(TaskNodeEvent event) {
    if (SwingUtilities.isEventDispatchThread()) {
        handleNodeAdded(event);
    } else {
        final TaskNodeEvent evt = event;

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                handleNodeAdded(evt);
            }
        });
    }
}