1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.awt.event.ComponentEvent;
|
---|
11 | import java.awt.event.ComponentListener;
|
---|
12 |
|
---|
13 | import javax.swing.BorderFactory;
|
---|
14 | import javax.swing.BoundedRangeModel;
|
---|
15 | import javax.swing.JButton;
|
---|
16 | import javax.swing.JDialog;
|
---|
17 | import javax.swing.JLabel;
|
---|
18 | import javax.swing.JOptionPane;
|
---|
19 | import javax.swing.JPanel;
|
---|
20 | import javax.swing.JProgressBar;
|
---|
21 | import javax.swing.JScrollPane;
|
---|
22 | import javax.swing.JTextArea;
|
---|
23 | import javax.swing.UIManager;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor.ProgressMonitorDialog;
|
---|
27 | import org.openstreetmap.josm.tools.GBC;
|
---|
28 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
29 |
|
---|
30 | public 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 JTextArea taLog = new JTextArea(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 | public void componentHidden(ComponentEvent e) {}
|
---|
70 | public void componentMoved(ComponentEvent e) {}
|
---|
71 | public void componentShown(ComponentEvent e) {}
|
---|
72 | public void componentResized(ComponentEvent ev) {
|
---|
73 | int w = getWidth();
|
---|
74 | if(w > 200) {
|
---|
75 | Main.pref.putInteger("progressdialog.size",w);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | });
|
---|
79 | }
|
---|
80 |
|
---|
81 | public PleaseWaitDialog(Component parent) {
|
---|
82 | super(JOptionPane.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL);
|
---|
83 | initDialog();
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void setIndeterminate(boolean newValue) {
|
---|
87 | UIManager.put("ProgressBar.cycleTime", UIManager.getInt("ProgressBar.repaintInterval") * 100);
|
---|
88 | progressBar.setIndeterminate(newValue);
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected void adjustLayout() {
|
---|
92 | invalidate();
|
---|
93 | pack();
|
---|
94 | setSize(Main.pref.getInteger("progressdialog.size", 600), getSize().height);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Sets a custom text line below currentAction. Can be used to display additional information
|
---|
99 | * @param text
|
---|
100 | */
|
---|
101 | public void setCustomText(String text) {
|
---|
102 | if(text == null || text.trim().length() == 0) {
|
---|
103 | customText.setVisible(false);
|
---|
104 | adjustLayout();
|
---|
105 | return;
|
---|
106 | }
|
---|
107 | customText.setText(text);
|
---|
108 | if (!customText.isVisible()) {
|
---|
109 | customText.setVisible(true);
|
---|
110 | adjustLayout();
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void setCurrentAction(String text) {
|
---|
115 | currentAction.setText(text);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Appends a log message to the progress dialog. If the log area isn't visible yet
|
---|
120 | * it becomes visible. The height of the progress dialog is slightly increased too.
|
---|
121 | *
|
---|
122 | * @param message the message to append to the log. Ignore if null or white space only.
|
---|
123 | */
|
---|
124 | public void appendLogMessage(String message) {
|
---|
125 | if (message == null || message.trim().length() ==0 )
|
---|
126 | return;
|
---|
127 | if (!spLog.isVisible()) {
|
---|
128 | spLog.setVisible(true);
|
---|
129 | taLog.setVisible(true);
|
---|
130 | adjustLayout();
|
---|
131 | }
|
---|
132 | taLog.append(message);
|
---|
133 | taLog.append("\n");
|
---|
134 | spLog.getVerticalScrollBar().setValue(spLog.getVerticalScrollBar().getMaximum());
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Sets whether the cancel button is enabled or not
|
---|
139 | *
|
---|
140 | * @param enabled true, if the cancel button is enabled; false otherwise
|
---|
141 | */
|
---|
142 | public void setCancelEnabled(boolean enabled) {
|
---|
143 | btnCancel.setEnabled(enabled);
|
---|
144 | }
|
---|
145 |
|
---|
146 | public void setInBackgroundPossible(boolean value) {
|
---|
147 | btnInBackground.setVisible(value);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Installs a callback for the cancel button. If callback is null, all action listeners
|
---|
152 | * are removed from the cancel button.
|
---|
153 | *
|
---|
154 | * @param callback the cancel callback
|
---|
155 | */
|
---|
156 | public void setCancelCallback(ActionListener callback) {
|
---|
157 | if (callback == null) {
|
---|
158 | ActionListener[] listeners = btnCancel.getActionListeners();
|
---|
159 | for (ActionListener l: listeners) {
|
---|
160 | btnCancel.removeActionListener(l);
|
---|
161 | }
|
---|
162 | } else {
|
---|
163 | btnCancel.addActionListener(callback);
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Installs a callback for the "In background" button. If callback is null, all action listeners
|
---|
169 | * are removed from the cancel button.
|
---|
170 | *
|
---|
171 | * @param callback the cancel callback
|
---|
172 | */
|
---|
173 | public void setInBackgroundCallback(ActionListener callback) {
|
---|
174 | if (callback == null) {
|
---|
175 | ActionListener[] listeners = btnInBackground.getActionListeners();
|
---|
176 | for (ActionListener l: listeners) {
|
---|
177 | btnInBackground.removeActionListener(l);
|
---|
178 | }
|
---|
179 | } else {
|
---|
180 | btnInBackground.addActionListener(callback);
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | @Override
|
---|
185 | public void updateProgress(int progress) {
|
---|
186 | this.progress.setValue(progress);
|
---|
187 | }
|
---|
188 |
|
---|
189 | }
|
---|