All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gdt.jgui.entity.graph.JGraphViewSelector Maven / Gradle / Ivy

package gdt.jgui.entity.graph;
/*
 * Copyright 2016 Alexander Imas
 * This file is extension of JEntigrator.

    JEntigrator is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    JEntigrator is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with JEntigrator.  If not, see .
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Stack;
import java.util.logging.Logger;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import gdt.data.entity.BaseHandler;
import gdt.data.entity.EdgeHandler;
import gdt.data.entity.EntityHandler;
import gdt.data.entity.GraphHandler;
import gdt.data.entity.NodeHandler;
import gdt.data.entity.facet.ExtensionHandler;
import gdt.data.entity.facet.FieldsHandler;
import gdt.data.grain.Core;
import gdt.data.grain.Identity;
import gdt.data.grain.Locator;
import gdt.data.grain.Sack;
import gdt.data.grain.Support;
import gdt.data.store.Entigrator;
import gdt.jgui.console.JConsoleHandler;
import gdt.jgui.console.JContext;
import gdt.jgui.console.JFacetRenderer;
import gdt.jgui.console.JItemPanel;
import gdt.jgui.console.JItemsListPanel;
import gdt.jgui.console.JMainConsole;
import gdt.jgui.console.JRequester;
import gdt.jgui.entity.JEntityFacetPanel;
import gdt.jgui.entity.JEntityPrimaryMenu;
import gdt.jgui.entity.JReferenceEntry;
import gdt.jgui.tool.JTextEditor;
/**
 * Displays a list of views provided for the graph facet.
 * @author imasa
 *
 */

public class JGraphViewSelector extends  JItemsListPanel implements   JFacetRenderer,JRequester{
	
	private static final long serialVersionUID = 1L;
	private Logger LOGGER=Logger.getLogger(getClass().getName());
	private static final String ACTION_CREATE_GRAPH="action create graph";
	String entihome$;
    String list$;
    String entityKey$;
    String entityLabel$;
    /**
     * The default constructor.
     */
    
