jdk/src/share/demo/jfc/Metalworks/MetalworksDocumentFrame.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 1998 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  *   - Redistributions in binary form must reproduce the above copyright
       
    12  *     notice, this list of conditions and the following disclaimer in the
       
    13  *     documentation and/or other materials provided with the distribution.
       
    14  *
       
    15  *   - Neither the name of Sun Microsystems nor the names of its
       
    16  *     contributors may be used to endorse or promote products derived
       
    17  *     from this software without specific prior written permission.
       
    18  *
       
    19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
       
    20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       
    21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
       
    23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
       
    27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
       
    28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       
    29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    30  */
       
    31 
       
    32 /*
       
    33  */
       
    34 
       
    35 import java.awt.*;
       
    36 import java.awt.event.*;
       
    37 import java.util.*;
       
    38 import javax.swing.*;
       
    39 import javax.swing.border.*;
       
    40 
       
    41 
       
    42 /**
       
    43   * This is a subclass of JInternalFrame which displays documents.
       
    44   *
       
    45   * @author Steve Wilson
       
    46   */
       
    47 public class MetalworksDocumentFrame extends JInternalFrame {
       
    48 
       
    49     static int openFrameCount = 0;
       
    50     static final int offset = 30;
       
    51 
       
    52     public MetalworksDocumentFrame() {
       
    53         super("", true, true, true, true);
       
    54         openFrameCount++;
       
    55         setTitle("Untitled Message " + openFrameCount);
       
    56 
       
    57         JPanel top = new JPanel();
       
    58         top.setBorder(new EmptyBorder(10, 10, 10, 10));
       
    59         top.setLayout(new BorderLayout());
       
    60         top.add(buildAddressPanel(), BorderLayout.NORTH);
       
    61 
       
    62         JTextArea content = new JTextArea( 15, 30 );
       
    63         content.setBorder( new EmptyBorder(0,5 ,0, 5) );
       
    64         content.setLineWrap(true);
       
    65 
       
    66 
       
    67 
       
    68         JScrollPane textScroller = new JScrollPane(content,
       
    69                                                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
       
    70                                                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
       
    71         top.add( textScroller, BorderLayout.CENTER);
       
    72 
       
    73 
       
    74         setContentPane(top);
       
    75         pack();
       
    76         setLocation( offset * openFrameCount, offset *openFrameCount);
       
    77 
       
    78     }
       
    79 
       
    80     private JPanel buildAddressPanel() {
       
    81         JPanel p = new JPanel();
       
    82         p.setLayout( new LabeledPairLayout() );
       
    83 
       
    84 
       
    85         JLabel toLabel = new JLabel("To: ", JLabel.RIGHT);
       
    86         JTextField toField = new JTextField(25);
       
    87         p.add(toLabel, "label");
       
    88         p.add(toField, "field");
       
    89 
       
    90 
       
    91         JLabel subLabel = new JLabel("Subj: ", JLabel.RIGHT);
       
    92         JTextField subField = new JTextField(25);
       
    93         p.add(subLabel, "label");
       
    94         p.add(subField, "field");
       
    95 
       
    96 
       
    97         JLabel ccLabel = new JLabel("cc: ", JLabel.RIGHT);
       
    98         JTextField ccField = new JTextField(25);
       
    99         p.add(ccLabel, "label");
       
   100         p.add(ccField, "field");
       
   101 
       
   102         return p;
       
   103 
       
   104     }
       
   105 
       
   106     class LabeledPairLayout implements LayoutManager {
       
   107 
       
   108       Vector labels = new Vector();
       
   109       Vector fields = new Vector();
       
   110 
       
   111       int yGap = 2;
       
   112       int xGap = 2;
       
   113 
       
   114       public void addLayoutComponent(String s, Component c) {
       
   115           if (s.equals("label")) {
       
   116               labels.addElement(c);
       
   117           }  else {
       
   118               fields.addElement(c);
       
   119           }
       
   120       }
       
   121 
       
   122       public void layoutContainer(Container c) {
       
   123           Insets insets = c.getInsets();
       
   124 
       
   125           int labelWidth = 0;
       
   126           Enumeration labelIter = labels.elements();
       
   127           while(labelIter.hasMoreElements()) {
       
   128               JComponent comp = (JComponent)labelIter.nextElement();
       
   129               labelWidth = Math.max( labelWidth, comp.getPreferredSize().width );
       
   130           }
       
   131 
       
   132           int yPos = insets.top;
       
   133 
       
   134           Enumeration fieldIter = fields.elements();
       
   135           labelIter = labels.elements();
       
   136           while(labelIter.hasMoreElements() && fieldIter.hasMoreElements()) {
       
   137               JComponent label = (JComponent)labelIter.nextElement();
       
   138               JComponent field = (JComponent)fieldIter.nextElement();
       
   139               int height = Math.max(label.getPreferredSize().height, field.getPreferredSize().height);
       
   140               label.setBounds( insets.left, yPos, labelWidth, height );
       
   141               field.setBounds( insets.left + labelWidth + xGap,
       
   142                                  yPos,
       
   143                                  c.getSize().width - (labelWidth +xGap + insets.left + insets.right),
       
   144                                  height );
       
   145               yPos += (height + yGap);
       
   146           }
       
   147 
       
   148       }
       
   149 
       
   150 
       
   151       public Dimension minimumLayoutSize(Container c) {
       
   152           Insets insets = c.getInsets();
       
   153 
       
   154           int labelWidth = 0;
       
   155           Enumeration labelIter = labels.elements();
       
   156           while(labelIter.hasMoreElements()) {
       
   157               JComponent comp = (JComponent)labelIter.nextElement();
       
   158               labelWidth = Math.max( labelWidth, comp.getPreferredSize().width );
       
   159           }
       
   160 
       
   161           int yPos = insets.top;
       
   162 
       
   163           labelIter = labels.elements();
       
   164           Enumeration fieldIter = fields.elements();
       
   165           while(labelIter.hasMoreElements() && fieldIter.hasMoreElements()) {
       
   166               JComponent label = (JComponent)labelIter.nextElement();
       
   167               JComponent field = (JComponent)fieldIter.nextElement();
       
   168               int height = Math.max(label.getPreferredSize().height, field.getPreferredSize().height);
       
   169               yPos += (height + yGap);
       
   170           }
       
   171           return new Dimension( labelWidth * 3 , yPos );
       
   172       }
       
   173 
       
   174       public Dimension preferredLayoutSize(Container c) {
       
   175           Dimension d = minimumLayoutSize(c);
       
   176           d.width *= 2;
       
   177           return d;
       
   178       }
       
   179 
       
   180       public void removeLayoutComponent(Component c) {}
       
   181 
       
   182 }
       
   183 
       
   184 
       
   185 }