1 package org.simantics.document.linking.wizard;
3 import java.util.ArrayList;
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;
39 * Wizard page for selecting report type and file.
41 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
44 public class ReportSelectionPage extends WizardPage{
46 protected ReportSelectionPage(String pageName) {
47 super(pageName,pageName,null);
48 setPageComplete(false);
55 ModelToComboListener modelToComboListener;
57 List<Button> reportSelectionButtons = new ArrayList<Button>();
58 List<ReportWriter<?>> reportWriters = new ArrayList<ReportWriter<?>>();
60 ReportWriter<?> selectedWriter;
61 Resource selectedModel;
65 public void createControl(Composite parent) {
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);
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");
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());
87 Group group = new Group(composite, SWT.NONE);
88 group.setText("Report templates");
89 group.setLayout(new FillLayout(SWT.VERTICAL));
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);
95 browseButton.addSelectionListener(new SelectionAdapter() {
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("");
106 filenameText.setText(filename);
112 SelectionListener listener = new SelectionAdapter() {
114 public void widgetSelected(SelectionEvent e) {
115 Button selected = (Button)e.widget;
120 for (ReportWriter<?> rw : reportWriters) {
121 Button b = new Button(group, SWT.RADIO);
122 b.setText(rw.getName());
124 b.addSelectionListener(listener);
125 reportSelectionButtons.add(b);
128 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
131 public void run(ReadGraph graph) throws DatabaseException {
132 Resource project = SimanticsUI.getProject().get();
133 modelToComboListener = new ModelToComboListener(modelCombo) {
135 public Resource getCurrentModel() {
136 return selectedModel;
140 public void loaded() {
144 graph.syncRequest(new ModelRead(project),modelToComboListener);
149 modelCombo.addSelectionListener(new SelectionAdapter() {
151 public void widgetSelected(SelectionEvent e) {
152 int index = modelCombo.getSelectionIndex();
155 Resource model = (Resource)modelCombo.getData(Integer.toString(index));
157 selectedModel = model;
162 select(reportSelectionButtons.get(0));
163 setControl(composite);
166 public List<Button> getReportSelectionButtons() {
167 return reportSelectionButtons;
170 private void select(Button selected) {
171 selected.setSelection(true);
172 for (Button b : reportSelectionButtons) {
174 b.setSelection(false);
176 selectedWriter = (ReportWriter<?>)selected.getData();
180 private void validate() {
181 setPageComplete(selectedWriter!= null && selectedModel != null && filenameText.getText().length() > 0);
184 public ReportWriter<?> getSelectedWriter() {
185 return selectedWriter;
188 public String getFilename() {
189 return filenameText.getText();
192 public Resource getSelectedModel() {
193 return selectedModel;
196 public void setInput(Resource input) {
200 private void preselectModel() {
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;
212 public void setVisible(boolean visible) {
216 super.setVisible(visible);