1 package org.simantics.document.linking.wizard;
3 import java.util.Collection;
4 import java.util.LinkedHashMap;
8 import org.eclipse.jface.dialogs.InputDialog;
9 import org.eclipse.jface.layout.GridDataFactory;
10 import org.eclipse.jface.resource.LocalResourceManager;
11 import org.eclipse.jface.viewers.IStructuredContentProvider;
12 import org.eclipse.jface.viewers.LabelProvider;
13 import org.eclipse.jface.viewers.Viewer;
14 import org.eclipse.jface.wizard.WizardPage;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.TabFolder;
22 import org.eclipse.swt.widgets.TabItem;
23 import org.eclipse.ui.dialogs.ListDialog;
24 import org.simantics.Simantics;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.Resource;
27 import org.simantics.db.common.NamedResource;
28 import org.simantics.db.common.request.PossibleObjectWithName;
29 import org.simantics.db.common.request.ReadRequest;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.request.Read;
32 import org.simantics.document.linking.report.evaluator.DBUtil;
33 import org.simantics.document.linking.report.templates.CustomizableContentProvider;
34 import org.simantics.document.linking.report.templates.ReportWriter;
35 import org.simantics.document.linking.report.templates.custom.CustomizableContent;
36 import org.simantics.document.linking.report.templates.custom.EvaluatorCustomizableContent;
37 import org.simantics.document.linking.report.templates.custom.SCLCustomizableContent;
38 import org.simantics.layer0.Layer0;
39 import org.simantics.utils.ui.validators.NotNullValidator;
45 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
48 public class ReportCustomizationPage extends WizardPage{
50 ReportWriter<?> reportWriter;
53 private Map<String,Composite> widgets = new LinkedHashMap<String, Composite>();
54 private TabFolder tabFolder;
55 private Map<String,TabItem> tabs = new LinkedHashMap<String, TabItem>();
57 private Button loadButton;
58 private Button saveButton;
59 private Button resetButton;
60 private Resource library;
62 protected ReportCustomizationPage(String pageName) {
63 super(pageName,pageName,null);
67 public void createControl(Composite parent) {
68 Composite composite = new Composite(parent, SWT.NONE);
69 composite.setLayout(new GridLayout(1,false));
70 setControl(composite);
71 tabFolder = new TabFolder(composite, SWT.NONE);
72 tabFolder.setLayout(new GridLayout());
73 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(tabFolder);
74 Composite buttonComposite = new Composite(composite, SWT.NONE);
75 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BOTTOM).grab(true, false).applyTo(buttonComposite);
76 buttonComposite.setLayout(new GridLayout(3, true));
77 loadButton = new Button(buttonComposite, SWT.PUSH);
78 saveButton = new Button(buttonComposite, SWT.PUSH);
79 resetButton = new Button(buttonComposite, SWT.PUSH);
80 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).applyTo(loadButton);
81 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).applyTo(saveButton);
82 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).applyTo(resetButton);
83 loadButton.setText("Load");
84 saveButton.setText("Save");
85 resetButton.setText("Defaults");
87 loadButton.addSelectionListener(new SelectionAdapter() {
89 public void widgetSelected(SelectionEvent e) {
94 saveButton.addSelectionListener(new SelectionAdapter() {
95 public void widgetSelected(SelectionEvent e) {
99 resetButton.addSelectionListener(new SelectionAdapter() {
100 public void widgetSelected(SelectionEvent e) {
107 public void setReportWriter(ReportWriter<?> reportWriter) {
108 this.reportWriter = reportWriter;
109 updateContentProviders();
113 public void setModel(final Resource model) {
118 // TODO : this code retrieves a proper location for storing the template configurations in a model.
119 // this should be changed so that model resource somehow tells where to store the configurations.
120 // Note: in other models, the location of templates is the model resource itself.
121 this.library = Simantics.getSession().syncRequest(new Read<Resource>() {
123 public Resource perform(ReadGraph graph) throws DatabaseException {
124 Layer0 l0 = Layer0.getInstance(graph);
125 Resource lib = graph.syncRequest(new PossibleObjectWithName(model, l0.ConsistsOf,"Library"));
132 } catch (DatabaseException e) {
133 this.library = model;
137 private void updateContentProviders() {
138 if (reportWriter instanceof CustomizableContentProvider) {
139 LocalResourceManager manager = ((ReportWizard)getWizard()).manager;
140 CustomizableContentProvider provider = (CustomizableContentProvider)reportWriter;
141 Collection<String> ids = provider.getContentIds();
142 for (String id : tabs.keySet()) {
143 if (!ids.contains(id)) {
144 tabs.remove(id).dispose();
145 Composite c = widgets.get(id);
146 if (c != null && !c.isDisposed())
150 for (String id : ids) {
151 CustomizableContent content = provider.getContent(id);
152 if (content instanceof EvaluatorCustomizableContent) {
153 TabItem item = tabs.get(id);
155 item = new TabItem(tabFolder, SWT.NONE);
158 item.setText(content.getCustomizationDescription());
159 EvaluatorConfigurationWidget w = new EvaluatorConfigurationWidget(tabFolder,manager, SWT.NONE);
160 w.setInput((EvaluatorCustomizableContent)content);
161 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(w);
162 Composite c = widgets.put(id, w);
166 } else if (content instanceof SCLCustomizableContent) {
175 public void setVisible(boolean visible) {
176 super.setVisible(visible);
181 private void updateContent() {
182 for (Composite c : widgets.values()) {
183 if (c instanceof EvaluatorConfigurationWidget)
184 ((EvaluatorConfigurationWidget)c).updateContent();
186 ((Composite)getControl()).layout(true);
189 private void load() {
191 List<NamedResource> templates = DBUtil.getTemplates(library);
193 ListDialog dialog = new ListDialog(getShell());
194 dialog.setContentProvider(new IStructuredContentProvider() {
197 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
202 public void dispose() {
206 @SuppressWarnings("unchecked")
208 public Object[] getElements(Object inputElement) {
209 List<NamedResource> list = (List<NamedResource>)inputElement;
210 return list.toArray();
213 dialog.setLabelProvider(new LabelProvider());
214 dialog.setInput(templates);
215 if (dialog.open() != ListDialog.OK)
217 Object result[] = dialog.getResult();
218 if (result == null || result.length != 1)
220 final NamedResource selectedTemplate = (NamedResource)result[0];
221 Simantics.getSession().syncRequest(new ReadRequest() {
224 public void run(ReadGraph graph) throws DatabaseException {
225 DBUtil.load(graph, selectedTemplate.getResource(), (CustomizableContentProvider)reportWriter);
229 updateContentProviders();
232 } catch (DatabaseException e) {
233 // TODO Auto-generated catch block
239 private void save() {
242 InputDialog dialog = new InputDialog(getShell(), "Save report customization", "Give template name", "Name", new NotNullValidator("Name cannot be empty"));
243 if (dialog.open() != InputDialog.OK)
245 final String name = dialog.getValue();
246 if (name == null || name.length() == 0)
248 DBUtil.save(library, name, (CustomizableContentProvider)reportWriter);
249 } catch (DatabaseException e) {
250 // TODO Auto-generated catch block
256 private void reset() {
257 if (reportWriter instanceof CustomizableContentProvider) {
258 CustomizableContentProvider ccp = (CustomizableContentProvider)reportWriter;
259 for (String id : ccp.getContentIds()) {
260 ccp.setDefaultContent(id);
262 updateContentProviders();