    public JGraphViewSelector() {
        super();
 	}
    /**
		 * Get the context locator.
		 * @return the context locator.
		 */		
    @Override
	public String getLocator() {
		//System.out.println("JGraphViewLocator:getLocator:BEGIN");
		Properties locator=new Properties();
	    locator.setProperty(Locator.LOCATOR_TYPE, JContext.CONTEXT_TYPE);
	    locator.setProperty(JContext.CONTEXT_TYPE,getType());
	    locator.setProperty(BaseHandler.HANDLER_LOCATION,GraphHandler.EXTENSION_KEY);
	    if(entihome$!=null){
	       locator.setProperty(Entigrator.ENTIHOME,entihome$);
	    Entigrator entigrator=console.getEntigrator(entihome$);
	    String icon$=ExtensionHandler.loadIcon(entigrator, GraphHandler.EXTENSION_KEY, "graph.png");
	    if(icon$!=null)
		    	locator.setProperty(Locator.LOCATOR_ICON,icon$);
	    }
	    if(entityKey$!=null)
		       locator.setProperty(EntityHandler.ENTITY_KEY,entityKey$);
	    if(entityLabel$!=null)
		       locator.setProperty(EntityHandler.ENTITY_LABEL,entityLabel$);
	    locator.setProperty(Locator.LOCATOR_TITLE, getTitle());
        locator.setProperty(BaseHandler.HANDLER_SCOPE,JConsoleHandler.CONSOLE_SCOPE);
	    locator.setProperty(BaseHandler.HANDLER_CLASS,getClass().getName());
		
		locator$=Locator.toString(locator);
		//System.out.println("JGraphViewLocator:getLocator:locator="+locator$);
	    return locator$ ;
	}
    /**
	 * Create a new facet renderer.
	 * @param console the main console.
	 * @param locator$ the locator string.
	 * @return the fields editor.
	 */
	@Override
	public JContext instantiate(JMainConsole console, String locator$) {
	    
		try{
		//	System.out.println("JGraphViewSelector:instantiate:locator="+locator$);
			 this.console=console;
			 Properties locator=Locator.toProperties(locator$);
			 entityKey$=locator.getProperty(EntityHandler.ENTITY_KEY);
			 entihome$=locator.getProperty(Entigrator.ENTIHOME);
        	 entityLabel$=locator.getProperty(EntityHandler.ENTITY_LABEL);
        	 if(entityLabel$==null&&entityKey$!=null){
        		 Entigrator entigrator=console.getEntigrator(entihome$);
        		 entityLabel$=entigrator.indx_getLabel(entityKey$);
        	 }
        	 ArrayListipl=new ArrayList();
        	 String actionLocator$=getNodesLocator();
        	 JItemPanel nodesItem=new JItemPanel(console, actionLocator$);
   		  	 ipl.add(nodesItem);
    		
   		  	 actionLocator$=getEdgesLocator();
    		 JItemPanel edgesItem=new JItemPanel(console, actionLocator$);
		  	 ipl.add(edgesItem);
		  	 
   		  	 actionLocator$=getMapLocator();
    		 JItemPanel graphItem=new JItemPanel(console, actionLocator$);
		  	 ipl.add(graphItem);
		  	  
		  	 actionLocator$=getFacetsLocator();
		  	actionLocator$=Locator.append(actionLocator$, Locator.LOCATOR_TITLE, "Entity");
    		 JItemPanel facetsItem=new JItemPanel(console, actionLocator$);
		  	 
    		 ipl.add(facetsItem);
		  	 
		  	  putItems(ipl.toArray(new JItemPanel[0]));
        	return this;
        }catch(Exception e){
        
        Logger.getLogger(JGraphViewSelector.class.getName()).severe(e.toString());
        }
        return null;
        }
	 /**
		 * Get the context menu.
		 * @return the context menu.
		 */
	@Override
	public JMenu getContextMenu() {
		final JMenu	menu=new JMenu("Context");
		   menu.setName("Context");
		   menu.addMenuListener(new MenuListener(){
				@Override
				public void menuSelected(MenuEvent e) {
					menu.removeAll();
					
					JMenuItem  recentItem = new JMenuItem("Put as recent");
					   recentItem.addActionListener(new ActionListener() {
							@Override
							public void actionPerformed(ActionEvent e) {
						      console.getRecents().put(getTitle(), getLocator());
							}
						} );
						menu.add(recentItem);	
						JMenuItem  rebuildItem = new JMenuItem("Rebuild");
						   rebuildItem.addActionListener(new ActionListener() {
								@Override
								public void actionPerformed(ActionEvent e) {
									//rebuild();
									Entigrator entigrator=console.getEntigrator(entihome$);
									NodeHandler.rebuild(entigrator, entityKey$);
								}
							} );
						   menu.add(rebuildItem);
				}

				@Override
				public void menuDeselected(MenuEvent e) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void menuCanceled(MenuEvent e) {
					// TODO Auto-generated method stub
					
				}
		   });
		  
			return menu;
	}

