-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathCollapsiblePanel.java
More file actions
225 lines (196 loc) · 8.02 KB
/
CollapsiblePanel.java
File metadata and controls
225 lines (196 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package com.marginallyclever.makelangelo;
import com.marginallyclever.donatello.select.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;
/**
* The user-triggered collapsable panel containing the component (trigger) in the titled border
*/
public class CollapsiblePanel extends JPanel {
private String title;
private final TitledBorder border;
private final JPanel innerPanel;
private final Window parentWindow;
private Dimension previousDimension;
private final int heightCollapsibleComponent;
private Dimension initialDimension;
private final boolean collapsedByDefault;
public CollapsiblePanel(Window parentWindow, String title, int heightCollapsibleComponent, boolean collapsedByDefault) {
this.parentWindow = parentWindow;
this.title = title;
border = BorderFactory.createTitledBorder(title);
setBorder(border);
BorderLayout borderLayout = new BorderLayout();
setLayout(borderLayout);
addMouseListener(mouseListener);
innerPanel = new JPanel(new GridLayout(1, 1), false);
parentWindow.addComponentListener(contentComponentListener);
super.add(innerPanel);
this.heightCollapsibleComponent = heightCollapsibleComponent;
this.collapsedByDefault = collapsedByDefault;
toggleVisibility(false);
}
MouseListener mouseListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
toggleVisibility();
}
};
ComponentListener contentComponentListener = new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
updateBorderTitle();
}
@Override
public void componentHidden(ComponentEvent e) {
updateBorderTitle();
}
};
public String getTitle() {
return title;
}
public void setTitle(String title) {
firePropertyChange("title", this.title, this.title = title);
}
@Override
public Component add(Component comp) {
comp.addComponentListener(contentComponentListener);
comp.setVisible(!collapsedByDefault);
Component r = innerPanel.add(comp);
updateBorderTitle();
return r;
}
@Override
public Component add(String name, Component comp) {
comp.addComponentListener(contentComponentListener);
comp.setVisible(!collapsedByDefault);
Component r = innerPanel.add(name, comp);
updateBorderTitle();
return r;
}
@Override
public Component add(Component comp, int index) {
comp.addComponentListener(contentComponentListener);
comp.setVisible(!collapsedByDefault);
Component r = innerPanel.add(comp, index);
updateBorderTitle();
return r;
}
@Override
public void add(Component comp, Object constraints) {
comp.addComponentListener(contentComponentListener);
comp.setVisible(!collapsedByDefault);
innerPanel.add(comp, constraints);
updateBorderTitle();
}
@Override
public void add(Component comp, Object constraints, int index) {
comp.addComponentListener(contentComponentListener);
comp.setVisible(!collapsedByDefault);
innerPanel.add(comp, constraints, index);
updateBorderTitle();
}
@Override
public void remove(int index) {
Component comp = innerPanel.getComponent(index);
comp.removeComponentListener(contentComponentListener);
innerPanel.remove(index);
}
@Override
public void remove(Component comp) {
comp.removeComponentListener(contentComponentListener);
innerPanel.remove(comp);
}
@Override
public void removeAll() {
for (Component c : getComponents()) {
c.removeComponentListener(contentComponentListener);
}
innerPanel.removeAll();
}
protected void toggleVisibility() {
toggleVisibility(!hasVisibleComponent());
}
protected void toggleVisibility(boolean visible) {
for (Component c : innerPanel.getComponents()) {
c.setVisible(visible);
}
updateBorderTitle();
if (initialDimension == null) {
initialDimension = new Dimension(parentWindow.getSize());
}
if (visible) {
// expands all elements
int height = previousDimension == null ? heightCollapsibleComponent: previousDimension.height;
Dimension toggle = new Dimension(parentWindow.getWidth(), height);
parentWindow.setPreferredSize(toggle);
parentWindow.setMinimumSize(new Dimension(initialDimension.width, heightCollapsibleComponent));
parentWindow.setMaximumSize(null);
} else {
// collapse all elements
previousDimension = parentWindow.getSize();
int height = previousDimension.height - innerPanel.getHeight();
Dimension toggle = new Dimension(previousDimension.width, height);
parentWindow.setPreferredSize(toggle);
parentWindow.setMinimumSize(initialDimension);
parentWindow.setMaximumSize(new Dimension(previousDimension.width, initialDimension.height));
}
parentWindow.validate();
parentWindow.repaint();
parentWindow.pack();
repaint();
}
private void updateBorderTitle() {
String arrow = "";
if (innerPanel.getComponentCount() > 0) {
arrow = (hasVisibleComponent() ? "-" : "+");
}
border.setTitle(title + " " + arrow + " ");
}
private boolean hasVisibleComponent() {
for (Component c : innerPanel.getComponents()) {
if (c.isVisible()) {
return true;
}
}
return false;
}
// TEST
public static void main(String[] args) {
JFrame frame = new JFrame("Collapsible Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jPanel = new JPanel(new GridBagLayout());
CollapsiblePanel cpanel = new CollapsiblePanel(frame, "lot of buttons", 400, true);
jPanel.add(cpanel, BorderLayout.CENTER);
SelectBoolean a = new SelectBoolean("A", "AAAAAAAAAAA", false);
SelectButton b = new SelectButton("B", "B");
SelectColor c = new SelectColor("C", "CCCCCC", Color.BLACK, frame);
SelectFile d = new SelectFile("D", "D", null,cpanel);
SelectDouble e = new SelectDouble("E", "E", 0.0f);
SelectInteger f = new SelectInteger("F", "FFF", 0);
String[] list = {"cars", "trains", "planes", "boats", "rockets"};
SelectOneOfMany g = new SelectOneOfMany("G", "G", list, 0);
String ipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
SelectReadOnlyText h = new SelectReadOnlyText("H", "H " + ipsum);
SelectSlider i = new SelectSlider("I", "I", 200, 0, 100);
SelectTextArea j = new SelectTextArea("J", "J", ipsum);
var inner = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
a.attach(inner, gbc); gbc.gridy++;
b.attach(inner,gbc); gbc.gridy++;
c.attach(inner,gbc); gbc.gridy++;
d.attach(inner,gbc); gbc.gridy++;
e.attach(inner,gbc); gbc.gridy++;
f.attach(inner,gbc); gbc.gridy++;
g.attach(inner,gbc); gbc.gridy++;
h.attach(inner,gbc); gbc.gridy++;
i.attach(inner,gbc); gbc.gridy++;
j.attach(inner,gbc); gbc.gridy++;
cpanel.add(inner);
frame.setPreferredSize(new Dimension(600, 90));
frame.add(jPanel);
frame.pack();
frame.setVisible(true);
}
}