-
Notifications
You must be signed in to change notification settings - Fork 41
/
ControlPanel.java
186 lines (165 loc) · 5.73 KB
/
ControlPanel.java
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
// Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
package example.detailed.ui;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.cef.OS;
import org.cef.browser.CefBrowser;
@SuppressWarnings("serial")
public class ControlPanel extends JPanel {
private final JButton backButton_;
private final JButton forwardButton_;
private final JButton reloadButton_;
private final JTextField address_field_;
private final JLabel zoom_label_;
private double zoomLevel_ = 0;
private final CefBrowser browser_;
public ControlPanel(CefBrowser browser) {
browser_ = browser;
setEnabled(browser_ != null);
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(Box.createHorizontalStrut(5));
add(Box.createHorizontalStrut(5));
backButton_ = new JButton("Back");
backButton_.setAlignmentX(LEFT_ALIGNMENT);
backButton_.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
browser_.goBack();
}
});
add(backButton_);
add(Box.createHorizontalStrut(5));
forwardButton_ = new JButton("Forward");
forwardButton_.setAlignmentX(LEFT_ALIGNMENT);
forwardButton_.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
browser_.goForward();
}
});
add(forwardButton_);
add(Box.createHorizontalStrut(5));
reloadButton_ = new JButton("Reload");
reloadButton_.setAlignmentX(LEFT_ALIGNMENT);
reloadButton_.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (reloadButton_.getText().equalsIgnoreCase("reload")) {
int mask = OS.isMacintosh()
? ActionEvent.META_MASK
: ActionEvent.CTRL_MASK;
if ((e.getModifiers() & mask) != 0) {
System.out.println("Reloading - ignoring cached values");
browser_.reloadIgnoreCache();
} else {
System.out.println("Reloading - using cached values");
browser_.reload();
}
} else {
browser_.stopLoad();
}
}
});
add(reloadButton_);
add(Box.createHorizontalStrut(5));
JLabel addressLabel = new JLabel("Address:");
addressLabel.setAlignmentX(LEFT_ALIGNMENT);
add(addressLabel);
add(Box.createHorizontalStrut(5));
address_field_ = new JTextField(100);
address_field_.setAlignmentX(LEFT_ALIGNMENT);
address_field_.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
browser_.loadURL(getAddress());
}
});
address_field_.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.clearGlobalFocusOwner();
address_field_.requestFocus();
}
});
add(address_field_);
add(Box.createHorizontalStrut(5));
JButton goButton = new JButton("Go");
goButton.setAlignmentX(LEFT_ALIGNMENT);
goButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
browser_.loadURL(getAddress());
}
});
add(goButton);
add(Box.createHorizontalStrut(5));
JButton minusButton = new JButton("-");
minusButton.setAlignmentX(CENTER_ALIGNMENT);
minusButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
browser_.setZoomLevel(--zoomLevel_);
zoom_label_.setText(new Double(zoomLevel_).toString());
}
});
add(minusButton);
zoom_label_ = new JLabel("0.0");
add(zoom_label_);
JButton plusButton = new JButton("+");
plusButton.setAlignmentX(CENTER_ALIGNMENT);
plusButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
browser_.setZoomLevel(++zoomLevel_);
zoom_label_.setText(new Double(zoomLevel_).toString());
}
});
add(plusButton);
}
public void update(CefBrowser browser, boolean isLoading, boolean canGoBack, boolean canGoForward) {
if (browser == browser_) {
backButton_.setEnabled(canGoBack);
forwardButton_.setEnabled(canGoForward);
reloadButton_.setText( isLoading ? "Abort" : "Reload");
}
}
public String getAddress() {
String address = address_field_.getText();
// If the URI format is unknown "new URI" will throw an
// exception. In this case we interpret the value of the
// address field as search request. Therefore we simply add
// the "search" scheme.
try {
address = address.replaceAll(" ", "%20");
URI test = new URI(address);
if (test.getScheme() != null)
return address;
if (test.getHost() != null && test.getPath() != null)
return address;
String specific = test.getSchemeSpecificPart();
if (specific.indexOf('.') == -1)
throw new URISyntaxException(specific, "No dot inside domain");
} catch (URISyntaxException e1) {
address = "search://" + address;
}
return address;
}
public void setAddress(CefBrowser browser, String address) {
if (browser == browser_)
address_field_.setText(address);
}
}