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

Last change on this file since 8207 was 6380, checked in by Don-vip, 10 years ago

update license/copyright information

  • 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 public final JLabel currentAction = new JLabel("");
35 private final JLabel customText = new JLabel("");
36 public final 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 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 @Override
72 public void componentMoved(ComponentEvent e) {}
73 @Override
74 public void componentShown(ComponentEvent e) {}
75 @Override
76 public void componentResized(ComponentEvent ev) {
77 int w = getWidth();
78 if(w > 200) {
79 Main.pref.putInteger("progressdialog.size",w);
80 }
81 }
82 });
83 }
84
85 /**
86 * Constructs a new {@code PleaseWaitDialog}.
87 * @param parent the {@code Component} from which the dialog is displayed. Can be {@code null}.
88 */
89 public PleaseWaitDialog(Component parent) {
90 super(JOptionPane.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL);
91 initDialog();
92 }
93
94 @Override
95 public void setIndeterminate(boolean newValue) {
96 UIManager.put("ProgressBar.cycleTime", UIManager.getInt("ProgressBar.repaintInterval") * 100);
97 progressBar.setIndeterminate(newValue);
98 }
99
100 protected void adjustLayout() {
101 invalidate();
102 setDropTarget(null); // Workaround to JDK bug 7027598/7100524/7169912 (#8613)
103 pack();
104 setSize(Main.pref.getInteger("progressdialog.size", 600), getSize().height);
105 }
106
107 /**
108 * Sets a custom text line below currentAction. Can be used to display additional information
109 * @param text
110 */
111 @Override
112 public void setCustomText(String text) {
113 if(text == null || text.trim().length() == 0) {
114 customText.setVisible(false);
115 adjustLayout();
116 return;
117 }
118 customText.setText(text);
119 if (!customText.isVisible()) {
120 customText.setVisible(true);
121 adjustLayout();
122 }
123 }
124
125 @Override
126 public void setCurrentAction(String text) {
127 currentAction.setText(text);
128 }
129
130 /**
131 * Appends a log message to the progress dialog. If the log area isn't visible yet
132 * it becomes visible. The height of the progress dialog is slightly increased too.
133 *
134 * @param message the message to append to the log. Ignore if null or white space only.
135 */
136 @Override
137 public void appendLogMessage(String message) {
138 if (message == null || message.trim().length() ==0 )
139 return;
140 if (!spLog.isVisible()) {
141 spLog.setVisible(true);
142 taLog.setVisible(true);
143 adjustLayout();
144 }
145 taLog.append(message);
146 taLog.append("\n");
147 spLog.getVerticalScrollBar().setValue(spLog.getVerticalScrollBar().getMaximum());
148 }
149
150 /**
151 * Sets whether the cancel button is enabled or not
152 *
153 * @param enabled true, if the cancel button is enabled; false otherwise
154 */
155 public void setCancelEnabled(boolean enabled) {
156 btnCancel.setEnabled(enabled);
157 }
158
159 public void setInBackgroundPossible(boolean value) {
160 btnInBackground.setVisible(value);
161 }
162
163 /**
164 * Installs a callback for the cancel button. If callback is null, all action listeners
165 * are removed from the cancel button.
166 *
167 * @param callback the cancel callback
168 */
169 public void setCancelCallback(ActionListener callback) {
170 if (callback == null) {
171 ActionListener[] listeners = btnCancel.getActionListeners();
172 for (ActionListener l: listeners) {
173 btnCancel.removeActionListener(l);
174 }
175 } else {
176 btnCancel.addActionListener(callback);
177 }
178 }
179
180 /**
181 * Installs a callback for the "In background" button. If callback is null, all action listeners
182 * are removed from the cancel button.
183 *
184 * @param callback the cancel callback
185 */
186 public void setInBackgroundCallback(ActionListener callback) {
187 if (callback == null) {
188 ActionListener[] listeners = btnInBackground.getActionListeners();
189 for (ActionListener l: listeners) {
190 btnInBackground.removeActionListener(l);
191 }
192 } else {
193 btnInBackground.addActionListener(callback);
194 }
195 }
196
197 @Override
198 public void updateProgress(int progress) {
199 this.progress.setValue(progress);
200 this.progressBar.repaint();
201 }
202
203}
Note: See TracBrowser for help on using the repository browser.