sketch load/save all utf-8. add item to tools menu for reload using local

encoding
This commit is contained in:
benfry
2008-06-11 03:43:31 +00:00
parent 8853294c3f
commit c76c7c7bc4
4 changed files with 83 additions and 38 deletions
+6 -6
View File
@@ -1432,7 +1432,8 @@ public class Base {
// empty code file.. no worries, might be getting filled up later
if (file.length() == 0) return "";
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader reader = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
@@ -1449,15 +1450,14 @@ public class Base {
/**
* Spew the contents of a String object out to a file.
*/
static public void saveFile(String str,
File file) throws IOException {
static public void saveFile(String str, File file) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
InputStreamReader isr = new InputStreamReader(bis);
BufferedReader reader = new BufferedReader(isr);
FileWriter fw = new FileWriter(file);
PrintWriter writer = new PrintWriter(new BufferedWriter(fw));
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
PrintWriter writer = new PrintWriter(osw);
String line = null;
while ((line = reader.readLine()) != null) {
+47
View File
@@ -683,7 +683,54 @@ public class Editor extends JFrame {
}
});
menu.add(item);
item = new JMenuItem("Fix encoding and reload file");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SketchCode code = sketch.current;
if (code.modified) {
int result =
JOptionPane.showConfirmDialog(Editor.this,
"Discard changes and reload?",
"Reload",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.NO_OPTION) {
return;
}
}
File file = code.file;
// empty code file.. no worries, might be getting filled up later
if (file.length() != 0) {
try {
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();
beginCompoundEdit();
textarea.setText(buffer.toString());
endCompoundEdit();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
});
menu.add(item);
/*
item = new JMenuItem("Export Folder...");
item.addActionListener(new ActionListener() {
+1 -2
View File
@@ -61,7 +61,6 @@ public class SketchCode {
public int scrollPosition;
public boolean modified;
//SketchHistory history; // TODO add history information
public String preprocName; // name of .java file after preproc
public int preprocOffset; // where this code starts relative to the concat'd code
@@ -75,7 +74,7 @@ public class SketchCode {
try {
load();
} catch (IOException e) {
System.err.println("error while loading code " + name);
System.err.println("Error while loading code " + name);
}
}