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

Last change on this file since 15835 was 15586, checked in by Don-vip, 4 years ago

code cleanup

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