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

Last change on this file since 12102 was 11673, checked in by michael2402, 7 years ago

Add more Javadoc for progress monitors.

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