FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/app/src/processing/app/ui/WebFrame.java at master · dimr/processing · GitHub
dimr
processing
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
ui
/
WebFrame.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
196 lines (162 loc) · 5.85 KB
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
ui
/
WebFrame.java
Copy path
File metadata and controls
196 lines (162 loc) · 5.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/* -*- 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
.
event
.
ActionEvent
;
import
java
.
awt
.
event
.
ActionListener
;
import
java
.
beans
.
PropertyChangeEvent
;
import
java
.
beans
.
PropertyChangeListener
;
import
java
.
io
.
File
;
import
java
.
io
.
IOException
;
import
java
.
net
.
URL
;
import
javax
.
swing
.*;
import
javax
.
swing
.
event
.*;
import
javax
.
swing
.
text
.
html
.*;
import
processing
.
app
.
Platform
;
import
processing
.
core
.
PApplet
;
import
processing
.
data
.
StringDict
;
public
class
WebFrame
extends
JFrame
{
JEditorPane
editorPane
;
HTMLEditorKit
editorKit
;
// int contentHeight;
boolean
ready
;
public
WebFrame
(
File
file
,
int
width
)
throws
IOException
{
// Need to use the URL version so that relative paths work for images
// https://github.com/processing/processing/issues/3494
URL
fileUrl
=
file
.
toURI
().
toURL
();
//.toExternalForm();
requestContentHeight
(
width
,
fileUrl
);
editorPane
=
new
JEditorPane
();
// Title cannot be set until the page has loaded
editorPane
.
addPropertyChangeListener
(
"page"
,
new
PropertyChangeListener
() {
public
void
propertyChange
(
PropertyChangeEvent
evt
) {
Object
title
=
editorPane
.
getDocument
().
getProperty
(
"title"
);
if
(
title
instanceof
String
) {
setTitle
((
String
)
title
);
}
}
});
editorPane
.
setPage
(
fileUrl
);
editorPane
.
setEditable
(
false
);
// set height to something generic
editorPane
.
setPreferredSize
(
new
Dimension
(
width
,
width
));
getContentPane
().
add
(
editorPane
);
Toolkit
.
registerWindowCloseKeys
(
getRootPane
(),
new
ActionListener
() {
@
Override
public
void
actionPerformed
(
ActionEvent
e
) {
handleClose
();
}
});
Toolkit
.
setIcon
(
this
);
editorKit
= (
HTMLEditorKit
)
editorPane
.
getEditorKit
();
editorKit
.
setAutoFormSubmission
(
false
);
editorPane
.
addHyperlinkListener
(
new
HyperlinkListener
() {
@
Override
public
void
hyperlinkUpdate
(
HyperlinkEvent
e
) {
//System.out.println(e);
if
(
e
instanceof
FormSubmitEvent
) {
String
result
= ((
FormSubmitEvent
)
e
).
getData
();
StringDict
dict
=
new
StringDict
();
if
(
result
.
trim
().
length
() !=
0
) {
String
[]
pairs
=
result
.
split
(
"&"
);
for
(
String
pair
:
pairs
) {
//System.out.println("pair is " + pair);
String
[]
pieces
=
pair
.
split
(
"="
);
String
attr
=
PApplet
.
urlDecode
(
pieces
[
0
]);
String
valu
=
PApplet
.
urlDecode
(
pieces
[
1
]);
dict
.
set
(
attr
,
valu
);
}
}
//dict.print();
handleSubmit
(
dict
);
}
else
if
(
e
.
getEventType
() ==
HyperlinkEvent
.
EventType
.
ACTIVATED
) {
//System.out.println("clicked " + e.getURL());
handleLink
(
e
.
getURL
().
toExternalForm
());
}
}
});
}
/* not yet working
public void addStyle(String rule) {
StyleSheet sheet = kit.getStyleSheet();
System.out.println(sheet);
sheet.addRule(rule);
kit.setStyleSheet(sheet);
//textPane.setEditorKit(kit); // nukes everything
}
*/
public
void
setVisible
(
final
boolean
visible
) {
new
Thread
(
new
Runnable
() {
public
void
run
() {
while
(!
ready
) {
try
{
Thread
.
sleep
(
5
);
}
catch
(
InterruptedException
e
) {
e
.
printStackTrace
();
}
}
WebFrame
.
super
.
setVisible
(
visible
);
}
}).
start
();
}
public
void
handleClose
() {
dispose
();
}
/**
* Override this to do something interesting when a form is submitted.
* To keep things simple, this doesn't allow for multiple params with the
* same name. If no params submitted, the Dict will be empty (not null).
*/
public
void
handleSubmit
(
StringDict
dict
) {
}
public
void
handleLink
(
String
link
) {
Platform
.
openURL
(
link
);
}
/*
// Why this doesn't work inline above is beyond 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;
}
*/
// Unlike the static version above that uses an (already loaded) String for
// the content, using setPage() makes things run asynchronously, causing
// getContentHeight() to fail because it returns zero. Instead we make
// things 10x more complicated so that images will work.
void
requestContentHeight
(
final
int
width
,
final
URL
url
) {
new
Thread
(
new
Runnable
() {
public
void
run
() {
final
JEditorPane
dummy
=
new
JEditorPane
();
dummy
.
addPropertyChangeListener
(
"page"
,
new
PropertyChangeListener
() {
@
Override
public
void
propertyChange
(
PropertyChangeEvent
evt
) {
int
high
=
dummy
.
getPreferredSize
().
height
;
editorPane
.
setPreferredSize
(
new
Dimension
(
width
,
high
));
pack
();
setLocationRelativeTo
(
null
);
ready
=
true
;
}
});
try
{
dummy
.
setPage
(
url
);
}
catch
(
IOException
e
) {
e
.
printStackTrace
();
}
dummy
.
setSize
(
width
,
Short
.
MAX_VALUE
);
}
}).
start
();
}
}
Back
|
FazBrowse Home
|
New Git URL