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