/* Copyright 2008-2010 Gephi Authors : Mathieu Bastian <mathieu.bastian@gephi.org> Website : http://www.gephi.org This file is part of Gephi. Gephi is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Gephi 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 Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with Gephi. If not, see <http://www.gnu.org/licenses/>. */ package org.gephi.filters.plugin.graph; import java.util.ArrayList; import java.util.List; import javax.swing.Icon; import javax.swing.JPanel; import org.gephi.filters.plugin.RangeFilter; import org.gephi.filters.api.FilterLibrary; import org.gephi.filters.api.Range; import org.gephi.filters.spi.Category; import org.gephi.filters.spi.Filter; import org.gephi.filters.spi.FilterBuilder; import org.gephi.filters.spi.FilterProperty; import org.gephi.filters.spi.NodeFilter; import org.gephi.graph.api.DirectedGraph; import org.gephi.graph.api.Graph; import org.gephi.graph.api.GraphController; import org.gephi.graph.api.GraphModel; import org.gephi.graph.api.HierarchicalDirectedGraph; import org.gephi.graph.api.HierarchicalGraph; import org.gephi.graph.api.Node; import org.openide.util.Exceptions; import org.openide.util.Lookup; import org.openide.util.NbBundle; import org.openide.util.lookup.ServiceProvider; /** * * @author Mathieu Bastian */ @ServiceProvider(service = FilterBuilder.class) public class MutualDegreeRangeBuilder implements FilterBuilder { public Category getCategory() { return FilterLibrary.TOPOLOGY; } public String getName() { return NbBundle.getMessage(MutualDegreeRangeBuilder.class, "MutualDegreeRangeBuilder.name"); } public Icon getIcon() { return null; } public String getDescription() { return NbBundle.getMessage(MutualDegreeRangeBuilder.class, "MutualDegreeRangeBuilder.description"); } public MutualDegreeRangeFilter getFilter() { return new MutualDegreeRangeFilter(); } public JPanel getPanel(Filter filter) { RangeUI ui = Lookup.getDefault().lookup(RangeUI.class); if (ui != null) { return ui.getPanel((MutualDegreeRangeFilter) filter); } return null; } public void destroy(Filter filter) { } public static class MutualDegreeRangeFilter implements RangeFilter, NodeFilter { private Integer min = 0; private Integer max = 0; private Range range; //States private List<Integer> values; public String getName() { return NbBundle.getMessage(MutualDegreeRangeFilter.class, "MutualDegreeRangeBuilder.name"); } private void refreshRange() { if (range == null) { range = new Range(min, max); } else { range.trimBounds(min, max); } } public boolean init(Graph graph) { if (!(graph instanceof DirectedGraph)) { return false; } if (range == null) { getValues(); refreshRange(); } values = new ArrayList<Integer>(((HierarchicalGraph)graph).getNodeCount()); min = Integer.MAX_VALUE; max = Integer.MIN_VALUE; return true; } public boolean evaluate(Graph graph, Node node) { int degree = ((HierarchicalDirectedGraph) graph).getMutualDegree(node); min = Math.min(min, degree); max = Math.max(max, degree); values.add(new Integer(degree)); return range.isInRange(degree); } public void finish() { refreshRange(); } public Object[] getValues() { if (values == null) { GraphModel gm = Lookup.getDefault().lookup(GraphController.class).getModel(); HierarchicalDirectedGraph hgraph = gm.getHierarchicalDirectedGraph(); Integer[] degrees = new Integer[hgraph.getNodeCount()]; int i = 0; min = Integer.MAX_VALUE; max = Integer.MIN_VALUE; for (Node n : hgraph.getNodes()) { int degree = hgraph.getMutualDegree(n); min = Math.min(min, degree); max = Math.max(max, degree); degrees[i++] = degree; } refreshRange(); return degrees; } else { return values.toArray(new Integer[0]); } } public FilterProperty[] getProperties() { try { return new FilterProperty[]{ FilterProperty.createProperty(this, Range.class, "range") }; } catch (Exception ex) { Exceptions.printStackTrace(ex); } return new FilterProperty[0]; } public FilterProperty getRangeProperty() { return getProperties()[0]; } public Object getMinimum() { return min; } public Object getMaximum() { return max; } public Range getRange() { return range; } public void setRange(Range range) { this.range = range; } } }