author | ysuenaga |
Wed, 13 Nov 2019 10:27:06 +0900 | |
changeset 59045 | 846fee5ea75e |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
8961 | 2 |
* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 15 |
* - Neither the name of Oracle nor the names of its |
2 | 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 |
||
10292
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8961
diff
changeset
|
32 |
/* |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8961
diff
changeset
|
33 |
* This source code is provided to illustrate the usage of a given feature |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8961
diff
changeset
|
34 |
* or technique and has been deliberately simplified. Additional steps |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8961
diff
changeset
|
35 |
* required for a production-quality application, such as security checks, |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8961
diff
changeset
|
36 |
* input validation and proper error handling, might not be present in |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8961
diff
changeset
|
37 |
* this sample code. |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8961
diff
changeset
|
38 |
*/ |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8961
diff
changeset
|
39 |
|
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8961
diff
changeset
|
40 |
|
2 | 41 |
|
8961 | 42 |
import java.awt.BorderLayout; |
43 |
import java.awt.Component; |
|
44 |
import java.awt.Container; |
|
45 |
import java.awt.Dimension; |
|
46 |
import java.awt.Insets; |
|
47 |
import java.awt.LayoutManager; |
|
48 |
import java.util.ArrayList; |
|
49 |
import java.util.Iterator; |
|
50 |
import java.util.List; |
|
51 |
import javax.swing.JComponent; |
|
52 |
import javax.swing.JInternalFrame; |
|
53 |
import javax.swing.JLabel; |
|
54 |
import javax.swing.JPanel; |
|
55 |
import javax.swing.JScrollPane; |
|
56 |
import javax.swing.JTextArea; |
|
57 |
import javax.swing.JTextField; |
|
58 |
import javax.swing.border.EmptyBorder; |
|
2 | 59 |
|
60 |
||
61 |
/** |
|
8961 | 62 |
* This is a subclass of JInternalFrame which displays documents. |
63 |
* |
|
64 |
* @author Steve Wilson |
|
65 |
*/ |
|
66 |
@SuppressWarnings("serial") |
|
2 | 67 |
public class MetalworksDocumentFrame extends JInternalFrame { |
68 |
||
69 |
static int openFrameCount = 0; |
|
70 |
static final int offset = 30; |
|
71 |
||
72 |
public MetalworksDocumentFrame() { |
|
73 |
super("", true, true, true, true); |
|
74 |
openFrameCount++; |
|
75 |
setTitle("Untitled Message " + openFrameCount); |
|
76 |
||
77 |
JPanel top = new JPanel(); |
|
78 |
top.setBorder(new EmptyBorder(10, 10, 10, 10)); |
|
79 |
top.setLayout(new BorderLayout()); |
|
80 |
top.add(buildAddressPanel(), BorderLayout.NORTH); |
|
81 |
||
8961 | 82 |
JTextArea content = new JTextArea(15, 30); |
83 |
content.setBorder(new EmptyBorder(0, 5, 0, 5)); |
|
2 | 84 |
content.setLineWrap(true); |
85 |
||
86 |
||
87 |
||
88 |
JScrollPane textScroller = new JScrollPane(content, |
|
8961 | 89 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, |
90 |
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
|
91 |
top.add(textScroller, BorderLayout.CENTER); |
|
2 | 92 |
|
93 |
||
94 |
setContentPane(top); |
|
95 |
pack(); |
|
8961 | 96 |
setLocation(offset * openFrameCount, offset * openFrameCount); |
2 | 97 |
|
98 |
} |
|
99 |
||
100 |
private JPanel buildAddressPanel() { |
|
101 |
JPanel p = new JPanel(); |
|
8961 | 102 |
p.setLayout(new LabeledPairLayout()); |
2 | 103 |
|
104 |
||
105 |
JLabel toLabel = new JLabel("To: ", JLabel.RIGHT); |
|
106 |
JTextField toField = new JTextField(25); |
|
107 |
p.add(toLabel, "label"); |
|
108 |
p.add(toField, "field"); |
|
109 |
||
110 |
||
111 |
JLabel subLabel = new JLabel("Subj: ", JLabel.RIGHT); |
|
112 |
JTextField subField = new JTextField(25); |
|
113 |
p.add(subLabel, "label"); |
|
114 |
p.add(subField, "field"); |
|
115 |
||
116 |
||
117 |
JLabel ccLabel = new JLabel("cc: ", JLabel.RIGHT); |
|
118 |
JTextField ccField = new JTextField(25); |
|
119 |
p.add(ccLabel, "label"); |
|
120 |
p.add(ccField, "field"); |
|
121 |
||
122 |
return p; |
|
123 |
||
124 |
} |
|
125 |
||
8961 | 126 |
|
2 | 127 |
class LabeledPairLayout implements LayoutManager { |
128 |
||
8961 | 129 |
List<Component> labels = new ArrayList<Component>(); |
130 |
List<Component> fields = new ArrayList<Component>(); |
|
131 |
int yGap = 2; |
|
132 |
int xGap = 2; |
|
2 | 133 |
|
8961 | 134 |
public void addLayoutComponent(String s, Component c) { |
135 |
if (s.equals("label")) { |
|
136 |
labels.add(c); |
|
137 |
} else { |
|
138 |
fields.add(c); |
|
139 |
} |
|
140 |
} |
|
2 | 141 |
|
8961 | 142 |
public void layoutContainer(Container c) { |
143 |
Insets insets = c.getInsets(); |
|
2 | 144 |
|
8961 | 145 |
int labelWidth = 0; |
146 |
for (Component comp : labels) { |
|
147 |
labelWidth = Math.max(labelWidth, comp.getPreferredSize().width); |
|
148 |
} |
|
2 | 149 |
|
8961 | 150 |
int yPos = insets.top; |
2 | 151 |
|
8961 | 152 |
Iterator<Component> fieldIter = fields.listIterator(); |
153 |
Iterator<Component> labelIter = labels.listIterator(); |
|
154 |
while (labelIter.hasNext() && fieldIter.hasNext()) { |
|
155 |
JComponent label = (JComponent) labelIter.next(); |
|
156 |
JComponent field = (JComponent) fieldIter.next(); |
|
157 |
int height = Math.max(label.getPreferredSize().height, field. |
|
158 |
getPreferredSize().height); |
|
159 |
label.setBounds(insets.left, yPos, labelWidth, height); |
|
160 |
field.setBounds(insets.left + labelWidth + xGap, |
|
161 |
yPos, |
|
162 |
c.getSize().width - (labelWidth + xGap + insets.left |
|
163 |
+ insets.right), |
|
164 |
height); |
|
165 |
yPos += (height + yGap); |
|
166 |
} |
|
2 | 167 |
|
8961 | 168 |
} |
2 | 169 |
|
8961 | 170 |
public Dimension minimumLayoutSize(Container c) { |
171 |
Insets insets = c.getInsets(); |
|
2 | 172 |
|
8961 | 173 |
int labelWidth = 0; |
174 |
for (Component comp : labels) { |
|
175 |
labelWidth = Math.max(labelWidth, comp.getPreferredSize().width); |
|
176 |
} |
|
2 | 177 |
|
8961 | 178 |
int yPos = insets.top; |
2 | 179 |
|
8961 | 180 |
Iterator<Component> labelIter = labels.listIterator(); |
181 |
Iterator<Component> fieldIter = fields.listIterator(); |
|
182 |
while (labelIter.hasNext() && fieldIter.hasNext()) { |
|
183 |
Component label = labelIter.next(); |
|
184 |
Component field = fieldIter.next(); |
|
185 |
int height = Math.max(label.getPreferredSize().height, field. |
|
186 |
getPreferredSize().height); |
|
187 |
yPos += (height + yGap); |
|
188 |
} |
|
189 |
return new Dimension(labelWidth * 3, yPos); |
|
190 |
} |
|
2 | 191 |
|
8961 | 192 |
public Dimension preferredLayoutSize(Container c) { |
193 |
Dimension d = minimumLayoutSize(c); |
|
194 |
d.width *= 2; |
|
195 |
return d; |
|
196 |
} |
|
2 | 197 |
|
8961 | 198 |
public void removeLayoutComponent(Component c) { |
199 |
} |
|
200 |
} |
|
2 | 201 |
} |