package fr.lirmm.rcr.cogui.saphir; import java.awt.Color; import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; import javax.swing.JToolBar; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; import fr.lirmm.rcr.cogui.CoGuiEditor; import fr.lirmm.rcr.cogui.CoguiConstants; import fr.lirmm.rcr.cogui.ProfileHandler; import fr.lirmm.rcr.cogui.appli.EditorManager; import fr.lirmm.rcr.cogui.appli.EditorMenuFactory; import fr.lirmm.rcr.cogui.appli.pref.FactGraphEditorProperty; import fr.lirmm.rcr.cogui.appli.util.EditorTabbedPane; import fr.lirmm.rcr.cogui.appli.util.GraphDuplicator; import fr.lirmm.rcr.cogui.appli.util.IdMaker; import fr.lirmm.rcr.cogui.appli.util.Patterns; import fr.lirmm.rcr.cogui.model.CGObject; import fr.lirmm.rcr.cogui.model.CGObjectConnection; import fr.lirmm.rcr.cogui.model.ColorProperty; import fr.lirmm.rcr.cogui.model.CorefProperty; import fr.lirmm.rcr.cogui.model.HierarchyProperty; import fr.lirmm.rcr.cogui.model.IntegerProperty; import fr.lirmm.rcr.cogui.model.NatureProperty; import fr.lirmm.rcr.cogui.model.NestedGraphFactory; import fr.lirmm.rcr.cogui.model.SGGraphProperty; import fr.lirmm.rcr.cogui.model.StringProperty; import fr.lirmm.rcr.cogui.model.TypeProperty; import fr.lirmm.rcr.cogui.util.ChoosePatternDialog; public class DisplayManager { public final static int EDIT_GRAPH_MODE_NORMAL =1; public final static int EDIT_GRAPH_MODE_REDUCED =2; public final static int EDIT_GRAPH_MODE_NESTED =3; public final static int EDIT_GRAPH_MODE_WHOLE =4; protected CGObject current_dimension=null; // singleton private static DisplayManager instance = null; /** * editor tabbed pane */ protected EditorTabbedPane editorTabbedPane = null; private DisplayManager() { editorTabbedPane=EditorManager.instance().getEditorPane(); } public static DisplayManager instance() { if (instance == null) instance = new DisplayManager(); return instance; } public void editGraphInCurrentDimension(CGObject graph,char c,Color color) { clearGraphs(); HierarchyProperty hprop = (HierarchyProperty) EditorManager.instance() .getGraphSet().getProperty("GRAPH_HIERARCHY"); Vector roots = hprop.getRoots(); for (CGObject root:(Vector)hprop.getRoots()) { if(hprop.isPrimitiveKindOf(graph, root)) { if(current_dimension==null) { EditorManager.instance().editGraph(root, true, color, c, null); JToolBar toolBar = EditorManager.instance().getCurrentToolBar(); // add dimension selector to toolbar DimensionSelectorButton ddbutton=new DimensionSelectorButton(root); ddbutton.addToToolBar(toolBar); return; } SGGraphProperty gprop = (SGGraphProperty) root .getProperty("GRAPH_PROPERTY"); // search current nesting CGObject concept=gprop.searchConceptById(root.getId()); if(concept==null) { System.out.println("missing resource concept root.getId="+root.getId()); } for(Iterator i=gprop.getNestedGraphIterator(concept);i.hasNext();) { CGObject nested_graph=(CGObject)i.next(); if(nested_graph.getType().getTypes().get(0).getId().equals(current_dimension.getId())) { CoGuiEditor ed=EditorManager.instance().editGraph(current_dimension.getId(),nested_graph, true, color, c, null); JToolBar toolBar = EditorManager.instance().getCurrentToolBar(); // add dimension selector to toolbar DimensionSelectorButton ddbutton=new DimensionSelectorButton(nested_graph); ddbutton.addToToolBar(toolBar); return; } } // nested graph doesnt exist: create one NestedGraphFactory ngFactory=new NestedGraphFactory(); CGObject new_ng=ngFactory.createCGObject(); TypeProperty typeProp=new TypeProperty(current_dimension); new_ng.addProperty(typeProp); new_ng.addProperty(new StringProperty("DISPLAY_TEXT",(String)current_dimension.getPropertyValue("LABEL"),null)); CGObject new_graph=createNestedGraph(root); new_ng.addProperty(new StringProperty("ID",new_graph.getId(),null)); gprop.addNestedGraph(root.getId(),new_ng); gprop.run(); /* Vector v=new Vector(); v.addAll(hprop.getVertices().values()); v.add(new_graph); v.addAll(hprop.getEdges()); v.add(createEdge(new_graph,root)); // try{hprop.updateBranch(new_graph, EditorManager.instance().updateGraph(new_graph)); // }catch(Exception e){e.printStackTrace();} hprop.update(v); */ hprop.addRoot(new_graph); EditorManager.instance().updateGraphSet2(); editGraphInCurrentDimension(new_graph,c,color); /* CGObject s=(CGObject)hprop.searchById(new_graph.getId()); System.out.println("graph in hprop="+s); for(CGObject cog:(Vector)hprop.getRoots()) { System.out.println("idg="+cog.getId()); } EditorManager.instance().updateGraphTree(); // graph hierarchy will be updated when editor is read or closed CoGuiEditor ed=EditorManager.instance().editGraph(new_graph, true, color, c, null); JToolBar toolBar = EditorManager.instance().getCurrentToolBar(); // add dimension selector to toolbar DimensionSelectorButton ddbutton=new DimensionSelectorButton(new_graph); ddbutton.addToToolBar(toolBar); */ return; } } } protected void editGraph(CGObject graph,char c,Color color,int edit_mode) { /* if(current_dimension!=null) { editGraphInCurrentDimension(graph,c,color); return; } */ if(selectEditor(graph)) return ; // graph property SGGraphProperty gprop = (SGGraphProperty) graph .getProperty("GRAPH_PROPERTY"); // fact graph options in prefs FactGraphEditorProperty gep = (FactGraphEditorProperty) ProfileHandler .instance().getProperty("general", "fact_graph_editor"); if (gprop.getVertexCount() > gep.getPreferredWholeGraphEditionLimit()) edit_mode=EDIT_GRAPH_MODE_REDUCED; switch(edit_mode) { /* * this mode force reduced edition */ case EDIT_GRAPH_MODE_REDUCED: { clearGraphs(); EditorManager.instance().editReducedGraph(graph,true,getSelectionOfConcepts(graph,1)); JToolBar toolBar = EditorManager.instance().getCurrentToolBar(); // add dimension selector to toolbar DimensionSelectorButton ddbutton=new DimensionSelectorButton(graph); ddbutton.addToToolBar(toolBar); break; } /* * whole graph edition (if preferred limit is not exceeded) */ case EDIT_GRAPH_MODE_WHOLE: case EDIT_GRAPH_MODE_NORMAL: case EDIT_GRAPH_MODE_NESTED: default: { clearGraphs(); EditorManager.instance().editGraph(graph, true, color, c, null); JToolBar toolBar = EditorManager.instance().getCurrentToolBar(); // add dimension selector to toolbar DimensionSelectorButton ddbutton=new DimensionSelectorButton(graph); ddbutton.addToToolBar(toolBar); break; } } } public boolean selectEditor(CGObject graph) { if (graph == null) return false; return (editorTabbedPane .selectEditor((String) graph.getId()) || editorTabbedPane .selectEditor("" + graph.getId() + "(r)")); } // remove all graphs that are not pinned protected void clearGraphs() { for(Enumeration e=EditorManager.instance().getEditedGraphs();e.hasMoreElements();) { CGObject g = (CGObject) e.nextElement(); CoGuiEditor ed=editorTabbedPane.getEditor(g.getId()); if(!ed.isPinned()) editorTabbedPane.remove(editorTabbedPane.getEditorIndex(g.getId())); } } Hashtable getSelectionOfConcepts(CGObject graph,int nb) { // graph property SGGraphProperty gprop = (SGGraphProperty) graph .getProperty("GRAPH_PROPERTY"); // arbitrary choose nb vertices to begin reduced edition Hashtable selection = new Hashtable(); Iterator it = gprop.getConceptIterator(); for (int i = 0; i < nb && it.hasNext(); i++) { CGObject concept = (CGObject) it.next(); selection.put(concept.getId(), concept); } return selection; } public CGObject getCurrentDimension() { return current_dimension; } public void setCurrentDimension(CGObject current_dimension) { this.current_dimension=current_dimension; } CGObject createNestedGraph(CGObject parent_graph) { CGObject new_graph = null; String new_graph_id = null; String label = null; HierarchyProperty hp = (HierarchyProperty) EditorManager .instance().getGraphSet().getProperty( "GRAPH_HIERARCHY"); new_graph = new CGObject(); new_graph_id = IdMaker.getId(); new_graph.addProperty(new StringProperty("ID", new_graph_id, "Identifier")); new_graph.addProperty(new NatureProperty( CoguiConstants.GRAPH)); new_graph.addProperty(new StringProperty( "graph_type", "fact", "nature of the graph")); new_graph.addProperty(new StringProperty("set", "default_set", "graph family")); new_graph.addProperty(new SGGraphProperty( "GRAPH_PROPERTY")); new_graph .addProperty(new CorefProperty("COREF")); if (label == null || label.length() < 1) label = new_graph.getId(); new_graph.addProperty(new StringProperty( "DISPLAY_TEXT", label, "displayed text")); new_graph.addProperty(new StringProperty("LABEL", label, "graph label")); return new_graph; } private CGObjectConnection createEdge(CGObject parent,CGObject child){ CGObjectConnection cnx = new CGObjectConnection(parent,child); cnx.addProperty(new NatureProperty(CoguiConstants.EDGE_KINDOF)); cnx.addProperty(new IntegerProperty("DISPLAY_EDGE_BEGIN_STYLE", CoguiConstants.EDGE_END_STYLE_SIMPLE2, "endline style")); cnx.addProperty(new ColorProperty("DISPLAY_LINE_COLOR", Color.black, "foreground")); return cnx; } }