FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/app/src/processing/app/ChangeDetector.java at master · botbit/processing · GitHub
botbit
processing
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
ChangeDetector.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
227 lines (194 loc) · 6.94 KB
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
ChangeDetector.java
Copy path
File metadata and controls
227 lines (194 loc) · 6.94 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
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
package
processing
.
app
;
import
java
.
awt
.
EventQueue
;
import
java
.
awt
.
Frame
;
import
java
.
awt
.
event
.
WindowEvent
;
import
java
.
awt
.
event
.
WindowFocusListener
;
import
java
.
io
.
File
;
import
java
.
io
.
FilenameFilter
;
import
java
.
lang
.
reflect
.
InvocationTargetException
;
import
javax
.
swing
.
JOptionPane
;
public
class
ChangeDetector
implements
WindowFocusListener
{
private
Sketch
sketch
;
private
Editor
editor
;
// Set true if the user selected 'no'. TODO this can't just skip once,
// because subsequent returns to the window w/o saving will keep firing.
private
boolean
skip
=
false
;
public
ChangeDetector
(
Editor
editor
) {
this
.
sketch
=
editor
.
sketch
;
this
.
editor
=
editor
;
}
@
Override
public
void
windowGainedFocus
(
WindowEvent
e
) {
// Keep the listener instantiated and check this to avoid a maze of
// adding and removing and re-adding with Preferences changes.
if
(
Preferences
.
getBoolean
(
"editor.watcher"
)) {
// if they selected no, skip the next focus event
if
(
skip
) {
skip
=
false
;
}
else
{
new
Thread
(
new
Runnable
() {
@
Override
public
void
run
() {
checkFileChange
();
}
}).
start
();
}
}
}
@
Override
public
void
windowLostFocus
(
WindowEvent
e
) {
// Shouldn't need to do anything here, and not storing anything here b/c we
// don't want to assume a loss of focus is required before change detection
}
private
void
checkFileChange
() {
//check that the content of each of the files in sketch matches what is in memory
if
(
sketch
==
null
) {
return
;
}
// make sure the sketch folder exists at all.
// if it does not, it will be re-saved, and no changes will be detected
sketch
.
ensureExistence
();
// check file count first
File
sketchFolder
=
sketch
.
getFolder
();
File
[]
sketchFiles
=
sketchFolder
.
listFiles
(
new
FilenameFilter
() {
@
Override
public
boolean
accept
(
File
dir
,
String
name
) {
for
(
String
s
:
editor
.
getMode
().
getExtensions
()) {
if
(
name
.
toLowerCase
().
endsWith
(
s
.
toLowerCase
())) {
return
true
;
}
}
return
false
;
}
});
int
fileCount
=
sketchFiles
.
length
;
if
(
fileCount
!=
sketch
.
getCodeCount
()) {
// if they chose to reload and there aren't any files left
if
(
reloadSketch
(
null
) &&
fileCount
<
1
) {
try
{
//make a blank file
sketch
.
getMainFile
().
createNewFile
();
}
catch
(
Exception
e1
) {
//if that didn't work, tell them it's un-recoverable
showErrorEDT
(
"Reload failed"
,
"The sketch contains no code files."
,
e1
);
//don't try to reload again after the double fail
//this editor is probably trashed by this point, but a save-as might be possible
skip
=
true
;
return
;
}
//it's okay to do this without confirmation, because they already confirmed to deleting the unsaved changes above
sketch
.
reload
();
showWarningEDT
(
"Modified Reload"
,
"You cannot delete the last code file in a sketch.
\n
"
+
"A new blank sketch file has been generated for you."
);
}
return
;
}
SketchCode
[]
codes
=
sketch
.
getCode
();
for
(
SketchCode
sc
:
codes
) {
File
sketchFile
=
sc
.
getFile
();
if
(
sketchFile
.
exists
()) {
long
diff
=
sketchFile
.
lastModified
() -
sc
.
lastModified
();
if
(
diff
!=
0
) {
if
(
Base
.
isMacOS
() &&
diff
==
1000L
) {
// Mac OS X has a one second difference. Not sure if it's a Java bug
// or something else about how OS X is writing files.
continue
;
}
System
.
out
.
println
(
sketchFile
.
getName
() +
" "
+
diff
);
reloadSketch
(
sc
);
return
;
}
}
else
{
// If a file in the sketch was not found, then it must have been
// deleted externally, so reload the sketch.
reloadSketch
(
sc
);
return
;
}
}
}
private
void
setSketchCodeModified
(
SketchCode
sc
) {
sc
.
setModified
(
true
);
sketch
.
setModified
(
true
);
}
/**
* @param changed The file that was known to be modified
* @return true if the files in the sketch have been reloaded
*/
private
boolean
reloadSketch
(
SketchCode
changed
) {
int
response
=
blockingYesNoPrompt
(
editor
,
"File Modified"
,
"Your sketch has been modified externally.<br>"
+
"Would you like to reload the sketch?"
,
"If you reload the sketch, any unsaved changes will be lost."
);
if
(
response
==
JOptionPane
.
YES_OPTION
) {
sketch
.
reload
();
rebuildHeaderEDT
();
return
true
;
}
// they said no (or canceled), make it possible to stop the msgs by saving
if
(
changed
!=
null
) {
//set it to be modified so that it will actually save to disk when the user saves from inside processing
setSketchCodeModified
(
changed
);
}
else
{
// Because the number of files changed, they may be working with a file
// that doesn't exist any more. So find the files that are missing,
// and mark them as modified so that the next "Save" will write them.
for
(
SketchCode
sc
:
sketch
.
getCode
()) {
if
(!
sc
.
getFile
().
exists
()) {
setSketchCodeModified
(
sc
);
}
}
// If files were simply added, then nothing needs done
}
rebuildHeaderEDT
();
skip
=
true
;
return
false
;
}
private
void
showErrorEDT
(
final
String
title
,
final
String
message
,
final
Exception
e
) {
EventQueue
.
invokeLater
(
new
Runnable
() {
@
Override
public
void
run
() {
Base
.
showError
(
title
,
message
,
e
);
}
});
}
private
void
showWarningEDT
(
final
String
title
,
final
String
message
) {
EventQueue
.
invokeLater
(
new
Runnable
() {
@
Override
public
void
run
() {
Base
.
showWarning
(
title
,
message
);
}
});
}
private
int
blockingYesNoPrompt
(
final
Frame
editor
,
final
String
title
,
final
String
message1
,
final
String
message2
) {
final
int
[]
result
= { -
1
};
// yuck
try
{
//have to wait for a response on this one
EventQueue
.
invokeAndWait
(
new
Runnable
() {
@
Override
public
void
run
() {
result
[
0
] =
Base
.
showYesNoQuestion
(
editor
,
title
,
message1
,
message2
);
}
});
}
catch
(
InvocationTargetException
e
) {
//occurs if Base.showYesNoQuestion throws an error, so, shouldn't happen
e
.
getTargetException
().
printStackTrace
();
}
catch
(
InterruptedException
e
) {
//occurs if the EDT is interrupted, so, shouldn't happen
e
.
printStackTrace
();
}
return
result
[
0
];
}
private
void
rebuildHeaderEDT
() {
EventQueue
.
invokeLater
(
new
Runnable
() {
@
Override
public
void
run
() {
editor
.
header
.
rebuild
();
}
});
}
}
Back
|
FazBrowse Home
|
New Git URL