source: josm/trunk/src/org/openstreetmap/josm/gui/PleaseWaitDialog.java @ 12675

Last change on this file since 12675 was 12675, checked in by Don-vip, 3 months ago

see #15182 - move the Swing-based ProgressMonitor implementations from gui.progress to gui.progress.swing. Progress monitor concept is used in very large parts of JOSM, a console-based implementation could be added later

  • Property svn:eol-style set to native
File size: 7.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.event.ActionListener;
10import java.awt.event.ComponentAdapter;
11import java.awt.event.ComponentEvent;
12
13import javax.swing.BorderFactory;
14import javax.swing.JButton;
15import javax.swing.JDialog;
16import javax.swing.JLabel;
17import javax.swing.JPanel;
18import javax.swing.JProgressBar;
19import javax.swing.JScrollPane;
20import javax.swing.UIManager;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor.ProgressMonitorDialog;
24import org.openstreetmap.josm.gui.util.GuiHelper;
25import org.openstreetmap.josm.gui.widgets.JosmTextArea;
26import org.openstreetmap.josm.tools.GBC;
27import org.openstreetmap.josm.tools.ImageProvider;
28
29/**
30 * This is a dialog that displays the progress of an action to the user.
31 */
32public class PleaseWaitDialog extends JDialog implements ProgressMonitorDialog {
33
34    private final JProgressBar progressBar = new JProgressBar();
35
36    private final JLabel currentAction = new JLabel("");
37    private final JLabel customText = new JLabel("");
38
39    private JButton btnCancel;
40    private JButton btnInBackground;
41    /** the text area and the scroll pane for the log */
42    private final JosmTextArea taLog = new JosmTextArea(5, 50);
43    private final JScrollPane spLog = new JScrollPane(taLog);
44
45
46    /**
47     * Constructs a new {@code PleaseWaitDialog}.
48     * @param parent the {@code Component} from which the dialog is displayed. Can be {@code null}.
49     */
50    public PleaseWaitDialog(Component parent) {
51        super(GuiHelper.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL);
52        initDialog();
53    }
54
55    private void initDialog() {
56        setLayout(new GridBagLayout());
57        JPanel pane = new JPanel(new GridBagLayout());
58        pane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
59        pane.add(currentAction, GBC.eol().fill(GBC.HORIZONTAL));
60        pane.add(customText, GBC.eol().fill(GBC.HORIZONTAL));
61        pane.add(progressBar, GBC.eop().fill(GBC.HORIZONTAL));
62        JPanel buttons = new JPanel(new GridBagLayout());
63        btnCancel = new JButton(tr("Cancel"));
64        btnCancel.setIcon(ImageProvider.get("cancel"));
65        btnCancel.setToolTipText(tr("Click to cancel the current operation"));
66        buttons.add(btnCancel);
67        btnInBackground = new JButton(tr("In background"));
68        btnInBackground.setToolTipText(tr("Click to run job in background"));
69        buttons.add(btnInBackground, GBC.std().fill(GBC.VERTICAL).insets(5, 0, 0, 0));
70        pane.add(buttons, GBC.eol().anchor(GBC.CENTER));
71        GridBagConstraints gc = GBC.eol().fill(GBC.BOTH);
72        gc.weighty = 1.0;
73        gc.weightx = 1.0;
74        pane.add(spLog, gc);
75        spLog.setVisible(false);
76        setContentPane(pane);
77        setCustomText("");
78        setLocationRelativeTo(getParent());
79        addComponentListener(new ComponentAdapter() {
80            @Override
81            public void componentResized(ComponentEvent ev) {
82                int w = getWidth();
83                if (w > 200) {
84                    Main.pref.putInteger("progressdialog.size", w);
85                }
86            }
87        });
88    }
89
90    @Override
91    public void setIndeterminate(boolean newValue) {
92        UIManager.put("ProgressBar.cycleTime", UIManager.getInt("ProgressBar.repaintInterval") * 100);
93        progressBar.setIndeterminate(newValue);
94    }
95
96    protected void adjustLayout() {
97        invalidate();
98        setDropTarget(null); // Workaround to JDK bug 7027598/7100524/7169912 (#8613)
99        pack();
100        setSize(Main.pref.getInteger("progressdialog.size", 600), getSize().height);
101    }
102
103    /**
104     * Sets a custom text line below currentAction. Can be used to display additional information.
105     * @param text custom text
106     */
107    @Override
108    public void setCustomText(String text) {
109        if (text == null || text.trim().isEmpty()) {
110            customText.setVisible(false);
111            adjustLayout();
112            return;
113        }
114        customText.setText(text);
115        if (!customText.isVisible()) {
116            customText.setVisible(true);
117            adjustLayout();
118        }
119    }
120
121    @Override
122    public void setCurrentAction(String text) {
123        currentAction.setText(text);
124    }
125
126    /**
127     * Appends a log message to the progress dialog. If the log area isn't visible yet
128     * it becomes visible. The height of the progress dialog is slightly increased too.
129     *
130     * @param message the message to append to the log. Ignore if null or white space only.
131     */
132    @Override
133    public void appendLogMessage(String message) {
134        if (message == null || message.trim().isEmpty())
135            return;
136        if (!spLog.isVisible()) {
137            spLog.setVisible(true);
138            taLog.setVisible(true);
139            adjustLayout();
140        }
141        taLog.append(message);
142        taLog.append("\n");
143        spLog.getVerticalScrollBar().setValue(spLog.getVerticalScrollBar().getMaximum());
144    }
145
146    /**
147     * Sets whether the cancel button is enabled or not.
148     *
149     * @param enabled true, if the cancel button is enabled; false otherwise
150     * @see #setCancelCallback(ActionListener)
151     */
152    public void setCancelEnabled(boolean enabled) {
153        btnCancel.setEnabled(enabled);
154    }
155
156    /**
157     * Enables / disables a button that can be pressed to run the task in background.
158     *
159     * @param value <code>true</code> iff that button should be displayed.
160     * @see #setInBackgroundCallback(ActionListener)
161     */
162    public void setInBackgroundPossible(boolean value) {
163        btnInBackground.setVisible(value);
164    }
165
166    /**
167     * Installs a callback for the cancel button. If callback is null, all action listeners
168     * are removed from the cancel button.
169     *
170     * @param callback the cancel callback
171     */
172    public void setCancelCallback(ActionListener callback) {
173        if (callback == null) {
174            ActionListener[] listeners = btnCancel.getActionListeners();
175            for (ActionListener l: listeners) {
176                btnCancel.removeActionListener(l);
177            }
178        } else {
179            btnCancel.addActionListener(callback);
180        }
181    }
182
183    /**
184     * Installs a callback for the "In background" button. If callback is null, all action listeners
185     * are removed from the cancel button.
186     *
187     * @param callback the cancel callback
188     */
189    public void setInBackgroundCallback(ActionListener callback) {
190        if (callback == null) {
191            ActionListener[] listeners = btnInBackground.getActionListeners();
192            for (ActionListener l: listeners) {
193                btnInBackground.removeActionListener(l);
194            }
195        } else {
196            btnInBackground.addActionListener(callback);
197        }
198    }
199
200    @Override
201    public void updateProgress(int progress) {
202        this.progressBar.setValue(progress);
203        this.progressBar.repaint();
204    }
205
206    /**
207     * Sets the maximum progress value.
208     * @param progressBarMax The value that represents the rightmost point of the progress bar (100%).
209     * @since 11672
210     */
211    public void setMaximumProgress(int progressBarMax) {
212        this.progressBar.setMaximum(progressBarMax);
213    }
214}
Note: See TracBrowser for help on using the repository browser.