001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Component;
007import java.awt.Dimension;
008import java.awt.Frame;
009import java.awt.GridBagConstraints;
010import java.awt.GridBagLayout;
011import java.awt.Insets;
012import java.awt.event.ActionEvent;
013import java.awt.event.KeyEvent;
014import java.util.ArrayList;
015import java.util.Arrays;
016import java.util.Collections;
017import java.util.HashSet;
018import java.util.List;
019import java.util.Set;
020
021import javax.swing.AbstractAction;
022import javax.swing.Action;
023import javax.swing.Icon;
024import javax.swing.JButton;
025import javax.swing.JDialog;
026import javax.swing.JLabel;
027import javax.swing.JOptionPane;
028import javax.swing.JPanel;
029import javax.swing.JScrollBar;
030import javax.swing.JScrollPane;
031import javax.swing.KeyStroke;
032import javax.swing.UIManager;
033import javax.swing.WindowConstants;
034
035import org.openstreetmap.josm.gui.help.HelpBrowser;
036import org.openstreetmap.josm.gui.help.HelpUtil;
037import org.openstreetmap.josm.gui.util.GuiHelper;
038import org.openstreetmap.josm.gui.util.WindowGeometry;
039import org.openstreetmap.josm.gui.util.WindowOnTopListener;
040import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
041import org.openstreetmap.josm.io.NetworkManager;
042import org.openstreetmap.josm.io.OnlineResource;
043import org.openstreetmap.josm.tools.GBC;
044import org.openstreetmap.josm.tools.ImageProvider;
045import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
046import org.openstreetmap.josm.tools.InputMapUtils;
047import org.openstreetmap.josm.tools.Logging;
048import org.openstreetmap.josm.tools.Utils;
049
050/**
051 * General configurable dialog window.
052 * <p>
053 * If dialog is modal, you can use {@link #getValue()} to retrieve the
054 * button index. Note that the user can close the dialog
055 * by other means. This is usually equivalent to cancel action.
056 * <p>
057 * For non-modal dialogs, {@link #buttonAction(int, ActionEvent)} can be overridden.
058 * <p>
059 * There are various options, see below.
060 * <p>
061 * Note: The button indices are counted from 1 and upwards.
062 * So for {@link #getValue()}, {@link #setDefaultButton(int)} and
063 * {@link #setCancelButton} the first button has index 1.
064 * <p>
065 * Simple example:
066 * <pre>
067 *  ExtendedDialog ed = new ExtendedDialog(
068 *          MainApplication.getMainFrame(), tr("Dialog Title"),
069 *          new String[] {tr("Ok"), tr("Cancel")});
070 *  ed.setButtonIcons(new String[] {"ok", "cancel"});   // optional
071 *  ed.setIcon(JOptionPane.WARNING_MESSAGE);            // optional
072 *  ed.setContent(tr("Really proceed? Interesting things may happen..."));
073 *  ed.showDialog();
074 *  if (ed.getValue() == 1) { // user clicked first button "Ok"
075 *      // proceed...
076 *  }
077 * </pre>
078 */
079public class ExtendedDialog extends JDialog implements IExtendedDialog {
080    private final boolean disposeOnClose;
081    private volatile int result;
082    public static final int DialogClosedOtherwise = 0;
083    private boolean toggleable;
084    private String rememberSizePref = "";
085    private transient WindowGeometry defaultWindowGeometry;
086    private String togglePref = "";
087    private int toggleValue = -1;
088    private ConditionalOptionPaneUtil.MessagePanel togglePanel;
089    private final Component parentComponent;
090    private Component content;
091    private final String[] bTexts;
092    private String[] bToolTipTexts;
093    private transient Icon[] bIcons;
094    private Set<Integer> cancelButtonIdx = Collections.emptySet();
095    private int defaultButtonIdx = 1;
096    protected JButton defaultButton;
097    private transient Icon icon;
098    private final boolean isModal;
099    private boolean focusOnDefaultButton;
100
101    /** true, if the dialog should include a help button */
102    private boolean showHelpButton;
103    /** the help topic */
104    private String helpTopic;
105
106    /**
107     * set to true if the content of the extended dialog should
108     * be placed in a {@link JScrollPane}
109     */
110    private boolean placeContentInScrollPane;
111
112    // For easy access when inherited
113    protected transient Insets contentInsets = new Insets(10, 5, 0, 5);
114    protected transient List<JButton> buttons = new ArrayList<>();
115
116    /**
117     * This method sets up the most basic options for the dialog. Add more
118     * advanced features with dedicated methods.
119     * Possible features:
120     * <ul>
121     *   <li><code>setButtonIcons</code></li>
122     *   <li><code>setContent</code></li>
123     *   <li><code>toggleEnable</code></li>
124     *   <li><code>toggleDisable</code></li>
125     *   <li><code>setToggleCheckboxText</code></li>
126     *   <li><code>setRememberWindowGeometry</code></li>
127     * </ul>
128     *
129     * When done, call <code>showDialog</code> to display it. You can receive
130     * the user's choice using <code>getValue</code>. Have a look at this function
131     * for possible return values.
132     *
133     * @param parent       The parent element that will be used for position and maximum size
134     * @param title        The text that will be shown in the window titlebar
135     * @param buttonTexts  String Array of the text that will appear on the buttons. The first button is the default one.
136     */
137    public ExtendedDialog(Component parent, String title, String... buttonTexts) {
138        this(parent, title, buttonTexts, true, true);
139    }
140
141    /**
142     * Same as above but lets you define if the dialog should be modal.
143     * @param parent The parent element that will be used for position and maximum size
144     * @param title The text that will be shown in the window titlebar
145     * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one.
146     * @param modal Set it to {@code true} if you want the dialog to be modal
147     */
148    public ExtendedDialog(Component parent, String title, String[] buttonTexts, boolean modal) {
149        this(parent, title, buttonTexts, modal, true);
150    }
151
152    /**
153     * Same as above but lets you define if the dialog should be disposed on close.
154     * @param parent The parent element that will be used for position and maximum size
155     * @param title The text that will be shown in the window titlebar
156     * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one.
157     * @param modal Set it to {@code true} if you want the dialog to be modal
158     * @param disposeOnClose whether to call {@link #dispose} when closing the dialog
159     */
160    public ExtendedDialog(Component parent, String title, String[] buttonTexts, boolean modal, boolean disposeOnClose) {
161        super(searchRealParent(parent), title, modal ? ModalityType.DOCUMENT_MODAL : ModalityType.MODELESS);
162        this.parentComponent = parent;
163        this.isModal = modal;
164        bTexts = Utils.copyArray(buttonTexts);
165        if (disposeOnClose) {
166            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
167        }
168        this.disposeOnClose = disposeOnClose;
169    }
170
171    private static Frame searchRealParent(Component parent) {
172        if (parent == null) {
173            return null;
174        } else {
175            return GuiHelper.getFrameForComponent(parent);
176        }
177    }
178
179    @Override
180    public ExtendedDialog setButtonIcons(Icon... buttonIcons) {
181        this.bIcons = Utils.copyArray(buttonIcons);
182        return this;
183    }
184
185    @Override
186    public ExtendedDialog setButtonIcons(String... buttonIcons) {
187        bIcons = new Icon[buttonIcons.length];
188        for (int i = 0; i < buttonIcons.length; ++i) {
189            bIcons[i] = ImageProvider.get(buttonIcons[i], ImageSizes.LARGEICON);
190        }
191        return this;
192    }
193
194    @Override
195    public ExtendedDialog setToolTipTexts(String... toolTipTexts) {
196        this.bToolTipTexts = Utils.copyArray(toolTipTexts);
197        return this;
198    }
199
200    @Override
201    public ExtendedDialog setContent(Component content) {
202        return setContent(content, true);
203    }
204
205    @Override
206    public ExtendedDialog setContent(Component content, boolean placeContentInScrollPane) {
207        this.content = content;
208        this.placeContentInScrollPane = placeContentInScrollPane;
209        return this;
210    }
211
212    @Override
213    public ExtendedDialog setContent(String message) {
214        return setContent(string2label(message), false);
215    }
216
217    @Override
218    public ExtendedDialog setIcon(Icon icon) {
219        this.icon = icon;
220        return this;
221    }
222
223    @Override
224    public ExtendedDialog setIcon(int messageType) {
225        switch (messageType) {
226            case JOptionPane.ERROR_MESSAGE:
227                return setIcon(UIManager.getIcon("OptionPane.errorIcon"));
228            case JOptionPane.INFORMATION_MESSAGE:
229                return setIcon(UIManager.getIcon("OptionPane.informationIcon"));
230            case JOptionPane.WARNING_MESSAGE:
231                return setIcon(UIManager.getIcon("OptionPane.warningIcon"));
232            case JOptionPane.QUESTION_MESSAGE:
233                return setIcon(UIManager.getIcon("OptionPane.questionIcon"));
234            case JOptionPane.PLAIN_MESSAGE:
235                return setIcon(null);
236            default:
237                throw new IllegalArgumentException("Unknown message type!");
238        }
239    }
240
241    @Override
242    public ExtendedDialog showDialog() {
243        // Check if the user has set the dialog to not be shown again
244        if (toggleCheckState()) {
245            result = toggleValue;
246            return this;
247        }
248
249        setupDialog();
250        if (defaultButton != null) {
251            getRootPane().setDefaultButton(defaultButton);
252        }
253        // Don't focus the "do not show this again" check box, but the default button.
254        if (toggleable || focusOnDefaultButton) {
255            requestFocusToDefaultButton();
256        }
257        if (MainApplication.getMainFrame() != null) {
258            applyComponentOrientation(MainApplication.getMainFrame().getComponentOrientation());
259        }
260        setVisible(true);
261        toggleSaveState();
262        return this;
263    }
264
265    @Override
266    public int getValue() {
267        return result;
268    }
269
270    private boolean setupDone;
271
272    @Override
273    public void setupDialog() {
274        if (setupDone)
275            return;
276        setupDone = true;
277
278        setupEscListener();
279
280        JButton button;
281        JPanel buttonsPanel = new JPanel(new GridBagLayout());
282
283        for (int i = 0; i < bTexts.length; i++) {
284            button = new JButton(createButtonAction(i));
285            if (i == defaultButtonIdx-1) {
286                defaultButton = button;
287            }
288            if (bIcons != null && bIcons[i] != null) {
289                button.setIcon(bIcons[i]);
290            }
291            if (bToolTipTexts != null && i < bToolTipTexts.length && bToolTipTexts[i] != null) {
292                button.setToolTipText(bToolTipTexts[i]);
293            }
294
295            buttonsPanel.add(button, GBC.std().insets(2, 2, 2, 2));
296            buttons.add(button);
297        }
298        if (showHelpButton) {
299            buttonsPanel.add(new JButton(new HelpAction()), GBC.std().insets(2, 2, 2, 2));
300            HelpUtil.setHelpContext(getRootPane(), helpTopic);
301        }
302
303        JPanel cp = new JPanel(new GridBagLayout());
304
305        GridBagConstraints gc = new GridBagConstraints();
306        gc.gridx = 0;
307        int y = 0;
308        gc.gridy = y++;
309        gc.weightx = 0.0;
310        gc.weighty = 0.0;
311
312        if (icon != null) {
313            JLabel iconLbl = new JLabel(icon);
314            gc.insets = new Insets(10, 10, 10, 10);
315            gc.anchor = GridBagConstraints.NORTH;
316            gc.weighty = 1.0;
317            cp.add(iconLbl, gc);
318            gc.anchor = GridBagConstraints.CENTER;
319            gc.gridx = 1;
320        }
321
322        gc.fill = GridBagConstraints.BOTH;
323        gc.insets = contentInsets;
324        gc.weightx = 1.0;
325        gc.weighty = 1.0;
326        cp.add(content, gc);
327
328        gc.fill = GridBagConstraints.NONE;
329        gc.gridwidth = GridBagConstraints.REMAINDER;
330        gc.weightx = 0.0;
331        gc.weighty = 0.0;
332
333        if (toggleable) {
334            togglePanel = new ConditionalOptionPaneUtil.MessagePanel(null, ConditionalOptionPaneUtil.isInBulkOperation(togglePref));
335            gc.gridx = icon != null ? 1 : 0;
336            gc.gridy = y++;
337            gc.anchor = GridBagConstraints.LINE_START;
338            gc.insets = new Insets(5, contentInsets.left, 5, contentInsets.right);
339            cp.add(togglePanel, gc);
340        }
341
342        gc.gridy = y;
343        gc.anchor = GridBagConstraints.CENTER;
344            gc.insets = new Insets(5, 5, 5, 5);
345        cp.add(buttonsPanel, gc);
346        if (placeContentInScrollPane) {
347            JScrollPane pane = new JScrollPane(cp);
348            GuiHelper.setDefaultIncrement(pane);
349            pane.setBorder(null);
350            setContentPane(pane);
351        } else {
352            setContentPane(cp);
353        }
354        pack();
355
356        // Try to make it not larger than the parent window or at least not larger than 2/3 of the screen
357        Dimension d = getSize();
358        Dimension x = findMaxDialogSize();
359
360        boolean limitedInWidth = d.width > x.width;
361        boolean limitedInHeight = d.height > x.height;
362
363        if (x.width > 0 && d.width > x.width) {
364            d.width = x.width;
365        }
366        if (x.height > 0 && d.height > x.height) {
367            d.height = x.height;
368        }
369
370        // We have a vertical scrollbar and enough space to prevent a horizontal one
371        if (!limitedInWidth && limitedInHeight) {
372            d.width += new JScrollBar().getPreferredSize().width;
373        }
374
375        setSize(d);
376        setLocationRelativeTo(parentComponent);
377    }
378
379    protected Action createButtonAction(final int i) {
380        return new AbstractAction(bTexts[i]) {
381            @Override
382            public void actionPerformed(ActionEvent evt) {
383                buttonAction(i, evt);
384            }
385        };
386    }
387
388    /**
389     * This gets performed whenever a button is clicked or activated
390     * @param buttonIndex the button index (first index is 0)
391     * @param evt the button event
392     */
393    protected void buttonAction(int buttonIndex, ActionEvent evt) {
394        result = buttonIndex+1;
395        setVisible(false);
396    }
397
398    /**
399     * Tries to find a good value of how large the dialog should be
400     * @return Dimension Size of the parent component if visible or 2/3 of screen size if not available or hidden
401     */
402    protected Dimension findMaxDialogSize() {
403        Dimension screenSize = GuiHelper.getScreenSize();
404        Dimension x = new Dimension(screenSize.width*2/3, screenSize.height*2/3);
405        if (parentComponent != null && parentComponent.isVisible()) {
406            x = GuiHelper.getFrameForComponent(parentComponent).getSize();
407        }
408        return x;
409    }
410
411    /**
412     * Makes the dialog listen to ESC keypressed
413     */
414    private void setupEscListener() {
415        Action actionListener = new AbstractAction() {
416            @Override
417            public void actionPerformed(ActionEvent actionEvent) {
418                // 0 means that the dialog has been closed otherwise.
419                // We need to set it to zero again, in case the dialog has been re-used
420                // and the result differs from its default value
421                result = ExtendedDialog.DialogClosedOtherwise;
422                if (Logging.isDebugEnabled()) {
423                    Logging.debug("{0} ESC action performed ({1}) from {2}",
424                            getClass().getName(), actionEvent, new Exception().getStackTrace()[1]);
425                }
426                setVisible(false);
427            }
428        };
429
430        InputMapUtils.addEscapeAction(getRootPane(), actionListener);
431    }
432
433    protected final void rememberWindowGeometry(WindowGeometry geometry) {
434        if (geometry != null) {
435            geometry.remember(rememberSizePref);
436        }
437    }
438
439    protected final WindowGeometry initWindowGeometry() {
440        return new WindowGeometry(rememberSizePref, defaultWindowGeometry);
441    }
442
443    /**
444     * Override setVisible to be able to save the window geometry if required
445     */
446    @Override
447    public void setVisible(boolean visible) {
448        if (visible) {
449            repaint();
450        }
451
452        if (Logging.isDebugEnabled()) {
453            Logging.debug(getClass().getName()+".setVisible("+visible+") from "+new Exception().getStackTrace()[1]);
454        }
455
456        // Ensure all required variables are available
457        if (!rememberSizePref.isEmpty() && defaultWindowGeometry != null) {
458            if (visible) {
459                initWindowGeometry().applySafe(this);
460            } else if (isShowing()) { // should fix #6438, #6981, #8295
461                rememberWindowGeometry(new WindowGeometry(this));
462            }
463        }
464        if (visible && isModal()) {
465            this.setAlwaysOnTop(true);
466            this.addWindowFocusListener(new WindowOnTopListener());
467        }
468        super.setVisible(visible);
469
470        if (!visible && disposeOnClose) {
471            dispose();
472        }
473    }
474
475    @Override
476    public ExtendedDialog setRememberWindowGeometry(String pref, WindowGeometry wg) {
477        rememberSizePref = pref == null ? "" : pref;
478        defaultWindowGeometry = wg;
479        return this;
480    }
481
482    @Override
483    public ExtendedDialog toggleEnable(String togglePref) {
484        if (!isModal) {
485            throw new IllegalStateException();
486        }
487        this.toggleable = true;
488        this.togglePref = togglePref;
489        return this;
490    }
491
492    @Override
493    public ExtendedDialog setDefaultButton(int defaultButtonIdx) {
494        this.defaultButtonIdx = defaultButtonIdx;
495        return this;
496    }
497
498    @Override
499    public ExtendedDialog setCancelButton(Integer... cancelButtonIdx) {
500        this.cancelButtonIdx = new HashSet<>(Arrays.asList(cancelButtonIdx));
501        return this;
502    }
503
504    @Override
505    public void setFocusOnDefaultButton(boolean focus) {
506        focusOnDefaultButton = focus;
507    }
508
509    private void requestFocusToDefaultButton() {
510        if (defaultButton != null) {
511            GuiHelper.runInEDT(defaultButton::requestFocusInWindow);
512        }
513    }
514
515    @Override
516    public final boolean toggleCheckState() {
517        toggleable = !Utils.isEmpty(togglePref);
518        toggleValue = ConditionalOptionPaneUtil.getDialogReturnValue(togglePref);
519        return toggleable && toggleValue != -1;
520    }
521
522    /**
523     * This function checks the state of the "Do not show again" checkbox and
524     * writes the corresponding pref.
525     */
526    protected void toggleSaveState() {
527        if (!toggleable ||
528                togglePanel == null ||
529                cancelButtonIdx.contains(result) ||
530                result == ExtendedDialog.DialogClosedOtherwise)
531            return;
532        togglePanel.getNotShowAgain().store(togglePref, result);
533    }
534
535    /**
536     * Convenience function that converts a given string into a JMultilineLabel
537     * @param msg the message to display
538     * @return JMultilineLabel displaying {@code msg}
539     */
540    private static JMultilineLabel string2label(String msg) {
541        JMultilineLabel lbl = new JMultilineLabel(msg);
542        // Make it not wider than 1/2 of the screen
543        Dimension screenSize = GuiHelper.getScreenSize();
544        lbl.setMaxWidth(screenSize.width/2);
545        // Disable default Enter key binding to allow dialog's one (then enables to hit default button from here)
546        lbl.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), new Object());
547        return lbl;
548    }
549
550    @Override
551    public ExtendedDialog configureContextsensitiveHelp(String helpTopic, boolean showHelpButton) {
552        this.helpTopic = helpTopic;
553        this.showHelpButton = showHelpButton;
554        return this;
555    }
556
557    class HelpAction extends AbstractAction {
558        /**
559         * Constructs a new {@code HelpAction}.
560         */
561        HelpAction() {
562            putValue(SHORT_DESCRIPTION, tr("Show help information"));
563            putValue(NAME, tr("Help"));
564            new ImageProvider("help").getResource().attachImageIcon(this, true);
565            setEnabled(!NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE));
566        }
567
568        @Override
569        public void actionPerformed(ActionEvent e) {
570            HelpBrowser.setUrlForHelpTopic(helpTopic);
571        }
572    }
573}