I am trying to use this code to change the opacity of some vertices connected to the vertex clicked in a jgraphx, and when you click somewhere outside the vertices it will rechange the opacity. When a vertex is clicked, its value/string is queried and I get all the objects connected to it added in a new object list. Then I process this object list to choose whats going to be highlighted and whats not. However I keep getting errors and cant fix it. I dont have any null object as far as I have checked. If anyone can help, it would be really appreciated.
The error refers to the line:
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);
Here is the code:
public void MassCellHighlight() {
int OPACITY_PALE = 15;
int OPACITY_HL = 100;
graphComponent.getGraphControl().addMouseListener(new MouseAdapter() {
public void mouseReleased (MouseEvent e1) {
if (e1.getButton() == 1 && e1.getClickCount() == 1) {
long x = e1.getX();
long y = e1.getY();
Object cell = graphComponent.getCellAt((int) x, (int)y);
String usecell = graph.convertValueToString(cell);
PropertyConfigurator.configure("\\jena-log4j.properties");
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
FileManager.get().readModel( model, "file" );
String queryString =
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"PREFIX owl: <http://www.w3.org/2002/07/owl#> " +
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
"PREFIX bi: <#> " +
" SELECT ?Path " +
" WHERE { bi:"+usecell+" bi:can_lead_to ?Path } " ;
Query query = QueryFactory.create(queryString);
QueryExecution qe= QueryExecutionFactory.create(query, model);
ResultSet resultset = qe.execSelect();
ResultSet results = ResultSetFactory.copyResults(resultset);
List<Object> values = new ArrayList<Object>();
while ( results.hasNext() ) {
values.add( results.next().get( "Path" ));
}
String s = values.toString();
Pattern p = Pattern.compile("(?<=#)([^,]*)(?=(,|\\]))");
Matcher m = p.matcher(s);
List<String> listhl = new ArrayList<String>() ;
while (m.find()) {
listhl.add(m.group(1));
}
listhl.add(usecell);
List<Object> objectlist = new ArrayList<Object>(listhl);
System.out.println(objectlist);
Object[] allCells = mxGraphModel.getChildren(graph.getModel(), graph.getDefaultParent());
if (cell != null) {
if (graph.getModel().isVertex(cell)) {
for( Object myCell : objectlist) {
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_TEXT_OPACITY, OPACITY_HL);
}
for ( Object myCell: allCells) {
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_PALE);
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_TEXT_OPACITY, OPACITY_PALE);
}
}else {
for( Object myCell: allCells) {
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_TEXT_OPACITY, OPACITY_HL);
}
}
mxRectangle bounds = graph.getBoundsForCells(allCells, true, true, true);
graph.repaint( bounds);
}
}
}
});
}
The error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUIquery$14.mouseReleased(GUIquery.java:1087)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
When I use this code which highlight only the children of the parent vertex it works properly. I just need to expand it so that more cells are highlighted, depending on the object list which occurs from the query:
public void CellHighlight() {
graphComponent.getGraphControl().addMouseListener(new MouseAdapter() {
public void mouseReleased (MouseEvent e1) {
if (e1.getButton() == 1 && e1.getClickCount() == 2) {
final Object selectedCell = graphComponent.getCellAt(e1.getX(), e1.getY());
Object[] allCells = mxGraphModel.getChildren(graph.getModel(), graph.getDefaultParent());
if (selectedCell != null) {
if (graph.getModel().isVertex( selectedCell)) {
for( Object myCell: allCells) {
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_PALE);
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_TEXT_OPACITY, OPACITY_PALE);
}
List<Object> cellList = new ArrayList<Object>();
cellList.add(selectedCell);
Object[] outgoingEdges = mxGraphModel.getOutgoingEdges( graph.getModel(), selectedCell);
for( Object edge: outgoingEdges) {
cellList.add( graph.getModel().getTerminal( edge, false));
}
cellList.addAll( Arrays.asList(outgoingEdges));
for( Object myCell: cellList) {
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_TEXT_OPACITY, OPACITY_HL);
}
} else {
for( Object myCell: allCells) {
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_TEXT_OPACITY, OPACITY_HL);
}
}
mxRectangle bounds = graph.getBoundsForCells(allCells, true, true, true);
graph.repaint( bounds);
}
}
}
});
}
I found the solution. Was adding object and the object list wasnt null, but the jgraphx doesnt recognise the same objects. Should have added the vertices/cells as objects immediately.