Skip to content
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
13 changes: 8 additions & 5 deletions src/main/java/org/scijava/io/DefaultRecentFileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
import org.scijava.module.ModuleService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.prefs.PrefService;
import org.scijava.service.AbstractService;
import org.scijava.service.Service;
import org.scijava.util.FileUtils;
import org.scijava.util.Prefs;

// TODO - DefaultRecentFileService, DefaultWindowService, and DefaultLUTService
// all build menus dynamically (see createInfo()). We may be able to abstract a
Expand Down Expand Up @@ -98,6 +98,9 @@ public final class DefaultRecentFileService extends AbstractService implements
@Parameter
private CommandService commandService;

@Parameter
private PrefService prefService;

private List<String> recentFiles;
private Map<String, ModuleInfo> recentModules;

Expand All @@ -112,7 +115,7 @@ public void add(final String path) {
recentFiles.add(path);

// persist the updated list
Prefs.putList(recentFiles, RECENT_FILES_KEY);
prefService.putList(recentFiles, RECENT_FILES_KEY);

if (present) {
// path already present; update linked module info
Expand All @@ -136,7 +139,7 @@ public boolean remove(final String path) {
final boolean success = recentFiles.remove(path);

// persist the updated list
Prefs.putList(recentFiles, RECENT_FILES_KEY);
prefService.putList(recentFiles, RECENT_FILES_KEY);

// remove linked module info
final ModuleInfo info = recentModules.remove(path);
Expand All @@ -148,7 +151,7 @@ public boolean remove(final String path) {
@Override
public void clear() {
recentFiles.clear();
Prefs.clear(RECENT_FILES_KEY);
prefService.clear(RECENT_FILES_KEY);

// unregister the modules with the module service
moduleService.removeModules(recentModules.values());
Expand All @@ -165,7 +168,7 @@ public List<String> getRecentFiles() {

@Override
public void initialize() {
recentFiles = Prefs.getList(RECENT_FILES_KEY);
recentFiles = prefService.getList(RECENT_FILES_KEY);
recentModules = new HashMap<String, ModuleInfo>();
for (final String path : recentFiles) {
recentModules.put(path, createInfo(path));
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/scijava/module/AbstractModuleItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public String getPersistKey() {
* value for null.
*/
@Override
@Deprecated
public T loadValue() {
// if there is nothing to load from persistence return nothing
if (!isPersisted()) return null;
Expand All @@ -157,6 +158,7 @@ public T loadValue() {
}

@Override
@Deprecated
public void saveValue(final T value) {
if (!isPersisted()) return;

Expand Down
53 changes: 52 additions & 1 deletion src/main/java/org/scijava/module/DefaultModuleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.plugin.PluginService;
import org.scijava.prefs.PrefService;
import org.scijava.service.AbstractService;
import org.scijava.service.Service;
import org.scijava.thread.ThreadService;
Expand Down Expand Up @@ -87,6 +88,9 @@ public class DefaultModuleService extends AbstractService implements
@Parameter
private ThreadService threadService;

@Parameter
private PrefService prefService;

/** Index of registered modules. */
private ModuleIndex moduleIndex;

Expand Down Expand Up @@ -255,6 +259,54 @@ public <T> ModuleItem<T> getSingleOutput(final Module module,
return getSingleItem(module, type, module.getInfo().outputs());
}

@Override
public <T> void save(final ModuleItem<T> item, final T value) {
if (!item.isPersisted()) return;

final String sValue = value == null ? "" : value.toString();

// do not persist if object cannot be converted back from a string
if (!ConversionUtils.canConvert(sValue, item.getType())) return;

final String persistKey = item.getPersistKey();
if (persistKey == null || persistKey.isEmpty()) {
// Attempt to use prefService
if (AbstractModuleItem.class.isAssignableFrom(item.getClass())) {
final Class<?> prefClass = ((AbstractModuleItem<T>)item).getDelegateClass();
final String prefKey = item.getName();
prefService.put(prefClass, prefKey, sValue);
}
// Have to use ModuleItem#saveValue
else item.saveValue(value);
}
else prefService.put(persistKey, sValue);
}

@Override
public <T> T load(final ModuleItem<T> item) {
// if there is nothing to load from persistence return nothing
if (!item.isPersisted()) return null;

final String sValue;
final String persistKey = item.getPersistKey();
if (persistKey == null || persistKey.isEmpty()) {
// Attempt to use prefService
if (AbstractModuleItem.class.isAssignableFrom(item.getClass())) {
final Class<?> prefClass = ((AbstractModuleItem<T>)item).getDelegateClass();
final String prefKey = item.getName();
sValue = prefService.get(prefClass, prefKey);
}
// Have to use ModuleItem#loadValue
else return item.loadValue();
}
else sValue = prefService.get(persistKey);

// if persisted value has never been set before return null
if (sValue == null) return null;

return ConversionUtils.convert(sValue, item.getType());
}

// -- Service methods --

@Override
Expand Down Expand Up @@ -383,5 +435,4 @@ private <T> ModuleItem<T> getSingleItem(final Module module,
}
return result;
}

}
8 changes: 8 additions & 0 deletions src/main/java/org/scijava/module/ModuleItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,21 @@ public interface ModuleItem<T> extends BasicDetails {
* Note that this is different than obtaining a module instance's current
* value for the input; see {@link #getValue(Module)} for that.
* </p>
*
* @deprecated
* @see ModuleService#load(ModuleItem)
*/
@Deprecated
T loadValue();

/**
* Saves the given value to persistent storage. This allows later restoration
* of the value via {@link #loadValue()}, even from a different JVM.
*
* @deprecated
* @see ModuleService#save(ModuleItem, Object)
*/
@Deprecated
void saveValue(T value);

/** Gets the function that is called to initialize the item's value. */
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/scijava/module/ModuleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.scijava.module.process.ModulePreprocessor;
import org.scijava.module.process.PostprocessorPlugin;
import org.scijava.module.process.PreprocessorPlugin;
import org.scijava.prefs.PrefService;
import org.scijava.service.SciJavaService;

/**
Expand Down Expand Up @@ -273,4 +274,15 @@ <M extends Module> Future<M> run(M module,
*/
<T> ModuleItem<T> getSingleOutput(Module module, Class<T> type);

/**
* Registers the given value for the given {@link ModuleItem} using the
* {@link PrefService}.
*/
<T> void save(ModuleItem<T> item, T value);

/**
* Returns the value, if any, stored in the {@link PrefService} for the given
* {@link ModuleItem}.
*/
<T> T load(ModuleItem<T> item);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

import org.scijava.module.Module;
import org.scijava.module.ModuleItem;
import org.scijava.module.ModuleService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.util.ConversionUtils;
import org.scijava.widget.InputHarvester;
Expand All @@ -52,6 +54,9 @@
priority = InputHarvester.PRIORITY + 1)
public class LoadInputsPreprocessor extends AbstractPreprocessorPlugin {

@Parameter
private ModuleService moduleService;

// -- ModuleProcessor methods --

@Override
Expand All @@ -69,9 +74,9 @@ private <T> void loadValue(final Module module, final ModuleItem<T> item) {
// skip input that has already been resolved
if (module.isResolved(item.getName())) return;

final T prefValue = moduleService.load(item);
final Class<T> type = item.getType();
final T defaultValue = item.getValue(module);
final T prefValue = item.loadValue();
final T value = getBestValue(prefValue, defaultValue, type);
item.setValue(module, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.scijava.Priority;
import org.scijava.module.Module;
import org.scijava.module.ModuleItem;
import org.scijava.module.ModuleService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

/**
Expand All @@ -51,6 +53,9 @@
priority = Priority.VERY_LOW_PRIORITY - 1)
public class SaveInputsPreprocessor extends AbstractPreprocessorPlugin {

@Parameter
private ModuleService moduleService;

// -- ModuleProcessor methods --

@Override
Expand All @@ -66,7 +71,7 @@ public void process(final Module module) {
/** Saves the value of the given module item to persistent storage. */
private <T> void saveValue(final Module module, final ModuleItem<T> item) {
final T value = item.getValue(module);
item.saveValue(value);
moduleService.save(item, value);
}

}
15 changes: 11 additions & 4 deletions src/main/java/org/scijava/options/OptionsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
import org.scijava.command.DynamicCommand;
import org.scijava.event.EventService;
import org.scijava.module.ModuleItem;
import org.scijava.module.ModuleService;
import org.scijava.options.event.OptionsEvent;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.SingletonPlugin;
import org.scijava.util.Prefs;
import org.scijava.prefs.PrefService;

// TODO - outline for how to address issues with options (initializing, aggregating into 1 dialog)

Expand Down Expand Up @@ -77,6 +78,12 @@ public class OptionsPlugin extends DynamicCommand implements SingletonPlugin {
@Parameter
protected EventService eventService;

@Parameter
private PrefService prefService;

@Parameter
private ModuleService moduleService;

// -- OptionsPlugin methods --

/** Loads option values from persistent storage. */
Expand All @@ -95,7 +102,7 @@ public void save() {

/** Clears option values from persistent storage. */
public void reset() {
Prefs.clear(getClass());
prefService.clear(getClass());
}

// -- Runnable methods --
Expand All @@ -116,13 +123,13 @@ public void run() {
// -- Helper methods --

private <T> void loadInput(final ModuleItem<T> input) {
final T value = input.loadValue();
final T value = moduleService.load(input);
if (value != null) input.setValue(this, value);
}

private <T> void saveInput(final ModuleItem<T> input) {
final T value = input.getValue(this);
input.saveValue(value);
moduleService.save(input, value);
}

}
53 changes: 53 additions & 0 deletions src/main/java/org/scijava/prefs/AbstractPrefService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2014 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package org.scijava.prefs;

import org.scijava.service.AbstractService;
import org.scijava.util.Prefs;

/**
* Abstract {@link PrefService} implementation. Calls
* {@link Prefs#setDelegateService(PrefService, double)} on this {@code Service}
* during initialization.
*
* @author Mark Hiner
*/
@SuppressWarnings({ "javadoc", "deprecation" })
public abstract class AbstractPrefService extends AbstractService implements
PrefService
{

@Override
public void initialize() {
Prefs.setDelegateService(this, getPriority());
}
}
Loading