Path: blob/master/src/demo/share/jfc/Metalworks/MetalworksDocumentFrame.java
41152 views
/*1* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import java.awt.BorderLayout;42import java.awt.Component;43import java.awt.Container;44import java.awt.Dimension;45import java.awt.Insets;46import java.awt.LayoutManager;47import java.util.ArrayList;48import java.util.Iterator;49import java.util.List;50import javax.swing.JComponent;51import javax.swing.JInternalFrame;52import javax.swing.JLabel;53import javax.swing.JPanel;54import javax.swing.JScrollPane;55import javax.swing.JTextArea;56import javax.swing.JTextField;57import javax.swing.border.EmptyBorder;585960/**61* This is a subclass of JInternalFrame which displays documents.62*63* @author Steve Wilson64*/65@SuppressWarnings("serial")66public class MetalworksDocumentFrame extends JInternalFrame {6768static int openFrameCount = 0;69static final int offset = 30;7071public MetalworksDocumentFrame() {72super("", true, true, true, true);73openFrameCount++;74setTitle("Untitled Message " + openFrameCount);7576JPanel top = new JPanel();77top.setBorder(new EmptyBorder(10, 10, 10, 10));78top.setLayout(new BorderLayout());79top.add(buildAddressPanel(), BorderLayout.NORTH);8081JTextArea content = new JTextArea(15, 30);82content.setBorder(new EmptyBorder(0, 5, 0, 5));83content.setLineWrap(true);84858687JScrollPane textScroller = new JScrollPane(content,88JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,89JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);90top.add(textScroller, BorderLayout.CENTER);919293setContentPane(top);94pack();95setLocation(offset * openFrameCount, offset * openFrameCount);9697}9899private JPanel buildAddressPanel() {100JPanel p = new JPanel();101p.setLayout(new LabeledPairLayout());102103104JLabel toLabel = new JLabel("To: ", JLabel.RIGHT);105JTextField toField = new JTextField(25);106p.add(toLabel, "label");107p.add(toField, "field");108109110JLabel subLabel = new JLabel("Subj: ", JLabel.RIGHT);111JTextField subField = new JTextField(25);112p.add(subLabel, "label");113p.add(subField, "field");114115116JLabel ccLabel = new JLabel("cc: ", JLabel.RIGHT);117JTextField ccField = new JTextField(25);118p.add(ccLabel, "label");119p.add(ccField, "field");120121return p;122123}124125126class LabeledPairLayout implements LayoutManager {127128List<Component> labels = new ArrayList<Component>();129List<Component> fields = new ArrayList<Component>();130int yGap = 2;131int xGap = 2;132133public void addLayoutComponent(String s, Component c) {134if (s.equals("label")) {135labels.add(c);136} else {137fields.add(c);138}139}140141public void layoutContainer(Container c) {142Insets insets = c.getInsets();143144int labelWidth = 0;145for (Component comp : labels) {146labelWidth = Math.max(labelWidth, comp.getPreferredSize().width);147}148149int yPos = insets.top;150151Iterator<Component> fieldIter = fields.listIterator();152Iterator<Component> labelIter = labels.listIterator();153while (labelIter.hasNext() && fieldIter.hasNext()) {154JComponent label = (JComponent) labelIter.next();155JComponent field = (JComponent) fieldIter.next();156int height = Math.max(label.getPreferredSize().height, field.157getPreferredSize().height);158label.setBounds(insets.left, yPos, labelWidth, height);159field.setBounds(insets.left + labelWidth + xGap,160yPos,161c.getSize().width - (labelWidth + xGap + insets.left162+ insets.right),163height);164yPos += (height + yGap);165}166167}168169public Dimension minimumLayoutSize(Container c) {170Insets insets = c.getInsets();171172int labelWidth = 0;173for (Component comp : labels) {174labelWidth = Math.max(labelWidth, comp.getPreferredSize().width);175}176177int yPos = insets.top;178179Iterator<Component> labelIter = labels.listIterator();180Iterator<Component> fieldIter = fields.listIterator();181while (labelIter.hasNext() && fieldIter.hasNext()) {182Component label = labelIter.next();183Component field = fieldIter.next();184int height = Math.max(label.getPreferredSize().height, field.185getPreferredSize().height);186yPos += (height + yGap);187}188return new Dimension(labelWidth * 3, yPos);189}190191public Dimension preferredLayoutSize(Container c) {192Dimension d = minimumLayoutSize(c);193d.width *= 2;194return d;195}196197public void removeLayoutComponent(Component c) {198}199}200}201202203