KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MetalworksDocumentFrame


1 /*
2  * @(#)MetalworksDocumentFrame.java 1.13 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)MetalworksDocumentFrame.java 1.13 05/11/17
39  */

40
41 import java.awt.*;
42 import java.awt.event.*;
43 import java.util.*;
44 import javax.swing.*;
45 import javax.swing.border.*;
46
47
48 /**
49   * This is a subclass of JInternalFrame which displays documents.
50   *
51   * @version 1.13 11/17/05
52   * @author Steve Wilson
53   */

54 public class MetalworksDocumentFrame extends JInternalFrame {
55   
56     static int openFrameCount = 0;
57     static final int offset = 30;
58
59     public MetalworksDocumentFrame() {
60     super("", true, true, true, true);
61     openFrameCount++;
62         setTitle("Untitled Message " + openFrameCount);
63
64     JPanel top = new JPanel();
65     top.setBorder(new EmptyBorder(10, 10, 10, 10));
66     top.setLayout(new BorderLayout());
67     top.add(buildAddressPanel(), BorderLayout.NORTH);
68
69     JTextArea content = new JTextArea( 15, 30 );
70     content.setBorder( new EmptyBorder(0,5 ,0, 5) );
71     content.setLineWrap(true);
72
73
74
75     JScrollPane textScroller = new JScrollPane(content,
76                            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
77                            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
78     top.add( textScroller, BorderLayout.CENTER);
79     
80
81     setContentPane(top);
82     pack();
83     setLocation( offset * openFrameCount, offset *openFrameCount);
84
85     }
86
87     private JPanel buildAddressPanel() {
88         JPanel p = new JPanel();
89     p.setLayout( new LabeledPairLayout() );
90     
91
92     JLabel toLabel = new JLabel("To: ", JLabel.RIGHT);
93     JTextField toField = new JTextField(25);
94     p.add(toLabel, "label");
95     p.add(toField, "field");
96
97
98     JLabel subLabel = new JLabel("Subj: ", JLabel.RIGHT);
99     JTextField subField = new JTextField(25);
100     p.add(subLabel, "label");
101     p.add(subField, "field");
102
103
104     JLabel ccLabel = new JLabel("cc: ", JLabel.RIGHT);
105     JTextField ccField = new JTextField(25);
106     p.add(ccLabel, "label");
107     p.add(ccField, "field");
108
109     return p;
110
111     }
112
113     class LabeledPairLayout implements LayoutManager {
114
115       Vector labels = new Vector();
116       Vector fields = new Vector();
117       
118       int yGap = 2;
119       int xGap = 2;
120
121       public void addLayoutComponent(String JavaDoc s, Component c) {
122       if (s.equals("label")) {
123           labels.addElement(c);
124       } else {
125           fields.addElement(c);
126       }
127       }
128
129       public void layoutContainer(Container c) {
130       Insets insets = c.getInsets();
131       
132       int labelWidth = 0;
133       Enumeration labelIter = labels.elements();
134       while(labelIter.hasMoreElements()) {
135           JComponent comp = (JComponent)labelIter.nextElement();
136           labelWidth = Math.max( labelWidth, comp.getPreferredSize().width );
137       }
138
139       int yPos = insets.top;
140
141       Enumeration fieldIter = fields.elements();
142       labelIter = labels.elements();
143       while(labelIter.hasMoreElements() && fieldIter.hasMoreElements()) {
144           JComponent label = (JComponent)labelIter.nextElement();
145           JComponent field = (JComponent)fieldIter.nextElement();
146           int height = Math.max(label.getPreferredSize().height, field.getPreferredSize().height);
147           label.setBounds( insets.left, yPos, labelWidth, height );
148           field.setBounds( insets.left + labelWidth + xGap,
149                  yPos,
150                  c.getSize().width - (labelWidth +xGap + insets.left + insets.right),
151                  height );
152           yPos += (height + yGap);
153       }
154       
155       }
156
157
158       public Dimension minimumLayoutSize(Container c) {
159       Insets insets = c.getInsets();
160       
161       int labelWidth = 0;
162       Enumeration labelIter = labels.elements();
163       while(labelIter.hasMoreElements()) {
164           JComponent comp = (JComponent)labelIter.nextElement();
165           labelWidth = Math.max( labelWidth, comp.getPreferredSize().width );
166       }
167
168       int yPos = insets.top;
169
170       labelIter = labels.elements();
171       Enumeration fieldIter = fields.elements();
172       while(labelIter.hasMoreElements() && fieldIter.hasMoreElements()) {
173           JComponent label = (JComponent)labelIter.nextElement();
174           JComponent field = (JComponent)fieldIter.nextElement();
175           int height = Math.max(label.getPreferredSize().height, field.getPreferredSize().height);
176           yPos += (height + yGap);
177       }
178       return new Dimension( labelWidth * 3 , yPos );
179       }
180   
181       public Dimension preferredLayoutSize(Container c) {
182       Dimension d = minimumLayoutSize(c);
183       d.width *= 2;
184           return d;
185       }
186    
187       public void removeLayoutComponent(Component c) {}
188
189 }
190
191
192 }
193
194
195
Popular Tags