1 package org.simantics.document.linking.wizard;
4 import java.lang.reflect.InvocationTargetException;
6 import org.eclipse.core.runtime.IProgressMonitor;
7 import org.eclipse.core.runtime.IStatus;
8 import org.eclipse.jface.operation.IRunnableWithProgress;
9 import org.eclipse.jface.wizard.WizardPage;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.SelectionAdapter;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.swt.widgets.Label;
18 import org.eclipse.ui.PartInitException;
19 import org.simantics.Simantics;
20 import org.simantics.db.Resource;
21 import org.simantics.document.linking.report.ExportToPDF;
22 import org.simantics.document.linking.report.templates.ReportWriter;
23 import org.simantics.editors.Editors;
24 import org.simantics.utils.ui.ErrorLogger;
25 import org.simantics.utils.ui.ExceptionUtils;
29 * Wizard page for generating a report.
31 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
34 public class ReportGeneratePage extends WizardPage {
36 private Resource model;
37 private String filename;
38 private ReportWriter<?> reportWriter;
41 private Label fileLabel;
42 private Label reportLabel;
43 private Label statusLabel;
44 private Button generateButton;
45 private Button showButton;
46 private boolean generated = false;
48 protected ReportGeneratePage(String pageName) {
49 super(pageName,pageName,null);
53 public void setFilename(String filename) {
54 this.filename = filename;
58 public void setReportWriter(ReportWriter<?> reportWriter) {
59 this.reportWriter = reportWriter;
63 public void setModel(Resource model) {
69 public void createControl(Composite parent) {
70 Composite composite = new Composite(parent, SWT.NONE);
71 composite.setLayout(new GridLayout(2,false));
72 Label label = new Label(composite, SWT.NONE);
73 label.setText("File:");
74 fileLabel = new Label(composite, SWT.NONE);
75 label = new Label(composite, SWT.NONE);
76 label.setText("Report:");
77 reportLabel = new Label(composite, SWT.NONE);
78 label = new Label(composite, SWT.NONE);
79 label.setText("Status:");
80 this.statusLabel = new Label(composite, SWT.NONE);
81 this.statusLabel.setText("Report has not been generated");
82 generateButton = new Button(composite, SWT.PUSH);
83 generateButton.setText("Generate report");
84 generateButton.addSelectionListener(new SelectionAdapter() {
86 public void widgetSelected(SelectionEvent e) {
90 showButton = new Button(composite, SWT.PUSH);
91 showButton.setText("Show Report");
92 showButton.addSelectionListener(new SelectionAdapter() {
95 public void widgetSelected(SelectionEvent e) {
96 File file = new File(filename);
97 if (file.exists() && file.canRead()) {
99 Editors.openExternalEditor(file);
100 } catch (PartInitException err) {
101 ExceptionUtils.logAndShowError(err);
106 showButton.setEnabled(false);
108 setControl(composite);
111 private void updateContent() {
112 fileLabel.setText(filename == null ? "File has not been selected" : filename);
113 reportLabel.setText(reportWriter == null ? "Report Writer has not been selected" : reportWriter.getName());
114 generateButton.setEnabled(filename != null && reportWriter != null && model != null);
115 showButton.setEnabled(generated);
116 generateButton.setEnabled(!generated);
118 statusLabel.setText("Report has not been generated");
121 public void setGenerated(boolean b) {
123 setPageComplete(generated);
126 private void generate() {
127 generateButton.setEnabled(false);
128 statusLabel.setText("Generating report");
130 getWizard().getContainer().run(true, false, new IRunnableWithProgress() {
133 public void run(final IProgressMonitor monitor) throws InvocationTargetException,
134 InterruptedException {
136 ExportToPDF exportToPDF = new ExportToPDF(Simantics.getSession(), model);
137 final IStatus status = exportToPDF.export(filename, reportWriter, monitor);
138 Display.getDefault().asyncExec(new Runnable() {
140 if (!statusLabel.isDisposed()) {
142 statusLabel.setText(status.getMessage());
144 statusLabel.setText(status.getMessage());
145 ExceptionUtils.logError(status.getException());
147 setPageComplete(true);
148 ((Composite)getControl()).layout(true, true);
156 } catch (InterruptedException err) {
157 setErrorMessage("Report failed: " + err.getMessage());
158 ErrorLogger.defaultLogError("Report failed.",err);
159 statusLabel.setText("Report failed.");
160 } catch (InvocationTargetException e) {
161 Throwable err = e.getCause();
162 setErrorMessage("Report failed: " + err.getMessage());
163 ErrorLogger.defaultLogError("Report failed.",err);
164 statusLabel.setText("Report failed.");
168 showButton.setEnabled(true);
169 generateButton.setEnabled(false);
170 getContainer().updateButtons();
174 public void setVisible(boolean visible) {
180 super.setVisible(visible);
183 public boolean isGenerated() {