Search in sources :

Example 26 with Element

use of org.gephi.graph.api.Element in project gephi by gephi.

the class SearchReplaceControllerImpl method replace.

@Override
public SearchResult replace(SearchResult result, String replacement) {
    if (result == null) {
        throw new IllegalArgumentException();
    }
    if (!canReplace(result)) {
        //Go to next search result
        return findNext(result);
    }
    GraphController gc = Lookup.getDefault().lookup(GraphController.class);
    Object value;
    String str;
    Element attributes;
    Column column;
    if (!result.getSearchOptions().isUseRegexReplaceMode()) {
        //Avoid using groups and other regex aspects in the replacement
        replacement = Matcher.quoteReplacement(replacement);
    }
    try {
        //Get value to re-match and replace:
        if (result.getFoundNode() != null) {
            attributes = result.getFoundNode();
            column = gc.getGraphModel().getNodeTable().getColumn(result.getFoundColumnIndex());
        } else {
            attributes = result.getFoundEdge();
            column = gc.getGraphModel().getEdgeTable().getColumn(result.getFoundColumnIndex());
        }
        GraphModel graphModel = column.getTable().getGraph().getModel();
        TimeFormat timeFormat = graphModel.getTimeFormat();
        DateTimeZone timeZone = graphModel.getTimeZone();
        value = attributes.getAttribute(column);
        str = value != null ? AttributeUtils.print(value, timeFormat, timeZone) : "";
        StringBuffer sb = new StringBuffer();
        //Match and replace the result:
        Matcher matcher = result.getSearchOptions().getRegexPattern().matcher(str.substring(result.getStart()));
        if (matcher.find()) {
            matcher.appendReplacement(sb, replacement);
            int replaceLong = sb.length();
            matcher.appendTail(sb);
            str = str.substring(0, result.getStart()) + sb.toString();
            result.getSearchOptions().setRegionStart(result.getStart() + replaceLong);
            Lookup.getDefault().lookup(AttributeColumnsController.class).setAttributeValue(str, attributes, column);
            //Go to next search result
            return findNext(result);
        } else {
            //Go to next search result
            return findNext(result);
        }
    } catch (Exception ex) {
        if (ex instanceof IndexOutOfBoundsException) {
            //Rethrow the exception when it is caused by a bad regex replacement
            throw new IndexOutOfBoundsException();
        }
        //Go to next search result
        return findNext(result);
    }
}
Also used : TimeFormat(org.gephi.graph.api.TimeFormat) Matcher(java.util.regex.Matcher) Element(org.gephi.graph.api.Element) DateTimeZone(org.joda.time.DateTimeZone) Column(org.gephi.graph.api.Column) GraphModel(org.gephi.graph.api.GraphModel) AttributeColumnsController(org.gephi.datalab.api.AttributeColumnsController) GraphController(org.gephi.graph.api.GraphController)

Example 27 with Element

use of org.gephi.graph.api.Element in project gephi by gephi.

the class AppearanceControllerImpl method transform.

