| 35 | |
| 36 | |
| 37 | public class FixEncoding implements Tool { |
| 38 | Editor editor; |
| 39 | |
| 40 | |
| 41 | public String getMenuTitle() { |
| 42 | return tr("Fix Encoding & Reload"); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | public void init(Editor editor) { |
| 47 | this.editor = editor; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | public void run() { |
| 52 | Sketch sketch = editor.getSketch(); |
| 53 | //SketchCode code = sketch.current; |
| 54 | |
| 55 | if (sketch.isModified()) { |
| 56 | int result = |
| 57 | JOptionPane.showConfirmDialog(editor, |
| 58 | tr("Discard all changes and reload sketch?"), |
| 59 | tr("Fix Encoding & Reload"), |
| 60 | JOptionPane.YES_NO_OPTION, |
| 61 | JOptionPane.QUESTION_MESSAGE); |
| 62 | |
| 63 | if (result == JOptionPane.NO_OPTION) { |
| 64 | return; |
| 65 | } |
| 66 | } |
| 67 | try { |
| 68 | for (int i = 0; i < sketch.getCodeCount(); i++) { |
| 69 | SketchFile file = sketch.getFile(i); |
| 70 | editor.findTab(file).setText(loadWithLocalEncoding(file.getFile())); |
| 71 | } |
| 72 | } catch (IOException e) { |
| 73 | String msg = |
| 74 | tr("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") + |
| 75 | e.getMessage(); |
| 76 | Base.showWarning(tr("Fix Encoding & Reload"), msg, e); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 81 | protected String loadWithLocalEncoding(File file) throws IOException { |
| 82 | // FileReader uses the default encoding, which is what we want. |
| 83 | BufferedReader reader = null; |
| 84 | try { |
| 85 | reader = new BufferedReader(new FileReader(file)); |
| 86 | |
| 87 | StringBuffer buffer = new StringBuffer(); |
| 88 | String line; |
| 89 | while ((line = reader.readLine()) != null) { |
| 90 | buffer.append(line); |
| 91 | buffer.append('\n'); |
| 92 | } |
| 93 | return buffer.toString(); |
| 94 | } finally { |
nothing calls this directly
no outgoing calls
no test coverage detected