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

Last change on this file since 9231 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

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