001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.Component; 007import java.awt.GridBagConstraints; 008import java.awt.GridBagLayout; 009import java.awt.event.ActionListener; 010import java.awt.event.ComponentEvent; 011import java.awt.event.ComponentListener; 012 013import javax.swing.BorderFactory; 014import javax.swing.BoundedRangeModel; 015import javax.swing.JButton; 016import javax.swing.JDialog; 017import javax.swing.JLabel; 018import javax.swing.JOptionPane; 019import javax.swing.JPanel; 020import javax.swing.JProgressBar; 021import javax.swing.JScrollPane; 022import javax.swing.UIManager; 023 024import org.openstreetmap.josm.Main; 025import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor.ProgressMonitorDialog; 026import org.openstreetmap.josm.gui.widgets.JosmTextArea; 027import org.openstreetmap.josm.tools.GBC; 028import org.openstreetmap.josm.tools.ImageProvider; 029 030public class PleaseWaitDialog extends JDialog implements ProgressMonitorDialog { 031 032 private final JProgressBar progressBar = new JProgressBar(); 033 034 private final JLabel currentAction = new JLabel(""); 035 private final JLabel customText = new JLabel(""); 036 public final transient BoundedRangeModel progress = progressBar.getModel(); 037 private JButton btnCancel; 038 private JButton btnInBackground; 039 /** the text area and the scroll pane for the log */ 040 private final JosmTextArea taLog = new JosmTextArea(5, 50); 041 private JScrollPane spLog; 042 043 private void initDialog() { 044 setLayout(new GridBagLayout()); 045 JPanel pane = new JPanel(new GridBagLayout()); 046 pane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 047 pane.add(currentAction, GBC.eol().fill(GBC.HORIZONTAL)); 048 pane.add(customText, GBC.eol().fill(GBC.HORIZONTAL)); 049 pane.add(progressBar, GBC.eop().fill(GBC.HORIZONTAL)); 050 JPanel buttons = new JPanel(new GridBagLayout()); 051 btnCancel = new JButton(tr("Cancel")); 052 btnCancel.setIcon(ImageProvider.get("cancel")); 053 btnCancel.setToolTipText(tr("Click to cancel the current operation")); 054 buttons.add(btnCancel); 055 btnInBackground = new JButton(tr("In background")); 056 btnInBackground.setToolTipText(tr("Click to run job in background")); 057 buttons.add(btnInBackground, GBC.std().fill(GBC.VERTICAL).insets(5, 0, 0, 0)); 058 pane.add(buttons, GBC.eol().anchor(GBC.CENTER)); 059 GridBagConstraints gc = GBC.eol().fill(GBC.BOTH); 060 gc.weighty = 1.0; 061 gc.weightx = 1.0; 062 pane.add(spLog = new JScrollPane(taLog), gc); 063 spLog.setVisible(false); 064 setContentPane(pane); 065 setCustomText(""); 066 setLocationRelativeTo(getParent()); 067 addComponentListener(new ComponentListener() { 068 @Override 069 public void componentHidden(ComponentEvent e) {} 070 071 @Override 072 public void componentMoved(ComponentEvent e) {} 073 074 @Override 075 public void componentShown(ComponentEvent e) {} 076 077 @Override 078 public void componentResized(ComponentEvent ev) { 079 int w = getWidth(); 080 if (w > 200) { 081 Main.pref.putInteger("progressdialog.size", w); 082 } 083 } 084 }); 085 } 086 087 /** 088 * Constructs a new {@code PleaseWaitDialog}. 089 * @param parent the {@code Component} from which the dialog is displayed. Can be {@code null}. 090 */ 091 public PleaseWaitDialog(Component parent) { 092 super(JOptionPane.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL); 093 initDialog(); 094 } 095 096 @Override 097 public void setIndeterminate(boolean newValue) { 098 UIManager.put("ProgressBar.cycleTime", UIManager.getInt("ProgressBar.repaintInterval") * 100); 099 progressBar.setIndeterminate(newValue); 100 } 101 102 protected void adjustLayout() { 103 invalidate(); 104 setDropTarget(null); // Workaround to JDK bug 7027598/7100524/7169912 (#8613) 105 pack(); 106 setSize(Main.pref.getInteger("progressdialog.size", 600), getSize().height); 107 } 108 109 /** 110 * Sets a custom text line below currentAction. Can be used to display additional information 111 * @param text custom text 112 */ 113 @Override 114 public void setCustomText(String text) { 115 if (text == null || text.trim().isEmpty()) { 116 customText.setVisible(false); 117 adjustLayout(); 118 return; 119 } 120 customText.setText(text); 121 if (!customText.isVisible()) { 122 customText.setVisible(true); 123 adjustLayout(); 124 } 125 } 126 127 @Override 128 public void setCurrentAction(String text) { 129 currentAction.setText(text); 130 } 131 132 /** 133 * Appends a log message to the progress dialog. If the log area isn't visible yet 134 * it becomes visible. The height of the progress dialog is slightly increased too. 135 * 136 * @param message the message to append to the log. Ignore if null or white space only. 137 */ 138 @Override 139 public void appendLogMessage(String message) { 140 if (message == null || message.trim().isEmpty()) 141 return; 142 if (!spLog.isVisible()) { 143 spLog.setVisible(true); 144 taLog.setVisible(true); 145 adjustLayout(); 146 } 147 taLog.append(message); 148 taLog.append("\n"); 149 spLog.getVerticalScrollBar().setValue(spLog.getVerticalScrollBar().getMaximum()); 150 } 151 152 /** 153 * Sets whether the cancel button is enabled or not 154 * 155 * @param enabled true, if the cancel button is enabled; false otherwise 156 */ 157 public void setCancelEnabled(boolean enabled) { 158 btnCancel.setEnabled(enabled); 159 } 160 161 public void setInBackgroundPossible(boolean value) { 162 btnInBackground.setVisible(value); 163 } 164 165 /** 166 * Installs a callback for the cancel button. If callback is null, all action listeners 167 * are removed from the cancel button. 168 * 169 * @param callback the cancel callback 170 */ 171 public void setCancelCallback(ActionListener callback) { 172 if (callback == null) { 173 ActionListener[] listeners = btnCancel.getActionListeners(); 174 for (ActionListener l: listeners) { 175 btnCancel.removeActionListener(l); 176 } 177 } else { 178 btnCancel.addActionListener(callback); 179 } 180 } 181 182 /** 183 * Installs a callback for the "In background" button. If callback is null, all action listeners 184 * are removed from the cancel button. 185 * 186 * @param callback the cancel callback 187 */ 188 public void setInBackgroundCallback(ActionListener callback) { 189 if (callback == null) { 190 ActionListener[] listeners = btnInBackground.getActionListeners(); 191 for (ActionListener l: listeners) { 192 btnInBackground.removeActionListener(l); 193 } 194 } else { 195 btnInBackground.addActionListener(callback); 196 } 197 } 198 199 @Override 200 public void updateProgress(int progress) { 201 this.progress.setValue(progress); 202 this.progressBar.repaint(); 203 } 204}