KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > ProgressDisplayer


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 package org.netbeans.modules.javacore;
20 import org.netbeans.api.progress.aggregate.AggregateProgressFactory;
21 import org.netbeans.api.progress.aggregate.AggregateProgressHandle;
22 import org.netbeans.api.progress.aggregate.ProgressContributor;
23 import org.netbeans.spi.java.classpath.ClassPathImplementation;
24 import org.netbeans.modules.javacore.classpath.MergedClassPathImplementation;
25 import org.netbeans.modules.javacore.internalapi.ProgressListener;
26 import org.netbeans.modules.javacore.internalapi.ProgressEvent;
27
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import java.text.MessageFormat JavaDoc;
31 import org.netbeans.api.progress.*;
32 import org.openide.util.NbBundle;
33
34
35 public class ProgressDisplayer implements ProgressListener, PropertyChangeListener JavaDoc {
36     private ClassPathImplementation cp;
37     private JMManager model;
38     private boolean dirty = false;
39     private ProgressContributor progressContributor;
40     private ProgressContributor startFake;
41     private ProgressContributor endFake;
42     private AggregateProgressHandle globalProgress;
43     
44
45     private ProgressDisplayer (JMManager model) {
46         this.model = model;
47         this.cp = model.getMergedClassPathImpl();
48         this.cp.addPropertyChangeListener (this);
49         this.model.getProgressSupport().addProgressListener (this);
50         globalProgress = AggregateProgressFactory.createHandle(NbBundle.getMessage(ProgressDisplayer.class, "TXT_ApplyingPathsTitle"), new ProgressContributor[]{}, null, null);
51         globalProgress.start();
52         startFake = AggregateProgressFactory.createProgressContributor("startFake"); //NOI18N
53
endFake = AggregateProgressFactory.createProgressContributor("endFake"); //NOI18N
54
globalProgress.addContributor(startFake);
55         globalProgress.addContributor(endFake);
56         startFake.start(1);
57         endFake.start(1);
58     }
59
60     public void start(ProgressEvent event) {
61         int ec = event.getCount();
62         progressContributor = AggregateProgressFactory.createProgressContributor(Integer.toString(event.hashCode()));
63         globalProgress.addContributor(progressContributor);
64         progressContributor.start(ec);
65         startFake.finish();
66     }
67
68     public void step(final ProgressEvent event) {
69         progressContributor.progress(event.getCount());
70     }
71
72     public void stop(ProgressEvent event) {
73         if (JMManager.SCAN_DEBUG) System.err.println("ProgressDisplayer: stop1"); // NOI18N
74
if (this.getDirty()) {
75             if (JMManager.SCAN_DEBUG) System.err.println("ProgressDisplayer: stop2 - dirty"); // NOI18N
76
progressContributor.finish();
77             this.startResolution ();
78         }
79         else {
80             if (JMManager.SCAN_DEBUG) System.err.println("ProgressDisplayer: stop3"); // NOI18N
81
endFake.finish();
82             globalProgress.finish();
83             this.model.getProgressSupport().removeProgressListener (this);
84             this.cp.removePropertyChangeListener(this);
85             instance=null;
86         }
87     }
88
89     public synchronized void propertyChange(PropertyChangeEvent JavaDoc evt) {
90         if (MergedClassPathImplementation.PROP_UNRESOLVED_ROOTS.equals(evt.getPropertyName())) {
91             if (JMManager.SCAN_DEBUG) System.err.println("ProgressDisplayer: globalpath changed"); // NOI18N
92
this.dirty = true;
93         }
94     }
95
96     private synchronized boolean getDirty () {
97         boolean res = this.dirty;
98         this.dirty = false;
99         return res;
100     }
101
102     private void startResolution() {
103         if (JMManager.SCAN_DEBUG) System.err.println("ProgressDisplayer: startResolution"); // NOI18N
104
model.startResolution();
105     }
106
107     private static ProgressDisplayer instance = null;
108     
109     private String JavaDoc formatMessage(String JavaDoc message, String JavaDoc parameter) {
110         return MessageFormat.format(message, new Object JavaDoc[] {parameter});
111     }
112     
113     public void updatePackage(final String JavaDoc packageName) {
114         progressContributor.progress(formatMessage(NbBundle.getMessage(ProgressDisplayer.class, "MSG_ScanningPackage"), packageName));
115     }
116     
117     public static ProgressDisplayer getVisibleProgressDisplayer() {
118         return instance;
119     }
120     
121     public static boolean showProgress(JMManager model) {
122         if (JMManager.SCAN_DEBUG) System.err.println("ProgressDisplayer: openProgressDialog"); // NOI18N
123
instance = new ProgressDisplayer(model);
124         instance.startResolution();
125         return true;
126     }
127
128 }
129
Popular Tags