Changeset 1838 in josm


Ignore:
Timestamp:
2009-07-25T16:34:55+02:00 (15 years ago)
Author:
Gubaer
Message:

new: two utility classes in GUI subsystem: OptionPaneUtil and ConditionalOptionPaneUtil. Both provide methods for displaying message and option dialogs which are never hidden by one of the JOSM windows.

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
7 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/OrthogonalizeAction.java

    r1820 r1838  
    2222import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2323import org.openstreetmap.josm.data.osm.Way;
    24 import org.openstreetmap.josm.tools.DontShowAgainInfo;
     24import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    2525import org.openstreetmap.josm.tools.Shortcut;
    2626
     
    9999                    "to undesirable results when doing rectangular alignments.<br>" +
    100100                    "Change your projection to get rid of this warning.<br>" +
    101             "Do you want to continue?");
    102 
    103             if (!DontShowAgainInfo.show("align_rectangular_4326", msg, false))
     101            "Do you want to continue?</html>");
     102            if (!ConditionalOptionPaneUtil.showConfirmationDialog(
     103                    "align_rectangular_4326",
     104                    Main.parent,
     105                    msg,
     106                    tr("Warning"),
     107                    JOptionPane.YES_NO_OPTION,
     108                    JOptionPane.QUESTION_MESSAGE,
     109                    JOptionPane.YES_OPTION))
    104110                return;
    105111        }
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

    r1823 r1838  
    499499                extendedWay = true;
    500500                getCurrentDataSet().setSelected(way);
     501                DataSet.fireSelectionChanged(getCurrentDataSet().getSelected());
    501502            }
    502503        }
     
    515516            }
    516517            getCurrentDataSet().setSelected(n);
     518            DataSet.fireSelectionChanged(getCurrentDataSet().getSelected());
    517519        } else if (!newNode) {
    518520            title = tr("Connect existing way to node");
     
    557559                    (posn0 < selectedWay.nodes.size()-1) && targetNode.equals(selectedWay.nodes.get(posn0+1))) {  // next node
    558560                getCurrentDataSet().setSelected(targetNode);
     561                DataSet.fireSelectionChanged(getCurrentDataSet().getSelected());
    559562                lastUsedNode = targetNode;
    560563                return true;
  • trunk/src/org/openstreetmap/josm/command/DeleteCommand.java

    r1826 r1838  
    3131import org.openstreetmap.josm.data.osm.WaySegment;
    3232import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
     33import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    3334import org.openstreetmap.josm.gui.ExtendedDialog;
    3435import org.openstreetmap.josm.gui.PrimitiveNameFormatter;
    35 import org.openstreetmap.josm.tools.DontShowAgainInfo;
    3636import org.openstreetmap.josm.tools.ImageProvider;
    3737
     
    391391                                        "<br>" +
    392392                                "Do you really want to delete?") + "</html>"));
    393                         return DontShowAgainInfo.show("delete_outside_nodes", msg, false, JOptionPane.YES_NO_OPTION, JOptionPane.YES_OPTION);
     393                        return ConditionalOptionPaneUtil.showConfirmationDialog(
     394                                "delete_outside_nodes",
     395                                Main.parent,
     396                                msg,
     397                                tr("Delete confirmation"),
     398                                JOptionPane.YES_NO_OPTION,
     399                                JOptionPane.QUESTION_MESSAGE,
     400                                JOptionPane.YES_OPTION
     401                        );
    394402                    }
    395 
    396403                }
    397404            }
  • trunk/src/org/openstreetmap/josm/gui/ConditionalOptionPaneUtil.java

    r1835 r1838  
    11// License: GPL. Copyright 2007 by Immanuel Scholz and others
    2 package org.openstreetmap.josm.tools;
     2package org.openstreetmap.josm.gui;
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.Container;
     6import java.awt.Component;
    77import java.awt.GridBagLayout;
     8import java.awt.HeadlessException;
    89
    910import javax.swing.JCheckBox;
     
    1314
    1415import org.openstreetmap.josm.Main;
     16import org.openstreetmap.josm.tools.GBC;
    1517
    16 public class DontShowAgainInfo {
    1718
    18     public static boolean show(String prefKey, String msg) {
    19         return show(prefKey, new JLabel(msg), false, JOptionPane.OK_CANCEL_OPTION, JOptionPane.OK_OPTION);
     19/**
     20 * ConditionalOptionPaneUtil provides static utility methods for displaying modal message dialogs
     21 * which can be enabled/disabled by the user.
     22 *
     23 * They wrap the methods provided by {@see JOptionPane}. Within JOSM you should use these
     24 * methods rather than the bare methods from {@see JOptionPane} because the methods provided
     25 * by ConditionalOptionPaneUtil ensure that a dialog window is always on top and isn't hidden by one of the
     26 * JOSM windows for detached dialogs, relation editors, history browser and the like.
     27 *
     28 */
     29public class ConditionalOptionPaneUtil {
     30
     31    /**
     32     * this is a static utility class only
     33     */
     34    private ConditionalOptionPaneUtil() {}
     35
     36    /**
     37     * Replies the preference value for the preference key "message." + <code>prefKey</code>.
     38     * The default value if the preference key is missing is true.
     39     *
     40     * @param  the preference key
     41     * @return prefKey the preference value for the preference key "message." + <code>prefKey</code>
     42     */
     43    public static boolean getDialogShowingEnabled(String prefKey) {
     44        return Main.pref.getBoolean("message."+prefKey, true);
    2045    }
    2146
    22     public static boolean show(String prefKey, String msg, Boolean state) {
    23         return show(prefKey, new JLabel(msg), state, JOptionPane.OK_CANCEL_OPTION, JOptionPane.OK_OPTION);
     47    /**
     48     * sets the value for the preference key "message." + <code>prefKey</code>.
     49     *
     50     * @param prefKey the key
     51     * @param enabled the value
     52     */
     53    public static void setDialogShowingEnabled(String prefKey, boolean enabled) {
     54        Main.pref.put("message."+prefKey, enabled);
    2455    }
    2556
    26     public static boolean show(String prefKey, Container msg) {
    27         return show(prefKey, msg, false, JOptionPane.OK_CANCEL_OPTION, JOptionPane.OK_OPTION);
     57
     58    /**
     59     * Displays an confirmation dialog with some option buttons given by <code>optionType</code>.
     60     * It is always on top even if there are other open windows like detached dialogs,
     61     * relation editors, history browsers and the like.
     62     *
     63     * Set <code>optionType</code> to {@see JOptionPane#YES_NO_OPTION} for a dialog with a YES and
     64     * a NO button.
     65
     66     * Set <code>optionType</code> to {@see JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES,
     67     * a NO and a CANCEL button
     68     *
     69     * Replies true, if the selected option is equal to <code>trueOption</code>, otherwise false.
     70     * Replies true, if the dialog is not displayed because the respective preference option
     71     * <code>preferenceKey</code> is set to false.
     72     *
     73     * @param preferenceKey the preference key
     74     * @param parent  the parent component
     75     * @param message  the message
     76     * @param title the title
     77     * @param optionType  the option type
     78     * @param messageType the message type
     79     * @param trueOption  if this option is selected the method replies true
     80     *
     81     *
     82     * @return true, if the selected option is equal to <code>trueOption</code>, otherwise false.
     83     *
     84     * @see JOptionPane#INFORMATION_MESSAGE
     85     * @see JOptionPane#WARNING_MESSAGE
     86     * @see JOptionPane#ERROR_MESSAGE
     87     */
     88    static public boolean showConfirmationDialog(String preferenceKey, Component parent, Object message, String title, int optionType, int messageType, int trueOption) throws HeadlessException {
     89        if (!getDialogShowingEnabled(preferenceKey))
     90            return true;
     91        MessagePanel pnl = new MessagePanel(preferenceKey, message);
     92        boolean ret = OptionPaneUtil.showConfirmationDialog(parent, message, title, optionType, messageType, trueOption);
     93        pnl.remeberDialogShowingEnabled();
     94        return ret;
    2895    }
    2996
    30     public static boolean show(String prefKey, Container msg, Boolean state, int options, int true_option) {
    31         if (!Main.pref.getBoolean("message."+prefKey, state)) {
    32             JCheckBox dontshowagain = new JCheckBox(tr("Do not show again"));
    33             dontshowagain.setSelected(Main.pref.getBoolean("message."+prefKey, state));
    34             JPanel all = new JPanel(new GridBagLayout());
    35             all.add(msg, GBC.eop());
    36             all.add(dontshowagain, GBC.eol());
    37             int answer = JOptionPane.showConfirmDialog(Main.parent, all, tr("Information"), options);
    38             if (answer != true_option)
    39                 return false;
    40             Main.pref.put("message."+prefKey, dontshowagain.isSelected());
     97    /**
     98     * Displays an message in modal dialog with an OK button. Makes sure the dialog
     99     * is always on top even if there are other open windows like detached dialogs,
     100     * relation editors, history browsers and the like.
     101     *
     102     * If there is a preference with key <code>preferenceKey</code> and value <code>false</code>
     103     * the dialog is not show.
     104     *
     105     * @param preferenceKey the preference key
     106     * @param parent  the parent component
     107     * @param message  the message
     108     * @param title the title
     109     * @param messageType the message type
     110     *
     111     * @see JOptionPane#INFORMATION_MESSAGE
     112     * @see JOptionPane#WARNING_MESSAGE
     113     * @see JOptionPane#ERROR_MESSAGE
     114     */
     115    static public void showMessageDialog(String preferenceKey, Component parent, Object message, String title,int messageType) {
     116        if (!getDialogShowingEnabled(preferenceKey))
     117            return;
     118        MessagePanel pnl = new MessagePanel(preferenceKey, message);
     119        OptionPaneUtil.showMessageDialog(parent, pnl, title, messageType);
     120        pnl.remeberDialogShowingEnabled();
     121    }
     122
     123    /**
     124     * This is a message panel used in dialogs which can be enabled/disabled with a preference
     125     * setting.
     126     * In addition to the normal message any {@see JOptionPane} would display it includes
     127     * a checkbox for enabling/disabling this particular dialog.
     128     *
     129     */
     130    private static class MessagePanel extends JPanel {
     131        JCheckBox cbShowDialog;
     132        String preferenceKey;
     133
     134        public MessagePanel(String preferenceKey, Object message) {
     135            this.preferenceKey = preferenceKey;
     136            cbShowDialog = new JCheckBox(tr("Do not show again"));
     137            cbShowDialog.setSelected(!ConditionalOptionPaneUtil.getDialogShowingEnabled(preferenceKey));
     138            setLayout(new GridBagLayout());
     139
     140            if (message instanceof Component) {
     141                add((Component)message, GBC.eop());
     142            } else {
     143                add(new JLabel(message.toString()),GBC.eop());
     144            }
     145            add(cbShowDialog, GBC.eol());
    41146        }
    42         return true;
     147
     148        public boolean getDialogShowingEnabled() {
     149            return cbShowDialog.isSelected();
     150        }
     151
     152        public void remeberDialogShowingEnabled() {
     153            ConditionalOptionPaneUtil.setDialogShowingEnabled(preferenceKey, !getDialogShowingEnabled());
     154        }
    43155    }
    44156}
  • trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java

    r1677 r1838  
    2222import javax.swing.JLabel;
    2323import javax.swing.JList;
     24import javax.swing.JOptionPane;
    2425import javax.swing.JPanel;
    2526import javax.swing.JScrollPane;
     
    3031
    3132import org.openstreetmap.josm.Main;
     33import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    3234import org.openstreetmap.josm.gui.ExtendedDialog;
    3335import org.openstreetmap.josm.gui.MapFrame;
     
    3739import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    3840import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
    39 import org.openstreetmap.josm.tools.DontShowAgainInfo;
    4041import org.openstreetmap.josm.tools.ImageProvider;
    4142import org.openstreetmap.josm.tools.ImageProvider.OverlayPosition;
     
    7879                {
    7980                    int result = new ExtendedDialog(Main.parent, tr("Unsaved Changes"),
    80                         tr("There are unsaved changes. Delete the layer anwyay?"),
    81                         new String[] {tr("Delete Layer"), tr("Cancel")},
    82                         new String[] {"dialogs/delete.png", "cancel.png"}).getValue();
     81                            tr("There are unsaved changes. Delete the layer anwyay?"),
     82                            new String[] {tr("Delete Layer"), tr("Cancel")},
     83                            new String[] {"dialogs/delete.png", "cancel.png"}).getValue();
    8384
    8485                    if(result != 1) return;
    8586                }
    86                 else if(!DontShowAgainInfo.show("delete_layer", tr("Do you really want to delete the whole layer?"), false))
     87                else if (!ConditionalOptionPaneUtil.showConfirmationDialog(
     88                        "delete_layer",
     89                        Main.parent,
     90                        tr("Do you really want to delete the whole layer?"),
     91                        tr("Confirmation"),
     92                        JOptionPane.YES_NO_OPTION,
     93                        JOptionPane.QUESTION_MESSAGE,
     94                        JOptionPane.YES_OPTION))
    8795                    return;
    8896            }
    8997            Main.main.removeLayer(l);
    90             if (sel >= instance.getModel().getSize())
     98            if (sel >= instance.getModel().getSize()) {
    9199                sel = instance.getModel().getSize()-1;
    92             if (instance.getSelectedValue() == null)
     100            }
     101            if (instance.getSelectedValue() == null) {
    93102                instance.setSelectedIndex(sel);
    94             if (Main.map != null)
     103            }
     104            if (Main.map != null) {
    95105                Main.map.mapView.setActiveLayer((Layer)instance.getSelectedValue());
     106            }
    96107        }
    97108    }
     
    163174    public LayerListDialog(MapFrame mapFrame) {
    164175        super(tr("Layers"), "layerlist", tr("Open a list of all loaded layers."),
    165         Shortcut.registerShortcut("subwindow:layers", tr("Toggle: {0}", tr("Layers")), KeyEvent.VK_L, Shortcut.GROUP_LAYER), 100);
     176                Shortcut.registerShortcut("subwindow:layers", tr("Toggle: {0}", tr("Layers")), KeyEvent.VK_L, Shortcut.GROUP_LAYER), 100);
    166177        instance = new JList(model);
    167178        listScrollPane = new JScrollPane(instance);
     
    174185                        layer.name, index, isSelected, cellHasFocus);
    175186                Icon icon = layer.getIcon();
    176                 if (!layer.visible)
     187                if (!layer.visible) {
    177188                    icon = ImageProvider.overlay(icon, "overlay/invisible", OverlayPosition.SOUTHEAST);
     189                }
    178190                label.setIcon(icon);
    179191                label.setToolTipText(layer.getToolTipText());
     
    185197
    186198        Collection<Layer> data = mapView.getAllLayers();
    187         for (Layer l : data)
     199        for (Layer l : data) {
    188200            model.addElement(l);
     201        }
    189202
    190203        instance.setSelectedValue(mapView.getActiveLayer(), true);
     
    194207                if (instance.getModel().getSize() == 0)
    195208                    return;
    196                 if (instance.getSelectedIndex() == -1)
     209                if (instance.getSelectedIndex() == -1) {
    197210                    instance.setSelectedIndex(e.getFirstIndex());
     211                }
    198212                mapView.setActiveLayer((Layer)instance.getSelectedValue());
    199213            }
     
    210224            }
    211225            @Override public void mousePressed(MouseEvent e) {
    212                 if (e.isPopupTrigger())
     226                if (e.isPopupTrigger()) {
    213227                    openPopup(e);
     228                }
    214229            }
    215230            @Override public void mouseReleased(MouseEvent e) {
    216                 if (e.isPopupTrigger())
     231                if (e.isPopupTrigger()) {
    217232                    openPopup(e);
     233                }
    218234            }
    219235            @Override public void mouseClicked(MouseEvent e) {
     
    258274
    259275        mergeButton = new SideButton("mergedown", "LayerList", tr("Merge the layer directly below into the selected layer."),
    260         new ActionListener(){
     276                new ActionListener(){
    261277            public void actionPerformed(ActionEvent e) {
    262278                Layer lTo = (Layer)instance.getSelectedValue();
     
    311327            return;
    312328        }
    313         if (instance.getSelectedIndex() == -1)
     329        if (instance.getSelectedIndex() == -1) {
    314330            instance.setSelectedIndex(0);
     331        }
    315332        updateButtonEnabled();
    316333    }
     
    320337     */
    321338    public void activeLayerChange(Layer oldLayer, Layer newLayer) {
    322         if (newLayer != instance.getSelectedValue())
     339        if (newLayer != instance.getSelectedValue()) {
    323340            instance.setSelectedValue(newLayer, true);
     341        }
    324342        updateButtonEnabled();
    325343    }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java

    r1833 r1838  
    1616import javax.swing.DefaultListModel;
    1717import javax.swing.JList;
     18import javax.swing.JOptionPane;
    1819import javax.swing.JPanel;
    1920import javax.swing.JScrollPane;
     
    2728import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2829import org.openstreetmap.josm.data.osm.Relation;
     30import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
     31import org.openstreetmap.josm.gui.OptionPaneUtil;
    2932import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
    3033import org.openstreetmap.josm.gui.SideButton;
  • trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java

    r1811 r1838  
    5757import org.openstreetmap.josm.data.osm.Way;
    5858import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
     59import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    5960import org.openstreetmap.josm.gui.MapView;
    6061import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
     
    6566import org.openstreetmap.josm.tools.AudioUtil;
    6667import org.openstreetmap.josm.tools.DateUtils;
    67 import org.openstreetmap.josm.tools.DontShowAgainInfo;
    6868import org.openstreetmap.josm.tools.GBC;
    6969import org.openstreetmap.josm.tools.ImageProvider;
     
    687687            msg.add(new JLabel(tr("<html>Upload of unprocessed GPS data as map data is considered harmful.<br>If you want to upload traces, look here:")), GBC.eol());
    688688            msg.add(new UrlLabel(tr("http://www.openstreetmap.org/traces")), GBC.eop());
    689             if (!DontShowAgainInfo.show("convert_to_data", msg))
     689            if (!ConditionalOptionPaneUtil.showConfirmationDialog(
     690                    "convert_to_data",
     691                    Main.parent,
     692                    msg,
     693                    tr("Warning"),
     694                    JOptionPane.OK_CANCEL_OPTION,
     695                    JOptionPane.WARNING_MESSAGE,
     696                    JOptionPane.OK_OPTION))
    690697                return;
    691698            DataSet ds = new DataSet();
  • trunk/src/org/openstreetmap/josm/gui/layer/RawGpsLayer.java

    r1808 r1838  
    4545import org.openstreetmap.josm.data.osm.Way;
    4646import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
     47import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    4748import org.openstreetmap.josm.gui.MapView;
    4849import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    4950import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
    5051import org.openstreetmap.josm.io.MultiPartFormOutputStream;
    51 import org.openstreetmap.josm.tools.DontShowAgainInfo;
    5252import org.openstreetmap.josm.tools.GBC;
    5353import org.openstreetmap.josm.tools.ImageProvider;
     
    7070            msg.add(new JLabel(tr("<html>Upload of unprocessed GPS data as map data is considered harmful.<br>If you want to upload traces, look here:")), GBC.eol());
    7171            msg.add(new UrlLabel(tr("http://www.openstreetmap.org/traces")), GBC.eop());
    72             if (!DontShowAgainInfo.show("convert_to_data", msg))
     72            if (!ConditionalOptionPaneUtil.showConfirmationDialog(
     73                    "convert_to_data",
     74                    Main.parent,
     75                    msg,
     76                    tr("Warning"),
     77                    JOptionPane.OK_CANCEL_OPTION,
     78                    JOptionPane.WARNING_MESSAGE,
     79                    JOptionPane.OK_OPTION))
    7380                return;
    7481            DataSet ds = new DataSet();
Note: See TracChangeset for help on using the changeset viewer.