[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[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;
|
---|
[1184] | 10 | import java.awt.event.ComponentEvent;
|
---|
| 11 | import java.awt.event.ComponentListener;
|
---|
[153] | 12 |
|
---|
| 13 | import javax.swing.BorderFactory;
|
---|
| 14 | import javax.swing.BoundedRangeModel;
|
---|
| 15 | import javax.swing.JButton;
|
---|
| 16 | import javax.swing.JDialog;
|
---|
| 17 | import javax.swing.JLabel;
|
---|
[2319] | 18 | import javax.swing.JOptionPane;
|
---|
[153] | 19 | import javax.swing.JPanel;
|
---|
| 20 | import javax.swing.JProgressBar;
|
---|
[2319] | 21 | import javax.swing.JScrollPane;
|
---|
| 22 | import javax.swing.JTextArea;
|
---|
[1372] | 23 | import javax.swing.UIManager;
|
---|
[153] | 24 |
|
---|
| 25 | import org.openstreetmap.josm.Main;
|
---|
| 26 | import org.openstreetmap.josm.tools.GBC;
|
---|
[2319] | 27 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[153] | 28 |
|
---|
| 29 | public class PleaseWaitDialog extends JDialog {
|
---|
| 30 |
|
---|
[1169] | 31 | private final JProgressBar progressBar = new JProgressBar();
|
---|
[153] | 32 |
|
---|
[1811] | 33 | public final JLabel currentAction = new JLabel("");
|
---|
[1465] | 34 | private final JLabel customText = new JLabel("");
|
---|
[1169] | 35 | public final BoundedRangeModel progress = progressBar.getModel();
|
---|
[2319] | 36 | private JButton btnCancel;
|
---|
| 37 | /** the text area and the scroll pane for the log */
|
---|
| 38 | private JTextArea taLog = new JTextArea(5,50);
|
---|
| 39 | private JScrollPane spLog;
|
---|
[153] | 40 |
|
---|
[1811] | 41 | private void initDialog() {
|
---|
[1169] | 42 | setLayout(new GridBagLayout());
|
---|
| 43 | JPanel pane = new JPanel(new GridBagLayout());
|
---|
| 44 | pane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
|
---|
| 45 | pane.add(currentAction, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
[1465] | 46 | pane.add(customText, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
[1169] | 47 | pane.add(progressBar, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
[2319] | 48 | btnCancel = new JButton(tr("Cancel"));
|
---|
| 49 | btnCancel.setIcon(ImageProvider.get("cancel"));
|
---|
| 50 | btnCancel.setToolTipText(tr("Click to cancel the current operation"));
|
---|
| 51 | pane.add(btnCancel, GBC.eol().anchor(GBC.CENTER));
|
---|
| 52 | GridBagConstraints gc = GBC.eol().fill(GBC.BOTH);
|
---|
| 53 | gc.weighty = 1.0;
|
---|
| 54 | gc.weightx = 1.0;
|
---|
| 55 | pane.add(spLog = new JScrollPane(taLog), gc);
|
---|
| 56 | spLog.setVisible(false);
|
---|
[1169] | 57 | setContentPane(pane);
|
---|
[1465] | 58 | setCustomText("");
|
---|
[2319] | 59 | setLocationRelativeTo(getParent());
|
---|
[1184] | 60 | addComponentListener(new ComponentListener() {
|
---|
| 61 | public void componentHidden(ComponentEvent e) {}
|
---|
| 62 | public void componentMoved(ComponentEvent e) {}
|
---|
| 63 | public void componentShown(ComponentEvent e) {}
|
---|
| 64 | public void componentResized(ComponentEvent ev) {
|
---|
[1856] | 65 | int w = getWidth();
|
---|
| 66 | if(w > 200) {
|
---|
| 67 | Main.pref.putInteger("progressdialog.size",w);
|
---|
| 68 | }
|
---|
[1184] | 69 | }
|
---|
| 70 | });
|
---|
[1169] | 71 | }
|
---|
[1647] | 72 |
|
---|
[2319] | 73 | public PleaseWaitDialog(Component parent) {
|
---|
[3501] | 74 | super(JOptionPane.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL);
|
---|
[1811] | 75 | initDialog();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[1647] | 78 | public void setIndeterminate(boolean newValue) {
|
---|
[1372] | 79 | UIManager.put("ProgressBar.cycleTime", UIManager.getInt("ProgressBar.repaintInterval") * 100);
|
---|
[1281] | 80 | progressBar.setIndeterminate(newValue);
|
---|
| 81 | }
|
---|
[1647] | 82 |
|
---|
[2319] | 83 | protected void adjustLayout() {
|
---|
| 84 | invalidate();
|
---|
| 85 | pack();
|
---|
| 86 | setSize(Main.pref.getInteger("progressdialog.size", 600), getSize().height);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[1465] | 89 | /**
|
---|
| 90 | * Sets a custom text line below currentAction. Can be used to display additional information
|
---|
| 91 | * @param text
|
---|
| 92 | */
|
---|
| 93 | public void setCustomText(String text) {
|
---|
[2319] | 94 | if(text == null || text.trim().length() == 0) {
|
---|
[1465] | 95 | customText.setVisible(false);
|
---|
[2319] | 96 | adjustLayout();
|
---|
[1465] | 97 | return;
|
---|
| 98 | }
|
---|
[2582] | 99 | customText.setText(text);
|
---|
[2319] | 100 | if (!customText.isVisible()) {
|
---|
| 101 | customText.setVisible(true);
|
---|
| 102 | adjustLayout();
|
---|
| 103 | }
|
---|
[1465] | 104 | }
|
---|
[2319] | 105 |
|
---|
| 106 | /**
|
---|
| 107 | * Appends a log message to the progress dialog. If the log area isn't visible yet
|
---|
| 108 | * it becomes visible. The height of the progress dialog is slightly increased too.
|
---|
[2512] | 109 | *
|
---|
[2319] | 110 | * @param message the message to append to the log. Ignore if null or white space only.
|
---|
| 111 | */
|
---|
| 112 | public void appendLogMessage(String message) {
|
---|
| 113 | if (message == null || message.trim().length() ==0 )
|
---|
| 114 | return;
|
---|
| 115 | if (!spLog.isVisible()) {
|
---|
| 116 | spLog.setVisible(true);
|
---|
| 117 | taLog.setVisible(true);
|
---|
| 118 | adjustLayout();
|
---|
| 119 | }
|
---|
| 120 | taLog.append(message);
|
---|
| 121 | taLog.append("\n");
|
---|
| 122 | spLog.getVerticalScrollBar().setValue(spLog.getVerticalScrollBar().getMaximum());
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * Sets whether the cancel button is enabled or not
|
---|
[2512] | 127 | *
|
---|
[2319] | 128 | * @param enabled true, if the cancel button is enabled; false otherwise
|
---|
| 129 | */
|
---|
| 130 | public void setCancelEnabled(boolean enabled) {
|
---|
| 131 | btnCancel.setEnabled(enabled);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * Installs a callback for the cancel button. If callback is null, all action listeners
|
---|
| 136 | * are removed from the cancel button.
|
---|
[2512] | 137 | *
|
---|
[2319] | 138 | * @param callback the cancel callback
|
---|
| 139 | */
|
---|
| 140 | public void setCancelCallback(ActionListener callback) {
|
---|
| 141 | if (callback == null) {
|
---|
| 142 | ActionListener[] listeners = btnCancel.getActionListeners();
|
---|
| 143 | for (ActionListener l: listeners) {
|
---|
| 144 | btnCancel.removeActionListener(l);
|
---|
| 145 | }
|
---|
| 146 | } else {
|
---|
| 147 | btnCancel.addActionListener(callback);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
[153] | 150 | }
|
---|