-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathSketchbookFrame.java
More file actions
215 lines (172 loc) · 6.94 KB
/
SketchbookFrame.java
File metadata and controls
215 lines (172 loc) · 6.94 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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2013-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.ui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeSelectionModel;
import processing.app.Base;
import processing.app.Language;
import processing.app.Platform;
import processing.app.SketchReference;
public class SketchbookFrame extends JFrame {
protected Base base;
public SketchbookFrame(final Base base) {
super(Language.text("sketchbook"));
this.base = base;
final ActionListener listener = e -> setVisible(false);
Toolkit.registerWindowCloseKeys(getRootPane(), listener);
Toolkit.setIcon(this);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
updateCenterPanel();
Container buttons = Box.createHorizontalBox();
JButton addButton = new JButton("Show Folder");
addButton.addActionListener(e -> Platform.openFolder(Base.getSketchbookFolder()));
buttons.add(Box.createHorizontalGlue());
buttons.add(addButton, BorderLayout.WEST);
JButton refreshButton = new JButton("Refresh");
refreshButton.addActionListener(e -> base.rebuildSketchbook());
buttons.add(refreshButton, BorderLayout.EAST);
buttons.add(Box.createHorizontalGlue());
final int high = addButton.getPreferredSize().height;
final int wide = 4 * Toolkit.getButtonWidth() / 3;
addButton.setPreferredSize(new Dimension(wide, high));
refreshButton.setPreferredSize(new Dimension(wide, high));
JPanel buttonPanel = new JPanel(); // adds extra border
// wasn't necessary to set a border b/c JPanel adds plenty
//buttonPanel.setBorder(new EmptyBorder(3, 0, 3, 0));
buttonPanel.add(buttons);
pane.add(buttonPanel, BorderLayout.SOUTH);
pack();
}
private JTree rebuildTree() {
final JTree tree = new JTree(base.buildSketchbookTree());
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setShowsRootHandles(true);
tree.expandRow(0);
tree.setRootVisible(false);
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
int selRow = tree.getRowForLocation(e.getX(), e.getY());
//TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
//if (node != null && node.isLeaf() && node.getPath().equals(selPath)) {
if (node != null && node.isLeaf() && selRow != -1) {
SketchReference sketch = (SketchReference) node.getUserObject();
base.handleOpen(sketch.getPath());
}
}
}
});
tree.addKeyListener(new KeyAdapter() {
// ESC doesn't fire keyTyped(), so we have to catch it on keyPressed
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
setVisible(false);
}
}
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (node != null && node.isLeaf()) {
SketchReference sketch = (SketchReference) node.getUserObject();
base.handleOpen(sketch.getPath());
}
}
}
});
final int border = Toolkit.zoom(5);
tree.setBorder(new EmptyBorder(border, border, border, border));
if (Platform.isMacOS()) {
tree.setToggleClickCount(2);
} else {
tree.setToggleClickCount(1);
}
// Special cell renderer that takes the UI zoom into account
tree.setCellRenderer(new ZoomTreeCellRenderer());
return tree;
}
private void updateCenterPanel() {
JTree tree = rebuildTree();
Container panel;
// Check whether sketchbook is empty or not
TreeModel treeModel = tree.getModel();
if (treeModel.getChildCount(treeModel.getRoot()) != 0) {
JScrollPane treePane = new JScrollPane(tree,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
treePane.setPreferredSize(Toolkit.zoom(250, 450));
treePane.setBorder(new EmptyBorder(0, 0, 0, 0));
//getContentPane().add(treePane);
panel = treePane;
} else {
JPanel emptyPanel = new JPanel();
emptyPanel.setBackground(Color.WHITE);
emptyPanel.setPreferredSize(Toolkit.zoom(250,450));
JLabel emptyLabel = new JLabel("Empty Sketchbook");
emptyLabel.setForeground(Color.GRAY);
emptyPanel.add(emptyLabel);
//setContentPane(emptyPanel);
panel = emptyPanel;
}
Container pane = getContentPane();
//pane.setLayout(new BorderLayout());
BorderLayout layout = (BorderLayout) pane.getLayout();
Component comp = layout.getLayoutComponent(BorderLayout.CENTER);
if (comp != null) {
pane.remove(comp);
}
pane.add(panel, BorderLayout.CENTER);
}
public void rebuild() {
updateCenterPanel();
// After replacing the tree/panel, this calls the layout manager,
// otherwise it'll just leave a frozen-looking component until
// the window is closed and re-opened.
getContentPane().validate();
}
public void setVisible() {
// TODO The ExamplesFrame code doesn't invokeLater(), is it necessary?
// Either one of them is wrong, or this is hiding a bug [fry 150811]
EventQueue.invokeLater(() -> {
// Space for the editor plus a li'l gap
int roughWidth = getWidth() + 20;
// If no window open, or the editor is at the edge of the screen
Editor editor = base.getActiveEditor();
if (editor == null) {
setLocationRelativeTo(null);
} else {
Point p = editor.getLocation();
if (p.x < roughWidth) {
// Center the window on the screen
setLocationRelativeTo(null);
} else {
// Open the window relative to the editor
setLocation(p.x - roughWidth, p.y);
}
}
setVisible(true);
});
}
}