KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > j2ee > ProgressEventSupport


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.sun.ide.j2ee;
21
22 import java.util.Vector JavaDoc;
23
24 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
25 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
26 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
27 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
28
29 import org.openide.util.RequestProcessor;
30 //The tomcat team will split the tomcat module in 2, so that this type of behaviour can be shared
31
// between web/app server plugins. This is really a shared utility class.
32
//
33
/**
34  * This is a utility class that can be used by ProgressObject's,
35  * You can use an instance of this class as a member field
36  * of your ProgressObject and delegate various work to it.
37  *
38  * @@author Radim Kubacki
39  */

40 public class ProgressEventSupport {
41
42     /** Source object. */
43     private Object JavaDoc obj;
44     
45     private Vector JavaDoc listeners;
46     
47     private DeploymentStatus JavaDoc status;
48     
49     private TargetModuleID JavaDoc tmID;
50     
51     /**
52      * Constructs a <code>ProgressEventSupport</code> object.
53      *
54      * @@param o Source for any events.
55      */

56     public ProgressEventSupport (Object JavaDoc o) {
57         if (o == null) {
58             throw new NullPointerException JavaDoc ();
59         }
60         obj = o;
61     }
62     
63     /** Add a ProgressListener to the listener list. */
64     public synchronized void addProgressListener (ProgressListener JavaDoc lsnr) {
65         boolean notify = false;
66         if (listeners == null) {
67             listeners = new java.util.Vector JavaDoc();
68         }
69         listeners.addElement(lsnr);
70         if (status != null && !status.isRunning ()) {
71             notify = true;
72         }
73         if (notify) {
74             // not to miss completion event
75
RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
76                 public void run () {
77                     fireHandleProgressEvent (tmID, status);
78                 }
79             });
80         }
81     }
82     
83     /** Remove a ProgressListener from the listener list. */
84     public synchronized void removeProgressListener (ProgressListener JavaDoc lsnr) {
85         if (listeners == null) {
86             return;
87         }
88         listeners.removeElement(lsnr);
89     }
90
91     /** Report event to any registered listeners. */
92     public void fireHandleProgressEvent (TargetModuleID JavaDoc targetModuleID,
93                                          DeploymentStatus JavaDoc sCode) {
94         ProgressEvent JavaDoc evt = new ProgressEvent JavaDoc (obj, targetModuleID, sCode);
95         status = sCode;
96         tmID = targetModuleID;
97         
98     Vector JavaDoc targets = null;
99     synchronized (this) {
100         if (listeners != null) {
101             targets = (Vector JavaDoc) listeners.clone();
102         }
103     }
104
105     if (targets != null) {
106         for (int i = 0; i < targets.size(); i++) {
107             ProgressListener JavaDoc target = (ProgressListener JavaDoc)targets.elementAt(i);
108             target.handleProgressEvent (evt);
109         }
110     }
111     }
112     
113     /** Returns last DeploymentStatus notified by {@@link fireHandleProgressEvent}
114      */

115     public DeploymentStatus JavaDoc getDeploymentStatus () {
116         return status;
117     }
118
119     public synchronized void clearProgressListener() {
120         listeners = null;
121     }
122 }
123
124
Popular Tags