package fr.lirmm.rcr.cogui.saphir;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
//import org.eclipse.core.resources.*;
import fr.ina.segmodel.pub.DomainEvent;
import fr.ina.segmodel.pub.IAllEventsListener;
import fr.ina.segmodel.pub.MediaDescriptionEvent;
import fr.ina.segmodel.pub.SegmentEvent;
import fr.ina.segmodel.pub.SegmentsLineEvent;
import fr.ina.segmodel.pub.StratumEvent;
import fr.ina.synch.pub.CurrentTimeEvent;
import fr.ina.synch.pub.ISynchListener;
import fr.ina.synch.pub.SelectEvent;
import fr.ina.utils.TimeRef;
import fr.lirmm.rcr.cogui.CoguiConstants;
import fr.lirmm.rcr.cogui.appli.EditorManager;
import fr.lirmm.rcr.cogui.model.*;
import java.io.*;
import javax.swing.JOptionPane;
import fr.lirmm.rcr.cogui.xml.*;
public class StudioAdapter implements IAllEventsListener, ISynchListener {
protected static final String STORAGE_DIR = "C:/Storage/cogui/";
private CGObject voc = null;
private CGObject graph_set = null;
private HierarchyProperty graph_hierarchy = null;
public StudioAdapter() {
}
protected void initListeners() throws Exception {
if (PluginResources.getInstance().model == null
|| PluginResources.getInstance().synch == null) {
throw new Exception("studio plugin cannot be listened");
}
PluginResources.getInstance().model.addDomainListener(this);
PluginResources.getInstance().model.addStratumListener(this);
PluginResources.getInstance().model.addSegmentsLineListener(this);
PluginResources.getInstance().model.addSegmentListener(this);
PluginResources.getInstance().model.addMediaDescriptionListener(this);
PluginResources.getInstance().synch.addSelectionListener(this);
}
public void saveGraphSet() throws Exception {
if (graph_set != null)
CoGXMLWriter.instance().writeGraphSet(
new File(STORAGE_DIR + "annotations.cogxml"), graph_set);
CoGXMLWriter.instance().writeGraphSet(
new File(STORAGE_DIR + "prototypics.cogxml"),EditorManager.instance().getProtoSet() );
CoGXMLWriter.instance().writeGraphSet(
new File(STORAGE_DIR + "patterns.cogxml"), EditorManager.instance().getPatternSet());
}
protected void initStorage() throws Exception {
// first load vocabulary
EditorManager.instance().getDocManager().clear();
File file = new File(STORAGE_DIR + "voc.cogxml");
if (!file.exists())
throw new Exception("can't find " + STORAGE_DIR + "voc.cogxml");
CoGXMLReader.instance().load(file);
voc = CoGXMLReader.instance().createVocabulary();
EditorManager.instance().setVocabulary(voc, null);
// load graph set
file = new File(STORAGE_DIR + "annotations.cogxml");
if (!file.exists())
throw new Exception("can't find " + STORAGE_DIR
+ "annotations.cogxml");
CoGXMLReader.instance().load(file);
graph_set = CoGXMLReader.instance().createGraphSet(voc);
CoGXMLReader.instance().createRules(voc, graph_set);
graph_hierarchy = (HierarchyProperty) graph_set
.getProperty("GRAPH_HIERARCHY");
// prototypics
file = new File(STORAGE_DIR + "prototypics.cogxml");
if (!file.exists()){
//throw new Exception("can't find " + STORAGE_DIR
// + "prototypics.cogxml");
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write("");
out.write("");
out.close();
file = new File(STORAGE_DIR + "prototypics.cogxml");
} catch (IOException e) {
}
}
CoGXMLReader.instance().load(file);
CGObject protoSet = CoGXMLReader.instance().createProtoSet(voc);
HierarchyProperty protoprop = (HierarchyProperty) protoSet
.getProperty("GRAPH_HIERARCHY");
protoprop.run();
EditorManager.instance().getDocManager().addProtoSet(protoSet, file);
//patterns
file = new File(STORAGE_DIR + "patterns.cogxml");
if (!file.exists()){
// throw new Exception("can't find " + STORAGE_DIR
// + "patterns.cogxml");
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write("");
out.write("");
out.close();
file = new File(STORAGE_DIR + "patterns.cogxml");
}
CoGXMLReader.instance().load(file);
CGObject patternSet = CoGXMLReader.instance()
.createPatternGraphSet(voc);
HierarchyProperty patternprop = (HierarchyProperty) patternSet
.getProperty("GRAPH_HIERARCHY");
patternprop.run();
// TODO verifier ce fichier null
EditorManager.instance().getDocManager().addPatternSet(
patternSet, file);
if (graph_set != null && graph_hierarchy.getVertexCount() > 0) {
graph_hierarchy.run();
if (graph_hierarchy.available())
EditorManager.instance().setGraphs(graph_set, null);
else {
throw new Exception(
"set of graphs not loaded: graph hierarchy cannot be built");
}
}
}
protected CGObject createAnnotation(String resourceId, int element_type) {
String type = null;
String cgtype=null;
switch (element_type) {
case SelectEvent.DOMAIN:
type = "domains";
cgtype="_DOMAIN";
break;
case SelectEvent.LINE:
type = "segment lines";
cgtype="_SEGMENT_LINE";
break;
case SelectEvent.STRATUM:
type = "stratums";
cgtype="_STRATUM";
break;
case SelectEvent.SEGMENT:
type = "segments";
cgtype="_SEGMENT";
break;
default:
type = "unrecognized resources";
cgtype="_RESOURCE";
}
CGObject new_graph = new CGObject();
new_graph.addProperty(new StringProperty("ID", resourceId, null));
new_graph.addProperty(new NatureProperty(CoguiConstants.GRAPH));
new_graph.addProperty(new StringProperty("graph_type", "fact",
"nature of the graph"));
new_graph.addProperty(new IntegerProperty("RESOURCE_TYPE",
element_type, "resource type"));
new_graph.addProperty(new StringProperty("set", type, "graph family"));
new_graph.addProperty(new StringProperty("DISPLAY_TEXT", resourceId,
"displayed text"));
new_graph.addProperty(new StringProperty("LABEL", resourceId,
"graph label"));
SGGraphProperty sgProp=new SGGraphProperty("GRAPH_PROPERTY");
new_graph.addProperty(sgProp);
new_graph.addProperty(new CorefProperty("COREF"));
ConceptFactory cFactory=new ConceptFactory();
CGObject concept=cFactory.createCGObject();
Vocabulary voc=EditorManager.instance().getVocabulary();
if(voc==null) return null;
HierarchyProperty hp=(HierarchyProperty) voc.getProperty("CONCEPT_TYPE_HIERARCHY");
CGObject ct=hp.searchById(cgtype);
if(ct==null)
{
JOptionPane
.showMessageDialog(
EditorManager.instance().getFrame(),
"Current used vocabulary doesnt contain required concept type: "+cgtype,
"Problem with vocabulary",
JOptionPane.ERROR_MESSAGE);
}else
{
concept.addProperty(new TypeProperty(ct, "concept type"));
concept.addProperty(new StringProperty("DISPLAY_TEXT", ""
+ ct.getPropertyValue("LABEL") + " : *", null));
}
concept.addProperty(new StringProperty("ID",resourceId,null));
sgProp.addConcept(concept);
// add in graph hierarchy
graph_hierarchy.addRoot(new_graph);
EditorManager.instance().updateGraphTree();
return new_graph;
}
public void currentTimeEventFired(CurrentTimeEvent event) {
}
public void domainEventFired(DomainEvent domainEvent) {
switch (domainEvent.getAction()) {
case DomainEvent.CREATED:
break;
case DomainEvent.REMOVED: {
CGObject annotation = graph_hierarchy.searchById(domainEvent
.getDomainId());
if (annotation != null) {
// poubelle
annotation.addProperty(new StringProperty("set", "poubelle",
"graph family"));
EditorManager.instance().updateGraphTree();
}
break;
}
case DomainEvent.UPDATED: // Renamed
{
CGObject annotation = graph_hierarchy.searchById(domainEvent
.getDomainId());
if (annotation != null) {
annotation.addProperty(new StringProperty("DISPLAY_TEXT",
domainEvent.getDomainName(), "displayed text"));
annotation.addProperty(new StringProperty("LABEL", domainEvent
.getDomainName(), "graph label"));
}
break;
}
default:
break;
}
}
public void mediaDescriptionEventFired(
MediaDescriptionEvent mediaDescriptionEvent) {
switch (mediaDescriptionEvent.getAction()) {
case MediaDescriptionEvent.CREATED:
// clear displayed graphs
// and display top-level media description graph
System.out.println("media_description created");
break;
case MediaDescriptionEvent.REMOVED:
System.out.println("media_description removed");
break;
default:
break;
}
}
public void segmentBorderUpdated(String segmentId, TimeRef newBorderValue,
int border) {
}
public void segmentEventFired(SegmentEvent sEvt) {
switch (sEvt.getAction()) {
case SegmentEvent.CREATED:
System.out.println("segment created");
break;
case SegmentEvent.REMOVED: {
CGObject annotation = graph_hierarchy.searchById(sEvt
.getSegmentId());
if (annotation != null) {
// poubelle
annotation.addProperty(new StringProperty("set", "poubelle",
"graph family"));
EditorManager.instance().updateGraphTree();
}
break;
}
case SegmentEvent.UPDATED:
CGObject annotation = graph_hierarchy.searchById(sEvt
.getSegmentId());
if (annotation != null) {
annotation.addProperty(new StringProperty("DISPLAY_TEXT", sEvt
.getTitle(), "displayed text"));
annotation.addProperty(new StringProperty("LABEL", sEvt
.getTitle(), "graph label"));
}
break;
default:
break;
}
}
public void segmentsLineEventFired(SegmentsLineEvent slEvent) {
switch (slEvent.getAction()) {
case SegmentsLineEvent.CREATED:
break;
case SegmentsLineEvent.REMOVED: {
CGObject annotation = graph_hierarchy.searchById(slEvent
.getSegmentsLineId());
if (annotation != null) {
// poubelle
annotation.addProperty(new StringProperty("set", "poubelle",
"graph family"));
EditorManager.instance().updateGraphTree();
}
break;
}
default:
break;
}
}
public void selectionEventFired(SelectEvent event) {
CGObject annotation = null;
switch (event.getSelectedElementType()) {
case SelectEvent.DOMAIN: {
String selectedDomainId = event.getSelectedElementId();
annotation = graph_hierarchy.searchById(selectedDomainId);
if (annotation == null) {
annotation = createAnnotation(selectedDomainId,
SelectEvent.DOMAIN);
}
DisplayManager.instance().editGraphInCurrentDimension(annotation, 'D',
Color.darkGray);
break;
}
case SelectEvent.STRATUM: {
String selectedStratumId = event.getSelectedElementId();
annotation = graph_hierarchy.searchById(selectedStratumId);
if (annotation == null) {
annotation = createAnnotation(selectedStratumId,
SelectEvent.STRATUM);
}
DisplayManager.instance().editGraphInCurrentDimension(annotation, 'T', Color.cyan);
break;
}
case SelectEvent.LINE: {
String selectedLineId = event.getSelectedElementId();
annotation = graph_hierarchy.searchById(selectedLineId);
if (annotation == null) {
annotation = createAnnotation(selectedLineId, SelectEvent.LINE);
}
DisplayManager.instance().editGraphInCurrentDimension(annotation, 'L', Color.orange);
break;
}
case SelectEvent.SEGMENT: {
String selectedSegmentId = event.getSelectedElementId();
annotation = graph_hierarchy.searchById(selectedSegmentId);
if (annotation == null) {
annotation = createAnnotation(selectedSegmentId,
SelectEvent.SEGMENT);
}
DisplayManager.instance().editGraphInCurrentDimension(annotation, 'S', Color.blue);
break;
}
default:
break;
}
}
public void stratumEventFired(StratumEvent stratumEvent) {
switch (stratumEvent.getAction()) {
case StratumEvent.CREATED:
break;
case StratumEvent.REMOVED: {
CGObject annotation = graph_hierarchy.searchById(stratumEvent
.getStratumId());
if (annotation != null) {
// poubelle
annotation.addProperty(new StringProperty("set", "poubelle",
"graph family"));
EditorManager.instance().updateGraphTree();
}
break;
}
case StratumEvent.UPDATED: // Renamed
{
CGObject annotation = graph_hierarchy.searchById(stratumEvent
.getStratumId());
if (annotation != null) {
annotation.addProperty(new StringProperty("DISPLAY_TEXT",
stratumEvent.getStratumName(), "displayed text"));
annotation.addProperty(new StringProperty("LABEL", stratumEvent
.getStratumName(), "graph label"));
}
break;
}
default:
break;
}
}
}