-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathManagerTabs.java
More file actions
291 lines (218 loc) · 7.23 KB
/
ManagerTabs.java
File metadata and controls
291 lines (218 loc) · 7.23 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - https://processing.org
Copyright (c) 2015-22 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app.contrib;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import processing.app.ui.Theme;
import processing.app.ui.Toolkit;
/**
* Tab button bar for Libraries, Modes, Tools, and Updates, across the
* top of the Contribution Manager. Tab content is in ContributionTab.
* Most of the sizing dimensions are identical to EditorHeader.
*/
public class ManagerTabs extends Box {
// height of this tab bar
static final int HIGH = Toolkit.zoom(31);
// amount of space around the entire window
static final int BORDER = Toolkit.zoom(8);
static final int CURVE_RADIUS = Toolkit.zoom(6);
// amount of extra space between individual tabs
static final int TAB_BETWEEN = Toolkit.zoom(2);
// amount of margin on the left/right for the text on the tab
static final int MARGIN = Toolkit.zoom(13);
static final int UNSELECTED = 0;
static final int SELECTED = 1;
Color[] textColor = new Color[2];
Color[] tabColor = new Color[2];
List<Tab> tabList = new ArrayList<>();
Font font;
int fontAscent;
Image gradient;
JPanel cardPanel;
CardLayout cardLayout;
Controller controller;
Component currentPanel;
public ManagerTabs() {
super(BoxLayout.Y_AXIS);
updateTheme();
setBorder(new EmptyBorder(BORDER, BORDER, BORDER, BORDER));
controller = new Controller();
add(controller);
cardLayout = new CardLayout();
cardPanel = new JPanel(cardLayout);
add(cardPanel);
}
protected void updateTheme() {
textColor[SELECTED] = Theme.getColor("manager.tab.text.selected.color");
textColor[UNSELECTED] = Theme.getColor("manager.tab.text.unselected.color");
font = Theme.getFont("manager.tab.text.font");
tabColor[SELECTED] = Theme.getColor("manager.tab.selected.color");
tabColor[UNSELECTED] = Theme.getColor("manager.tab.unselected.color");
gradient = Theme.makeGradient("manager.tab", Toolkit.zoom(400), HIGH);
repaint();
}
/**
* Add a panel with a name.
* @param comp Component that will be shown when this tab is selected
* @param name Title to appear on the tab itself
*/
public void addPanel(Component comp, String name) {
if (tabList.isEmpty()) {
currentPanel = comp;
}
tabList.add(new Tab(comp, name));
cardPanel.add(name, comp);
}
public void setPanel(Component comp) {
for (Tab tab : tabList) {
if (tab.comp == comp) {
currentPanel = comp;
cardLayout.show(cardPanel, tab.name);
repaint();
}
}
}
public Component getPanel() {
return currentPanel;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
class Controller extends JComponent {
Controller() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int x = e.getX();
for (Tab tab : tabList) {
if (tab.contains(x)) {
//cardLayout.show(cardPanel, tab.name);
setPanel(tab.comp);
repaint();
}
}
}
});
}
public void paintComponent(Graphics g) {
if (g == null) return;
g.setFont(font); // need to set this each time through
if (fontAscent == 0) {
fontAscent = (int) Toolkit.getAscent(g);
}
Graphics2D g2 = Toolkit.prepareGraphics(g);
g.drawImage(gradient, 0, 0, getWidth(), getHeight(), this);
// reset all tab positions
for (Tab tab : tabList) {
tab.textWidth = (int)
font.getStringBounds(tab.name, g2.getFontRenderContext()).getWidth();
}
placeTabs();
// now actually draw the tabs
drawTabs(g2);
}
private void placeTabs() {
int x = 0;
for (Tab tab : tabList) {
tab.left = x;
x += MARGIN;
x += tab.textWidth + MARGIN;
tab.right = x;
x += TAB_BETWEEN;
}
// Align the final tab (the "updates") to the right-hand side
Tab lastTab = tabList.get(tabList.size() - 1);
int offset = getWidth() - lastTab.right;
lastTab.left += offset;
lastTab.right += offset;
}
private void drawTabs(Graphics2D g) {
for (Tab tab: tabList) {
tab.draw(g);
}
}
public Dimension getPreferredSize() {
return new Dimension(300, HIGH);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public Dimension getMaximumSize() {
return new Dimension(super.getMaximumSize().width, HIGH);
}
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
class Tab {
String name;
Component comp;
boolean notification;
int left;
int right;
int textWidth;
Tab(Component comp, String name) {
this.comp = comp;
this.name = name;
}
boolean contains(int x) {
return x >= left && x <= right;
}
boolean isCurrent() {
return comp.isVisible();
}
boolean hasLeftNotch() {
return (tabList.get(0) == this ||
tabList.get(tabList.size() - 1) == this);
}
boolean hasRightNotch() {
return (tabList.get(tabList.size() - 1) == this ||
tabList.get(tabList.size() - 2) == this);
}
int getTextLeft() {
return left + ((right - left) - textWidth) / 2;
}
void draw(Graphics g) {
int state = isCurrent() ? SELECTED : UNSELECTED;
g.setColor(tabColor[state]);
Graphics2D g2 = (Graphics2D) g;
g2.fill(Toolkit.createRoundRect(left, 0,
right, HIGH,
hasLeftNotch() ? CURVE_RADIUS : 0,
hasRightNotch() ? CURVE_RADIUS : 0,
0, 0));
int textLeft = getTextLeft();
if (notification && state == UNSELECTED) {
g.setColor(textColor[SELECTED]);
} else {
g.setColor(textColor[state]);
}
int baseline = (HIGH + fontAscent) / 2;
g.drawString(name, textLeft, baseline);
}
}
}