forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWelcome.java
More file actions
385 lines (318 loc) · 12.4 KB
/
Welcome.java
File metadata and controls
385 lines (318 loc) · 12.4 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2015 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
version 2, as published by the Free Software Foundation.
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.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;
import processing.app.Base;
import processing.core.PApplet;
import processing.data.StringDict;
public class Welcome {
static public void main(String[] args) {
Base.initPlatform();
//File indexFile = Base.getLibFile("welcome/index.html");
final File indexFile = new File("../build/shared/lib/welcome/index.html");
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Welcome to Processing 3");
// frame.setSize(500, 400);
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
String[] lines = PApplet.loadStrings(indexFile);
String content = PApplet.join(lines, "\n");
int high = getContentHeight(400, content);
//System.out.println(high);
//JTextPane textPane = new JTextPane();
//JEditorPane textPane = new JEditorPane();
JEditorPane textPane = new JEditorPane("text/html", content);
//textPane.setContentType("text/html");
textPane.setEditable(false);
textPane.setPreferredSize(new Dimension(400, high));
//String[] lines = PApplet.loadStrings(indexFile);
//textPane.setText(PApplet.join(lines, "\n"));
// System.out.println(textPane.getPreferredSize().height);
// textPane.setSize(400, textPane.getPreferredSize().height);
//frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.getContentPane().add(textPane);
Toolkit.registerWindowCloseKeys(frame.getRootPane(), new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
HTMLEditorKit kit = (HTMLEditorKit) textPane.getEditorKit();
kit.setAutoFormSubmission(false);
Object title = textPane.getDocument().getProperty("title");
if (title instanceof String) {
frame.setTitle((String) title);
}
textPane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
//System.out.println(e);
if (e instanceof FormSubmitEvent) {
//System.out.println("got submit event");
String result = ((FormSubmitEvent) e).getData();
StringDict dict = new StringDict();
String[] pairs = result.split("&");
for (String pair : pairs) {
String[] pieces = pair.split("=");
String attr = PApplet.urlDecode(pieces[0]);
String valu = PApplet.urlDecode(pieces[1]);
dict.set(attr, valu);
}
dict.print();
} else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
//System.out.println("clicked " + e.getURL());
Base.openURL(e.getURL().toExternalForm());
}
}
});
}
});
}
// Why this doesn't work inline above is beyoned me
static int getContentHeight(int width, String content) {
JEditorPane dummy = new JEditorPane("text/html", content);
dummy.setSize(width, Short.MAX_VALUE);
return dummy.getPreferredSize().height;
}
}
/*
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.embed.swing.JFXPanel;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javax.swing.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import processing.app.Base;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
//import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import static javafx.concurrent.Worker.State.FAILED;
public class Welcome extends JFrame {
private final JFXPanel jfxPanel = new JFXPanel();
private WebEngine engine;
private final JPanel panel = new JPanel(new BorderLayout());
private final JLabel lblStatus = new JLabel();
private final JButton btnGo = new JButton("Go");
private final JTextField txtURL = new JTextField();
private final JProgressBar progressBar = new JProgressBar();
public Welcome() {
super("Welcome to Processing 3");
initComponents();
}
private void initComponents() {
createScene();
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loadURL(txtURL.getText());
}
};
btnGo.addActionListener(al);
txtURL.addActionListener(al);
progressBar.setPreferredSize(new Dimension(150, 18));
progressBar.setStringPainted(true);
JPanel topBar = new JPanel(new BorderLayout(5, 0));
topBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
topBar.add(txtURL, BorderLayout.CENTER);
topBar.add(btnGo, BorderLayout.EAST);
JPanel statusBar = new JPanel(new BorderLayout(5, 0));
statusBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
statusBar.add(lblStatus, BorderLayout.CENTER);
statusBar.add(progressBar, BorderLayout.EAST);
panel.add(topBar, BorderLayout.NORTH);
panel.add(jfxPanel, BorderLayout.CENTER);
panel.add(statusBar, BorderLayout.SOUTH);
getContentPane().add(panel);
setPreferredSize(new Dimension(500, 500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
private void createScene() {
Platform.runLater(new Runnable() {
@Override
public void run() {
WebView view = new WebView();
engine = view.getEngine();
engine.titleProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, final String newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Welcome.this.setTitle(newValue);
}
});
}
});
engine.setOnStatusChanged(new EventHandler<WebEvent<String>>() {
@Override
public void handle(final WebEvent<String> event) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
lblStatus.setText(event.getData());
}
});
}
});
engine.locationProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> ov, String oldValue, final String newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
txtURL.setText(newValue);
}
});
}
});
engine.getLoadWorker().workDoneProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, final Number newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
progressBar.setValue(newValue.intValue());
}
});
}
});
engine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
@Override
public void changed(ObservableValue<? extends Throwable> o, Throwable old, final Throwable value) {
if (engine.getLoadWorker().getState() == FAILED) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(panel,
(value != null) ?
engine.getLocation() + "\n" + value.getMessage() :
engine.getLocation() + "\nUnexpected error.",
"Loading error...",
JOptionPane.ERROR_MESSAGE);
}
});
}
}
});
final String EVENT_TYPE_CLICK = "click";
engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
@Override
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
EventListener listener = new EventListener() {
@Override
public void handleEvent(final Event ev) {
String domEventType = ev.getType();
//System.err.println("EventType: " + domEventType);
if (domEventType.equals(EVENT_TYPE_CLICK)) {
//System.out.println("href is " + href);
EventQueue.invokeLater(new Runnable() {
public void run() {
String href = ((Element)ev.getTarget()).getAttribute("href");
Base.openURL(href);
}
});
}
}
};
Document doc = engine.getDocument();
NodeList nodeList = doc.getElementsByTagName("a");
for (int i = 0; i < nodeList.getLength(); i++) {
((EventTarget) nodeList.item(i)).addEventListener(EVENT_TYPE_CLICK, listener, false);
//((EventTarget) nodeList.item(i)).addEventListener(EVENT_TYPE_MOUSEOVER, listener, false);
//((EventTarget) nodeList.item(i)).addEventListener(EVENT_TYPE_MOUSEOVER, listener, false);
}
}
}
});
jfxPanel.setScene(new Scene(view));
}
});
}
public void loadFile(final File htmlFile) {
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
engine.load(htmlFile.toURI().toURL().toExternalForm());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void loadURL(final String url) {
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
engine.load(new URL(url).toExternalForm());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Welcome browser = new Welcome();
browser.setVisible(true);
Base.initPlatform();
try {
//System.out.println(System.getProperty("user.dir"));
//File indexFile = Base.getLibFile("welcome/index.html");
File indexFile = new File("../build/shared/lib/welcome/index.html");
browser.loadFile(indexFile);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
*/