001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.gui.util;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    
006    import java.awt.Component;
007    import java.awt.Container;
008    import java.awt.Image;
009    import java.awt.Toolkit;
010    import java.awt.image.FilteredImageSource;
011    import java.lang.reflect.InvocationTargetException;
012    
013    import javax.swing.GrayFilter;
014    import javax.swing.Icon;
015    import javax.swing.ImageIcon;
016    import javax.swing.JOptionPane;
017    import javax.swing.SwingUtilities;
018    
019    import org.openstreetmap.josm.Main;
020    import org.openstreetmap.josm.gui.ExtendedDialog;
021    import org.openstreetmap.josm.tools.ImageProvider;
022    
023    /**
024     * basic gui utils
025     */
026    public class GuiHelper {
027        /**
028         * disable / enable a component and all its child components
029         */
030        public static void setEnabledRec(Container root, boolean enabled) {
031            root.setEnabled(enabled);
032            Component[] children = root.getComponents();
033            for (Component child : children) {
034                if(child instanceof Container) {
035                    setEnabledRec((Container) child, enabled);
036                } else {
037                    child.setEnabled(enabled);
038                }
039            }
040        }
041    
042        public static void runInEDT(Runnable task) {
043            if (SwingUtilities.isEventDispatchThread()) {
044                task.run();
045            } else {
046                SwingUtilities.invokeLater(task);
047            }
048        }
049    
050        public static void runInEDTAndWait(Runnable task) {
051            if (SwingUtilities.isEventDispatchThread()) {
052                task.run();
053            } else {
054                try {
055                    SwingUtilities.invokeAndWait(task);
056                } catch (InterruptedException e) {
057                    e.printStackTrace();
058                } catch (InvocationTargetException e) {
059                    e.printStackTrace();
060                }
061            }
062        }
063        
064        /**
065         * returns true if the user wants to cancel, false if they
066         * want to continue
067         */
068        public static final boolean warnUser(String title, String content, ImageIcon baseActionIcon, String continueToolTip) {
069            ExtendedDialog dlg = new ExtendedDialog(Main.parent,
070                    title, new String[] {tr("Cancel"), tr("Continue")});
071            dlg.setContent(content);
072            dlg.setButtonIcons(new Icon[] {
073                    ImageProvider.get("cancel"),
074                    ImageProvider.overlay(
075                            ImageProvider.get("upload"),
076                            new ImageIcon(ImageProvider.get("warning-small").getImage().getScaledInstance(10 , 10, Image.SCALE_SMOOTH)),
077                            ImageProvider.OverlayPosition.SOUTHEAST)});
078            dlg.setToolTipTexts(new String[] {
079                    tr("Cancel"),
080                    continueToolTip});
081            dlg.setIcon(JOptionPane.WARNING_MESSAGE);
082            dlg.setCancelButton(1);
083            return dlg.showDialog().getValue() != 2;
084        }
085        
086        /**
087         * Replies the disabled (grayed) version of the specified image.
088         * @param image The image to disable
089         * @return The disabled (grayed) version of the specified image, brightened by 20%.
090         * @since 5484
091         */
092        public static final Image getDisabledImage(Image image) {
093            return Toolkit.getDefaultToolkit().createImage(
094                    new FilteredImageSource(image.getSource(), new GrayFilter(true, 20)));
095        }
096    
097        /**
098         * Replies the disabled (grayed) version of the specified icon.
099         * @param icon The icon to disable
100         * @return The disabled (grayed) version of the specified icon, brightened by 20%.
101         * @since 5484
102         */
103        public static final ImageIcon getDisabledIcon(ImageIcon icon) {
104            return new ImageIcon(getDisabledImage(icon.getImage()));
105        }
106    }