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