001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.forms.tutorial.pitfalls;
032:
033: import java.awt.Color;
034: import java.awt.Dimension;
035: import java.awt.event.ComponentAdapter;
036: import java.awt.event.ComponentEvent;
037:
038: import javax.swing.*;
039: import javax.swing.border.CompoundBorder;
040: import javax.swing.border.EmptyBorder;
041: import javax.swing.border.LineBorder;
042:
043: import com.jgoodies.forms.builder.DefaultFormBuilder;
044: import com.jgoodies.forms.builder.PanelBuilder;
045: import com.jgoodies.forms.layout.CellConstraints;
046: import com.jgoodies.forms.layout.FormLayout;
047:
048: /**
049: * Demonstrates how a JTextArea's preferred size grows with the container
050: * if no columns and rows are set.
051: *
052: * @author Karsten Lentzsch
053: * @version $Revision: 1.8 $
054: */
055: public final class GrowingTextAreaExample {
056:
057: public static void main(String[] args) {
058: try {
059: UIManager
060: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
061: } catch (Exception e) {
062: // Likely PlasticXP is not in the class path; ignore.
063: }
064: JFrame frame = new JFrame();
065: frame.setTitle("Forms Tutorial :: Growing Text Area");
066: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
067: JComponent panel = new GrowingTextAreaExample().buildPanel();
068: frame.getContentPane().add(panel);
069: frame.pack();
070: frame.setVisible(true);
071: }
072:
073: public JComponent buildPanel() {
074: JTabbedPane tabbedPane = new JTabbedPane();
075: tabbedPane.putClientProperty("jgoodies.noContentBorder",
076: Boolean.TRUE);
077:
078: String example1Text = "Here the layout uses a fixed initial size of 200 dlu. "
079: + "The area's minimum and preferred sizes will be ignored. "
080: + "And so, the area will grow and shrink.";
081:
082: tabbedPane.add("1", buildTab("Fixed Size (Good)",
083: "fill:200dlu:grow", createArea(example1Text, true, 0,
084: null)));
085:
086: String example2Text = "This text area has line wrapping disabled\n"
087: + "and uses a hand-wrapped text (using '\\n').\n\n"
088: + "Its minimum and preferred sizes are constant and so,\n"
089: + "the area will grow but shrink down to its minimum size.";
090:
091: tabbedPane.add("2", buildTab(
092: "Pref Size, Line Wrap Disabled (Good)",
093: "fill:pref:grow", createArea(example2Text, false, 0,
094: null)));
095:
096: String example3Text = "This text area grows horizontally and will never shrink again. "
097: + "Since line wrapping is enabled, "
098: + "the area's preferred size is defined by its size. "
099: + "(See BasicTextAreaUI#getPreferredSize(Component).\n\n"
100: + "If the layout container grows, the layout manager "
101: + "sets a larger size, and hence, the preferred size grows."
102: + "The FormLayout honors the area's preferred size to compute "
103: + "the new column width, and so the area won't shrink again.\n\n"
104: + "Even if you use a 'default' width the column won't shrink.";
105:
106: tabbedPane.add("3", buildTab(
107: "Pref Size, Line Wrap Enabled (Never Shrinks)",
108: "fill:pref:grow", createArea(example3Text, true, 0,
109: null)));
110:
111: String example4Text = "This text area grows but never shrinks. "
112: + "Since line wrapping is enabled, the area's "
113: + "minimum and preferred sizes are defined by its size.\n\n"
114: + "If the layout container grows, the layout manager "
115: + "sets a larger size, and hence, the minimum and preferred sizes grow. "
116: + "If the layout container shrinks, the layout manager can shrink "
117: + "the column width down to the area's minimum width. "
118: + "But the minimum size is like the preferred size determined "
119: + "by the size previously set, and so, the column won't shrink.\n\n"
120: + "A solution to this problem is to set a custom minimum size.";
121:
122: tabbedPane.add("4", buildTab(
123: "Default Size, Line Wrap Enabled (Never Shrinks)",
124: "fill:default:grow", createArea(example4Text, true, 0,
125: null)));
126:
127: String example5Text = "This text area has uses a column width of 30 characters. "
128: + "But that just affects the initial preferred and minimum size."
129: + "The area grows and won't shrink again - just as in tabs 3 and 4.";
130:
131: tabbedPane
132: .add(
133: "5",
134: buildTab(
135: "Default Size, Line Wrap Enabled, Columns Set (Never Shrinks)",
136: "fill:default:grow", createArea(
137: example5Text, true, 30, null)));
138:
139: String example6Text = "This text area grows and shrinks. "
140: + "Since line wrapping is enabled, "
141: + "the area's preferred size is defined by its size. "
142: + "Here a custom minimum size (100, 32) has been set.\n\n"
143: + "If the layout container grows, the layout manager "
144: + "sets a larger size, and hence, the preferred size grows. "
145: + "However, if the layout container shrinks, the layout can "
146: + "shrink the column down to the area's minimum width, which is 100; "
147: + "the minimum size is independent from the size previously set.";
148:
149: tabbedPane.add("6", buildTab(
150: "Default Size, Line Wrap Enabled, Min Size Set (Good)",
151: "fill:default:grow", createArea(example6Text, true, 0,
152: new Dimension(100, 32))));
153:
154: return tabbedPane;
155: }
156:
157: private JTextArea createArea(String text, boolean lineWrap,
158: int columns, Dimension minimumSize) {
159: JTextArea area = new JTextArea(text);
160: area.setBorder(new CompoundBorder(new LineBorder(Color.GRAY),
161: new EmptyBorder(1, 3, 1, 1)));
162: area.setLineWrap(lineWrap);
163: area.setWrapStyleWord(true);
164: area.setColumns(columns);
165: if (minimumSize != null) {
166: area.setMinimumSize(new Dimension(100, 32));
167: }
168: return area;
169: }
170:
171: private JComponent buildTab(String title, String columnSpec,
172: JTextArea area) {
173: JLabel columnSpecLabel = new JLabel(columnSpec);
174: columnSpecLabel.setHorizontalAlignment(JLabel.CENTER);
175:
176: FormLayout layout = new FormLayout(columnSpec,
177: "pref, 9dlu, pref, 3dlu, fill:default:grow, 9dlu, pref");
178: PanelBuilder builder = new PanelBuilder(layout);
179: builder.setDefaultDialogBorder();
180: CellConstraints cc = new CellConstraints();
181: builder.addTitle(title, cc.xy(1, 1));
182: builder.add(columnSpecLabel, cc.xy(1, 3));
183: builder.add(area, cc.xy(1, 5));
184: builder.add(buildInfoPanel(area), cc.xy(1, 7));
185:
186: return builder.getPanel();
187: }
188:
189: private JComponent buildInfoPanel(JTextArea area) {
190: JLabel sizeLabel = new JLabel();
191: JLabel minSizeLabel = new JLabel();
192: JLabel prefSizeLabel = new JLabel();
193: JLabel lineWrapLabel = new JLabel(
194: area.getLineWrap() ? "enabled" : "disabled");
195: JLabel customMinLabel = new JLabel(
196: area.isMinimumSizeSet() ? "set" : "computed");
197: JLabel columnsLabel = new JLabel(
198: area.getColumns() == 0 ? "not specified" : String
199: .valueOf(area.getColumns()));
200:
201: area.addComponentListener(new SizeChangeHandler(area,
202: sizeLabel, minSizeLabel, prefSizeLabel));
203:
204: FormLayout layout = new FormLayout(
205: "pref, 4dlu, pref, 21dlu, pref, 4dlu, pref");
206: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
207: builder.append("Size:", sizeLabel);
208: builder.append("Line wrap:", lineWrapLabel);
209:
210: builder.append("Min size:", minSizeLabel);
211: builder.append("Min size:", customMinLabel);
212:
213: builder.append("Pref size:", prefSizeLabel);
214: builder.append("Columns:", columnsLabel);
215: return builder.getPanel();
216: }
217:
218: /**
219: * Listens to area size changes and writes the formatted sizes to the given labels.
220: */
221: private static final class SizeChangeHandler extends
222: ComponentAdapter {
223:
224: private final JTextArea area;
225: private final JLabel sizeLabel;
226: private final JLabel minSizeLabel;
227: private final JLabel prefSizeLabel;
228:
229: private SizeChangeHandler(JTextArea area, JLabel sizeLabel,
230: JLabel minSizeLabel, JLabel prefSizeLabel) {
231: this .area = area;
232: this .sizeLabel = sizeLabel;
233: this .minSizeLabel = minSizeLabel;
234: this .prefSizeLabel = prefSizeLabel;
235: }
236:
237: public void componentResized(ComponentEvent evt) {
238: sizeLabel.setText(format(area.getSize()));
239: minSizeLabel.setText(format(area.getMinimumSize()));
240: prefSizeLabel.setText(format(area.getPreferredSize()));
241: }
242:
243: private String format(Dimension d) {
244: return String.valueOf(d.width) + ", " + d.height;
245: }
246:
247: }
248:
249: }
|