package ie.dcu.apps.ist.actions; import ie.dcu.apps.ist.*; import ie.dcu.segment.Segmenter; import ie.dcu.util.OsUtils; import java.io.IOException; import java.util.*; import java.util.logging.Logger; /** * Manages application actions. * * @author Kevin McGuinness */ public class ActionManager { private static final Logger log = Logger.getLogger("ActionManager"); private final AppWindow window; private final Map actions; private ArrayList segmenterActions; private Properties props; public ActionManager(AppWindow window) { this.window = window; this.actions = new HashMap(); try { this.props = Application.loadProperties(getPropertiesFile()); } catch (IOException e) { log.severe("Error loading action properties: " + e.getLocalizedMessage()); throw new RuntimeException(e); } init(); } private String getPropertiesFile() { return OsUtils.isMacOS() ? "actions.mac" : "actions"; } Properties getProperties() { return props; } AppWindow getWindow() { return window; } public void init() { add(new OpenAction(this)); add(new SaveAction(this)); add(new SaveAsAction(this)); add(new AboutAction(this)); add(new CopyAction(this)); add(new CloseAction(this)); add(new ExitAction(this)); add(new ExportViewAction(this)); add(new ExportImageMapAction(this)); add(new ExportTransparentPNGAction(this)); add(new HelpAction(this)); add(new PreferencesAction(this)); add(new PrintAction(this)); add(new RedoAction(this)); add(new UndoAction(this)); add(new NextAction(this)); add(new PreviousAction(this)); add(new OpenExperimentAction(this)); add(new ConfigureSegmenterAction(this)); // Add select segmenter actions SegmenterRegistry registry = SegmenterRegistry.getInstance(); for (Segmenter s : registry) { add(new SelectSegmenterAction(this, registry, s)); } } public void add(AppAction action) { actions.put(action.id(), action); } public T get(Class clazz) { return clazz.cast(actions.get(id(clazz))); } public T get(Class clazz, String id) { return clazz.cast(actions.get(id)); } public void setEnabled(String id, boolean enabled) { AppAction action = actions.get(id); if (action != null) { action.setEnabled(enabled); } } public void setEnabled(Class clazz, boolean enabled) { AppAction action = get(clazz); if (action != null) { action.setEnabled(enabled); } } public Collection getSegmentationActions() { if (segmenterActions == null) { segmenterActions = new ArrayList(); for (AppAction a : actions.values()) { if (a instanceof SelectSegmenterAction) { segmenterActions.add((SelectSegmenterAction) a); } } } return Collections.unmodifiableCollection(segmenterActions); } private String id(Class clazz) { return clazz.getName(); } }