@Override
public void transform(Function function) {
    if (model != null) {
        GraphModel graphModel = model.getGraphModel();
        Graph graph = graphModel.getGraphVisible();
        ElementIterable<? extends Element> iterable;
        if (function.getElementClass().equals(Node.class)) {
            iterable = graph.getNodes();
        } else {
            iterable = graph.getEdges();
        }
        try {
            for (Element element : iterable) {
                function.transform(element, graph);
            }
        } catch (Exception e) {
            iterable.doBreak();
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : Graph(org.gephi.graph.api.Graph) GraphModel(org.gephi.graph.api.GraphModel) Element(org.gephi.graph.api.Element)

Example 28 with Element

use of org.gephi.graph.api.Element in project gephi by gephi.

the class AttributePartitionImpl method refresh.

@Override
protected void refresh() {
    if (graph != null) {
        parts.clear();
        elements = 0;
        ElementIterable<? extends Element> iterable = AttributeUtils.isNodeColumn(column) ? graph.getNodes() : graph.getEdges();
        for (Element el : iterable) {
            TimeMap val = (TimeMap) el.getAttribute(column);
            if (val != null) {
                Object[] va = val.toValuesArray();
                for (Object v : va) {
                    Integer count = parts.get(v);
                    if (count == null) {
                        count = 0;
                    }
                    parts.put(v, ++count);
                    elements++;
                }
            }
        }
    }
}
Also used : Element(org.gephi.graph.api.Element) TimeMap(org.gephi.graph.api.types.TimeMap)

Example 29 with Element

use of org.gephi.graph.api.Element in project gephi by gephi.

the class AttributeRankingImpl method refreshNotIndexed.

protected void refreshNotIndexed(ElementIterable<? extends Element> iterable) {
    double minN = Double.POSITIVE_INFINITY;
    double maxN = Double.NEGATIVE_INFINITY;
    for (Element el : iterable) {
        double num = ((Number) el.getAttribute(column)).doubleValue();
        if (num < minN) {
            minN = num;
        }
        if (num > maxN) {
            maxN = num;
        }
    }
    min = minN;
    max = maxN;
}
Also used : Element(org.gephi.graph.api.Element)

Example 30 with Element

use of org.gephi.graph.api.Element in project gephi by gephi.

the class DataTableTopComponent method exportTableAsCSV.

/**
     * <p>Exports a AttributeTable to a CSV file showing first a dialog to select the
     * file to write.</p>
     *
     * @param parent Parent window
     * @param visibleOnly Show only visible graph
     * @param table Table to export
     * @param separator Separator to use for separating values of a row in the
     * CSV file. If null ',' will be used.
     * @param charset Charset encoding for the file
     * @param columnsToExport Indicates the indexes of the columns to export.
     * All columns will be exported if null
     */
public static void exportTableAsCSV(JComponent parent, boolean visibleOnly, Table table, boolean edgesTable, Character separator, Charset charset, Integer[] columnsToExport, String fileName) {
    //Validate that at least 1 column is selected:
    if (columnsToExport.length < 1) {
        return;
    }
    String lastPath = NbPreferences.forModule(JTableCSVExporter.class).get(LAST_PATH, null);
    final JFileChooser chooser = new JFileChooser(lastPath);
    chooser.setAcceptAllFileFilterUsed(false);
    DialogFileFilter dialogFileFilter = new DialogFileFilter(NbBundle.getMessage(DataTableTopComponent.class, "TableCSVExporter.filechooser.csvDescription"));
    dialogFileFilter.addExtension("csv");
    chooser.addChoosableFileFilter(dialogFileFilter);
    File selectedFile = new File(chooser.getCurrentDirectory(), fileName);
    chooser.setSelectedFile(selectedFile);
    int returnFile = chooser.showSaveDialog(null);
    if (returnFile != JFileChooser.APPROVE_OPTION) {
        return;
    }
    File file = chooser.getSelectedFile();
    if (!file.getPath().endsWith(".csv")) {
        file = new File(file.getPath() + ".csv");
    }
    //Save last path
    String defaultDirectory = file.getParentFile().getAbsolutePath();
    NbPreferences.forModule(JTableCSVExporter.class).put(LAST_PATH, defaultDirectory);
    try {
        Element[] rows;
        Graph graph;
        if (visibleOnly) {
            graph = Lookup.getDefault().lookup(GraphController.class).getGraphModel().getGraphVisible();
        } else {
            graph = Lookup.getDefault().lookup(GraphController.class).getGraphModel().getGraph();
        }
        if (edgesTable) {
            rows = graph.getEdges().toArray();
        } else {
            rows = graph.getNodes().toArray();
        }
        AttributeTableCSVExporter.writeCSVFile(graph, table, file, separator, charset, columnsToExport, rows);
        JOptionPane.showMessageDialog(parent, NbBundle.getMessage(DataTableTopComponent.class, "TableCSVExporter.dialog.success"));
    } catch (Exception ex) {
        Logger.getLogger("").log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(parent, NbBundle.getMessage(DataTableTopComponent.class, "TableCSVExporter.dialog.error"), NbBundle.getMessage(DataTableTopComponent.class, "TableCSVExporter.dialog.error.title"), JOptionPane.ERROR_MESSAGE);
    }
}
Also used : JTableCSVExporter(org.gephi.utils.JTableCSVExporter) Graph(org.gephi.graph.api.Graph) JFileChooser(javax.swing.JFileChooser) Element(org.gephi.graph.api.Element) DialogFileFilter(org.gephi.ui.utils.DialogFileFilter) File(java.io.File) GraphController(org.gephi.graph.api.GraphController)

Aggregations

Element (org.gephi.graph.api.Element)30 Column (org.gephi.graph.api.Column)19 AttributeColumnsController (org.gephi.datalab.api.AttributeColumnsController)15 BigDecimal (java.math.BigDecimal)8 GraphController (org.gephi.graph.api.GraphController)4 TimeFormat (org.gephi.graph.api.TimeFormat)4 DateTimeZone (org.joda.time.DateTimeZone)4 Matcher (java.util.regex.Matcher)3 GraphModel (org.gephi.graph.api.GraphModel)3 TimeMap (org.gephi.graph.api.types.TimeMap)3 ArrayList (java.util.ArrayList)2 Graph (org.gephi.graph.api.Graph)2 Interval (org.gephi.graph.api.Interval)2 IntervalSet (org.gephi.graph.api.types.IntervalSet)2 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 JFileChooser (javax.swing.JFileChooser)1 Index (org.gephi.graph.api.Index)1 Node (org.gephi.graph.api.Node)1