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: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.ui.components;
042:
043: import java.awt.*;
044: import javax.swing.*;
045:
046: /**
047: * A container for two contents that are animated in traansition between them.
048: *
049: * @author Vlada Nemec
050: */
051: public class AnimatedContainer extends javax.swing.JPanel {
052: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
053:
054: public static final int WIDTH = 10;
055: public static final int HEIGHT = 20;
056: private static final int BOTH = 30;
057:
058: //~ Instance fields ----------------------------------------------------------------------------------------------------------
059:
060: //custom layout manager used for placing content component
061: private AnimationLayout layout = new AnimationLayout();
062: private CustomTaskButtonBorder border;
063:
064: //content of the container - limited to one component
065: private JComponent content;
066:
067: //target content for the animation
068: private JComponent targetContent;
069:
070: //temporary content used during animation
071: private JComponent transContent;
072: private int animatedDimension = BOTH;
073: private int origHeight;
074:
075: //original dimensions
076: private int origWidth;
077: private int targetHeight;
078:
079: //new dimension for animation (if needed)
080: private int targetWidth;
081:
082: //~ Constructors -------------------------------------------------------------------------------------------------------------
083:
084: /**
085: * Creates a new instance of AnimatedContainer
086: */
087: public AnimatedContainer(Color backgroundColor) {
088: setLayout(layout);
089:
090: border = new CustomTaskButtonBorder(backgroundColor, super
091: .getBackground());
092: setBorder(border);
093: }
094:
095: //~ Methods ------------------------------------------------------------------------------------------------------------------
096:
097: public void setAnimatedDimension(int dimension) {
098: animatedDimension = dimension;
099: }
100:
101: /**
102: * Setups animation parameters.
103: */
104: public void setAnimation(JComponent targetContent,
105: JComponent transContent) {
106: //setup starting point dimension
107: origWidth = content.getWidth();
108: origHeight = content.getHeight();
109:
110: //set contents
111: if (targetContent == null) {
112: this .targetContent = content;
113: } else if (transContent == null) {
114: this .transContent = content;
115: } else {
116: this .targetContent = targetContent;
117: this .transContent = transContent;
118:
119: targetWidth = (int) targetContent.getPreferredSize()
120: .getWidth();
121: targetHeight = (int) targetContent.getPreferredSize()
122: .getHeight();
123: }
124: }
125:
126: /**
127: * Setups animation parameters. <code>transContent</code> is set to <code>content</code>
128: */
129: public void setAnimation(JComponent aTargetContent) {
130: //setup starting point dimension
131: origWidth = content.getWidth();
132: origHeight = content.getHeight();
133:
134: if (aTargetContent == null) {
135: targetContent = content;
136: } else {
137: targetContent = aTargetContent;
138:
139: targetWidth = (int) targetContent.getPreferredSize()
140: .getWidth();
141: targetHeight = (int) targetContent.getPreferredSize()
142: .getHeight();
143: }
144: }
145:
146: public void setAnimation(Dimension dimension) {
147: //setup starting point dimension
148: origWidth = content.getWidth();
149: origHeight = content.getHeight();
150:
151: targetContent = null;
152: transContent = content;
153:
154: targetWidth = (int) dimension.getWidth();
155: targetHeight = (int) dimension.getHeight();
156: }
157:
158: public void setAnimation() {
159: origWidth = content.getWidth();
160: origHeight = content.getHeight();
161:
162: transContent = content;
163: targetContent = content;
164: }
165:
166: /**
167: * Sets content component reference. This method does NOT place any component into container.
168: */
169: public void setContent(JComponent content) {
170: this .content = content;
171: }
172:
173: public void setDefaultBorder() {
174: border.setDefault();
175: repaint();
176: }
177:
178: /**
179: * Sets the state of the container to finish state with target size and content
180: */
181: public void setFinishState() {
182: //empty container
183: removeAll();
184:
185: //add target content
186: if (targetContent != null) {
187: add(targetContent);
188: //resize to final size
189: setPreferredSize(new Dimension(targetWidth, targetHeight));
190: setMinimumSize(new Dimension(targetWidth, targetHeight));
191: } else if (transContent != null) {
192: add(transContent);
193: //resize to final size
194: setPreferredSize(transContent.getPreferredSize());
195: setMinimumSize(transContent.getPreferredSize());
196: }
197:
198: //resize to final size
199: setPreferredSize(content.getPreferredSize());
200: setMinimumSize(content.getPreferredSize());
201: }
202:
203: public void setFocusedBorder() {
204: border.setFocused();
205: repaint();
206: }
207:
208: public void setSelectedBorder() {
209: border.setSelected();
210: repaint();
211: }
212:
213: /**
214: * Sets the state of the container - this method provides the resizing of container for animation
215: */
216: public void setState(int percents) {
217: int newWidth;
218: int newHeight;
219:
220: newWidth = targetWidth;
221: newHeight = targetHeight;
222:
223: origWidth = (int) getSize().getWidth();
224: origHeight = (int) getSize().getHeight();
225:
226: int deltaWidth = newWidth - origWidth;
227: int deltaHeight = newHeight - origHeight;
228:
229: double perc = (double) percents / 100.0;
230:
231: Dimension d;
232:
233: if (animatedDimension == WIDTH) {
234: d = new Dimension((int) (origWidth + (deltaWidth * perc)),
235: origHeight);
236: } else if (animatedDimension == HEIGHT) {
237: d = new Dimension(origWidth,
238: (int) (origHeight + (deltaHeight * perc)));
239: } else {
240: d = new Dimension((int) (origWidth + (deltaWidth * perc)),
241: (int) (origHeight + (deltaHeight * perc)));
242: }
243:
244: setPreferredSize(d);
245: setMinimumSize(d);
246: }
247:
248: /**
249: * Sets target content component reference. This method does NOT place any component into container.
250: */
251: public void setTargetContent(JComponent targetContent) {
252: this .targetContent = targetContent;
253: }
254:
255: /**
256: * Sets transient content component reference. This method does NOT place any component into container.
257: */
258: public void setTransContent(JComponent transContent) {
259: this .transContent = transContent;
260: }
261:
262: /**
263: * Overriden - we need to store reference to the content component
264: */
265: public Component add(Component component) {
266: content = (JComponent) component;
267:
268: return super .add(component);
269: }
270:
271: /**
272: * locks the content size - while resizing the content component size and layout remains the same
273: */
274: public void lockContentResizing(boolean lock) {
275: if ((lock) && (transContent != null)) {
276: layout.setLockedSize(transContent.getSize());
277: } else {
278: layout.setLockedSize(null);
279: }
280: }
281: }
|