001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.compapp.casaeditor.graph;
042:
043: import java.awt.Color;
044: import java.awt.Font;
045: import java.awt.Point;
046: import java.awt.Rectangle;
047: import javax.swing.SwingUtilities;
048: import org.netbeans.api.visual.border.Border;
049: import org.netbeans.api.visual.border.BorderFactory;
050: import org.netbeans.api.visual.widget.LabelWidget;
051: import org.netbeans.api.visual.widget.Scene;
052: import org.netbeans.api.visual.widget.Widget;
053: import org.netbeans.modules.compapp.casaeditor.Utilities;
054: import org.netbeans.modules.compapp.casaeditor.design.CasaModelGraphScene;
055: import org.netbeans.modules.compapp.casaeditor.design.CasaModelGraphUtilities;
056: import org.netbeans.modules.compapp.projects.jbi.api.JbiBuildTask;
057: import org.openide.util.NbBundle;
058:
059: /**
060: *
061: * @author Josh Sandusky
062: */
063: public class WaitMessageHandler {
064:
065: private static Widget getBuildMessageWidget(
066: CasaModelGraphScene scene) {
067: for (Widget child : scene.getDragLayer().getChildren()) {
068: if (child instanceof WaitMessageWidget) {
069: return child;
070: }
071: }
072: return null;
073: }
074:
075: public static void addToScene(CasaModelGraphScene scene,
076: JbiBuildTask task) {
077: // This is already in the EDT.
078: if (getBuildMessageWidget(scene) == null) {
079: WaitMessageWidget messageWidget = new WaitMessageWidget(
080: scene, NbBundle.getMessage(
081: WaitMessageHandler.class,
082: "LBL_WaitMessage3"));
083: messageWidget.setTask(task);
084: messageWidget.setAnimationText(NbBundle.getMessage(
085: WaitMessageHandler.class, "LBL_WaitMessage1"),
086: NbBundle.getMessage(WaitMessageHandler.class,
087: "LBL_WaitMessage2"), NbBundle.getMessage(
088: WaitMessageHandler.class,
089: "LBL_WaitMessage3"));
090: scene.getDragLayer().addChild(messageWidget);
091: scene.validate();
092: }
093: }
094:
095: public static void removeFromScene(final CasaModelGraphScene scene) {
096: // Make sure it is in the EDT.
097: SwingUtilities.invokeLater(new Runnable() {
098: public void run() {
099: Widget messageWidget = getBuildMessageWidget(scene);
100: if (messageWidget != null) {
101: scene.getDragLayer().removeChild(messageWidget);
102: scene.validate();
103: }
104: }
105: });
106: }
107:
108: private static class WaitMessageWidget extends LabelWidget {
109:
110: private static final Font FONT_MESSAGE = new Font("SansSerif",
111: Font.BOLD, 16);
112:
113: private static final Border BORDER = BorderFactory
114: .createCompositeBorder(
115: // outer border
116: BorderFactory.createLineBorder(2, new Color(
117: 220, 220, 200)),
118: // round-rect line
119: BorderFactory.createRoundedBorder(8, 8, null,
120: Color.LIGHT_GRAY),
121: // inner border
122: BorderFactory.createLineBorder(4, 8, 4, 8,
123: new Color(255, 255, 255, 200)));
124:
125: private DependenciesRegistry mDependenciesRegistry;
126: private String[] mAnimationText;
127: private boolean mIsLockPosition;
128:
129: private Runnable mCurrentAnimator;
130: private JbiBuildTask mBuildTask;
131:
132: /**
133: * Creates a new wait message widget with the given initial label.
134: * If animation text is set, for best results, the label passed into
135: * the constructor should be as long or longer than the longest
136: * animation text - otherwise the widget will change its size
137: * to accomodate longer text, which is not as visibly pleasing.
138: */
139: public WaitMessageWidget(Scene scene, String label) {
140: super (scene);
141: setLabel(label);
142: setBorder(BORDER);
143: setFont(FONT_MESSAGE);
144: setForeground(Color.DARK_GRAY);
145: setBackground(new Color(255, 255, 255, 200));
146: setOpaque(true);
147: }
148:
149: /**
150: * Sets the animation text.
151: * The animation must be set before the widget is added to a parent.
152: */
153: public void setAnimationText(String... text) {
154: mAnimationText = text;
155: if (getParentWidget() != null) {
156: // This method must be called before the widget is
157: // added to a parent.
158: throw new IllegalStateException();
159: }
160: }
161:
162: public void setTask(JbiBuildTask task) {
163: mBuildTask = task;
164: }
165:
166: protected void notifyAdded() {
167: super .notifyAdded();
168:
169: mDependenciesRegistry = new DependenciesRegistry(
170: getParentWidget());
171:
172: setMinimumSize(getPreferredBounds().getSize());
173:
174: center();
175: Widget.Dependency centerer = new Widget.Dependency() {
176: public void revalidateDependency() {
177: if (!mIsLockPosition) {
178: center();
179: }
180: }
181: };
182: getRegistry().registerDependency(centerer);
183:
184: startAnimation();
185: }
186:
187: private void startAnimation() {
188: if (mAnimationText != null) {
189: Runnable animator = new Runnable() {
190: private int mAnimationIndex = 0;
191: private boolean isForcingCleanup = false;
192: private int mForcedCleanupDelay = 0;
193:
194: public void run() {
195: do {
196: checkTaskFinished();
197: try {
198: mForcedCleanupDelay += 800;
199: Thread.sleep(800);
200: if (mCurrentAnimator == this ) {
201: SwingUtilities
202: .invokeLater(new Runnable() {
203: public void run() {
204: updateLabel();
205: }
206: });
207: }
208: } catch (Throwable t) {
209: t.printStackTrace(System.err);
210: }
211: } while (mCurrentAnimator == this );
212: }
213:
214: private void updateLabel() {
215: try {
216: mIsLockPosition = true;
217: setLabel(mAnimationText[mAnimationIndex]);
218: getScene().revalidate();
219: getScene().validate();
220: mAnimationIndex = (mAnimationIndex + 1)
221: % mAnimationText.length;
222: } finally {
223: mIsLockPosition = false;
224: }
225: }
226:
227: private void checkTaskFinished() {
228: if (!isForcingCleanup && mBuildTask != null
229: && mBuildTask.isFinished()) {
230: // The standard task listener should fire a completion
231: // notification, but sometimes this does not happen.
232: // We give the task listener a few seconds but if it
233: // does not fire then we force the cleanup.
234: isForcingCleanup = true;
235: mForcedCleanupDelay = 0;
236: } else if (isForcingCleanup
237: && mForcedCleanupDelay >= 5000) {
238: if (mCurrentAnimator == this ) {
239: CasaModelGraphUtilities
240: .setSceneEnabled(
241: (CasaModelGraphScene) getScene(),
242: true);
243: }
244: }
245: }
246: };
247: mCurrentAnimator = animator;
248: new Thread(animator).start();
249: }
250: }
251:
252: protected void notifyRemoved() {
253: super .notifyRemoved();
254:
255: mCurrentAnimator = null;
256:
257: if (getRegistry() != null) {
258: getRegistry().removeAllDependencies();
259: }
260: mDependenciesRegistry = null;
261: }
262:
263: private void center() {
264: Rectangle bounds = getPreferredBounds();
265: CasaModelGraphScene scene = (CasaModelGraphScene) getScene();
266: Rectangle layerBounds = scene.getView().getVisibleRect();
267: if (getParentWidget() == scene.getDragLayer()
268: && bounds != null && layerBounds != null) {
269: Point location = Utilities.center(bounds, layerBounds);
270: setPreferredLocation(location);
271: }
272: }
273:
274: private DependenciesRegistry getRegistry() {
275: return mDependenciesRegistry;
276: }
277: }
278: }
|