
de.tsl2.nano.repeat.impl.MacroRecorder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tsl2.nano.repeatable Show documentation
Show all versions of tsl2.nano.repeatable Show documentation
TSL2 Framework Repeatable (generic Undo/Redo and Macro mechanism)
The newest version!
package de.tsl2.nano.repeat.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;
import de.tsl2.nano.bean.BeanUtil;
import de.tsl2.nano.repeat.ICommand;
import de.tsl2.nano.repeat.IMacroManager;
/**
* Simple implementation of {@link IMacroManager}
*
* @param
* @author Tom, Thomas Schneider
* @version $Revision$
*/
public class MacroRecorder implements IMacroManager {
Map>> macros;
String recordingID;
public MacroRecorder() {
this(Integer.MAX_VALUE);
}
/**
* constructor
*/
public MacroRecorder(int capacity) {
super();
macros = new HashMap>>();
}
@Override
public void record(String id, ICommand... m) {
if (recordingID == null) {
if (id == null) {
throw new IllegalStateException(
"please call 'record' with a filled 'id' to start the recording process!");
}
recordingID = id;
}
macro().addAll(copy(m));
}
private Collection extends ICommand>> copy(ICommand[] m) {
List> copy = new ArrayList>();
for (int i = 0; i < m.length; i++) {
copy.add(BeanUtil.copy(m[i]));
}
return copy;
}
private Deque> macro() {
return macro(recordingID);
}
/**
* macro
*
* @return the current macro (the last record...if not stopped!)
*/
private Deque> macro(String recordingID) {
Deque> m = macros.get(recordingID);
if (m == null) {
m = new LinkedBlockingDeque>();
macros.put(recordingID, m);
}
return m;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isRecording() {
return recordingID != null;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public int play(String id, CONTEXT context) {
if (isRecording()) {
throw new IllegalStateException("please stop macro recording first!");
}
Deque> m = macro(id);
int size = m.size();
for (ICommand cmd : m) {
cmd.setContext(context);
cmd.run();
}
return size;
}
/**
* {@inheritDoc}
*/
@Override
public int stop() {
int size = macro().size();
recordingID = null;
return size;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy