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.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;
40 * Wizard page for selecting report type and file.
42 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
45 public class ReportSelectionPage extends WizardPage{
47 protected ReportSelectionPage(String pageName) {
48 super(pageName,pageName,null);
49 setPageComplete(false);
56 ModelToComboListener modelToComboListener;
58 List<Button> reportSelectionButtons = new ArrayList<Button>();
59 List<ReportWriter<?>> reportWriters = new ArrayList<ReportWriter<?>>();
61 ReportWriter<?> selectedWriter;
62 Resource selectedModel;
66 public void createControl(Composite parent) {
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);
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");
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());
88 Group group = new Group(composite, SWT.NONE);
89 group.setText("Report templates");
90 group.setLayout(new FillLayout(SWT.VERTICAL));
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);
96 browseButton.addSelectionListener(new SelectionAdapter() {
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("");
107 filenameText.setText(filename);
113 SelectionListener listener = new SelectionAdapter() {
115 public void widgetSelected(SelectionEvent e) {
116 Button selected = (Button)e.widget;
121 for (ReportWriter<?> rw : reportWriters) {
122 Button b = new Button(group, SWT.RADIO);
123 b.setText(rw.getName());
125 b.addSelectionListener(listener);
126 reportSelectionButtons.add(b);
129 Simantics.getSession().asyncRequest(new ReadRequest() {
132 public void run(ReadGraph graph) throws DatabaseException {
133 Resource project = Simantics.getProject().get();
134 modelToComboListener = new ModelToComboListener(modelCombo) {
136 public Resource getCurrentModel() {
137 return selectedModel;
141 public void loaded() {
145 graph.syncRequest(new ModelRead(project),modelToComboListener);
150 modelCombo.addSelectionListener(new SelectionAdapter() {
152 public void widgetSelected(SelectionEvent e) {
153 int index = modelCombo.getSelectionIndex();
156 Resource model = (Resource)modelCombo.getData(Integer.toString(index));
158 selectedModel = model;
163 select(reportSelectionButtons.get(0));
164 setControl(composite);
167 public List<Button> getReportSelectionButtons() {
168 return reportSelectionButtons;
171 private void select(Button selected) {
172 selected.setSelection(true);
173 for (Button b : reportSelectionButtons) {
175 b.setSelection(false);
177 selectedWriter = (ReportWriter<?>)selected.getData();
181 private void validate() {
182 setPageComplete(selectedWriter!= null && selectedModel != null && filenameText.getText().length() > 0);
185 public ReportWriter<?> getSelectedWriter() {
186 return selectedWriter;
189 public String getFilename() {
190 return filenameText.getText();
193 public Resource getSelectedModel() {
194 return selectedModel;
197 public void setInput(Resource input) {
201 private void preselectModel() {
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;
213 public void setVisible(boolean visible) {
217 super.setVisible(visible);