Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/demo/share/jfc/Metalworks/MetalworksDocumentFrame.java
41152 views
1
/*
2
* Copyright (c) 1998, 2011, Oracle and/or its affiliates. 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 Oracle 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
* This source code is provided to illustrate the usage of a given feature
34
* or technique and has been deliberately simplified. Additional steps
35
* required for a production-quality application, such as security checks,
36
* input validation and proper error handling, might not be present in
37
* this sample code.
38
*/
39
40
41
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;
59
60
61
/**
62
* This is a subclass of JInternalFrame which displays documents.
63
*
64
* @author Steve Wilson
65
*/
66
@SuppressWarnings("serial")
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
82
JTextArea content = new JTextArea(15, 30);
83
content.setBorder(new EmptyBorder(0, 5, 0, 5));
84
content.setLineWrap(true);
85
86
87
88
JScrollPane textScroller = new JScrollPane(content,
89
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
90
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
91
top.add(textScroller, BorderLayout.CENTER);
92
93
94
setContentPane(top);
95
pack();
96
setLocation(offset * openFrameCount, offset * openFrameCount);
97
98
}
99
100
private JPanel buildAddressPanel() {
101
JPanel p = new JPanel();
102
p.setLayout(new LabeledPairLayout());
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
126
127
class LabeledPairLayout implements LayoutManager {
128
129
List<Component> labels = new ArrayList<Component>();
130
List<Component> fields = new ArrayList<Component>();
131
int yGap = 2;
132
int xGap = 2;
133
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
}
141
142
public void layoutContainer(Container c) {
143
Insets insets = c.getInsets();
144
145
int labelWidth = 0;
146
for (Component comp : labels) {
147
labelWidth = Math.max(labelWidth, comp.getPreferredSize().width);
148
}
149
150
int yPos = insets.top;
151
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
}
167
168
}
169
170
public Dimension minimumLayoutSize(Container c) {
171
Insets insets = c.getInsets();
172
173
int labelWidth = 0;
174
for (Component comp : labels) {
175
labelWidth = Math.max(labelWidth, comp.getPreferredSize().width);
176
}
177
178
int yPos = insets.top;
179
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
}
191
192
public Dimension preferredLayoutSize(Container c) {
193
Dimension d = minimumLayoutSize(c);
194
d.width *= 2;
195
return d;
196
}
197
198
public void removeLayoutComponent(Component c) {
199
}
200
}
201
}
202
203