]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/wizard/ReportSelectionPage.java
da8ff028a00ba9d677cb829ff1fa40c76e5f6606
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / wizard / ReportSelectionPage.java
1 package org.simantics.document.linking.wizard;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.eclipse.jface.layout.GridDataFactory;
7 import org.eclipse.jface.wizard.WizardPage;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.custom.CCombo;
10 import org.eclipse.swt.events.SelectionAdapter;
11 import org.eclipse.swt.events.SelectionEvent;
12 import org.eclipse.swt.events.SelectionListener;
13 import org.eclipse.swt.layout.FillLayout;
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.FileDialog;
19 import org.eclipse.swt.widgets.Group;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Text;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.request.ReadRequest;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.document.linking.report.templates.CompleteStructureWriter;
27 import org.simantics.document.linking.report.templates.DiagramStructureWithDependenciesWriter;
28 import org.simantics.document.linking.report.templates.DiagramStructureWriter;
29 import org.simantics.document.linking.report.templates.DocumentStructureWriter;
30 import org.simantics.document.linking.report.templates.ModelDocumentWriter;
31 import org.simantics.document.linking.report.templates.ReferredDocumentWriter;
32 import org.simantics.document.linking.report.templates.ReportWriter;
33 import org.simantics.document.linking.views.ModelRead;
34 import org.simantics.document.linking.views.ModelToComboListener;
35 import org.simantics.ui.SimanticsUI;
36
37
38 /**
39  * Wizard page for selecting report type and file.
40  * 
41  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
42  *
43  */
44 public class ReportSelectionPage extends WizardPage{
45         
46         protected ReportSelectionPage(String pageName) {
47                 super(pageName,pageName,null);
48                 setPageComplete(false);
49         }
50
51         CCombo modelCombo;
52         Text filenameText;
53         Button browseButton;
54         
55         ModelToComboListener modelToComboListener;
56         
57         List<Button> reportSelectionButtons = new ArrayList<Button>();
58         List<ReportWriter<?>> reportWriters = new ArrayList<ReportWriter<?>>();
59         
60         ReportWriter<?> selectedWriter;
61         Resource selectedModel;
62         Resource input;
63         
64         @Override
65         public void createControl(Composite parent) {
66                 
67                 
68                 Composite composite = new Composite(parent, SWT.NONE);
69                 composite.setLayout(new GridLayout(3,false));
70                 Label label = new Label(composite, SWT.NONE);
71                 label.setText("Model:");
72                 modelCombo = new CCombo(composite, SWT.BORDER|SWT.READ_ONLY);
73                 
74                 label = new Label(composite, SWT.NONE);
75                 label.setText("File:");
76                 filenameText = new Text(composite, SWT.BORDER|SWT.SINGLE);
77                 browseButton = new Button(composite, SWT.PUSH);
78                 browseButton.setText("Browse");
79                 
80                 reportWriters.add(new ModelDocumentWriter());
81                 reportWriters.add(new ReferredDocumentWriter());
82                 reportWriters.add(new DocumentStructureWriter());
83                 reportWriters.add(new DiagramStructureWriter());
84                 reportWriters.add(new DiagramStructureWithDependenciesWriter());
85                 reportWriters.add(new CompleteStructureWriter());
86                 
87                 Group group = new Group(composite, SWT.NONE);
88                 group.setText("Report templates");
89                 group.setLayout(new FillLayout(SWT.VERTICAL));
90                 
91                 GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(filenameText);
92                 GridDataFactory.fillDefaults().span(2, 1).applyTo(modelCombo);
93                 GridDataFactory.fillDefaults().span(3, 1).applyTo(group);
94                 
95                 browseButton.addSelectionListener(new SelectionAdapter() {
96                         
97                         @Override
98                         public void widgetSelected(SelectionEvent e) {
99                                 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.SAVE);
100                                 dialog.setFilterExtensions(new String[]{"*.pdf","*.html"});
101                                 dialog.setFilterNames(new String[]{"PDF Document","HTML Document"});
102                                 String filename = dialog.open();
103                                 if (filename == null)
104                                         filenameText.setText("");
105                                 else
106                                         filenameText.setText(filename);
107                                 validate();
108                         }
109                 });
110                 
111                 
112                 SelectionListener listener = new SelectionAdapter() {
113                         @Override
114                         public void widgetSelected(SelectionEvent e) {
115                                 Button selected = (Button)e.widget;
116                                 select(selected);
117                         }
118                 };
119                 
120                 for (ReportWriter<?> rw : reportWriters) {
121                         Button b = new Button(group, SWT.RADIO);
122                         b.setText(rw.getName());
123                         b.setData(rw);
124                         b.addSelectionListener(listener);
125                         reportSelectionButtons.add(b);
126                 }
127                 
128                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
129                         
130                         @Override
131                         public void run(ReadGraph graph) throws DatabaseException {
132                                 Resource project = SimanticsUI.getProject().get();
133                                 modelToComboListener = new ModelToComboListener(modelCombo) {
134                                         @Override
135                                         public Resource getCurrentModel() {
136                                                 return selectedModel;
137                                         } 
138                                         
139                                         @Override
140                                         public void loaded() {
141                                                 preselectModel();
142                                         }
143                                 };
144                                 graph.syncRequest(new ModelRead(project),modelToComboListener);
145                                 
146                         }
147                 });
148                 
149                 modelCombo.addSelectionListener(new SelectionAdapter() {
150                         @Override
151                         public void widgetSelected(SelectionEvent e) {
152                                 int index = modelCombo.getSelectionIndex();
153                                 if (index == -1)
154                                         return;
155                                 Resource model = (Resource)modelCombo.getData(Integer.toString(index));
156                                 if (model != null)
157                                         selectedModel = model;
158                                 validate();
159                         }
160                 });
161                 
162                 select(reportSelectionButtons.get(0));
163                 setControl(composite);
164         }
165         
166         public List<Button> getReportSelectionButtons() {
167                 return reportSelectionButtons;
168         }
169         
170         private void select(Button selected) {
171                 selected.setSelection(true);
172                 for (Button b : reportSelectionButtons) {
173                         if (b != selected)
174                                 b.setSelection(false);
175                 }
176                 selectedWriter = (ReportWriter<?>)selected.getData();
177                 validate();
178         }
179         
180         private void validate() {
181                 setPageComplete(selectedWriter!= null && selectedModel != null && filenameText.getText().length() > 0);
182         }
183         
184         public ReportWriter<?> getSelectedWriter() {
185                 return selectedWriter;
186         }
187         
188         public String getFilename() {
189                 return filenameText.getText();
190         }
191         
192         public Resource getSelectedModel() {
193                 return selectedModel;
194         }
195         
196         public void setInput(Resource input) {
197                 this.input = input;
198         }
199         
200         private void preselectModel() {
201                 if (input != null) {
202                         for (int i = 0; i < modelCombo.getItemCount(); i++) {
203                                 if (input.equals(modelCombo.getData(Integer.toString(i)))) {
204                                         modelCombo.select(i);
205                                         selectedModel = input;
206                                 }
207                         }
208                 }
209         }
210         
211         @Override
212         public void setVisible(boolean visible) {
213                 if (visible) {
214                         preselectModel();
215                 }
216                 super.setVisible(visible);
217         }
218
219 }