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);
}
}
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);
}
}
}
}
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++;
}
}
}
}
}
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;
}
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);
}
}
Aggregations