我正在研究Eclipse RPC项目。我需要保存并恢复所有视图的所有内容。我没有在我的项目中使用EMF,因此我无法使用所有EMF API。
现在我可以使用IMemento
。 IViewPart
有一个saveState
方法,IMemento
作为参数。
我正在调用saveState()
并尝试将其置于视野中。
示例代码:
@Override
public void saveState(IMemento memento) {
ColorBarModel model = _multiColorBarContainer.getColorBar().getColorBarUIModel().getModel();
boolean isCutOff = _multiColorBarContainer.getColorBar().getColorBarUIModel().isCutOffEnabled();
String colorMapName = model.getColorMap().getColorMapName();
memento.putString("colorMapName", model.toString());
memento.createChild("colorMapName1", model.toString());
String selectionsMomento = memento.getString("colorMapName");
IStructuredSelection ss = new StructuredSelection(memento.getChild("colorMapName1"));
}
我需要保存作为类引用的模型(ColorBarModel.java
是我视图的模型),这样我就可以保存模型状态并在调用时恢复它。
但IMemento
仅提供putString()
,createChild()
方法
有人可以指导我如何通过IMemento
保存班级对象并在需要时将其恢复?
我可以保存该类的引用ID,但我不知道如何通过referenceId
以字符串形式获取该类的属性和行为。
有可能以某种方式获取吗?
答案 0 :(得分:0)
如果要保存大量数据,则应将其置于插件状态位置。你得到这个:
IPath path = Platform.getStateLocation(bundle);
bundle
是您插件的Bundle
类。您可以从传递给插件激活器的BundleContext
或使用
Bundle bundle = Platform.getBundle("plugin id");
状态位置位于工作区.metadata/.plugins
目录中。只需使用普通的Java文件I / O操作即可在此位置读取和写入数据。
答案 1 :(得分:0)
如果你很懒,可以使用Googles GSON将模型转换为JSON并将其存储为String。 然后,您可以再次使用GSON从JSON字符串加载已定义的类。
答案 2 :(得分:0)
IMemento
提供了一个界面,使用层次结构将您的视图状态存储在XML中。你可以:
String
并将其放入纪念品答案 3 :(得分:0)
我创建了类SimpleModelSerializer
,以便能够轻松地在不同格式之间编写和读取EMF模型。使用IMemento
和readMemento
方法处理writeMemento
个问题。该课程可以很容易地扩展到其他格式。
非常最小,只能处理简单模型。以下是一些限制:
尽管如此,它对于保存视图配置等小的简单事物非常有用。
这是代码:
/**
* Contains code for reading and writing EMF models to and from other formats.
*/
public class SimpleModelSerializer {
public interface ModelReader {
List<String> getMultiAttribute(String name);
String getAttribute(String name);
String getDefaultAttribute(String name);
List<ModelReader> getChildren(String name);
}
public interface ModelWriter {
void setAttribute(String name, String value);
void setDefaultAttribute(String name, String value);
void setMultiAttribute(String name, List<String> values);
ModelWriter createChild(String name);
}
@SuppressWarnings("unchecked")
private static void setFeature(EObject o, EStructuralFeature feature, Object value) {
if (feature.isMany()) {
((List<Object>) o.eGet(feature)).add(value);
} else {
o.eSet(feature, value);
}
}
@SuppressWarnings("unchecked")
private static List<?> getFeature(EObject o, EStructuralFeature feature) {
if (feature.isMany()) {
return (List<Object>) o.eGet(feature);
} else {
return Collections.singletonList(o.eGet(feature));
}
}
public static <T extends EObject> T readMemento(IMemento mem, EClass cls, Class<T> instCls, ExtendedMetaData meta) {
return readCastedObject(cls, instCls, new MementoReader(mem), meta);
}
public static <T extends EObject> List<T> readMementos(IMemento mem, String name, EClass cls, Class<T> instCls, ExtendedMetaData meta) {
return readObjects(cls, instCls, new MementoReader(mem), name, meta);
}
public static <T extends EObject> List<T> readObjects(EClass cls, Class<T> instCls, ModelReader reader, String name, ExtendedMetaData meta) {
List<T> result = new ArrayList<>();
for (ModelReader childReader : reader.getChildren(name)) {
result.add(instCls.cast(readObject(cls, childReader, meta)));
}
return result;
}
public static <T extends EObject> T readCastedObject(EClass cls, Class<T> instCls, ModelReader reader, ExtendedMetaData meta) {
return instCls.cast(readObject(cls, reader, meta));
}
public static EObject readObject(EClass cls, ModelReader reader, ExtendedMetaData meta) {
if (meta == null) meta = ExtendedMetaData.INSTANCE;
EObject obj = EcoreUtil.create(cls);
for (EStructuralFeature feature : cls.getEAllStructuralFeatures()) {
if (feature.isTransient()) continue;
String name = meta.getName(feature);
List<Object> values = new ArrayList<>();
if (feature instanceof EAttribute) {
EAttribute attr = (EAttribute) feature;
List<String> attrs;
if (attr.isMany()) attrs = reader.getMultiAttribute(name);
else if (name.equals(":0")) attrs = singletonList(reader.getDefaultAttribute(name));
else attrs = singletonList(reader.getAttribute(name));
for (String attrText : attrs) {
if (attrText != null) {
values.add(EcoreUtil.createFromString(attr.getEAttributeType(), attrText));
}
}
} else if (feature instanceof EReference) {
EReference ref = (EReference) feature;
for (ModelReader childReader : reader.getChildren(name)) {
values.add(readObject(ref.getEReferenceType(), childReader, meta));
}
}
for (Object value : values) {
setFeature(obj, feature, value);
}
}
return obj;
}
public static void writeMemento(IMemento mem, EObject o, ExtendedMetaData meta) {
writeObject(new MementoWriter(mem), o, meta);
}
public static void writeMementos(IMemento mem, String childName, Collection<? extends EObject> os, ExtendedMetaData meta) {
MementoWriter writer = new MementoWriter(mem);
for (EObject o : os) {
writeObject(writer.createChild(childName), o, meta);
}
}
public static void writeObject(ModelWriter writer, EObject obj, ExtendedMetaData meta) {
if (meta == null) meta = ExtendedMetaData.INSTANCE;
EClass cls = obj.eClass();
for (EStructuralFeature feature : cls.getEAllStructuralFeatures()) {
if (feature.isTransient()) continue;
String name = meta.getName(feature);
if (feature instanceof EAttribute) {
List<String> outputValues = new ArrayList<>();
for (Object e : getFeature(obj, feature)) {
if (!Objects.equals(e, feature.getDefaultValue())) {
String textValue = EcoreUtil.convertToString((EDataType) feature.getEType(), e);
outputValues.add(textValue);
}
}
if (feature.isMany()) {
writer.setMultiAttribute(name, outputValues);
} else if (!outputValues.isEmpty()) {
if (name.equals(":0")) {
writer.setDefaultAttribute(name, outputValues.get(0));
} else {
writer.setAttribute(name, outputValues.get(0));
}
}
} else if (feature instanceof EReference) {
for (Object value : getFeature(obj, feature)) {
if (value != null) {
writeObject(writer.createChild(name), (EObject) value, meta);
}
}
}
}
}
private static class MementoReader implements ModelReader {
private final IMemento memento;
public MementoReader(IMemento memento) {
this.memento = memento;
}
@Override
public String getDefaultAttribute(String name) {
return memento.getTextData();
}
@Override
public String getAttribute(String name) {
return memento.getString(name);
}
@Override
public List<String> getMultiAttribute(String name) {
IMemento[] childMems = memento.getChildren(name);
List<String> attrs = new ArrayList<>(childMems.length);
for (IMemento mem : childMems) {
attrs.add(mem.getTextData());
}
return attrs;
}
@Override
public List<ModelReader> getChildren(String name) {
return Arrays.stream(memento.getChildren(name)).map(MementoReader::new).collect(toList());
}
}
private static class MementoWriter implements ModelWriter {
private final IMemento memento;
public MementoWriter(IMemento memento) {
this.memento = memento;
}
@Override
public void setAttribute(String name, String value) {
memento.putString(name, value);
}
@Override
public void setMultiAttribute(String name, List<String> values) {
for (String value : values) {
memento.createChild(name).putTextData(value);
}
}
@Override
public ModelWriter createChild(String name) {
return new MementoWriter(memento.createChild(name));
}
@Override
public void setDefaultAttribute(String name, String value) {
memento.putTextData(value);
}
}
}