KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CompViewer


1 /*
2  * @(#)CompViewer.java 1.3 99/12/06
3  *
4  * Copyright 1997-1999 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * This software is the proprietary information of Sun Microsystems, Inc.
7  * Use is subject to license terms.
8  *
9  */

10
11 import java.awt.*;
12 import java.beans.*;
13 import java.lang.reflect.Method JavaDoc;
14 import java.io.*;
15 import java.awt.event.*;
16
17 /**
18  * Class <code>CompViewer</code> creates a 'viewer' component
19  * that implements the CommandObject interface.
20  *
21  */

22 public class CompViewer extends Frame implements WindowListener {
23
24     /**
25      * Our constructor...
26      */

27     public CompViewer(){
28     super("Component");
29     this.initCompViewer(null);
30     }
31
32     public CompViewer(String JavaDoc name){
33     super(name);
34     this.initCompViewer(name);
35     }
36
37     public void initCompViewer(String JavaDoc name){
38     if (name != null)
39         setTitle(name);
40     setSize(400,400);
41     setLayout(new BorderLayout());
42     this.addWindowListener(this);
43     }
44     
45     ////////////////////////////////////////////////////////////////////////
46
// we got our bean as a component display it!
47
void setBean(Component new_bean)
48     {
49         Dimension start_dim = null;
50         add((Component)new_bean, "Center");
51         start_dim = ((Component)new_bean).getPreferredSize();
52         
53         if(start_dim.width != 0 && start_dim.height != 0) {
54         // this is what we do under normal conditions
55
start_dim.height += 40;
56         start_dim.width += 15;
57         this.setSize( start_dim );
58         ((Component)new_bean).invalidate();
59         ((Component)new_bean).validate();
60         ((Component)new_bean).doLayout();
61         show();
62         }
63         else {
64         // we get here if for some reason our child's
65
// getPref size needs to have it's peer created
66
// first...
67
show();
68         start_dim = ((Component)new_bean).getPreferredSize();
69         start_dim.height += 40;
70         start_dim.width += 15;
71         this.setSize( start_dim );
72         ((Component)new_bean).validate();
73         }
74         this.setSize(this.getSize());
75         validate();
76     }
77
78
79     /**
80      * Make the bean based on it's class loader and name
81      */

82     private Object JavaDoc makeBean(ClassLoader JavaDoc cls, String JavaDoc beanName) {
83     Object JavaDoc new_bean = null;
84
85     try {
86         try {
87         new_bean = java.beans.Beans.instantiate(cls, beanName);
88         }
89         catch(IOException e) {
90         System.out.println("CompViewer:Beans.instantiate:IOException " + beanName + ".");
91         System.out.println(e);
92         System.exit(1);
93         }
94     }
95     catch (ClassNotFoundException JavaDoc e) {
96         System.out.println("CompViewer:Beans.instantiate:ClassNotFoundException " + beanName + ".");
97         System.out.println(e);
98         System.exit(1);
99     }
100       
101     if( !(new_bean instanceof Component) ) {
102         System.out.println("CompViewer: " + beanName + " not instance of awt.Component exiting");
103         System.exit(1);
104     }
105     return new_bean;
106     }
107
108     public void windowOpened(WindowEvent e) {}
109     public void windowClosing(WindowEvent e) {
110     this.setVisible(false);
111     }
112     public void windowClosed(WindowEvent e) {}
113     public void windowIconified(WindowEvent e) {}
114     public void windowDeiconified(WindowEvent e) {}
115     public void windowActivated(WindowEvent e) {}
116     public void windowDeactivated(WindowEvent e) {}
117
118 }
119
120
121
122
Popular Tags