Skip to content

Commit

Permalink
Fix several problems with filters
Browse files Browse the repository at this point in the history
Fix #1511 Filter is changed after saving file and opening again
Fix #1458 Filter settings incorrect and causing crashes after reloading a .gephi file
  • Loading branch information
eduramiba committed Aug 20, 2017
1 parent 087e6ef commit 736e5de
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ private void duplicateQuery(FilterController filterController, Query parent, Que
FilterBuilder builder = filterController.getModel().getLibrary().getBuilder(filter);

Query childQuery = filterController.createQuery(builder);
childQuery.setName(child.getName());

Filter filterCopy = childQuery.getFilter();
FilterProperty[] filterProperties = filter.getProperties();
Expand All @@ -212,7 +213,7 @@ private void duplicateQuery(FilterController filterController, Query parent, Que
filterCopyProperties[i].setValue(filterProperties[i].getValue());
}
}

if (parent == null) {
filterController.add(childQuery);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public interface Query {
* @return query's name
*/
public String getName();

/**
* Sets the query name to a custom value
*
* @param name Custom name
*/
public void setName(String name);

/**
* Returns queries that are children of this query.
Expand Down
4 changes: 4 additions & 0 deletions modules/FiltersImpl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>visualization-api</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>utils</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>core-library-wrapper</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void add(Query query) {
model.addFirst(absQuery);

//Init filters with default graph
Graph graph = null;
Graph graph;
if (model != null && model.getGraphModel() != null) {
graph = model.getGraphModel().getGraph();
} else {
Expand Down Expand Up @@ -208,7 +208,7 @@ public void rename(Query query, String name) {
public void setSubQuery(Query query, Query subQuery) {
//Init subquery when new filter
if (subQuery.getParent() == null && subQuery != model.getCurrentQuery()) {
Graph graph = null;
Graph graph;
if (model != null && model.getGraphModel() != null) {
graph = model.getGraphModel().getGraph();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,14 @@ public boolean hasQuery(Query query) {
}

public void addFirst(Query function) {
updateParameters(function);
queries.addFirst(function);
currentQuery = function;
fireChangeEvent();
}

public void addLast(Query function) {
updateParameters(function);
queries.addLast(function);
fireChangeEvent();
}
Expand All @@ -138,6 +140,8 @@ public void rename(Query query, String name) {
}

public void setSubQuery(Query query, Query subQuery) {
updateParameters(subQuery);

//Clean
if (queries.contains(subQuery)) {
queries.remove(subQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ Development and Distribution License("CDDL") (collectively, the
*/
package org.gephi.filters;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLStreamException;
Expand All @@ -52,11 +50,10 @@ Development and Distribution License("CDDL") (collectively, the
import org.gephi.filters.api.Query;
import org.gephi.filters.spi.*;
import org.gephi.graph.api.Graph;
import org.gephi.graph.api.GraphController;
import org.gephi.graph.api.GraphModel;
import org.gephi.project.api.Workspace;
import org.gephi.project.spi.WorkspacePersistenceProvider;
import org.gephi.project.spi.WorkspaceXMLPersistenceProvider;
import org.gephi.utils.Serialization;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.lookup.ServiceProvider;
Expand Down Expand Up @@ -123,6 +120,8 @@ public void writeXML(XMLStreamWriter writer, FilterModelImpl model) throws XMLSt
}

private void writeQuery(String code, XMLStreamWriter writer, FilterModelImpl model, Query query, int parentId) throws XMLStreamException {
Serialization serialization = new Serialization(model.getGraphModel());

writer.writeStartElement(code);
int id = queryId++;
writer.writeAttribute("id", String.valueOf(id));
Expand All @@ -133,11 +132,12 @@ private void writeQuery(String code, XMLStreamWriter writer, FilterModelImpl mod
FilterBuilder builder = model.getLibrary().getBuilder(filter);
writer.writeAttribute("builder", builder.getClass().getName());
writer.writeAttribute("filter", filter.getClass().getName());
writer.writeAttribute("name", query.getName());

//Params
for (int i = 0; i < query.getPropertiesCount(); i++) {
FilterProperty prop = query.getFilter().getProperties()[i];
writeParameter(writer, i, prop);
writeParameter(writer, i, prop, serialization);
}

writer.writeEndElement();
Expand All @@ -147,27 +147,20 @@ private void writeQuery(String code, XMLStreamWriter writer, FilterModelImpl mod
}
}

private void writeParameter(XMLStreamWriter writer, int index, FilterProperty property) {
private void writeParameter(XMLStreamWriter writer, int index, FilterProperty property, Serialization serialization) {
try {
PropertyEditor editor = property.getPropertyEditor();
if (editor == null) {
editor = PropertyEditorManager.findEditor(property.getValueType());
}
if (editor == null) {
return;
}
Object val = property.getValue();
editor.setValue(val);
writer.writeStartElement("parameter");
writer.writeAttribute("index", String.valueOf(index));
writer.writeCharacters(editor.getAsText());
writer.writeCharacters(serialization.toText(property.getValue(), property.getValueType()));
writer.writeEndElement();
} catch (Exception e) {
Exceptions.printStackTrace(e);
}
}

public void readXML(XMLStreamReader reader, FilterModelImpl model) throws XMLStreamException {
Serialization serialization = new Serialization(model.getGraphModel());

Map<Integer, Query> idMap = new HashMap<>();
boolean end = false;
while (reader.hasNext() && !end) {
Expand All @@ -180,7 +173,7 @@ public void readXML(XMLStreamReader reader, FilterModelImpl model) throws XMLStr
} else if ("query".equalsIgnoreCase(name)) {
String id = reader.getAttributeValue(null, "id");
String parent = reader.getAttributeValue(null, "parent");
Query query = readQuery(reader, model);
Query query = readQuery(reader, model, serialization);
if (query != null) {
idMap.put(Integer.parseInt(id), query);
if (parent != null) {
Expand All @@ -200,7 +193,7 @@ public void readXML(XMLStreamReader reader, FilterModelImpl model) throws XMLStr
} else if ("savedquery".equalsIgnoreCase(name)) {
String id = reader.getAttributeValue(null, "id");
String parent = reader.getAttributeValue(null, "parent");
Query query = readQuery(reader, model);
Query query = readQuery(reader, model, serialization);
if (query != null) {
idMap.put(Integer.parseInt(id), query);
if (parent != null) {
Expand Down Expand Up @@ -238,9 +231,10 @@ public void readXML(XMLStreamReader reader, FilterModelImpl model) throws XMLStr
}
}

private Query readQuery(XMLStreamReader reader, FilterModelImpl model) throws XMLStreamException {
private Query readQuery(XMLStreamReader reader, FilterModelImpl model, Serialization serialization) throws XMLStreamException {
String builderClassName = reader.getAttributeValue(null, "builder");
String filterClassName = reader.getAttributeValue(null, "filter");
String queryName = reader.getAttributeValue(null, "name");
FilterBuilder builder = null;
for (FilterBuilder fb : model.getLibrary().getLookup().lookupAll(FilterBuilder.class)) {
if (fb.getClass().getName().equals(builderClassName)) {
Expand All @@ -255,6 +249,7 @@ private Query readQuery(XMLStreamReader reader, FilterModelImpl model) throws XM
}
}
}

if (builder == null) {
for (CategoryBuilder catBuilder : Lookup.getDefault().lookupAll(CategoryBuilder.class)) {
for (FilterBuilder fb : catBuilder.getBuilders(model.getWorkspace())) {
Expand Down Expand Up @@ -283,6 +278,8 @@ private Query readQuery(XMLStreamReader reader, FilterModelImpl model) throws XM
query = new FilterQueryImpl(builder, filter);
}

query.setName(queryName);

FilterProperty property = null;
boolean end = false;
while (reader.hasNext() && !end) {
Expand All @@ -295,27 +292,10 @@ private Query readQuery(XMLStreamReader reader, FilterModelImpl model) throws XM
}
} else if (eventType.equals(XMLStreamReader.CHARACTERS) && property != null) {
try {
PropertyEditor editor = property.getPropertyEditor();
if (editor == null) {
editor = PropertyEditorManager.findEditor(property.getValueType());
}
if (editor != null) {
String textValue = reader.getText();

if (editor instanceof AttributeColumnPropertyEditor) {
GraphController gc = Lookup.getDefault().lookup(GraphController.class);
GraphModel graphModel = gc.getGraphModel(model.getWorkspace());
((AttributeColumnPropertyEditor) editor).setGraphModel(graphModel);
}

editor.setAsText(textValue);
property.setValue(editor.getValue());
model.updateParameters(query);

if (editor instanceof AttributeColumnPropertyEditor) {
((AttributeColumnPropertyEditor) editor).setGraphModel(null);
}
}
String textValue = reader.getText();
Object value = serialization.fromText(textValue, property.getValueType());
property.setValue(value);
model.updateParameters(query);
} catch (Exception e) {
Exceptions.printStackTrace(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public String getAsText() {
if (val != null) {
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
//FIXME: using java serialization is a bit dangerous, change this but keep compatibility with old saved files
try {
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public boolean evaluate(Graph graph, Edge edge) {
Object destValue = partition.getValue(edge.getTarget(), graph);
srcValue = srcValue == null ? NULL : srcValue;
destValue = destValue == null ? NULL : destValue;
return parts.contains(srcValue) && parts.contains(destValue) && !srcValue.equals(destValue);
return parts.contains(srcValue) && parts.contains(destValue) && srcValue.equals(destValue);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public boolean evaluate(Graph graph, Edge edge) {
Object destValue = partition.getValue(edge.getTarget(), graph);
srcValue = srcValue == null ? NULL : srcValue;
destValue = destValue == null ? NULL : destValue;
return parts.contains(srcValue) && parts.contains(destValue) && srcValue.equals(destValue);
return parts.contains(srcValue) && parts.contains(destValue) && !srcValue.equals(destValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public void writeXML(XMLStreamWriter writer) throws XMLStreamException {
String propertyName = property.getName();
Object propertyValue = property.getValue();
if (propertyValue != null) {
String text = Serialization.getValueAsText(propertyValue);
String text = Serialization.getValueAsText(propertyValue, property.getType());
if (text != null) {
writer.writeStartElement("previewproperty");
writer.writeAttribute("name", propertyName);
Expand All @@ -416,7 +416,7 @@ public void writeXML(XMLStreamWriter writer) throws XMLStreamException {
Object value = simpleValueEntry.getValue();
if (value != null) {
Class clazz = value.getClass();
String text = Serialization.getValueAsText(value);
String text = Serialization.getValueAsText(value, clazz);
if (text != null) {
writer.writeStartElement("previewsimplevalue");
writer.writeAttribute("name", simpleValueEntry.getKey());
Expand Down
Loading

0 comments on commit 736e5de

Please sign in to comment.