KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > ui > ProgressUI


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.deployment.impl.ui;
21 import java.awt.GridBagConstraints JavaDoc;
22 import java.awt.Insets JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import javax.enterprise.deploy.shared.StateType JavaDoc;
26 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
27 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
28 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
29 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
30 import javax.swing.JButton JavaDoc;
31 import javax.swing.JComponent JavaDoc;
32 import javax.swing.JDialog JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JPanel JavaDoc;
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 /**
45  * Progress UI provides a feedback for long lasting taks like deploying to a server,
46  * starting or stopping a server, etc. The progress bar is indeterminate, displayed
47  * in the status bar if in non-modal mode, otherwise in a modal dialog.
48  *
49  * @author sherold
50  */

51 public class ProgressUI implements ProgressListener JavaDoc {
52     
53     private String JavaDoc title;
54     private boolean modal;
55     private Deployment.Logger logger;
56     
57     private ProgressHandle handle;
58     private ProgressObject JavaDoc progObj;
59     
60     private JDialog JavaDoc dialog;
61     private JLabel JavaDoc messageLabel;
62     private String JavaDoc lastMessage;
63     private JComponent JavaDoc progressComponent;
64     private boolean finished;
65     
66     /** helps to avoid double processing of the completion event */
67     private boolean completionEventProcessed;
68     
69     /** Creates a new instance of ProgressUI */
70     public ProgressUI(String JavaDoc title, boolean modal) {
71         this(title, modal, null);
72     }
73     
74     public ProgressUI(String JavaDoc 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     /** Start the progress indication for indeterminate task. */
82     public void start() {
83         if (modal) {
84             progressComponent = ProgressHandleFactory.createProgressComponent(handle);
85         }
86         handle.start();
87     }
88     
89     /** Display the modal progress dialog. This method should be called from the
90         AWT Event Dispatch thread. */

91     public void showProgressDialog() {
92         if (finished) {
93             return; // do not display the dialog if we are done
94
}
95         dialog = new JDialog JavaDoc(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     /** Displays a specified progress message. */
107     public void progress(final String JavaDoc message) {
108         handle.progress(message);
109         if (modal) {
110             Mutex.EVENT.readAccess(new Runnable JavaDoc() {
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     /** Finish the task, unregister the progress object listener and dispose the ui. */
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 JavaDoc() {
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     /** Display a failure dialog with the specified message and call finish. */
143     public void failed(String JavaDoc message) {
144         finish();
145         if (logger != null) {
146             log(message);
147         }
148     }
149     
150     /** Set a progress object this progress UI will monitor. */
151     public void setProgressObject(ProgressObject JavaDoc obj) {
152         // do not listen to the old progress object anymore
153
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             // safety-catch to not to miss the completion event, if it had come
163
// before the progress listener was registered
164
DeploymentStatus JavaDoc status = progObj.getDeploymentStatus();
165             if (status.isCompleted() || status.isFailed()) {
166                 handleDeploymentStatus(status);
167             }
168         }
169     }
170     
171     /** Set a logger to where all the progress messages will be copied. */
172     public void setLogger(Deployment.Logger logger) {
173         this.logger = logger;
174     }
175     
176     // private helper methods
177

178     private void log(String JavaDoc msg) {
179         if (logger != null && msg != null) {
180             logger.log(msg);
181         }
182     }
183     
184     private JComponent JavaDoc createProgressDialog(ProgressHandle handle, String JavaDoc message) {
185         JPanel JavaDoc panel = new JPanel JavaDoc();
186         messageLabel = new JLabel JavaDoc();
187                                                                                                                                                                            
188         panel.setLayout(new java.awt.GridBagLayout JavaDoc());
189                                                                                                                                                                            
190         messageLabel.setText(message);
191         GridBagConstraints JavaDoc gridBagConstraints = new GridBagConstraints JavaDoc();
192         gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
193         gridBagConstraints.insets = new Insets JavaDoc(12, 12, 0, 12);
194         panel.add(messageLabel, gridBagConstraints);
195                                                                                                                                                                            
196         gridBagConstraints = new GridBagConstraints JavaDoc();
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 JavaDoc(5, 12, 0, 12);
203         panel.add(progressComponent, gridBagConstraints);
204
205         gridBagConstraints = new GridBagConstraints JavaDoc();
206         gridBagConstraints.gridx = 0;
207         gridBagConstraints.gridy = 2;
208         gridBagConstraints.anchor = GridBagConstraints.CENTER;
209         gridBagConstraints.weightx = 1.0;
210         gridBagConstraints.insets = new Insets JavaDoc(11, 12, 12, 12);
211         JButton JavaDoc cancel = new JButton JavaDoc(NbBundle.getMessage(ProgressUI.class,"LBL_Cancel"));
212         cancel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ProgressUI.class,"AD_Cancel"));
213         cancel.addActionListener(new ActionListener JavaDoc() {
214             public void actionPerformed(ActionEvent JavaDoc e) {
215                 finish();
216             }
217         });
218         panel.add(cancel, gridBagConstraints);
219         
220         return panel;
221     }
222     
223     // ProgressListener implementation ----------------------------------------
224

225     public void handleProgressEvent(ProgressEvent JavaDoc progressEvent) {
226         handleDeploymentStatus(progressEvent.getDeploymentStatus());
227     }
228
229     private void handleDeploymentStatus(DeploymentStatus JavaDoc status) {
230         synchronized (this) {
231             if (completionEventProcessed) {
232                 // this event has already been processed
233
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