-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathAddressBar.java
More file actions
184 lines (152 loc) · 5.05 KB
/
AddressBar.java
File metadata and controls
184 lines (152 loc) · 5.05 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
package nodebox.client;
import nodebox.node.Node;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class AddressBar extends JPanel implements MouseListener {
public static Image addressGradient;
public static Image addressArrow;
static {
try {
addressGradient = ImageIO.read(new File("res/address-gradient.png"));
addressArrow = ImageIO.read(new File("res/address-arrow.png"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//private ArrayList<Node> parts = new List<Node>[]{"root", "poster", "background"};
private int[] positions;
private int armed = -1;
private OnPartClickListener onPartClickListener;
private Node activeNetwork;
private JProgressBar progressBar;
public AddressBar() {
addMouseListener(this);
setMinimumSize(new Dimension(0, 25));
setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
setPreferredSize(new Dimension(Integer.MAX_VALUE, 25));
setLayout(null);
progressBar = new JProgressBar();
progressBar.putClientProperty("JProgressBar.style", "circular");
progressBar.setIndeterminate(true);
progressBar.setBorderPainted(false);
progressBar.setVisible(false);
add(progressBar);
}
public Node getActiveNetwork() {
return activeNetwork;
}
public void setActiveNetwork(Node activeNetwork) {
this.activeNetwork = activeNetwork;
repaint();
}
/**
* Returns the part-click callback registered for this address bar.
*
* @return The callback, or null if one is not registered.
*/
public OnPartClickListener getOnPartClickListener() {
return onPartClickListener;
}
/**
* Register a callback to be invoked when a part was clicked in the address bar.
*
* @param l the callback that will run.
*/
public void setOnPartClickListener(OnPartClickListener l) {
onPartClickListener = l;
}
public boolean getProgressVisible() {
return progressBar.isVisible();
}
public void setProgressVisible(boolean visible) {
progressBar.setVisible(visible);
}
private java.util.List<Node> getNetworkParts() {
ArrayList<Node> parts = new ArrayList<Node>();
if (activeNetwork == null) return parts;
Node currentNode = activeNetwork;
parts.add(0, currentNode);
while (currentNode.getParent() != null) {
parts.add(0, currentNode.getParent());
currentNode = currentNode.getParent();
}
return parts;
}
@Override
protected void paintComponent(Graphics g) {
java.util.List<Node> nodes = getNetworkParts();
positions = new int[nodes.size()];
Graphics2D g2 = (Graphics2D) g;
g2.setFont(Theme.SMALL_BOLD_FONT);
g2.drawImage(addressGradient, 0, 0, getWidth(), 25, null);
int x = 10;
for (int i = 0; i < nodes.size(); i++) {
Node part = nodes.get(i);
if (i == armed) {
g2.setColor(Theme.TEXT_ARMED_COLOR);
} else {
g2.setColor(Theme.TEXT_NORMAL_COLOR);
}
SwingUtils.drawShadowText(g2, part.getName(), x, 16);
int width = (int) g2.getFontMetrics().stringWidth(part.getName());
x += width + 5;
positions[i] = x + 10;
g2.drawImage(addressArrow, x, 1, null);
x += 15;
}
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
int mx = e.getX();
armed = partIndex(mx);
repaint();
}
public void mouseReleased(MouseEvent e) {
armed = -1;
int mx = e.getX();
int partIndex = partIndex(mx);
if (partIndex == -1) return;
java.util.List<Node> nodes = getNetworkParts();
Node selectedNode = nodes.get(partIndex);
if (selectedNode != null && onPartClickListener != null)
onPartClickListener.onPartClicked(selectedNode);
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
armed = -1;
repaint();
}
private int partIndex(int x) {
if (positions == null) return -1;
for (int i = 0; i < positions.length; i++) {
if (x < positions[i])
return i;
}
return -1;
}
@Override
public void doLayout() {
final int width = getWidth();
progressBar.setBounds(width - 23, 3, 20, 20);
}
/**
* Callback listener to be invoked when an address part has been clicked.
*/
public static interface OnPartClickListener {
/**
* Called when a part has been clicked.
*
* @param n the part that was clicked.
*/
public void onPartClicked(Node n);
}
}