1 2 24 package org.enhydra.tool.common; 25 26 import org.enhydra.tool.common.event.CancelEvent; 28 import org.enhydra.tool.common.event.CancelListener; 29 import org.enhydra.tool.common.event.ProgressEvent; 30 import org.enhydra.tool.common.event.ProgressListener; 31 32 import java.awt.*; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 import java.beans.*; 37 import java.util.Vector ; 38 import javax.swing.*; 39 import java.util.ResourceBundle ; 40 41 public class ProgressMeter extends DialogPanel implements ProgressListener { 43 static ResourceBundle res = 44 ResourceBundle.getBundle("org.enhydra.tool.common.Res"); 46 private LocalButtonListener buttonListener = null; 48 private Vector cancelVector = new Vector (); 49 private JPanel panelMain = null; 50 private GridBagLayout layoutMain = null; 51 private JProgressBar progressBar = null; 52 private JTextArea textMessage = null; 53 private JButton buttonCancel = null; 54 private boolean disposeOnClose = true; 55 private boolean cancel = false; 56 57 public static void main(String [] args) { 58 SwingUtil.setLookAndFeelToSystem(); 59 ProgressMeter meter; 60 61 meter = new ProgressMeter(); 62 meter.setStandalone(true); 63 meter.startDialogThread(); 64 for (int i = 0; i < 101; i++) { 65 ProgressEvent event = new ProgressEvent(meter, i, 66 new String () + i); 67 68 try { 69 Thread.sleep(100); 70 } catch (Exception e) { 71 e.printStackTrace(); 72 } 73 meter.onProgress(event); 74 } 75 } 76 77 public ProgressMeter() { 78 try { 79 jbInit(); 80 pmInit(); 81 } catch (Exception ex) { 82 ex.printStackTrace(); 83 } 84 } 85 86 public CancelListener[] getCancelListeners() { 87 CancelListener[] cancelListeners = new CancelListener[0]; 88 89 cancelListeners = new CancelListener[cancelVector.size()]; 90 cancelListeners = 91 (CancelListener[]) cancelVector.toArray(cancelListeners); 92 return cancelListeners; 93 } 94 95 public void addCancelListener(CancelListener l) { 96 if (!cancelVector.contains(l)) { 97 cancelVector.addElement(l); 98 } 99 } 100 101 public void removeCancelListener(CancelListener l) { 102 if (cancelVector.contains(l)) { 103 cancelVector.removeElement(l); 104 } 105 } 106 107 private void notifyCancel() { 108 CancelListener[] cancelListeners = getCancelListeners(); 109 110 if (cancelListeners.length == 0) { 111 112 } else { 114 CancelEvent event = null; 115 116 event = new CancelEvent(this); 117 for (int i = 0; i < cancelListeners.length; i++) { 118 cancelListeners[i].onCancel(event); 119 } 120 } 121 } 122 123 public void onProgress(ProgressEvent event) { 124 if (event.getProgress() > 99) { 125 clearDialog(); 126 } else if (isCancelled()) { 127 128 } else { 130 updateProgress(event.getMessage(), event.getProgress()); 131 } 132 } 133 134 public void setDisposeOnClose(boolean b) { 135 disposeOnClose = b; 136 } 137 138 public boolean isDisposeOnClose() { 139 return disposeOnClose; 140 } 141 142 public boolean isCancelled() { 143 return cancel; 144 } 145 146 protected void clearDialog() { 149 cancelVector.clear(); 150 cancelVector.trimToSize(); 151 if (getDialog() != null) { 152 if (isDisposeOnClose()) { 153 super.clearDialog(); 154 } else { 155 hideDialog(); 156 clearProgress(); 157 } 158 } 159 } 160 161 private void updateProgress(final String message, final int progress) { 164 ProgressUpdate runUpdate = null; 165 166 runUpdate = new ProgressUpdate(textMessage, message, progressBar, 167 progress); 168 try { 169 if (SwingUtilities.isEventDispatchThread()) { 170 runUpdate.run(); 171 } else { 172 SwingUtilities.invokeAndWait(runUpdate); 173 } 174 } catch (Exception e) { 175 e.printStackTrace(); 176 } 177 } 178 179 private void clearProgress() { 180 textMessage.setText(new String ()); 181 progressBar.setValue(1); 182 cancel = false; 183 } 184 185 private void doCancel() { 186 setOption(DialogPanel.CANCEL_OPTION); 187 textMessage.setText(res.getString("Cancelling_operation_")); 188 cancel = true; 189 notifyCancel(); 190 } 191 192 private void pmInit() { 193 JLabel label = new JLabel(); 194 Dimension d = new Dimension(); 195 196 d.height = 25; 197 d.width = 300; 198 textMessage.setFont(label.getFont()); 199 textMessage.setPreferredSize(d); 200 progressBar.getModel().setMinimum(0); 201 progressBar.getModel().setMaximum(100); 202 clearProgress(); 203 buttonListener = new LocalButtonListener(); 204 buttonCancel.addActionListener(buttonListener); 205 } 206 207 private void jbInit() throws Exception { 208 panelMain = (JPanel) Beans.instantiate(getClass().getClassLoader(), 209 JPanel.class.getName()); 210 layoutMain = 211 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 212 GridBagLayout.class.getName()); 213 progressBar = 214 (JProgressBar) Beans.instantiate(getClass().getClassLoader(), 215 JProgressBar.class.getName()); 216 textMessage = 217 (JTextArea) Beans.instantiate(getClass().getClassLoader(), 218 JTextArea.class.getName()); 219 buttonCancel = 220 (JButton) Beans.instantiate(getClass().getClassLoader(), 221 JButton.class.getName()); 222 panelMain.setLayout(layoutMain); 223 textMessage.setWrapStyleWord(true); 224 textMessage.setLineWrap(true); 225 textMessage.setRequestFocusEnabled(false); 226 textMessage.setEnabled(false); 227 textMessage.setDisabledTextColor(SystemColor.controlText); 228 textMessage.setBackground(SystemColor.control); 229 textMessage.setEditable(false); 230 buttonCancel.setText(res.getString("Cancel")); 231 panelMain.add(progressBar, 232 new GridBagConstraints(0, 2, 2, 1, 0.1, 0.0, 233 GridBagConstraints.CENTER, 234 GridBagConstraints.HORIZONTAL, 235 new Insets(5, 10, 20, 10), 0, 236 0)); 237 panelMain.add(textMessage, 238 new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0, 239 GridBagConstraints.CENTER, 240 GridBagConstraints.BOTH, 241 new Insets(20, 10, 5, 5), 0, 242 10)); 243 panelMain.add(buttonCancel, 244 new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 245 GridBagConstraints.CENTER, 246 GridBagConstraints.NONE, 247 new Insets(5, 5, 5, 10), 0, 0)); 248 this.add(panelMain); 249 } 250 251 private class LocalButtonListener implements ActionListener { 252 public void actionPerformed(ActionEvent e) { 253 Object source = e.getSource(); 254 255 if (source == buttonCancel) { 256 doCancel(); 257 } 258 } 259 260 } 261 private class ProgressUpdate implements Runnable { 262 int prog = 0; 263 String mess = new String (); 264 JTextArea ta = null; 265 JProgressBar bar = null; 266 267 protected ProgressUpdate(JTextArea t, String m, JProgressBar b, 268 int p) { 269 ta = t; 270 mess = m; 271 bar = b; 272 prog = p; 273 } 274 275 synchronized public void run() { 277 bar.getModel().setValueIsAdjusting(true); 278 if (!mess.equals(ta.getText())) { 279 ta.setText(mess); 280 } 281 bar.getModel().setValue(prog); 282 bar.getModel().setValueIsAdjusting(false); 283 bar.updateUI(); 284 bar.repaint(); 285 repaint(); 286 } 287 288 } 289 } 290 | Popular Tags |