001: /*
002: * @(#)Builder.java 1.5 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package basis;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030: import java.lang.reflect.*;
031: import java.util.ArrayList;
032: import java.util.EventObject;
033: import basis.demos.*;
034:
035: public class Builder extends Container {
036: public static final Color SUN_BLUE = new Color(89, 79, 191);
037: public static final Color SUN_YELLOW = new Color(251, 226, 73);
038: public static final Color SUN_RED = new Color(209, 33, 36);
039: public static final Color SUN_LIGHTBLUE = new Color(204, 204, 255);
040: private ArrayList demos;
041: private Container demoContainer = new Container();
042: private CardLayout cardLayout = new CardLayout();
043: private Status status;
044:
045: public Builder(ArrayList demos) {
046: this .demos = demos;
047: }
048:
049: public void build(Container container) throws Exception {
050: container.setBackground(Color.white);
051: container.setLayout(new BorderLayout());
052: container.add(this , BorderLayout.CENTER);
053: status = new Status();
054: if (demos.size() > 1) {
055: setLayout(new BorderLayout());
056: demoContainer.setLayout(cardLayout);
057: Container buttonContainer = new Container();
058: add(buttonContainer, BorderLayout.NORTH);
059: add(demoContainer, BorderLayout.CENTER);
060: add(status, BorderLayout.SOUTH);
061: buttonContainer.setLayout(new GridLayout(1, demos.size()));
062: DemoButtonListener listener = new DemoButtonListener() {
063: public void buttonPressed(EventObject e) {
064: DemoButton b = (DemoButton) e.getSource();
065: showDemo(b.getLabel());
066: }
067: };
068: for (int i = 0; i < demos.size(); i++) {
069: Class clazz = Class.forName((String) demos.get(i));
070: Component component = (Component) clazz.newInstance();
071: String label = (String) demos.get(i);
072: label = label.substring(label.lastIndexOf(".") + 1,
073: label.lastIndexOf("Demo"));
074: if (i == 0) {
075: setStatus(label);
076: }
077: demoContainer.add(label, component);
078: DemoButton b = new DemoButton(label);
079: buttonContainer.add(b);
080: b.addDemoButtonListener(listener);
081: }
082: } else {
083: String demoName = (String) demos.get(0);
084: Class clazz = Class.forName(demoName);
085: Demo demo = (Demo) clazz.newInstance();
086: setLayout(new BorderLayout());
087: add(demo, BorderLayout.CENTER);
088: add(status, BorderLayout.SOUTH);
089: String label = demoName;
090: label = label.substring(label.lastIndexOf(".") + 1, label
091: .lastIndexOf("Demo"));
092: setStatus(label);
093: }
094: }
095:
096: public void showDemo(String name) {
097: cardLayout.show(demoContainer, name);
098: setStatus(name);
099: }
100:
101: public void setStatus(String text) {
102: if (status == null) {
103: return;
104: }
105: if (text.equals(getStatus())) {
106: return;
107: }
108: status.setText(text);
109: status.repaint();
110: }
111:
112: public String getStatus() {
113: if (status == null) {
114: return null;
115: }
116: return status.getText();
117: }
118:
119: class Status extends Component {
120: private String text = "";
121: private Font font = new Font("sanserif", Font.BOLD, 12);
122: private Dimension preferredSize;
123:
124: public Status() {
125: setBackground(SUN_YELLOW);
126: setForeground(SUN_RED);
127: }
128:
129: public void setText(String text) {
130: this .text = text;
131: if (status.isShowing()) {
132: Graphics g = getGraphics();
133: FontMetrics fm = g.getFontMetrics(font);
134: int fw = fm.stringWidth(text);
135: int fh = fm.getHeight();
136: preferredSize = new Dimension(fw + 4, fh + 4);
137: }
138: }
139:
140: public String getText() {
141: return text;
142: }
143:
144: public Dimension getPreferredSize() {
145: if (preferredSize == null) {
146: Graphics g = getGraphics();
147: FontMetrics fm = g.getFontMetrics(font);
148: int fw = fm.stringWidth(text);
149: int fh = fm.getHeight();
150: preferredSize = new Dimension(fw + 4, fh + 4);
151: }
152: return preferredSize;
153: }
154:
155: public Dimension getMinimumSize() {
156: return getPreferredSize();
157: }
158:
159: public Dimension getMaximumSize() {
160: return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
161: }
162:
163: public void paint(Graphics g) {
164: Color background = getBackground();
165: Color foreground = getForeground();
166: Dimension size = getSize();
167: g.setColor(background);
168: g.fillRect(0, 0, size.width, size.height);
169: g.setColor(foreground);
170: g.setFont(font);
171: FontMetrics fm = g.getFontMetrics(font);
172: int w = fm.stringWidth(text);
173: g.drawString(text, (size.width - w) / 2,
174: 2 * size.height / 3);
175: }
176: }
177: }
|