1 19 20 package org.netbeans.modules.j2ee.deployment.impl.ui; 21 import java.awt.GridBagConstraints ; 22 import java.awt.Insets ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import javax.enterprise.deploy.shared.StateType ; 26 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 27 import javax.enterprise.deploy.spi.status.ProgressEvent ; 28 import javax.enterprise.deploy.spi.status.ProgressListener ; 29 import javax.enterprise.deploy.spi.status.ProgressObject ; 30 import javax.swing.JButton ; 31 import javax.swing.JComponent ; 32 import javax.swing.JDialog ; 33 import javax.swing.JLabel ; 34 import javax.swing.JPanel ; 35 import org.netbeans.api.progress.ProgressHandle; 36 import org.netbeans.api.progress.ProgressHandleFactory; 37 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 38 import org.openide.util.Mutex; 39 import org.openide.util.NbBundle; 40 import org.openide.util.Utilities; 41 import org.openide.windows.WindowManager; 42 43 44 51 public class ProgressUI implements ProgressListener { 52 53 private String title; 54 private boolean modal; 55 private Deployment.Logger logger; 56 57 private ProgressHandle handle; 58 private ProgressObject progObj; 59 60 private JDialog dialog; 61 private JLabel messageLabel; 62 private String lastMessage; 63 private JComponent progressComponent; 64 private boolean finished; 65 66 67 private boolean completionEventProcessed; 68 69 70 public ProgressUI(String title, boolean modal) { 71 this(title, modal, null); 72 } 73 74 public ProgressUI(String title, boolean modal, Deployment.Logger logger) { 75 this.modal = modal; 76 this.title = title; 77 this.logger = logger; 78 handle = ProgressHandleFactory.createHandle(title); 79 } 80 81 82 public void start() { 83 if (modal) { 84 progressComponent = ProgressHandleFactory.createProgressComponent(handle); 85 } 86 handle.start(); 87 } 88 89 91 public void showProgressDialog() { 92 if (finished) { 93 return; } 95 dialog = new JDialog (WindowManager.getDefault().getMainWindow(), title, true); 96 dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 97 dialog.getContentPane().add(createProgressDialog( 98 handle, 99 lastMessage != null ? lastMessage : title)); 100 dialog.pack(); 101 dialog.setBounds(Utilities.findCenterBounds(dialog.getSize())); 102 dialog.setLocationRelativeTo(WindowManager.getDefault().getMainWindow()); 103 dialog.setVisible(true); 104 } 105 106 107 public void progress(final String message) { 108 handle.progress(message); 109 if (modal) { 110 Mutex.EVENT.readAccess(new Runnable () { 111 public void run() { 112 if (messageLabel != null) { 113 messageLabel.setText(message); 114 } else { 115 lastMessage = message; 116 } 117 } 118 }); 119 } 120 log(message); 121 } 122 123 124 public void finish() { 125 handle.finish(); 126 if (progObj != null) { 127 progObj.removeProgressListener(this); 128 progObj = null; 129 } 130 Mutex.EVENT.readAccess(new Runnable () { 131 public void run() { 132 finished = true; 133 if (dialog != null) { 134 dialog.setVisible(false); 135 dialog.dispose(); 136 dialog = null; 137 } 138 } 139 }); 140 } 141 142 143 public void failed(String message) { 144 finish(); 145 if (logger != null) { 146 log(message); 147 } 148 } 149 150 151 public void setProgressObject(ProgressObject obj) { 152 if (progObj != null) { 154 progObj.removeProgressListener(this); 155 } 156 progObj = obj; 157 if (progObj != null) { 158 synchronized (this) { 159 completionEventProcessed = false; 160 } 161 progObj.addProgressListener(this); 162 DeploymentStatus status = progObj.getDeploymentStatus(); 165 if (status.isCompleted() || status.isFailed()) { 166 handleDeploymentStatus(status); 167 } 168 } 169 } 170 171 172 public void setLogger(Deployment.Logger logger) { 173 this.logger = logger; 174 } 175 176 178 private void log(String msg) { 179 if (logger != null && msg != null) { 180 logger.log(msg); 181 } 182 } 183 184 private JComponent createProgressDialog(ProgressHandle handle, String message) { 185 JPanel panel = new JPanel (); 186 messageLabel = new JLabel (); 187 188 panel.setLayout(new java.awt.GridBagLayout ()); 189 190 messageLabel.setText(message); 191 GridBagConstraints gridBagConstraints = new GridBagConstraints (); 192 gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; 193 gridBagConstraints.insets = new Insets (12, 12, 0, 12); 194 panel.add(messageLabel, gridBagConstraints); 195 196 gridBagConstraints = new GridBagConstraints (); 197 gridBagConstraints.gridx = 0; 198 gridBagConstraints.gridy = 1; 199 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 200 gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; 201 gridBagConstraints.weightx = 1.0; 202 gridBagConstraints.insets = new Insets (5, 12, 0, 12); 203 panel.add(progressComponent, gridBagConstraints); 204 205 gridBagConstraints = new GridBagConstraints (); 206 gridBagConstraints.gridx = 0; 207 gridBagConstraints.gridy = 2; 208 gridBagConstraints.anchor = GridBagConstraints.CENTER; 209 gridBagConstraints.weightx = 1.0; 210 gridBagConstraints.insets = new Insets (11, 12, 12, 12); 211 JButton cancel = new JButton (NbBundle.getMessage(ProgressUI.class,"LBL_Cancel")); 212 cancel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ProgressUI.class,"AD_Cancel")); 213 cancel.addActionListener(new ActionListener () { 214 public void actionPerformed(ActionEvent e) { 215 finish(); 216 } 217 }); 218 panel.add(cancel, gridBagConstraints); 219 220 return panel; 221 } 222 223 225 public void handleProgressEvent(ProgressEvent progressEvent) { 226 handleDeploymentStatus(progressEvent.getDeploymentStatus()); 227 } 228 229 private void handleDeploymentStatus(DeploymentStatus status) { 230 synchronized (this) { 231 if (completionEventProcessed) { 232 return; 234 } 235 if (status.isCompleted() || status.isFailed()) { 236 completionEventProcessed = true; 237 } 238 } 239 if (status.isFailed()) { 240 failed(status.getMessage()); 241 } else { 242 progress(status.getMessage()); 243 } 244 } 245 } 246 | Popular Tags |