Base controller to work off of/invoke commands on. @author Matt
| 28 | * @author Matt |
| 29 | */ |
| 30 | public abstract class Controller implements Runnable { |
| 31 | private final Map<Class<?>, Supplier<Callable<?>>> actions = new HashMap<>(); |
| 32 | private final ConfigManager configs = new ConfigManager(Recaf.getDirectory("config")); |
| 33 | private Workspace workspace; |
| 34 | protected Path initialWorkspace; |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * @param workspace |
| 39 | * Initial workspace path. Can point to a file to load <i>(class, jar)</i> or a workspace |
| 40 | * configuration <i>(json)</i>. |
| 41 | */ |
| 42 | public Controller(Path workspace) { |
| 43 | this.initialWorkspace = workspace; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param workspace Workspace to set. |
| 48 | */ |
| 49 | public void setWorkspace(Workspace workspace) { |
| 50 | Collection<WorkspacePlugin> plugins = PluginsManager.getInstance().ofType(WorkspacePlugin.class); |
| 51 | Workspace old = this.workspace; |
| 52 | if (old != null) { |
| 53 | plugins.forEach(plugin -> plugin.onClosed(old)); |
| 54 | } |
| 55 | this.workspace = workspace; |
| 56 | Recaf.setCurrentWorkspace(workspace); |
| 57 | plugins.forEach(plugin -> plugin.onOpened(workspace)); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @return Current workspace. |
| 62 | */ |
| 63 | public final Workspace getWorkspace() { |
| 64 | return workspace; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @return Config manager. |
| 69 | */ |
| 70 | public ConfigManager config() { |
| 71 | return configs; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void run() { |
| 76 | // Load workspace |
| 77 | try { |
| 78 | loadInitialWorkspace(); |
| 79 | } catch(Exception ex) { |
| 80 | error(ex, "Error loading workspace from file: " + initialWorkspace); |
| 81 | } |
| 82 | // Call startup plugins |
| 83 | PluginsManager.getInstance().ofType(StartupPlugin.class).forEach(plugin -> plugin.onStart(this)); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Try to load the passed initial workspace |
nothing calls this directly
no test coverage detected