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

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

see #15182 - move the Swing-based ProgressMonitor implementations from gui.progress to gui.progress.swing. Progress monitor concept is used in very large parts of JOSM, a console-based implementation could be added later

  • Property svn:eol-style set to native
File size: 7.3 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[153]2package org.openstreetmap.josm.gui;
3
[2319]4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GridBagConstraints;
[153]8import java.awt.GridBagLayout;
[2319]9import java.awt.event.ActionListener;
[10173]10import java.awt.event.ComponentAdapter;
[1184]11import java.awt.event.ComponentEvent;
[153]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;
[2319]19import javax.swing.JScrollPane;
[1372]20import javax.swing.UIManager;
[153]21
22import org.openstreetmap.josm.Main;
[12675]23import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor.ProgressMonitorDialog;
[10035]24import org.openstreetmap.josm.gui.util.GuiHelper;
[5886]25import org.openstreetmap.josm.gui.widgets.JosmTextArea;
[153]26import org.openstreetmap.josm.tools.GBC;
[2319]27import org.openstreetmap.josm.tools.ImageProvider;
[153]28
[11672]29/**
30 * This is a dialog that displays the progress of an action to the user.
31 */
[4718]32public class PleaseWaitDialog extends JDialog implements ProgressMonitorDialog {
[153]33
[1169]34 private final JProgressBar progressBar = new JProgressBar();
[153]35
[8376]36 private final JLabel currentAction = new JLabel("");
[1465]37 private final JLabel customText = new JLabel("");
[12285]38
[4718]39 private JButton btnCancel;
40 private JButton btnInBackground;
[2319]41 /** the text area and the scroll pane for the log */
[8510]42 private final JosmTextArea taLog = new JosmTextArea(5, 50);
[10179]43 private final JScrollPane spLog = new JScrollPane(taLog);
[153]44
[11672]45
46 /**
47 * Constructs a new {@code PleaseWaitDialog}.
48 * @param parent the {@code Component} from which the dialog is displayed. Can be {@code null}.
49 */
50 public PleaseWaitDialog(Component parent) {
51 super(GuiHelper.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL);
52 initDialog();
53 }
54
[1811]55 private void initDialog() {
[1169]56 setLayout(new GridBagLayout());
57 JPanel pane = new JPanel(new GridBagLayout());
[8510]58 pane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
[1169]59 pane.add(currentAction, GBC.eol().fill(GBC.HORIZONTAL));
[1465]60 pane.add(customText, GBC.eol().fill(GBC.HORIZONTAL));
[1169]61 pane.add(progressBar, GBC.eop().fill(GBC.HORIZONTAL));
[9543]62 JPanel buttons = new JPanel(new GridBagLayout());
[2319]63 btnCancel = new JButton(tr("Cancel"));
64 btnCancel.setIcon(ImageProvider.get("cancel"));
65 btnCancel.setToolTipText(tr("Click to cancel the current operation"));
[4718]66 buttons.add(btnCancel);
67 btnInBackground = new JButton(tr("In background"));
68 btnInBackground.setToolTipText(tr("Click to run job in background"));
69 buttons.add(btnInBackground, GBC.std().fill(GBC.VERTICAL).insets(5, 0, 0, 0));
70 pane.add(buttons, GBC.eol().anchor(GBC.CENTER));
[2319]71 GridBagConstraints gc = GBC.eol().fill(GBC.BOTH);
72 gc.weighty = 1.0;
73 gc.weightx = 1.0;
[10179]74 pane.add(spLog, gc);
[2319]75 spLog.setVisible(false);
[1169]76 setContentPane(pane);
[1465]77 setCustomText("");
[2319]78 setLocationRelativeTo(getParent());
[10173]79 addComponentListener(new ComponentAdapter() {
[6084]80 @Override
[1184]81 public void componentResized(ComponentEvent ev) {
[1856]82 int w = getWidth();
[8510]83 if (w > 200) {
84 Main.pref.putInteger("progressdialog.size", w);
[1856]85 }
[1184]86 }
87 });
[1169]88 }
[1647]89
[6084]90 @Override
[1647]91 public void setIndeterminate(boolean newValue) {
[1372]92 UIManager.put("ProgressBar.cycleTime", UIManager.getInt("ProgressBar.repaintInterval") * 100);
[1281]93 progressBar.setIndeterminate(newValue);
94 }
[1647]95
[2319]96 protected void adjustLayout() {
97 invalidate();
[5895]98 setDropTarget(null); // Workaround to JDK bug 7027598/7100524/7169912 (#8613)
[2319]99 pack();
100 setSize(Main.pref.getInteger("progressdialog.size", 600), getSize().height);
101 }
102
[1465]103 /**
[11672]104 * Sets a custom text line below currentAction. Can be used to display additional information.
[8470]105 * @param text custom text
[1465]106 */
[6084]107 @Override
[1465]108 public void setCustomText(String text) {
[8510]109 if (text == null || text.trim().isEmpty()) {
[1465]110 customText.setVisible(false);
[2319]111 adjustLayout();
[1465]112 return;
113 }
[2582]114 customText.setText(text);
[2319]115 if (!customText.isVisible()) {
116 customText.setVisible(true);
117 adjustLayout();
118 }
[1465]119 }
[2319]120
[6084]121 @Override
[4739]122 public void setCurrentAction(String text) {
123 currentAction.setText(text);
124 }
125
[2319]126 /**
127 * Appends a log message to the progress dialog. If the log area isn't visible yet
128 * it becomes visible. The height of the progress dialog is slightly increased too.
[2512]129 *
[2319]130 * @param message the message to append to the log. Ignore if null or white space only.
131 */
[6084]132 @Override
[2319]133 public void appendLogMessage(String message) {
[8426]134 if (message == null || message.trim().isEmpty())
[2319]135 return;
136 if (!spLog.isVisible()) {
137 spLog.setVisible(true);
138 taLog.setVisible(true);
139 adjustLayout();
140 }
141 taLog.append(message);
142 taLog.append("\n");
143 spLog.getVerticalScrollBar().setValue(spLog.getVerticalScrollBar().getMaximum());
144 }
145
146 /**
[11672]147 * Sets whether the cancel button is enabled or not.
[2512]148 *
[2319]149 * @param enabled true, if the cancel button is enabled; false otherwise
[11672]150 * @see #setCancelCallback(ActionListener)
[2319]151 */
152 public void setCancelEnabled(boolean enabled) {
153 btnCancel.setEnabled(enabled);
154 }
155
[11672]156 /**
157 * Enables / disables a button that can be pressed to run the task in background.
158 *
159 * @param value <code>true</code> iff that button should be displayed.
160 * @see #setInBackgroundCallback(ActionListener)
161 */
[4718]162 public void setInBackgroundPossible(boolean value) {
163 btnInBackground.setVisible(value);
164 }
165
[2319]166 /**
167 * Installs a callback for the cancel button. If callback is null, all action listeners
168 * are removed from the cancel button.
[2512]169 *
[2319]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 }
[4718]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
[4739]188 */
[4718]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) {
[11672]202 this.progressBar.setValue(progress);
[5534]203 this.progressBar.repaint();
[4718]204 }
[11672]205
206 /**
207 * Sets the maximum progress value.
208 * @param progressBarMax The value that represents the rightmost point of the progress bar (100%).
[11673]209 * @since 11672
[11672]210 */
211 public void setMaximumProgress(int progressBarMax) {
212 this.progressBar.setMaximum(progressBarMax);
213 }
[153]214}
Note: See TracBrowser for help on using the repository browser.