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

Last change on this file since 10035 was 10035, checked in by Don-vip, 8 years ago

code refactoring to ease creation of unit tests by avoiding as much as possible HeadlessException

  • Property svn:eol-style set to native
File size: 7.0 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.ComponentEvent;
11import java.awt.event.ComponentListener;
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
30public 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 JScrollPane spLog;
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 = new JScrollPane(taLog), gc);
63 spLog.setVisible(false);
64 setContentPane(pane);
65 setCustomText("");
66 setLocationRelativeTo(getParent());
67 addComponentListener(new ComponentListener() {
68 @Override
69 public void componentHidden(ComponentEvent e) {}
70
71 @Override
72 public void componentMoved(ComponentEvent e) {}
73
74 @Override
75 public void componentShown(ComponentEvent e) {}
76
77 @Override
78 public void componentResized(ComponentEvent ev) {
79 int w = getWidth();
80 if (w > 200) {
81 Main.pref.putInteger("progressdialog.size", w);
82 }
83 }
84 });
85 }
86
87 /**
88 * Constructs a new {@code PleaseWaitDialog}.
89 * @param parent the {@code Component} from which the dialog is displayed. Can be {@code null}.
90 */
91 public PleaseWaitDialog(Component parent) {
92 super(GuiHelper.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL);
93 initDialog();
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 */
157 public void setCancelEnabled(boolean enabled) {
158 btnCancel.setEnabled(enabled);
159 }
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.progress.setValue(progress);
202 this.progressBar.repaint();
203 }
204}
Note: See TracBrowser for help on using the repository browser.