Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PluginSettings to store additional settings #291

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -306,4 +316,5 @@ private void haltAllDevices() {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ public class PositionUpdater {

private SwingWorker<Void, Void> worker_;

private PositionPanel positionPanel_; // TODO: update multiple setup panels
private PositionPanel positionPanel_; // TODO: update multiple setup panels for diSPIM

private final NavigationPanel navPanel_;

private final AtomicBoolean isPolling_;

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<Void, Void>() {
@Override
protected Void doInBackground() {
Expand All @@ -40,44 +42,69 @@ 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_);
} catch (InterruptedException e) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class LightSheetManagerModel implements LightSheetManager {

private String errorText_;

private UserSettings settings_;
private UserSettings userSettings_;
private PluginSettings pluginSettings_;

private DeviceManager deviceManager_;

Expand All @@ -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
Expand Down Expand Up @@ -79,7 +81,7 @@ public boolean setup() {
}

// load settings
settings_.load();
userSettings_.load();

// if we made it here then everything loaded correctly
return true;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* This class is used to save and load settings using JSON strings.
*/
public class UserSettings {

Expand All @@ -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();
}

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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());
}
}

/**
Expand All @@ -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
Expand Down
Loading