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