	/**
	 * Get title of the context.  
	 * @return the title of the context.
	 */	
	@Override
	public String getTitle() {
		if(entityLabel$!=null)
		 return entityLabel$;
		else
			return "Graph view selector";
	}
	/**
	 * Get subtitle of the context.  
	 * @return the subtitle of the context.
	 */	
	@Override
	public String getSubtitle() {
		return entihome$;
	}
	 /**
     * Get type of the  context.  
     * @return the type of the context.
     */	
	@Override
	public String getType() {
		return "Graph view selector";
	}
/**
 * No action
 */
	@Override
	public void close() {
		
	}
private String getNodesLocator(){
	try{
		JGraphNodes gn=new JGraphNodes();
		String gn$=gn.getLocator();
		gn$=Locator.append(gn$, Entigrator.ENTIHOME, entihome$);
		gn$=Locator.append(gn$, EntityHandler.ENTITY_KEY, entityKey$);
		gn$=Locator.append(gn$, EntityHandler.ENTITY_LABEL, entityLabel$);
		Entigrator entigrator=console.getEntigrator(entihome$);
		String icon$=ExtensionHandler.loadIcon(entigrator, NodeHandler.EXTENSION_KEY, "node.png");
		gn$=Locator.append(gn$,Locator.LOCATOR_ICON,icon$);
		return gn$;
		//gn.instantiate(console, gn$);
		//return gn.getLocator();
	}catch(Exception e){
		 Logger.getLogger(JGraphViewSelector.class.getName()).severe(e.toString());
	}
	return null;
}
private String getEdgesLocator(){

	try{
		JGraphEdgesPanel ep=new JGraphEdgesPanel();
		String ep$=ep.getLocator();
		ep$=Locator.append(ep$, Entigrator.ENTIHOME, entihome$);
		ep$=Locator.append(ep$, EntityHandler.ENTITY_KEY, entityKey$);
		ep$=Locator.append(ep$, EntityHandler.ENTITY_LABEL, entityLabel$);
		Entigrator entigrator=console.getEntigrator(entihome$);
		String icon$=ExtensionHandler.loadIcon(entigrator, EdgeHandler.EXTENSION_KEY, "edge.png");
		ep$=Locator.append(ep$,Locator.LOCATOR_ICON,icon$);
		return ep$;
		//ep.instantiate(console, ep$);
		//return ep.getLocator();
	}catch(Exception e){
		 Logger.getLogger(JGraphViewSelector.class.getName()).severe(e.toString());
	}
	
	return null;
}
private String getMapLocator(){

	try{
		JGraphRenderer gr=new JGraphRenderer();
		String gr$=gr.getLocator();
		gr$=Locator.append(gr$, Entigrator.ENTIHOME, entihome$);
		gr$=Locator.append(gr$, EntityHandler.ENTITY_KEY, entityKey$);
		gr$=Locator.append(gr$, EntityHandler.ENTITY_LABEL, entityLabel$);
		Entigrator entigrator=console.getEntigrator(entihome$);
		String icon$=ExtensionHandler.loadIcon(entigrator, GraphHandler.EXTENSION_KEY, "map.png");
		gr$=Locator.append(gr$,Locator.LOCATOR_ICON,icon$);
		return gr$;
		//gr.instantiate(console, gr$);
		//return gr.getLocator();
	}catch(Exception e){
		 Logger.getLogger(JGraphViewSelector.class.getName()).severe(e.toString());
	}

	return null;
}
private String getFacetsLocator(){

	try{
		JEntityFacetPanel fp=new JEntityFacetPanel();
		String fp$=fp.getLocator();
		fp$=Locator.append(fp$, Entigrator.ENTIHOME, entihome$);
		fp$=Locator.append(fp$, EntityHandler.ENTITY_KEY, entityKey$);
		fp$=Locator.append(fp$, EntityHandler.ENTITY_LABEL, "Entity");
		Entigrator entigrator=console.getEntigrator(entihome$);
		String icon$=Support.readHandlerIcon(entigrator, JEntityFacetPanel.class, "facet.png");
				//ExtensionHandler.loadIcon(entigrator, GraphHandler.EXTENSION_KEY, "map.png");
		fp$=Locator.append(fp$,Locator.LOCATOR_ICON,icon$);
		return fp$;
	}catch(Exception e){
		 Logger.getLogger(JGraphViewSelector.class.getName()).severe(e.toString());
	}

	return null;
}
@Override
/**
 * No action
 */
public String addIconToLocator(String locator$) {
	// TODO Auto-generated method stub
	return null;
}
/**
 * Get the facet handler instance.
 * @return the facet handler instance.	
 */
@Override
public String getFacetHandler() {
	return GraphHandler.class.getName();
}
/**
 * Get the type of the entity.
 * @return the type of the entity.	
 */
@Override
public String getEntityType() {
	return "graph";
}
/**
 * Get facet icon as a Base64 string. 
 * @return the icon string.
 */
@Override
public String getCategoryIcon() {
	try{
		Entigrator entigrator=console.getEntigrator(entihome$);
		return ExtensionHandler.loadIcon(entigrator, GraphHandler.EXTENSION_KEY,"graph.png");
}catch(Exception e){
	LOGGER.severe(e.toString());
	return null;
}
}
/**
 * Get category title for entities having the facet type.
 * @return the category title.
 */

@Override
public String getCategoryTitle() {
	return "Graphs";
}
/**
 * Adapt cloned entity.
 * @param console the main console.
 * @param locator$ the locator string.
 */
@Override
public void adaptClone(JMainConsole console, String locator$) {
	try{
		Properties locator=Locator.toProperties(locator$);
		String entihome$=locator.getProperty(Entigrator.ENTIHOME);
		String entityKey$=locator.getProperty(EntityHandler.ENTITY_KEY);
		Entigrator entigrator=console.getEntigrator(entihome$);
		Sack entity=entigrator.getEntityAtKey(entityKey$);
		String entityLocator$=EntityHandler.getEntityLocator(entigrator, entity);
		GraphHandler graphHandler=new GraphHandler();
		graphHandler.instantiate(entityLocator$);
		graphHandler.adaptClone(entigrator);
	}catch(Exception e){
		LOGGER.severe(e.toString());
	}
	
	
}
/**
 * Adapt renamed entity.
 * @param console the main console.
 * @param locator$ the locator string.
 */

@Override
public void adaptRename(JMainConsole console, String locator$) {
	try{
		Properties locator=Locator.toProperties(locator$);
		String entihome$=locator.getProperty(Entigrator.ENTIHOME);
		String entityKey$=locator.getProperty(EntityHandler.ENTITY_KEY);
		Entigrator entigrator=console.getEntigrator(entihome$);
		Sack entity=entigrator.getEntityAtKey(entityKey$);
		String entityLocator$=EntityHandler.getEntityLocator(entigrator, entity);
		GraphHandler graphHandler=new GraphHandler();
		graphHandler.instantiate(entityLocator$);
		graphHandler.adaptRename(entigrator);
	}catch(Exception e){
		LOGGER.severe(e.toString());
	}
	
}
/**
 * Add all facet-relevant related entities within the database 
 * to the relations list. 
 * @param entigrator the entigrator.
 * @param entiyKey$ the key of the origin entity.
 * @param sl the list of relations.
 */
@Override
public void collectReferences(Entigrator entigrator, String entityKey$, ArrayList sl) {
	try{
		this.entityKey$=entityKey$;
		GraphHandler gh=new GraphHandler();
		String entityLocator$=EntityHandler.getEntityLocatorAtKey(entigrator, entityKey$);
		if(!gh.isApplied(entigrator, entityLocator$))
			return ;
		Sack graph=entigrator.getEntityAtKey(entityKey$);
		String[] sa=graph.elementListNoSorted("node.select");
		if(sa==null)
			sa=graph.elementListNoSorted("node");
		Core[] ca=graph.elementGet("bond");
		ArrayListrl=new ArrayList();
		String edgeKey$;
		Sack edge;
		//String detailKey$;
		for(Core c:ca){
			for( String s:sa){
			try{
				if(!rl.contains(s))
					rl.add(s);
			if(s.equals(c.type)||s.equals(c.value)){
				edgeKey$=graph.getElementItemAt("edge.entity", c.name);
				if(!rl.contains(edgeKey$))
				   rl.add(edgeKey$);
				edge=entigrator.getEntityAtKey(edgeKey$);
			}
			}catch(Exception ee){
				System.out.println("JGraphViewSelector:collectReferences:"+ee.toString());
			}
			}
		}
		for(String r:rl){
				if(r==null||entityKey$.equals(r))
					continue;
				JReferenceEntry.getReference(entigrator,r, sl);
		}
		//System.out.println("JBookmarksEditor:collectReferences:FINISH");
	}catch(Exception e){
		Logger.getLogger(getClass().getName()).severe(e.toString());
	}
	
}
/**
 * Rebuild the facet-relevant indexes for the entity.
 * @param console the main console.
 * @param entigrator the entigrator.
 * @param entity the entity.
 */
@Override
public void reindex(JMainConsole console, Entigrator entigrator, Sack entity) {
	try{	
    	String graphHandler$=GraphHandler.class.getName();
    	if(entity.getElementItem("fhandler", graphHandler$)!=null){
			entity.putElementItem("jfacet", new Core(null,graphHandler$,JGraphFacetOpenItem.class.getName()));
			entigrator.save(entity);
		}
    }catch(Exception e){
    	Logger.getLogger(getClass().getName()).severe(e.toString());
    }
	
	
}
/**
 * Create a new entity having the facet type.
 * @param console the main console. 
 * @param locator$ the locator string.
 * @return the new entity.
 */
@Override
public String newEntity(JMainConsole console, String locator$) {
	try{
		Properties locator=Locator.toProperties(locator$);
		String entihome$=locator.getProperty(Entigrator.ENTIHOME);
		JTextEditor textEditor=new JTextEditor();
	    String teLocator$=textEditor.getLocator();
	    teLocator$=Locator.append(teLocator$, Entigrator.ENTIHOME,entihome$);
	    teLocator$=Locator.append(teLocator$, JTextEditor.TEXT_TITLE,"New graph");
	    String text$="NewGraph"+Identity.key().substring(0, 4);
	    teLocator$=Locator.append(teLocator$, JTextEditor.TEXT,text$);
	    JGraphRenderer gr=new JGraphRenderer();
	    String grLocator$=gr.getLocator();
	    grLocator$=Locator.append(grLocator$, Entigrator.ENTIHOME,entihome$);
	    grLocator$=Locator.append(grLocator$, EntityHandler.ENTITY_KEY,entityKey$);
	    grLocator$=Locator.append(grLocator$, BaseHandler.HANDLER_METHOD,"response");
	    grLocator$=Locator.append(grLocator$, JRequester.REQUESTER_ACTION,ACTION_CREATE_GRAPH);
	    String requesterResponseLocator$=Locator.compressText(grLocator$);
	    teLocator$=Locator.append(teLocator$,JRequester.REQUESTER_RESPONSE_LOCATOR,requesterResponseLocator$);
	    JConsoleHandler.execute(console, teLocator$);
	}catch(Exception ee){   
		LOGGER.severe(ee.toString());
		
	}
	return null;
}
/**
 * Response on call from the other context.
 *	@param console main console
 *  @param locator$ action's locator 
 */  	
@Override
public void response(JMainConsole console, String locator$) {
	System.out.println("JGraphrenderer:response:"+Locator.remove(locator$,Locator.LOCATOR_ICON ));
	try{
		Properties locator=Locator.toProperties(locator$);
		String action$=locator.getProperty(JRequester.REQUESTER_ACTION);
		entihome$=locator.getProperty(Entigrator.ENTIHOME);
		Entigrator entigrator=console.getEntigrator(entihome$);
		String text$=locator.getProperty(JTextEditor.TEXT);

		if(ACTION_CREATE_GRAPH.equals(action$)){
			Sack newEntity=entigrator.ent_new("graph", text$);
			newEntity.createElement("field");
			newEntity.putElementItem("field", new Core(null,"name","value"));
			newEntity.createElement("fhandler");
			newEntity.putElementItem("fhandler", new Core(null,GraphHandler.class.getName(),GraphHandler.EXTENSION_KEY));
			newEntity.putElementItem("fhandler", new Core(null,FieldsHandler.class.getName(),null));
			newEntity.createElement("jfacet");
			newEntity.putElementItem("jfacet", new Core("gdt.jgui.entity.graph.JGraphFacetAddItem",EdgeHandler.class.getName(),"gdt.jgui.entity.graph.JGraphFacetOpenItem"));
			newEntity.putAttribute(new Core (null,"icon","graph.png"));
			entigrator.save(newEntity);
			entigrator.ent_assignProperty(newEntity, "fields", text$);
			entigrator.ent_assignProperty(newEntity, "graph", text$);
			String icons$=entihome$+"/"+Entigrator.ICONS;
			Support.addHandlerIcon(JGraphRenderer.class, "graph.png", icons$);
			newEntity=entigrator.ent_reindex(newEntity);
			JEntityFacetPanel efp=new JEntityFacetPanel(); 
			String efpLocator$=efp.getLocator();
			efpLocator$=Locator.append(efpLocator$,Locator.LOCATOR_TITLE,newEntity.getProperty("label"));
			efpLocator$=Locator.append(efpLocator$, Entigrator.ENTIHOME, entihome$);
			efpLocator$=Locator.append(efpLocator$, EntityHandler.ENTITY_KEY, newEntity.getKey());
			efpLocator$=Locator.append(efpLocator$, EntityHandler.ENTITY_LABEL, newEntity.getProperty("label"));
			JEntityPrimaryMenu.reindexEntity(console, efpLocator$);
			Stack s=console.getTrack();
			s.pop();
			console.setTrack(s);
			JConsoleHandler.execute(console, efpLocator$);
			return;
		}
		}catch(Exception e){
		Logger.getLogger(getClass().getName()).severe(e.toString());
	}
	
	
}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy