]> gerrit.simantics Code Review - simantics/platform.git/blob
4db863f2af714f72c31f5c6ab96d960509208348
[simantics/platform.git] /
1 package org.simantics.document.linking.wizard;
2
3 import java.io.File;
4 import java.lang.reflect.InvocationTargetException;
5
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;
26
27
28 /**
29  * Wizard page for generating a report.
30  * 
31  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
32  *
33  */
34 public class ReportGeneratePage extends WizardPage {
35         
36         private Resource model;
37         private String filename;
38         private ReportWriter<?> reportWriter;
39         
40         
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;
47         
48         protected ReportGeneratePage(String pageName) {
49                 super(pageName,pageName,null);
50                 setGenerated(false);
51         }
52         
53         public void setFilename(String filename) {
54                 this.filename = filename;
55                 setGenerated(false);
56         }
57         
58         public void setReportWriter(ReportWriter<?> reportWriter) {
59                 this.reportWriter = reportWriter;
60                 setGenerated(false);
61         }
62         
63         public void setModel(Resource model) {
64                 this.model = model;
65                 setGenerated(false);
66         }
67
68         @Override
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() {
85                         @Override
86                         public void widgetSelected(SelectionEvent e) {
87                                 generate();
88                         }
89                 });
90                 showButton = new Button(composite, SWT.PUSH);
91                 showButton.setText("Show Report");
92                 showButton.addSelectionListener(new SelectionAdapter() {
93                         
94                         @Override
95                         public void widgetSelected(SelectionEvent e) {
96                                 File file = new File(filename);
97                                 if (file.exists() && file.canRead()) {
98                                         try {
99                                                 Editors.openExternalEditor(file);
100                                         } catch (PartInitException err) {
101                                                 ExceptionUtils.logAndShowError(err);
102                                         }
103                                 }
104                         }
105                 });
106                 showButton.setEnabled(false);
107                 updateContent();
108                 setControl(composite);
109         }
110         
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);
117                 if (!generated)
118                         statusLabel.setText("Report has not been generated");
119         }
120         
121         public void setGenerated(boolean b) {
122                 generated = b;
123                 setPageComplete(generated);
124         }
125         
126         private void generate() {
127                 generateButton.setEnabled(false);
128                 statusLabel.setText("Generating report");
129                 try {
130                         getWizard().getContainer().run(true, false, new IRunnableWithProgress() {
131                                 
132                                 @Override
133                                 public void run(final IProgressMonitor monitor) throws InvocationTargetException,
134                                                 InterruptedException {
135
136                                         ExportToPDF exportToPDF = new ExportToPDF(Simantics.getSession(), model);
137                                         final IStatus status = exportToPDF.export(filename, reportWriter, monitor);
138                                         Display.getDefault().asyncExec(new Runnable() {
139                                                 public void run() {
140                                                         if (!statusLabel.isDisposed()) {
141                                                                 if (status.isOK())
142                                                                         statusLabel.setText(status.getMessage());
143                                                                 else {
144                                                                         statusLabel.setText(status.getMessage());
145                                                                         ExceptionUtils.logError(status.getException());
146                                                                 }
147                                                                 setPageComplete(true);  
148                                                                 ((Composite)getControl()).layout(true, true);
149                                                         }
150                                                         
151                                                 };
152                                         });
153                                         monitor.done();
154                                 }
155                         });
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.");
165                 }
166                 setGenerated(true);
167                 
168                 showButton.setEnabled(true);
169                 generateButton.setEnabled(false);
170                 getContainer().updateButtons();
171         }
172         
173         @Override
174         public void setVisible(boolean visible) {
175                 if (visible) {
176                         updateContent();
177                 } else {
178                         setGenerated(false);
179                 }
180                 super.setVisible(visible);
181         }
182         
183         public boolean isGenerated() {
184                 return generated;
185         }
186
187 }