diff --git a/src/main/java/org/micromanager/lightsheetmanager/LightSheetManagerPlugin.java b/src/main/java/org/micromanager/lightsheetmanager/LightSheetManagerPlugin.java index 3546219..a8a256f 100644 --- a/src/main/java/org/micromanager/lightsheetmanager/LightSheetManagerPlugin.java +++ b/src/main/java/org/micromanager/lightsheetmanager/LightSheetManagerPlugin.java @@ -14,7 +14,7 @@ public class LightSheetManagerPlugin implements MenuPlugin, SciJavaPlugin { public static final String copyright = "Applied Scientific Instrumentation (ASI), 2022-2024"; public static final String description = "A plugin to control various types of light sheet microscopes."; public static final String menuName = "Light Sheet Manager"; - public static final String version = "0.4.2"; + public static final String version = "0.4.3"; private Studio studio_; private LightSheetManagerFrame frame_; diff --git a/src/main/java/org/micromanager/lightsheetmanager/gui/LightSheetManagerFrame.java b/src/main/java/org/micromanager/lightsheetmanager/gui/LightSheetManagerFrame.java index d6a67ad..d001233 100644 --- a/src/main/java/org/micromanager/lightsheetmanager/gui/LightSheetManagerFrame.java +++ b/src/main/java/org/micromanager/lightsheetmanager/gui/LightSheetManagerFrame.java @@ -135,11 +135,13 @@ private void createUserInterface() { tabPanel_.getNavigationTab().getNavigationPanel() .getPositionUpdater().setPositionPanel(positionPanel); - tabPanel_.getNavigationTab().getNavigationPanel().startPolling(); + if (model_.pluginSettings().isPollingPositions()) { + tabPanel_.getNavigationTab().getNavigationPanel().startPolling(); + } WindowUtils.registerWindowClosingEvent(this, event -> { tabPanel_.getNavigationTab().getNavigationPanel().stopPolling(); - model_.getUserSettings().save(); + model_.userSettings().save(); studio_.logs().logMessage("user settings saved"); }); diff --git a/src/main/java/org/micromanager/lightsheetmanager/gui/frames/NavigationFrame.java b/src/main/java/org/micromanager/lightsheetmanager/gui/frames/NavigationFrame.java index f02ecbf..ed64837 100644 --- a/src/main/java/org/micromanager/lightsheetmanager/gui/frames/NavigationFrame.java +++ b/src/main/java/org/micromanager/lightsheetmanager/gui/frames/NavigationFrame.java @@ -18,11 +18,9 @@ public NavigationFrame(final LightSheetManagerModel model) { add(navigationPanel_, ""); } + // TODO: this would need ui and events public void init() { - navigationPanel_.createUserInterface(); + //navigationPanel_.createUserInterface(); } - public void stopTimer() { - navigationPanel_.stopPolling(); - } } diff --git a/src/main/java/org/micromanager/lightsheetmanager/gui/tabs/navigation/NavigationPanel.java b/src/main/java/org/micromanager/lightsheetmanager/gui/tabs/navigation/NavigationPanel.java index 491dcc4..7a35601 100644 --- a/src/main/java/org/micromanager/lightsheetmanager/gui/tabs/navigation/NavigationPanel.java +++ b/src/main/java/org/micromanager/lightsheetmanager/gui/tabs/navigation/NavigationPanel.java @@ -41,13 +41,16 @@ public NavigationPanel(final LightSheetManagerModel model) { devices_ = model_.devices(); controlPanels_ = new ArrayList<>(); + positionUpdater_ = new PositionUpdater(this); createUserInterface(); createEventHandlers(); - positionUpdater_ = new PositionUpdater(this); } - public void createUserInterface() { + /** + * Create the control panels for each device. + */ + private void createUserInterface() { Panel.setMigLayoutDefault( "", //""debug 1000", @@ -57,7 +60,9 @@ public void createUserInterface() { btnHaltDevices_ = new Button("HALT", 120, 30); btnRefreshPanel_ = new Button("Refresh", 120, 30); - cbxPollPositions_ = new CheckBox("Poll Positions", true); + + cbxPollPositions_ = new CheckBox("Poll Positions", + model_.pluginSettings().isPollingPositions()); final int numImagingPaths = devices_.getDeviceAdapter().getNumImagingPaths(); final int numIlluminationPaths = devices_.getDeviceAdapter().getNumIlluminationPaths(); @@ -228,22 +233,27 @@ public void createUserInterface() { private void createEventHandlers() { + // refresh devices and ui btnRefreshPanel_.registerListener(e -> { //System.out.println("refresh pressed"); removeAll(); createUserInterface(); createEventHandlers(); + revalidate(); + repaint(); if (cbxPollPositions_.isSelected()) { positionUpdater_.startPolling(); } - revalidate(); - repaint(); }); + // halt stage devices btnHaltDevices_.registerListener(e -> haltAllDevices()); + // position polling cbxPollPositions_.registerListener(e -> { - if (cbxPollPositions_.isSelected()) { + final boolean isSelected = cbxPollPositions_.isSelected(); + model_.pluginSettings().setPollingPositions(isSelected); + if (isSelected) { positionUpdater_.startPolling(); } else { positionUpdater_.stopPolling(); @@ -306,4 +316,5 @@ private void haltAllDevices() { } } } + } diff --git a/src/main/java/org/micromanager/lightsheetmanager/gui/tabs/navigation/PositionUpdater.java b/src/main/java/org/micromanager/lightsheetmanager/gui/tabs/navigation/PositionUpdater.java index 575d558..0f2d465 100644 --- a/src/main/java/org/micromanager/lightsheetmanager/gui/tabs/navigation/PositionUpdater.java +++ b/src/main/java/org/micromanager/lightsheetmanager/gui/tabs/navigation/PositionUpdater.java @@ -17,7 +17,7 @@ public class PositionUpdater { private SwingWorker worker_; - private PositionPanel positionPanel_; // TODO: update multiple setup panels + private PositionPanel positionPanel_; // TODO: update multiple setup panels for diSPIM private final NavigationPanel navPanel_; @@ -25,12 +25,14 @@ public class PositionUpdater { public PositionUpdater(final NavigationPanel navPanel) { navPanel_ = Objects.requireNonNull(navPanel); - isPolling_ = new AtomicBoolean(true); + isPolling_ = new AtomicBoolean(false); pollingDelayMs_ = 500; } + /** + * Create the polling thread without starting it. + */ private void createPollingTask() { - isPolling_.set(true); worker_ = new SwingWorker() { @Override protected Void doInBackground() { @@ -40,11 +42,7 @@ protected Void doInBackground() { navPanel_.updatePositions(); positionPanel_.updatePositions(); } catch (Exception e) { - e.printStackTrace(); - } - if (!isPolling_.get()) { - //System.out.println("break!"); - break; + //e.printStackTrace(); } try { Thread.sleep(pollingDelayMs_); @@ -52,32 +50,61 @@ protected Void doInBackground() { throw new RuntimeException(e); } } + //System.out.println("done!"); return null; } }; } + /** + * Create the polling task and start the thread. + */ public void startPolling() { + isPolling_.set(true); createPollingTask(); worker_.execute(); } + /** + * Stops the position polling thread. + */ public void stopPolling() { isPolling_.set(false); } + /** + * Return true if polling positions. + * + * @return true if polling positions + */ public boolean isPolling() { return isPolling_.get(); } + /** + * Set the polling delay in milliseconds. + * + * @param delayMs delay in milliseconds + */ public void setPollingDelayMs(final int delayMs) { pollingDelayMs_ = delayMs; } + /** + * Return the polling delay in milliseconds. + * + * @return the polling delay in milliseconds + */ public int getPollingDelayMs() { return pollingDelayMs_; } + // TODO: better way to connect this? + /** + * Set the position panel to update in addition to the {@code NavigationPanel}. + * + * @param panel the reference to the panel + */ public void setPositionPanel(final PositionPanel panel) { positionPanel_ = panel; } diff --git a/src/main/java/org/micromanager/lightsheetmanager/model/LightSheetManagerModel.java b/src/main/java/org/micromanager/lightsheetmanager/model/LightSheetManagerModel.java index 10bfb95..1a73bcb 100644 --- a/src/main/java/org/micromanager/lightsheetmanager/model/LightSheetManagerModel.java +++ b/src/main/java/org/micromanager/lightsheetmanager/model/LightSheetManagerModel.java @@ -24,7 +24,8 @@ public class LightSheetManagerModel implements LightSheetManager { private String errorText_; - private UserSettings settings_; + private UserSettings userSettings_; + private PluginSettings pluginSettings_; private DeviceManager deviceManager_; @@ -37,7 +38,8 @@ public LightSheetManagerModel(final Studio studio) { studio_ = Objects.requireNonNull(studio); core_ = studio_.core(); - settings_ = new UserSettings(this); + pluginSettings_ = new PluginSettings(); + userSettings_ = new UserSettings(this); xyzGrid_ = new XYZGrid(this); // set during setup if there is an error @@ -79,7 +81,7 @@ public boolean setup() { } // load settings - settings_.load(); + userSettings_.load(); // if we made it here then everything loaded correctly return true; @@ -114,8 +116,16 @@ public DeviceManager devices() { return deviceManager_; } - public UserSettings getUserSettings() { - return settings_; + public UserSettings userSettings() { + return userSettings_; + } + + public PluginSettings pluginSettings() { + return pluginSettings_; + } + + public void pluginSettings(final PluginSettings pluginSettings) { + pluginSettings_ = Objects.requireNonNull(pluginSettings); } public CMMCore getCore() { diff --git a/src/main/java/org/micromanager/lightsheetmanager/model/UserSettings.java b/src/main/java/org/micromanager/lightsheetmanager/model/UserSettings.java index 67b8c4a..435fa9a 100644 --- a/src/main/java/org/micromanager/lightsheetmanager/model/UserSettings.java +++ b/src/main/java/org/micromanager/lightsheetmanager/model/UserSettings.java @@ -12,7 +12,9 @@ import java.util.Optional; /** - * This wraps Micro-Manager's settings profiles to store the plugin settings. + * Micro-Manager user settings for the current user. + *

+ * This class is used to save and load settings using JSON strings. */ public class UserSettings { @@ -34,9 +36,9 @@ public class UserSettings { public UserSettings(final LightSheetManagerModel model) { model_ = Objects.requireNonNull(model); // setup user profile - UserProfile profile = model_.getStudio().getUserProfile(); - userName_ = profile.getProfileName(); + final UserProfile profile = model_.getStudio().getUserProfile(); settings_ = profile.getSettings(UserSettings.class); + userName_ = profile.getProfileName(); } /** @@ -51,14 +53,14 @@ public MutablePropertyMapView get() { /** * Returns the name of the user profile. * - * @return a String containing the name + * @return a {@code String} containing the name */ public String getUserName() { return userName_; } /** - * Clears all user settings associated with this class name. + * Clears all user settings associated with this profile name. */ public void clear() { settings_.clear(); @@ -92,6 +94,16 @@ public void load() { + model_.acquisitions().settings().toPrettyJson()); } } + + // load plugin settings or default plugin settings + final String jsonStr = settings_.getString(SETTINGS_KEY, SETTINGS_NOT_FOUND); + if (jsonStr.equals(SETTINGS_NOT_FOUND)) { + model_.studio().logs().logDebugMessage("settings not found, using default plugin settings."); + } else { + model_.pluginSettings(PluginSettings.fromJson(jsonStr)); + model_.studio().logs().logDebugMessage("loaded PluginSettings from " + SETTINGS_KEY + ": " + + model_.pluginSettings().toPrettyJson()); + } } /** @@ -100,17 +112,25 @@ public void load() { public void save() { // make settings current before saving model_.acquisitions().updateAcquisitionSettings(); - // settings key + + // settings key based on geometry type final GeometryType geometryType = model_.devices() .getDeviceAdapter().getMicroscopeGeometry(); final String key = SETTINGS_PREFIX_KEY + geometryType.toString().toUpperCase(); - // save in user settings + + // save acquisition settings settings_.putString(key, model_.acquisitions().settings().toJson()); model_.studio().logs().logDebugMessage("saved JSON to " + key + ": " + model_.acquisitions().settings().toPrettyJson()); + + // save plugin settings + settings_.putString(SETTINGS_KEY, model_.pluginSettings().toJson()); + model_.studio().logs().logDebugMessage("saved PluginSettings to " + SETTINGS_KEY + ": " + + model_.pluginSettings().toPrettyJson()); } + // TODO: this can add new keys but doesn't delete renamed keys /** * Returns the {@code JSONObject} after checking if it matches the schema of the * default acquisition settings object. If it does not, then any new settings