The call has already checked to make sure this sketch is not modified, now change the mode. @return true if mode is changed.
(Mode mode)
| 989 | * @return true if mode is changed. |
| 990 | */ |
| 991 | public boolean changeMode(Mode mode) { |
| 992 | Mode oldMode = activeEditor.getMode(); |
| 993 | if (oldMode != mode) { |
| 994 | Sketch sketch = activeEditor.getSketch(); |
| 995 | nextMode = mode; |
| 996 | |
| 997 | if (sketch.isUntitled()) { |
| 998 | // The current sketch is empty, just close and start fresh. |
| 999 | // (Otherwise the editor would lose its 'untitled' status.) |
| 1000 | handleClose(activeEditor, true); |
| 1001 | handleNew(); |
| 1002 | |
| 1003 | } else { |
| 1004 | // If the current editor contains file extensions that the new mode can handle, then |
| 1005 | // write a sketch.properties file with the new mode specified, and reopen. |
| 1006 | boolean newModeCanHandleCurrentSource = true; |
| 1007 | for (final SketchCode code : sketch.getCode()) { |
| 1008 | if (!mode.validExtension(code.getExtension())) { |
| 1009 | newModeCanHandleCurrentSource = false; |
| 1010 | break; |
| 1011 | } |
| 1012 | } |
| 1013 | if (!newModeCanHandleCurrentSource) { |
| 1014 | return false; |
| 1015 | } else { |
| 1016 | final File props = new File(sketch.getCodeFolder(), "sketch.properties"); |
| 1017 | saveModeSettings(props, nextMode); |
| 1018 | handleClose(activeEditor, true); |
| 1019 | Editor editor = handleOpen(sketch.getMainFilePath()); |
| 1020 | if (editor == null) { |
| 1021 | // the Mode change failed (probably code that's out of date) |
| 1022 | // re-open the sketch using the mode we were in before |
| 1023 | saveModeSettings(props, oldMode); |
| 1024 | handleOpen(sketch.getMainFilePath()); |
| 1025 | return false; |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | return true; |
| 1031 | } |
| 1032 | |
| 1033 | |
| 1034 | private static class ModeInfo { |
no test coverage detected