+ Composite buttonBarsComposite = new Composite(parent, SWT.NONE);
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonBarsComposite);
+ GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonBarsComposite);
+
+ Button saveVisualisationTemplateAsButton = new Button(buttonBarsComposite, SWT.NONE);
+ saveVisualisationTemplateAsButton.setText("Save as visualisation template");
+ saveVisualisationTemplateAsButton.addSelectionListener(new SelectionAdapter() {
+
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ showSaveVisualisationTemplateDialog(e.widget.getDisplay().getActiveShell());
+ }
+ });
+
+ removeVisualisationTemplateButton = new Button(buttonBarsComposite, SWT.NONE);
+ removeVisualisationTemplateButton.setText("Remove");
+ removeVisualisationTemplateButton.setEnabled(visualisation != null && visualisation.getVisualisationResource() != null);
+ removeVisualisationTemplateButton.addSelectionListener(new SelectionAdapter() {
+
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ removeVisualisationTemplate(visualisation.getName(), Optional.of(visualisation.getVisualisationResource()));
+ }
+ });
+
+ }
+
+ protected void removeVisualisationTemplate(String name, Optional<Resource> of) {
+ if (of.isPresent()) {
+ Resource visualisation = of.get();
+ Simantics.getSession().asyncRequest(new WriteRequest() {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ RemoverUtil.remove(graph, visualisation);
+ }
+ });
+ }
+ }
+
+ private void showSaveVisualisationTemplateDialog(Shell shell) {
+
+ InputDialog dialog = new InputDialog(shell, "Save visualisation template", "Give template a name", "", new IInputValidator() {
+
+ @Override
+ public String isValid(String newText) {
+ if (newText == null || newText.isEmpty())
+ return "Name cannot be empty";
+ return null;
+ }
+ });
+
+ if (dialog.open() == Dialog.OK) {
+ String name = dialog.getValue();
+ try {
+ persistVisualisationTemplate(name, Optional.empty());
+ } catch (Exception e) {
+ LOGGER.error("Could not persist visualisation template", e);
+ ShowError.showError("Could not persist visualisation template", e.getMessage(), e);
+ }
+ }
+ }
+
+ private void persistCurrentVisualisationTemplateIfAvailable() {
+ if (visualisation != null) {
+ try {
+ persistVisualisationTemplate(visualisation.getName(), Optional.of(visualisation.getVisualisationResource()));
+ } catch (Exception e1) {
+ LOGGER.error("Could not persist visualisation template", e1);
+ ShowError.showError("Could not persist visualisation template", e1.getMessage(), e1);
+ }
+ } else {
+ LOGGER.info("No current visualisation template selected for saving");
+ }
+ }
+
+ private void persistVisualisationTemplate(String templateName, Optional<Resource> existing) throws Exception {
+
+ List<Pair<String, DynamicColorContribution>> colorCollect = colorSuppliers.stream().map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList());
+
+ String colorLocation = colorLocationCombo.getItem(colorLocationCombo.getSelectionIndex());
+ String colorSize = colorSizeCombo.getItem(colorSizeCombo.getSelectionIndex());
+
+ ColorBarOptions colorBarOptions = new ColorBarOptions()
+ .showColorBars(showColorButton.getSelection())
+ .showColorBarsTicks(colorTicksButton.getSelection())
+ .useGradients(colorGradientButton.getSelection())
+ .withLocation(ColorBarsLocation.valueOf(colorLocation))
+ .withSize(ColorBarsSize.valueOf(colorSize));
+
+ List<Pair<String, DynamicSizeContribution>> sizeCollect = sizeSuppliers.stream().map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList());
+
+ String sizeLocation = sizeLocationCombo.getItem(sizeLocationCombo.getSelectionIndex());
+ String sizeSize = sizeSizeCombo.getItem(sizeSizeCombo.getSelectionIndex());
+
+ SizeBarOptions sizeBarOptions = new SizeBarOptions()
+ .showSizeBars(showSizeButton.getSelection())
+ .showSizeBarsTicks(sizeTicksButton.getSelection())
+ .useGradients(sizeGradientButton.getSelection())
+ .withLocation(SizeBarsLocation.valueOf(sizeLocation))
+ .withSize(SizeBarsSize.valueOf(sizeSize));
+
+ Simantics.getSession().asyncRequest(new WriteRequest() {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ Resource exist;
+ if (existing.isPresent()) {
+ exist = existing.get();
+ } else {
+ exist = DistrictNetworkUtil.createVisualisation(graph, parentResource, templateName);
+ }
+
+ DistrictNetworkUtil.setColorContributions(graph, exist, colorCollect);
+
+ DistrictNetworkUtil.setColorBarOptions(graph, exist, colorBarOptions);
+ DistrictNetworkUtil.setSizeContributions(graph, exist, sizeCollect);
+ DistrictNetworkUtil.setSizeBarOptions(graph, exist, sizeBarOptions);
+ }
+ });