]> gerrit.simantics Code Review - simantics/platform.git/blob
37f76332f6ad3e7de787f5263b390fee327e2d3e
[simantics/platform.git] /
1 package org.simantics.document.linking.wizard;
2
3 import java.util.Collection;
4 import java.util.LinkedHashMap;
5 import java.util.List;
6 import java.util.Map;
7
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;
40
41
42 /**
43  * 
44  * 
45  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
46  *
47  */
48 public class ReportCustomizationPage extends WizardPage{
49         
50         ReportWriter<?> reportWriter;
51         Resource model;
52
53         private Map<String,Composite> widgets = new LinkedHashMap<String, Composite>();
54         private TabFolder tabFolder;
55         private Map<String,TabItem> tabs = new LinkedHashMap<String, TabItem>();
56         
57         private Button loadButton;
58         private Button saveButton;
59         private Button resetButton;
60         private Resource library;
61         
62         protected ReportCustomizationPage(String pageName) {
63                 super(pageName,pageName,null);
64         }
65
66         @Override
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");
86                 
87                 loadButton.addSelectionListener(new SelectionAdapter() {
88                         @Override
89                         public void widgetSelected(SelectionEvent e) {
90                                 load();
91                         }
92                         
93                 });
94                 saveButton.addSelectionListener(new SelectionAdapter() {
95                         public void widgetSelected(SelectionEvent e) {
96                                 save();
97                         };
98                 });
99                 resetButton.addSelectionListener(new SelectionAdapter() {
100                         public void widgetSelected(SelectionEvent e) {
101                                 reset();
102                         };
103                 });
104                 updateContent();
105         }
106         
107         public void setReportWriter(ReportWriter<?> reportWriter) {
108                 this.reportWriter = reportWriter;
109                 updateContentProviders();
110                 updateContent();
111         }
112         
113         public void setModel(final Resource model) {
114                 this.model = model;
115                 
116                 
117                 try {
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>() {
122                                 @Override
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"));
126                                         if (lib != null)
127                                                 return lib;
128                                         
129                                         return model;
130                                 }
131                         });
132                 } catch (DatabaseException e) {
133                         this.library = model;
134                 }
135         }
136         
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())
147                                                 c.dispose();
148                                 }
149                         }
150                         for (String id : ids) {
151                                 CustomizableContent content = provider.getContent(id);
152                                 if (content instanceof EvaluatorCustomizableContent) {
153                                         TabItem item = tabs.get(id);
154                                         if (item == null) {
155                                                 item = new TabItem(tabFolder, SWT.NONE);
156                                                 tabs.put(id, item);
157                                         }
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);
163                                         if (c != null)
164                                                 c.dispose();
165                                         item.setControl(w);
166                                 } else if (content instanceof SCLCustomizableContent) {
167                                         
168                                 }
169                         }
170                 }
171         }
172
173         
174         @Override
175         public void setVisible(boolean visible) {
176                 super.setVisible(visible);
177                 updateContent();
178         }
179         
180         
181         private void updateContent() {
182                 for (Composite c : widgets.values()) {
183                         if (c instanceof EvaluatorConfigurationWidget)
184                                 ((EvaluatorConfigurationWidget)c).updateContent();
185                 }
186                 ((Composite)getControl()).layout(true);
187         }
188         
189         private void load() {
190                 try {
191                         List<NamedResource> templates = DBUtil.getTemplates(library);
192                         
193                         ListDialog dialog = new ListDialog(getShell());
194                         dialog.setContentProvider(new IStructuredContentProvider() {
195                                 
196                                 @Override
197                                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
198                                         
199                                 }
200                                 
201                                 @Override
202                                 public void dispose() {
203                                         
204                                 }
205                                 
206                                 @SuppressWarnings("unchecked")
207                                 @Override
208                                 public Object[] getElements(Object inputElement) {
209                                         List<NamedResource> list = (List<NamedResource>)inputElement;
210                                         return list.toArray();
211                                 }
212                         });
213                         dialog.setLabelProvider(new LabelProvider());
214                         dialog.setInput(templates);
215                         if (dialog.open() != ListDialog.OK)
216                                 return;
217                         Object result[] = dialog.getResult();
218                         if (result == null || result.length != 1)
219                                 return;
220                         final NamedResource selectedTemplate = (NamedResource)result[0];
221                         Simantics.getSession().syncRequest(new ReadRequest() {
222                                 
223                                 @Override
224                                 public void run(ReadGraph graph) throws DatabaseException {
225                                         DBUtil.load(graph, selectedTemplate.getResource(), (CustomizableContentProvider)reportWriter);
226                                         
227                                 }
228                         });
229                         updateContentProviders();
230                         updateContent();
231                         
232                 } catch (DatabaseException e) {
233                         // TODO Auto-generated catch block
234                         e.printStackTrace();
235                 }
236                 
237         }
238         
239         private void save() {
240                 try {
241                         
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)
244                                 return;
245                         final String name = dialog.getValue();
246                         if (name == null || name.length() == 0)
247                                 return;
248                         DBUtil.save(library, name, (CustomizableContentProvider)reportWriter);
249                 } catch (DatabaseException e) {
250                         // TODO Auto-generated catch block
251                         e.printStackTrace();
252                 }
253                         
254         }
255         
256         private void reset() {
257                 if (reportWriter instanceof CustomizableContentProvider) {
258                         CustomizableContentProvider ccp = (CustomizableContentProvider)reportWriter;
259                         for (String id : ccp.getContentIds()) {
260                                 ccp.setDefaultContent(id);
261                         }
262                         updateContentProviders();
263                         updateContent();
264                 }
265         }
266         
267         
268
269 }