[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[153] | 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
[2319] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.Component;
|
---|
| 7 | import java.awt.GridBagConstraints;
|
---|
[153] | 8 | import java.awt.GridBagLayout;
|
---|
[2319] | 9 | import java.awt.event.ActionListener;
|
---|
[10173] | 10 | import java.awt.event.ComponentAdapter;
|
---|
[1184] | 11 | import java.awt.event.ComponentEvent;
|
---|
[153] | 12 |
|
---|
| 13 | import javax.swing.BorderFactory;
|
---|
| 14 | import javax.swing.JButton;
|
---|
| 15 | import javax.swing.JDialog;
|
---|
| 16 | import javax.swing.JLabel;
|
---|
| 17 | import javax.swing.JPanel;
|
---|
| 18 | import javax.swing.JProgressBar;
|
---|
[2319] | 19 | import javax.swing.JScrollPane;
|
---|
[1372] | 20 | import javax.swing.UIManager;
|
---|
[153] | 21 |
|
---|
| 22 | import org.openstreetmap.josm.Main;
|
---|
[12675] | 23 | import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor.ProgressMonitorDialog;
|
---|
[10035] | 24 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[5886] | 25 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
[153] | 26 | import org.openstreetmap.josm.tools.GBC;
|
---|
[2319] | 27 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[153] | 28 |
|
---|
[11672] | 29 | /**
|
---|
| 30 | * This is a dialog that displays the progress of an action to the user.
|
---|
| 31 | */
|
---|
[4718] | 32 | public class PleaseWaitDialog extends JDialog implements ProgressMonitorDialog {
|
---|
[153] | 33 |
|
---|
[1169] | 34 | private final JProgressBar progressBar = new JProgressBar();
|
---|
[153] | 35 |
|
---|
[8376] | 36 | private final JLabel currentAction = new JLabel("");
|
---|
[1465] | 37 | private final JLabel customText = new JLabel("");
|
---|
[12285] | 38 |
|
---|
[4718] | 39 | private JButton btnCancel;
|
---|
| 40 | private JButton btnInBackground;
|
---|
[2319] | 41 | /** the text area and the scroll pane for the log */
|
---|
[8510] | 42 | private final JosmTextArea taLog = new JosmTextArea(5, 50);
|
---|
[10179] | 43 | private final JScrollPane spLog = new JScrollPane(taLog);
|
---|
[153] | 44 |
|
---|
[11672] | 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 |
|
---|
[1811] | 55 | private void initDialog() {
|
---|
[1169] | 56 | setLayout(new GridBagLayout());
|
---|
| 57 | JPanel pane = new JPanel(new GridBagLayout());
|
---|
[8510] | 58 | pane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
---|
[1169] | 59 | pane.add(currentAction, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
[1465] | 60 | pane.add(customText, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
[1169] | 61 | pane.add(progressBar, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
[9543] | 62 | JPanel buttons = new JPanel(new GridBagLayout());
|
---|
[2319] | 63 | btnCancel = new JButton(tr("Cancel"));
|
---|
| 64 | btnCancel.setIcon(ImageProvider.get("cancel"));
|
---|
| 65 | btnCancel.setToolTipText(tr("Click to cancel the current operation"));
|
---|
[4718] | 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));
|
---|
[2319] | 71 | GridBagConstraints gc = GBC.eol().fill(GBC.BOTH);
|
---|
| 72 | gc.weighty = 1.0;
|
---|
| 73 | gc.weightx = 1.0;
|
---|
[10179] | 74 | pane.add(spLog, gc);
|
---|
[2319] | 75 | spLog.setVisible(false);
|
---|
[1169] | 76 | setContentPane(pane);
|
---|
[1465] | 77 | setCustomText("");
|
---|
[2319] | 78 | setLocationRelativeTo(getParent());
|
---|
[10173] | 79 | addComponentListener(new ComponentAdapter() {
|
---|
[6084] | 80 | @Override
|
---|
[1184] | 81 | public void componentResized(ComponentEvent ev) {
|
---|
[1856] | 82 | int w = getWidth();
|
---|
[8510] | 83 | if (w > 200) {
|
---|
| 84 | Main.pref.putInteger("progressdialog.size", w);
|
---|
[1856] | 85 | }
|
---|
[1184] | 86 | }
|
---|
| 87 | });
|
---|
[1169] | 88 | }
|
---|
[1647] | 89 |
|
---|
[6084] | 90 | @Override
|
---|
[1647] | 91 | public void setIndeterminate(boolean newValue) {
|
---|
[1372] | 92 | UIManager.put("ProgressBar.cycleTime", UIManager.getInt("ProgressBar.repaintInterval") * 100);
|
---|
[1281] | 93 | progressBar.setIndeterminate(newValue);
|
---|
| 94 | }
|
---|
[1647] | 95 |
|
---|
[2319] | 96 | protected void adjustLayout() {
|
---|
| 97 | invalidate();
|
---|
[5895] | 98 | setDropTarget(null); // Workaround to JDK bug 7027598/7100524/7169912 (#8613)
|
---|
[2319] | 99 | pack();
|
---|
| 100 | setSize(Main.pref.getInteger("progressdialog.size", 600), getSize().height);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[1465] | 103 | /**
|
---|
[11672] | 104 | * Sets a custom text line below currentAction. Can be used to display additional information.
|
---|
[8470] | 105 | * @param text custom text
|
---|
[1465] | 106 | */
|
---|
[6084] | 107 | @Override
|
---|
[1465] | 108 | public void setCustomText(String text) {
|
---|
[8510] | 109 | if (text == null || text.trim().isEmpty()) {
|
---|
[1465] | 110 | customText.setVisible(false);
|
---|
[2319] | 111 | adjustLayout();
|
---|
[1465] | 112 | return;
|
---|
| 113 | }
|
---|
[2582] | 114 | customText.setText(text);
|
---|
[2319] | 115 | if (!customText.isVisible()) {
|
---|
| 116 | customText.setVisible(true);
|
---|
| 117 | adjustLayout();
|
---|
| 118 | }
|
---|
[1465] | 119 | }
|
---|
[2319] | 120 |
|
---|
[6084] | 121 | @Override
|
---|
[4739] | 122 | public void setCurrentAction(String text) {
|
---|
| 123 | currentAction.setText(text);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[2319] | 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.
|
---|
[2512] | 129 | *
|
---|
[2319] | 130 | * @param message the message to append to the log. Ignore if null or white space only.
|
---|
| 131 | */
|
---|
[6084] | 132 | @Override
|
---|
[2319] | 133 | public void appendLogMessage(String message) {
|
---|
[8426] | 134 | if (message == null || message.trim().isEmpty())
|
---|
[2319] | 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 | /**
|
---|
[11672] | 147 | * Sets whether the cancel button is enabled or not.
|
---|
[2512] | 148 | *
|
---|
[2319] | 149 | * @param enabled true, if the cancel button is enabled; false otherwise
|
---|
[11672] | 150 | * @see #setCancelCallback(ActionListener)
|
---|
[2319] | 151 | */
|
---|
| 152 | public void setCancelEnabled(boolean enabled) {
|
---|
| 153 | btnCancel.setEnabled(enabled);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[11672] | 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 | */
|
---|
[4718] | 162 | public void setInBackgroundPossible(boolean value) {
|
---|
| 163 | btnInBackground.setVisible(value);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[2319] | 166 | /**
|
---|
| 167 | * Installs a callback for the cancel button. If callback is null, all action listeners
|
---|
| 168 | * are removed from the cancel button.
|
---|
[2512] | 169 | *
|
---|
[2319] | 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 | }
|
---|
[4718] | 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
|
---|
[4739] | 188 | */
|
---|
[4718] | 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) {
|
---|
[11672] | 202 | this.progressBar.setValue(progress);
|
---|
[5534] | 203 | this.progressBar.repaint();
|
---|
[4718] | 204 | }
|
---|
[11672] | 205 |
|
---|
| 206 | /**
|
---|
| 207 | * Sets the maximum progress value.
|
---|
| 208 | * @param progressBarMax The value that represents the rightmost point of the progress bar (100%).
|
---|
[11673] | 209 | * @since 11672
|
---|
[11672] | 210 | */
|
---|
| 211 | public void setMaximumProgress(int progressBarMax) {
|
---|
| 212 | this.progressBar.setMaximum(progressBarMax);
|
---|
| 213 | }
|
---|
[153] | 214 | }
|
---|