forked from facchinm/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixEncoding.java
More file actions
98 lines (75 loc) · 3.02 KB
/
Copy pathFixEncoding.java
File metadata and controls
98 lines (75 loc) · 3.02 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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2008 Ben Fry and Casey Reas
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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.tools;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JOptionPane;
import processing.app.*;
import static processing.app.I18n._;
public class FixEncoding implements Tool {
Editor editor;
public String getMenuTitle() {
return _("Fix Encoding & Reload");
}
public void init(Editor editor) {
this.editor = editor;
}
public void run() {
Sketch sketch = editor.getSketch();
//SketchCode code = sketch.current;
if (sketch.isModified()) {
int result =
JOptionPane.showConfirmDialog(editor,
_("Discard all changes and reload sketch?"),
_("Fix Encoding & Reload"),
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.NO_OPTION) {
return;
}
}
try {
for (int i = 0; i < sketch.getCodeCount(); i++) {
SketchCode code = sketch.getCode(i);
code.setProgram(loadWithLocalEncoding(code.getFile()));
code.setModified(true); // yes, because we want them to save this
}
// Update the currently visible program with its code
editor.setText(sketch.getCurrentCode().getProgram());
} catch (IOException e) {
String msg =
_("An error occurred while trying to fix the file encoding.\nDo not attempt to save this sketch as it may overwrite\nthe old version. Use Open to re-open the sketch and try again.\n") +
e.getMessage();
Base.showWarning(_("Fix Encoding & Reload"), msg, e);
}
}
protected String loadWithLocalEncoding(File file) throws IOException {
// FileReader uses the default encoding, which is what we want.
FileReader fr = new FileReader(file);
BufferedReader reader = new BufferedReader(fr);
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
buffer.append('\n');
}
reader.close();
return buffer.toString();
}
}