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

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

sonar - squid:AssignmentInSubExpressionCheck - Assignments should not be made from within sub-expressions

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