home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / jfc / Metalworks / src / MetalworksDocumentFrame.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  5.9 KB  |  194 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)MetalworksDocumentFrame.java    1.9 02/06/13
  38.  */
  39.  
  40. import java.awt.*;
  41. import java.awt.event.*;
  42. import java.util.*;
  43. import javax.swing.*;
  44. import javax.swing.border.*;
  45.  
  46.  
  47. /**
  48.   * This is a subclass of JInternalFrame which displays documents.
  49.   *
  50.   * @version 1.9 06/13/02
  51.   * @author Steve Wilson
  52.   */
  53. public class MetalworksDocumentFrame extends JInternalFrame {
  54.   
  55.     static int openFrameCount = 0;
  56.     static final int offset = 30;
  57.  
  58.     public MetalworksDocumentFrame() {
  59.     super("", true, true, true, true);
  60.     openFrameCount++;
  61.         setTitle("Untitled Message " + openFrameCount);
  62.  
  63.     JPanel top = new JPanel();
  64.     top.setBorder(new EmptyBorder(10, 10, 10, 10));
  65.     top.setLayout(new BorderLayout());
  66.     top.add(buildAddressPanel(), BorderLayout.NORTH);
  67.  
  68.     JTextArea content = new JTextArea( 15, 30 );
  69.     content.setBorder( new EmptyBorder(0,5 ,0, 5) );
  70.     content.setLineWrap(true);
  71.  
  72.  
  73.  
  74.     JScrollPane textScroller = new JScrollPane(content, 
  75.                            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
  76.                            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
  77.     top.add( textScroller, BorderLayout.CENTER);
  78.     
  79.  
  80.     setContentPane(top);
  81.     pack();
  82.     setLocation( offset * openFrameCount, offset *openFrameCount);
  83.  
  84.     }
  85.  
  86.     private JPanel buildAddressPanel() {
  87.         JPanel p = new JPanel();
  88.     p.setLayout( new LabeledPairLayout() );
  89.     
  90.  
  91.     JLabel toLabel = new JLabel("To: ", JLabel.RIGHT);
  92.     JTextField toField = new JTextField(25);
  93.     p.add(toLabel, "label");
  94.     p.add(toField, "field");
  95.  
  96.  
  97.     JLabel subLabel = new JLabel("Subj: ", JLabel.RIGHT);
  98.     JTextField subField = new JTextField(25);
  99.     p.add(subLabel, "label");
  100.     p.add(subField, "field");
  101.  
  102.  
  103.     JLabel ccLabel = new JLabel("cc: ", JLabel.RIGHT);
  104.     JTextField ccField = new JTextField(25);
  105.     p.add(ccLabel, "label");
  106.     p.add(ccField, "field");
  107.  
  108.     return p;
  109.  
  110.     }
  111.  
  112.     class LabeledPairLayout implements LayoutManager {
  113.  
  114.       Vector labels = new Vector();
  115.       Vector fields = new Vector();
  116.       
  117.       int yGap = 2;
  118.       int xGap = 2;
  119.  
  120.       public void addLayoutComponent(String s, Component c) {
  121.       if (s.equals("label")) {
  122.           labels.addElement(c);
  123.       }  else {
  124.           fields.addElement(c);
  125.       }
  126.       }
  127.  
  128.       public void layoutContainer(Container c) {
  129.       Insets insets = c.getInsets();
  130.       
  131.       int labelWidth = 0;
  132.       Enumeration labelIter = labels.elements();
  133.       while(labelIter.hasMoreElements()) {
  134.           JComponent comp = (JComponent)labelIter.nextElement();
  135.           labelWidth = Math.max( labelWidth, comp.getPreferredSize().width );
  136.       }
  137.  
  138.       int yPos = insets.top;
  139.  
  140.       Enumeration fieldIter = fields.elements();
  141.       labelIter = labels.elements();
  142.       while(labelIter.hasMoreElements() && fieldIter.hasMoreElements()) {
  143.           JComponent label = (JComponent)labelIter.nextElement();
  144.           JComponent field = (JComponent)fieldIter.nextElement();
  145.           int height = Math.max(label.getPreferredSize().height, field.getPreferredSize().height);
  146.           label.setBounds( insets.left, yPos, labelWidth, height ); 
  147.           field.setBounds( insets.left + labelWidth + xGap, 
  148.                  yPos, 
  149.                  c.getSize().width - (labelWidth +xGap + insets.left + insets.right), 
  150.                  height ); 
  151.           yPos += (height + yGap);
  152.       }
  153.       
  154.       }
  155.  
  156.  
  157.       public Dimension minimumLayoutSize(Container c) {
  158.       Insets insets = c.getInsets();
  159.       
  160.       int labelWidth = 0;
  161.       Enumeration labelIter = labels.elements();
  162.       while(labelIter.hasMoreElements()) {
  163.           JComponent comp = (JComponent)labelIter.nextElement();
  164.           labelWidth = Math.max( labelWidth, comp.getPreferredSize().width );
  165.       }
  166.  
  167.       int yPos = insets.top;
  168.  
  169.       labelIter = labels.elements();
  170.       Enumeration fieldIter = fields.elements();
  171.       while(labelIter.hasMoreElements() && fieldIter.hasMoreElements()) {
  172.           JComponent label = (JComponent)labelIter.nextElement();
  173.           JComponent field = (JComponent)fieldIter.nextElement();
  174.           int height = Math.max(label.getPreferredSize().height, field.getPreferredSize().height);
  175.           yPos += (height + yGap);
  176.       }
  177.       return new Dimension( labelWidth * 3 , yPos );
  178.       }
  179.   
  180.       public Dimension preferredLayoutSize(Container c) {
  181.       Dimension d = minimumLayoutSize(c);
  182.       d.width *= 2;
  183.           return d;
  184.       }
  185.    
  186.       public void removeLayoutComponent(Component c) {}
  187.  
  188. }
  189.  
  190.  
  191. }
  192.  
  193.  
  194.