]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/wizard/ReportGeneratePage.java
Externalize org.simantics.document.linking.ui
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / wizard / ReportGeneratePage.java
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.osgi.util.NLS;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.SelectionAdapter;
13 import org.eclipse.swt.events.SelectionEvent;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.widgets.Button;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.ui.PartInitException;
20 import org.simantics.Simantics;
21 import org.simantics.db.Resource;
22 import org.simantics.document.linking.report.ExportToPDF;
23 import org.simantics.document.linking.report.templates.ReportWriter;
24 import org.simantics.editors.Editors;
25 import org.simantics.utils.ui.ErrorLogger;
26 import org.simantics.utils.ui.ExceptionUtils;
27
28
29 /**
30  * Wizard page for generating a report.
31  * 
32  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
33  *
34  */
35 public class ReportGeneratePage extends WizardPage {
36         
37         private Resource model;
38         private String filename;
39         private ReportWriter<?> reportWriter;
40         
41         
42         private Label fileLabel;
43         private Label reportLabel;
44         private Label statusLabel;
45         private Button generateButton;
46         private Button showButton;
47         private boolean generated = false;
48         
49         protected ReportGeneratePage(String pageName) {
50                 super(pageName,pageName,null);
51                 setGenerated(false);
52         }
53         
54         public void setFilename(String filename) {
55                 this.filename = filename;
56                 setGenerated(false);
57         }
58         
59         public void setReportWriter(ReportWriter<?> reportWriter) {
60                 this.reportWriter = reportWriter;
61                 setGenerated(false);
62         }
63         
64         public void setModel(Resource model) {
65                 this.model = model;
66                 setGenerated(false);
67         }
68
69         @Override
70         public void createControl(Composite parent) {
71                 Composite composite = new Composite(parent, SWT.NONE);
72                 composite.setLayout(new GridLayout(2,false));
73                 Label label = new Label(composite, SWT.NONE);
74                 label.setText(Messages.ReportGeneratePage_File);
75                 fileLabel = new Label(composite, SWT.NONE);
76                 label = new Label(composite, SWT.NONE);
77                 label.setText(Messages.ReportGeneratePage_Report);
78                 reportLabel = new Label(composite, SWT.NONE);
79                 label = new Label(composite, SWT.NONE);
80                 label.setText(Messages.ReportGeneratePage_Status);
81                 this.statusLabel = new Label(composite, SWT.NONE);
82                 this.statusLabel.setText(Messages.ReportGeneratePage_ReportNotGenerated);
83                 generateButton = new Button(composite, SWT.PUSH);
84                 generateButton.setText(Messages.ReportGeneratePage_GenerateReport);
85                 generateButton.addSelectionListener(new SelectionAdapter() {
86                         @Override
87                         public void widgetSelected(SelectionEvent e) {
88                                 generate();
89                         }
90                 });
91                 showButton = new Button(composite, SWT.PUSH);
92                 showButton.setText(Messages.ReportGeneratePage_ShowReport);
93                 showButton.addSelectionListener(new SelectionAdapter() {
94                         
95                         @Override
96                         public void widgetSelected(SelectionEvent e) {
97                                 File file = new File(filename);
98                                 if (file.exists() && file.canRead()) {
99                                         try {
100                                                 Editors.openExternalEditor(file);
101                                         } catch (PartInitException err) {
102                                                 ExceptionUtils.logAndShowError(err);
103                                         }
104                                 }
105                         }
106                 });
107                 showButton.setEnabled(false);
108                 updateContent();
109                 setControl(composite);
110         }
111         
112         private void updateContent() {
113                 fileLabel.setText(filename == null ? Messages.ReportGeneratePage_FileNotSelected : filename);
114                 reportLabel.setText(reportWriter == null ? Messages.ReportGeneratePage_ReportWriterNotSelected : reportWriter.getName());
115                 generateButton.setEnabled(filename != null && reportWriter != null && model != null);
116                 showButton.setEnabled(generated);
117                 generateButton.setEnabled(!generated);
118                 if (!generated)
119                         statusLabel.setText(Messages.ReportGeneratePage_ReportNotGenerated);
120         }
121         
122         public void setGenerated(boolean b) {
123                 generated = b;
124                 setPageComplete(generated);
125         }
126         
127         private void generate() {
128                 generateButton.setEnabled(false);
129                 statusLabel.setText(Messages.ReportGeneratePage_GeneratingReport);
130                 try {
131                         getWizard().getContainer().run(true, false, new IRunnableWithProgress() {
132                                 
133                                 @Override
134                                 public void run(final IProgressMonitor monitor) throws InvocationTargetException,
135                                                 InterruptedException {
136
137                                         ExportToPDF exportToPDF = new ExportToPDF(Simantics.getSession(), model);
138                                         final IStatus status = exportToPDF.export(filename, reportWriter, monitor);
139                                         Display.getDefault().asyncExec(new Runnable() {
140                                                 public void run() {
141                                                         if (!statusLabel.isDisposed()) {
142                                                                 if (status.isOK())
143                                                                         statusLabel.setText(status.getMessage());
144                                                                 else {
145                                                                         statusLabel.setText(status.getMessage());
146                                                                         ExceptionUtils.logError(status.getException());
147                                                                 }
148                                                                 setPageComplete(true);  
149                                                                 ((Composite)getControl()).layout(true, true);
150                                                         }
151                                                         
152                                                 };
153                                         });
154                                         monitor.done();
155                                 }
156                         });
157                 }  catch (InterruptedException err) {
158                         setErrorMessage(NLS.bind(Messages.ReportGeneratePage_ReportFailed , err.getMessage()));
159                         ErrorLogger.defaultLogError(Messages.ReportGeneratePage_ReportFail,err);
160                         statusLabel.setText(Messages.ReportGeneratePage_ReportFail);
161                 } catch (InvocationTargetException e) {
162                     Throwable err = e.getCause();
163                         setErrorMessage(NLS.bind(Messages.ReportGeneratePage_ReportFailed , err.getMessage()));
164                         ErrorLogger.defaultLogError(Messages.ReportGeneratePage_ReportFail,err);
165                         statusLabel.setText(Messages.ReportGeneratePage_ReportFail);
166                 }
167                 setGenerated(true);
168                 
169                 showButton.setEnabled(true);
170                 generateButton.setEnabled(false);
171                 getContainer().updateButtons();
172         }
173         
174         @Override
175         public void setVisible(boolean visible) {
176                 if (visible) {
177                         updateContent();
178                 } else {
179                         setGenerated(false);
180                 }
181                 super.setVisible(visible);
182         }
183         
184         public boolean isGenerated() {
185                 return generated;
186         }
187
188 }