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