use of org.gephi.preview.api.PreviewPreset in project gephi by gephi.
the class PreviewUIControllerImpl method savePreset.
@Override
public void savePreset(String name) {
if (model != null) {
PreviewModel previewModel = previewController.getModel();
Map<String, Object> map = new HashMap<>();
for (PreviewProperty p : previewModel.getProperties().getProperties()) {
map.put(p.getName(), p.getValue());
}
for (Entry<String, Object> p : previewModel.getProperties().getSimpleValues()) {
map.put(p.getKey(), p.getValue());
}
PreviewPreset preset = new PreviewPreset(name, map);
presetUtils.savePreset(preset);
model.setCurrentPreset(preset);
}
}
use of org.gephi.preview.api.PreviewPreset in project gephi by gephi.
the class PresetUtils method readXML.
private PreviewPreset readXML(Document document) {
// For retrieving property class if it is not in the xml (old serialization)
DefaultPreset defaultPreset = new DefaultPreset();
Element presetE = document.getDocumentElement();
Map<String, Object> propertiesMap = new HashMap<>();
String presetName = presetE.getAttribute("name");
NodeList propertyList = presetE.getElementsByTagName("previewproperty");
for (int i = 0; i < propertyList.getLength(); i++) {
Node n = propertyList.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element propertyE = (Element) n;
String name = propertyE.getAttribute("name");
String valueClassName = propertyE.hasAttribute(name) ? propertyE.getAttribute("class") : null;
String stringValue = propertyE.getTextContent();
Class valueClass = null;
if (valueClassName != null) {
try {
valueClass = Class.forName(valueClassName);
} catch (ClassNotFoundException ex) {
Exceptions.printStackTrace(ex);
}
} else {
Object defaultValue = defaultPreset.getProperties().get(name);
if (defaultValue != null) {
valueClass = defaultValue.getClass();
}
}
if (valueClass != null) {
Object value = PreviewProperties.readValueFromText(stringValue, valueClass);
if (value != null) {
propertiesMap.put(name, value);
}
}
}
}
return new PreviewPreset(presetName, propertiesMap);
}
use of org.gephi.preview.api.PreviewPreset in project gephi by gephi.
the class PreviewSettingsTopComponent method setup.
public void setup(PreviewUIModel previewModel) {
propertySheet.setNodes(new Node[] { new PreviewNode(propertySheet) });
PreviewUIController previewUIController = Lookup.getDefault().lookup(PreviewUIController.class);
if (previewModel != null) {
ratioSlider.setValue((int) (previewModel.getVisibilityRatio() * 100));
}
// Presets
if (previewModel == null) {
saveButton.setEnabled(false);
labelPreset.setEnabled(false);
presetComboBox.setEnabled(false);
presetComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "---" }));
} else {
saveButton.setEnabled(true);
labelPreset.setEnabled(true);
presetComboBox.setEnabled(true);
DefaultComboBoxModel comboBoxModel = new DefaultComboBoxModel();
defaultPresetLimit = 0;
for (PreviewPreset preset : previewUIController.getDefaultPresets()) {
comboBoxModel.addElement(preset);
defaultPresetLimit++;
}
PreviewPreset[] userPresets = previewUIController.getUserPresets();
if (userPresets.length > 0) {
comboBoxModel.addElement(NO_SELECTION);
for (PreviewPreset preset : userPresets) {
comboBoxModel.addElement(preset);
}
}
presetComboBox.setSelectedItem(previewModel.getCurrentPreset());
presetComboBox.setModel(comboBoxModel);
}
// Refresh tabs
int tabCount = tabbedPane.getTabCount();
for (int i = 2; i < tabCount; i++) {
// Start at 2, not removing settings and renderer manager tabs
tabbedPane.removeTabAt(i);
}
for (PreviewUI pui : Lookup.getDefault().lookupAll(PreviewUI.class)) {
pui.unsetup();
}
if (previewModel != null) {
PreviewController previewController = Lookup.getDefault().lookup(PreviewController.class);
PreviewModel pModel = previewController.getModel();
// Add new tabs
for (PreviewUI pui : Lookup.getDefault().lookupAll(PreviewUI.class)) {
pui.setup(pModel);
JPanel pluginPanel = pui.getPanel();
if (UIUtils.isAquaLookAndFeel()) {
pluginPanel.setBackground(UIManager.getColor("NbExplorerView.background"));
}
if (pui.getIcon() != null) {
tabbedPane.addTab(pui.getPanelTitle(), pui.getIcon(), pluginPanel);
} else {
tabbedPane.addTab(pui.getPanelTitle(), pluginPanel);
}
}
}
}
use of org.gephi.preview.api.PreviewPreset in project gephi by gephi.
the class PresetUtils method savePreset.
public void savePreset(PreviewPreset preset) {
int exist = -1;
for (int i = 0; i < presets.size(); i++) {
PreviewPreset p = presets.get(i);
if (p.getName().equals(preset.getName())) {
exist = i;
break;
}
}
if (exist == -1) {
addPreset(preset);
} else {
presets.set(exist, preset);
}
try {
// Create file if dont exist
FileObject folder = FileUtil.getConfigFile("previewpresets");
if (folder == null) {
folder = FileUtil.getConfigRoot().createFolder("previewpresets");
}
// Safe filename
String filename = DigestUtils.sha1Hex(preset.getName());
FileObject presetFile = folder.getFileObject(filename, "xml");
if (presetFile == null) {
presetFile = folder.createData(filename, "xml");
}
// Create doc
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
final Document document = documentBuilder.newDocument();
document.setXmlVersion("1.0");
document.setXmlStandalone(true);
// Write doc
writeXML(document, preset);
// Write XML file
try (OutputStream outputStream = presetFile.getOutputStream()) {
Source source = new DOMSource(document);
Result result = new StreamResult(outputStream);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);
}
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
}
use of org.gephi.preview.api.PreviewPreset in project gephi by gephi.
the class PresetUtils method loadPresets.
private void loadPresets() {
FileObject folder = FileUtil.getConfigFile("previewpresets");
if (folder != null) {
for (FileObject child : folder.getChildren()) {
if (child.isValid() && child.hasExt("xml")) {
try (InputStream stream = child.getInputStream()) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(stream);
PreviewPreset preset = readXML(document);
addPreset(preset);
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
}
}
}
}
Aggregations