-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.modeling.ui.pdf;\r
-\r
-import java.io.IOException;\r
-import java.lang.reflect.InvocationTargetException;\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.Deque;\r
-import java.util.Iterator;\r
-import java.util.LinkedList;\r
-import java.util.List;\r
-import java.util.Set;\r
-import java.util.TreeSet;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.preferences.InstanceScope;\r
-import org.eclipse.jface.dialogs.MessageDialog;\r
-import org.eclipse.jface.operation.IRunnableWithProgress;\r
-import org.eclipse.jface.preference.IPersistentPreferenceStore;\r
-import org.eclipse.jface.preference.IPreferenceStore;\r
-import org.eclipse.jface.viewers.IFilter;\r
-import org.eclipse.jface.viewers.IStructuredSelection;\r
-import org.eclipse.jface.wizard.Wizard;\r
-import org.eclipse.ui.IExportWizard;\r
-import org.eclipse.ui.IMemento;\r
-import org.eclipse.ui.IWorkbench;\r
-import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
-import org.simantics.Simantics;\r
-import org.simantics.browsing.ui.graph.impl.request.GetName;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.NamedResource;\r
-import org.simantics.db.common.request.ObjectsWithType;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.request.ActiveModels;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.modeling.requests.Node;\r
-import org.simantics.modeling.ui.Activator;\r
-import org.simantics.modeling.ui.utils.NoProjectPage;\r
-import org.simantics.project.IProject;\r
-import org.simantics.project.ProjectKeys;\r
-import org.simantics.simulation.ontology.SimulationResource;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.ui.utils.ResourceAdaptionUtils;\r
-import org.simantics.utils.FileUtils;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-import org.simantics.utils.ui.ExceptionUtils;\r
-import org.simantics.utils.ui.workbench.StringMemento;\r
-\r
-public class PDFDiagramExportWizard extends Wizard implements IExportWizard {\r
-\r
- private static final int MAX_RECENT_EXPORT_PATHS = 10;\r
-\r
- private static final String TAG_PATH = "path";\r
-\r
- private static final String ATTR_NAME = "name";\r
-\r
- Deque<String> recentExportPaths;\r
- boolean zoomToFit;\r
- boolean attachTG, attachWiki;\r
-\r
- PDFExportPlan exportPlan;\r
-\r
- private boolean readPreferences() {\r
- IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
-\r
- String recentPathsPref = store.getString(Preferences.DIAGRAM_EXPORT_PDF_PATH);\r
- recentExportPaths = decodePaths(recentPathsPref);\r
- zoomToFit = store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ZOOM_TO_FIT);\r
- attachTG = store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_TG);\r
- attachWiki = store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_WIKI);\r
-\r
- return true;\r
- }\r
-\r
- private void writePreferences() throws IOException {\r
- IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
-\r
- store.putValue(Preferences.DIAGRAM_EXPORT_PDF_PATH, encodePaths(recentExportPaths));\r
- store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ZOOM_TO_FIT, String.valueOf(zoomToFit));\r
- store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_TG, String.valueOf(attachTG));\r
- store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_WIKI, String.valueOf(attachWiki));\r
-\r
- if (store.needsSaving())\r
- store.save();\r
- }\r
-\r
- private Deque<String> decodePaths(String recentPathsPref) {\r
- Deque<String> result = new LinkedList<String>();\r
- try {\r
- StringMemento sm = new StringMemento(recentPathsPref);\r
- for (IMemento m : sm.getChildren(TAG_PATH)) {\r
- String name = m.getString(ATTR_NAME);\r
- if (name != null && !name.isEmpty())\r
- result.add(name);\r
- }\r
- } catch (IllegalArgumentException e) {\r
- }\r
- return result;\r
- }\r
-\r
- private String encodePaths(Deque<String> recentPaths) {\r
- StringMemento sm = new StringMemento();\r
- for (String path : recentPaths) {\r
- IMemento m = sm.createChild(TAG_PATH);\r
- m.putString(ATTR_NAME, path);\r
- }\r
- return sm.toString();\r
- }\r
-\r
- public PDFDiagramExportWizard() {\r
- setWindowTitle("Export Diagrams to PDF");\r
- setNeedsProgressMonitor(true);\r
- }\r
-\r
- @Override\r
- public void addPages() {\r
- super.addPages();\r
- if (exportPlan != null) {\r
- addPage(new PDFExportPage(exportPlan));\r
- } else {\r
- addPage(new NoProjectPage("Export Diagrams to PDF"));\r
- }\r
- }\r
-\r
- private NamedResource toNamedResource(ReadGraph graph, Resource r) throws DatabaseException {\r
- String name = graph.syncRequest(new GetName(r));\r
- return new NamedResource(name, r);\r
- }\r
-\r
- @Override\r
- public void init(IWorkbench workbench, IStructuredSelection selection) {\r
- readPreferences();\r
-\r
- ISessionContext ctx = SimanticsUI.getSessionContext();\r
- if (ctx == null)\r
- return;\r
- IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);\r
- if (project == null)\r
- return;\r
-\r
- exportPlan = new PDFExportPlan(ctx, recentExportPaths);\r
- exportPlan.project = project;\r
- final Object selectedObject = selection.getFirstElement();\r
- exportPlan.fitContentToPageMargins = zoomToFit;\r
- exportPlan.attachTG = attachTG;\r
- exportPlan.attachWiki = attachWiki;\r
- \r
- // Get all model names\r
- try {\r
- exportPlan.sessionContext.getSession().syncRequest(new ReadRequest() {\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- Resource selection = ResourceAdaptionUtils.toSingleResource(selectedObject);\r
- if (selection != null) {\r
- //exportModel.selection = new NamedResource(name + " (input selection)", selection);\r
- exportPlan.selection = toNamedResource(graph, selection);\r
- exportPlan.selectableModels.add(exportPlan.selection);\r
- } else {\r
- for (Resource activeModel : graph.syncRequest(new ActiveModels(exportPlan.project.get()))) {\r
- selection = activeModel;\r
- exportPlan.selection = toNamedResource(graph, activeModel);\r
- exportPlan.selectableModels.add( exportPlan.selection );\r
- break;\r
- }\r
- }\r
-\r
- List<NamedResource> models = new ArrayList<NamedResource>();\r
- \r
- Collection<Resource> ontologies = Simantics.applySCL("Simantics/SharedOntologies", "traverseSharedOntologies", graph, graph.getRootLibrary());\r
- for (Resource model : ontologies) {\r
- if (model.equals(selection))\r
- continue;\r
- models.add( toNamedResource(graph, model) );\r
- }\r
- \r
- for (Resource model : graph.syncRequest(new ObjectsWithType(exportPlan.project.get(),\r
- Layer0.getInstance(graph).ConsistsOf, SimulationResource.getInstance(graph).Model))) {\r
- if (model.equals(selection))\r
- continue;\r
- models.add( toNamedResource(graph, model) );\r
- }\r
- Collections.sort(models);\r
- exportPlan.selectableModels.addAll(models);\r
- if (selection == null && !exportPlan.selectableModels.isEmpty()) {\r
- exportPlan.selection = exportPlan.selectableModels.get(0);\r
- }\r
- }\r
- });\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
-\r
- @Override\r
- public boolean performFinish() {\r
- if (exportPlan.exportLocation.exists()) {\r
- boolean confirmed = MessageDialog.openConfirm(getShell(), "Overwrite", "Are you sure you want to overwrite " + exportPlan.exportLocation);\r
- if (!confirmed)\r
- return false;\r
-\r
- try {\r
- FileUtils.deleteAll(exportPlan.exportLocation);\r
- } catch (IOException e) {\r
- ExceptionUtils.logAndShowError(e);\r
- return false;\r
- }\r
- }\r
-\r
- try {\r
- recentExportPaths.addFirst(exportPlan.exportLocation.getAbsolutePath());\r
-\r
- // Remove duplicates\r
- Set<String> dups = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);\r
- for (Iterator<String> it = recentExportPaths.iterator(); it.hasNext();) {\r
- String path = it.next();\r
- if (!dups.add(path)) {\r
- it.remove();\r
- }\r
- }\r
-\r
- if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS)\r
- recentExportPaths.pollLast();\r
-\r
- zoomToFit = exportPlan.fitContentToPageMargins;\r
- attachTG = exportPlan.attachTG;\r
- attachWiki = exportPlan.attachWiki;\r
-\r
- writePreferences();\r
- } catch (IOException e) {\r
- ErrorLogger.defaultLogError("Failed to write preferences", e);\r
- }\r
-\r
- // Make sure that the diagrams are printed in the same order as the user\r
- // saw them in the wizard.\r
- exportPlan.selectedNodes = exportPlan.nodes.depthFirstFlatten(new IFilter() {\r
- @Override\r
- public boolean select(Object toTest) {\r
- Node n = (Node) toTest;\r
- return exportPlan.selectedNodeSet.contains(n) && n.getDiagramResource() != null;\r
- }\r
- }, Node.CASE_INSENSITIVE_COMPARATOR);\r
-\r
- long start = System.currentTimeMillis();\r
- try {\r
- getContainer().run(true, true, new IRunnableWithProgress() {\r
- @Override\r
- public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
- try {\r
- // Print pdf\r
- DiagramPrinter.printToPdf(monitor, exportPlan, exportPlan.exportLocation.toString(), exportPlan.selectedNodes, exportPlan.sessionContext);\r
- \r
- } catch (PdfException e) {\r
- throw new InvocationTargetException(e);\r
- } finally {\r
- monitor.done();\r
- }\r
- }\r
- });\r
- } catch (InvocationTargetException e) {\r
- Throwable t = e.getTargetException();\r
- ExceptionUtils.logAndShowError(t);\r
- return false;\r
- } catch (InterruptedException e) {\r
- return false;\r
- }\r
- long end = System.currentTimeMillis();\r
- System.out.println("PDF export took " + ((end - start) * 1e-3) + " seconds.");\r
-\r
- return true;\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.ui.pdf;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Deque;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.jface.preference.IPersistentPreferenceStore;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.jface.viewers.IFilter;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.ui.IExportWizard;
+import org.eclipse.ui.IMemento;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.preferences.ScopedPreferenceStore;
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.graph.impl.request.GetName;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.request.ObjectsWithType;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.ActiveModels;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.requests.Node;
+import org.simantics.modeling.ui.Activator;
+import org.simantics.modeling.ui.utils.NoProjectPage;
+import org.simantics.project.IProject;
+import org.simantics.project.ProjectKeys;
+import org.simantics.simulation.ontology.SimulationResource;
+import org.simantics.ui.SimanticsUI;
+import org.simantics.ui.utils.ResourceAdaptionUtils;
+import org.simantics.utils.FileUtils;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.ExceptionUtils;
+import org.simantics.utils.ui.workbench.StringMemento;
+
+public class PDFDiagramExportWizard extends Wizard implements IExportWizard {
+
+ private static final int MAX_RECENT_EXPORT_PATHS = 10;
+
+ private static final String TAG_PATH = "path";
+
+ private static final String ATTR_NAME = "name";
+
+ Deque<String> recentExportPaths;
+ boolean zoomToFit;
+ boolean attachTG, attachWiki;
+
+ PDFExportPlan exportPlan;
+
+ private boolean readPreferences() {
+ IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
+
+ String recentPathsPref = store.getString(Preferences.DIAGRAM_EXPORT_PDF_PATH);
+ recentExportPaths = decodePaths(recentPathsPref);
+ zoomToFit = store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ZOOM_TO_FIT);
+ attachTG = store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_TG);
+ attachWiki = store.getBoolean(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_WIKI);
+
+ return true;
+ }
+
+ private void writePreferences() throws IOException {
+ IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
+
+ store.putValue(Preferences.DIAGRAM_EXPORT_PDF_PATH, encodePaths(recentExportPaths));
+ store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ZOOM_TO_FIT, String.valueOf(zoomToFit));
+ store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_TG, String.valueOf(attachTG));
+ store.putValue(Preferences.DIAGRAM_EXPORT_PDF_ATTACH_WIKI, String.valueOf(attachWiki));
+
+ if (store.needsSaving())
+ store.save();
+ }
+
+ private Deque<String> decodePaths(String recentPathsPref) {
+ Deque<String> result = new LinkedList<String>();
+ try {
+ StringMemento sm = new StringMemento(recentPathsPref);
+ for (IMemento m : sm.getChildren(TAG_PATH)) {
+ String name = m.getString(ATTR_NAME);
+ if (name != null && !name.isEmpty())
+ result.add(name);
+ }
+ } catch (IllegalArgumentException e) {
+ }
+ return result;
+ }
+
+ private String encodePaths(Deque<String> recentPaths) {
+ StringMemento sm = new StringMemento();
+ for (String path : recentPaths) {
+ IMemento m = sm.createChild(TAG_PATH);
+ m.putString(ATTR_NAME, path);
+ }
+ return sm.toString();
+ }
+
+ public PDFDiagramExportWizard() {
+ setWindowTitle("Export Diagrams to PDF");
+ setNeedsProgressMonitor(true);
+ }
+
+ @Override
+ public void addPages() {
+ super.addPages();
+ if (exportPlan != null) {
+ addPage(new PDFExportPage(exportPlan));
+ } else {
+ addPage(new NoProjectPage("Export Diagrams to PDF"));
+ }
+ }
+
+ private NamedResource toNamedResource(ReadGraph graph, Resource r) throws DatabaseException {
+ String name = graph.syncRequest(new GetName(r));
+ return new NamedResource(name, r);
+ }
+
+ @Override
+ public void init(IWorkbench workbench, IStructuredSelection selection) {
+ readPreferences();
+
+ ISessionContext ctx = SimanticsUI.getSessionContext();
+ if (ctx == null)
+ return;
+ IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
+ if (project == null)
+ return;
+
+ exportPlan = new PDFExportPlan(ctx, recentExportPaths);
+ exportPlan.project = project;
+ final Object selectedObject = selection.getFirstElement();
+ exportPlan.fitContentToPageMargins = zoomToFit;
+ exportPlan.attachTG = attachTG;
+ exportPlan.attachWiki = attachWiki;
+
+ // Get all model names
+ try {
+ exportPlan.sessionContext.getSession().syncRequest(new ReadRequest() {
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ Resource selection = ResourceAdaptionUtils.toSingleResource(selectedObject);
+ if (selection != null) {
+ //exportModel.selection = new NamedResource(name + " (input selection)", selection);
+ exportPlan.selection = toNamedResource(graph, selection);
+ exportPlan.selectableModels.add(exportPlan.selection);
+ } else {
+ for (Resource activeModel : graph.syncRequest(new ActiveModels(exportPlan.project.get()))) {
+ selection = activeModel;
+ exportPlan.selection = toNamedResource(graph, activeModel);
+ exportPlan.selectableModels.add( exportPlan.selection );
+ break;
+ }
+ }
+
+ List<NamedResource> models = new ArrayList<NamedResource>();
+
+ Collection<Resource> ontologies = Simantics.applySCL("Simantics/SharedOntologies", "traverseSharedOntologies", graph, graph.getRootLibrary());
+ for (Resource model : ontologies) {
+ if (model.equals(selection))
+ continue;
+ models.add( toNamedResource(graph, model) );
+ }
+
+ for (Resource model : graph.syncRequest(new ObjectsWithType(exportPlan.project.get(),
+ Layer0.getInstance(graph).ConsistsOf, SimulationResource.getInstance(graph).Model))) {
+ if (model.equals(selection))
+ continue;
+ models.add( toNamedResource(graph, model) );
+ }
+ Collections.sort(models);
+ exportPlan.selectableModels.addAll(models);
+ if (selection == null && !exportPlan.selectableModels.isEmpty()) {
+ exportPlan.selection = exportPlan.selectableModels.get(0);
+ }
+ }
+ });
+ } catch (DatabaseException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public boolean performFinish() {
+ if (exportPlan.exportLocation.exists()) {
+ boolean confirmed = MessageDialog.openConfirm(getShell(), "Overwrite", "Are you sure you want to overwrite " + exportPlan.exportLocation);
+ if (!confirmed)
+ return false;
+
+ try {
+ FileUtils.deleteAll(exportPlan.exportLocation);
+ } catch (IOException e) {
+ ExceptionUtils.logAndShowError(e);
+ return false;
+ }
+ }
+
+ try {
+ recentExportPaths.addFirst(exportPlan.exportLocation.getAbsolutePath());
+
+ // Remove duplicates
+ Set<String> dups = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
+ for (Iterator<String> it = recentExportPaths.iterator(); it.hasNext();) {
+ String path = it.next();
+ if (!dups.add(path)) {
+ it.remove();
+ }
+ }
+
+ if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS)
+ recentExportPaths.pollLast();
+
+ zoomToFit = exportPlan.fitContentToPageMargins;
+ attachTG = exportPlan.attachTG;
+ attachWiki = exportPlan.attachWiki;
+
+ writePreferences();
+ } catch (IOException e) {
+ ErrorLogger.defaultLogError("Failed to write preferences", e);
+ }
+
+ // Make sure that the diagrams are printed in the same order as the user
+ // saw them in the wizard.
+ exportPlan.selectedNodes = exportPlan.nodes.depthFirstFlatten(new IFilter() {
+ @Override
+ public boolean select(Object toTest) {
+ Node n = (Node) toTest;
+ return exportPlan.selectedNodeSet.contains(n) && n.getDiagramResource() != null;
+ }
+ }, Node.CASE_INSENSITIVE_COMPARATOR);
+
+ long start = System.currentTimeMillis();
+ try {
+ getContainer().run(true, true, new IRunnableWithProgress() {
+ @Override
+ public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+ try {
+ // Print pdf
+ DiagramPrinter.printToPdf(monitor, exportPlan, exportPlan.exportLocation.toString(), exportPlan.selectedNodes, exportPlan.sessionContext);
+
+ } catch (PdfException e) {
+ throw new InvocationTargetException(e);
+ } finally {
+ monitor.done();
+ }
+ }
+ });
+ } catch (InvocationTargetException e) {
+ Throwable t = e.getTargetException();
+ ExceptionUtils.logAndShowError(t);
+ return false;
+ } catch (InterruptedException e) {
+ return false;
+ }
+ long end = System.currentTimeMillis();
+ System.out.println("PDF export took " + ((end - start) * 1e-3) + " seconds.");
+
+ return true;
+ }
+
+}