]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
da399f6b6dc588ebf76acd34d813e09f43f31482
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.jfreechart.chart.properties;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.Iterator;\r
17 \r
18 import org.eclipse.jface.fieldassist.ContentProposal;\r
19 import org.eclipse.jface.fieldassist.IContentProposal;\r
20 import org.eclipse.jface.fieldassist.IContentProposalProvider;\r
21 import org.eclipse.swt.widgets.Control;\r
22 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
23 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
24 import org.simantics.db.Resource;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.db.management.ISessionContext;\r
27 import org.simantics.db.procedure.Listener;\r
28 import org.simantics.ui.SimanticsUI;\r
29 import org.simantics.utils.ui.AdaptionUtils;\r
30 \r
31 /**\r
32  * \r
33  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
34  *\r
35  */\r
36 public class VariableProposalProvider implements IContentProposalProvider, Widget {\r
37 \r
38         /*\r
39          * The proposals provided.\r
40          */\r
41         private Collection<ChartVariable> proposals;\r
42 \r
43         /*\r
44          * The proposals mapped to IContentProposal. Cached for speed in the case\r
45          * where filtering is not used.\r
46          */\r
47         private IContentProposal[] contentProposals;\r
48 \r
49         /*\r
50          * Boolean that tracks whether filtering is used.\r
51          */\r
52         private boolean filterProposals = false;\r
53 \r
54 \r
55         private boolean compareRVI = false;\r
56         /**\r
57          * Return an array of Objects representing the valid content proposals for a\r
58          * field. \r
59          * \r
60          * @param contents\r
61          *            the current contents of the field (only consulted if filtering\r
62          *            is set to <code>true</code>)\r
63          * @param position\r
64          *            the current cursor position within the field (ignored)\r
65          * @return the array of Objects that represent valid proposals for the field\r
66          *         given its current content.\r
67          */\r
68         @SuppressWarnings("unchecked")\r
69         public IContentProposal[] getProposals(String contents, int position) {\r
70                 if (filterProposals) {\r
71                         @SuppressWarnings("rawtypes")\r
72             ArrayList list = new ArrayList();\r
73                         if (compareRVI) {\r
74                                 for (ChartVariable proposal : proposals) {\r
75                                         if (proposal.getRvi().length() >= contents.length() && proposal.getRvi().substring(0, contents.length()).equalsIgnoreCase(contents)) {\r
76                                                 if (proposal.getLabel() != null)\r
77                                                         list.add(new ContentProposal(proposal.getRvi(),proposal.getLabel(), null));\r
78                                                 else\r
79                                                         list.add(new ContentProposal(proposal.getRvi()));\r
80                                         } else if (proposal.getLabel() != null && proposal.getLabel().length() >= contents.length() && proposal.getLabel().substring(0, contents.length()).equalsIgnoreCase(contents)) {\r
81                                                 list.add(new ContentProposal(proposal.getRvi(),proposal.getLabel(), null));\r
82                                         }\r
83                                 }\r
84                         } else {\r
85                                 for (ChartVariable proposal : proposals) {\r
86                                         if (proposal.getLabel() != null && proposal.getLabel().length() >= contents.length() && proposal.getLabel().substring(0, contents.length()).equalsIgnoreCase(contents)) {\r
87                                                 list.add(new ContentProposal(proposal.getRvi(),proposal.getLabel(), null));\r
88                                         }\r
89                                 }\r
90                         }\r
91                         \r
92                         return (IContentProposal[]) list.toArray(new IContentProposal[list\r
93                                         .size()]);\r
94                 }\r
95                 if (contentProposals == null) {\r
96                         contentProposals = new IContentProposal[proposals.size()];\r
97                         Iterator<ChartVariable> iter = proposals.iterator();\r
98                         for (int i = 0; i < proposals.size(); i++) {\r
99                                 ChartVariable proposal = iter.next();\r
100                                 if (proposal.getLabel() != null)\r
101                                         contentProposals[i] = new ContentProposal(proposal.getRvi(),proposal.getLabel(),null);\r
102                                 else\r
103                                         contentProposals[i] = new ContentProposal(proposal.getRvi());\r
104                         }\r
105                 }\r
106                 return contentProposals;\r
107         }\r
108 \r
109         /**\r
110          * Set the Strings to be used as content proposals.\r
111          * \r
112          * @param items\r
113          *            the array of Strings to be used as proposals.\r
114          */\r
115         public void setProposals(Collection<ChartVariable> items) {\r
116                 this.proposals = items;\r
117                 contentProposals = null;\r
118         }\r
119 \r
120         /**\r
121          * Set the boolean that controls whether proposals are filtered according to\r
122          * the current field content.\r
123          * \r
124          * @param filterProposals\r
125          *            <code>true</code> if the proposals should be filtered to\r
126          *            show only those that match the current contents of the field,\r
127          *            and <code>false</code> if the proposals should remain the\r
128          *            same, ignoring the field content.\r
129          * @since 3.3\r
130          */\r
131         public void setFiltering(boolean filterProposals) {\r
132                 this.filterProposals = filterProposals;\r
133                 // Clear any cached proposals.\r
134                 contentProposals = null;\r
135         }\r
136         \r
137     /**\r
138      * Provides all variables a model contains. Given resource needs to be\r
139      * part of a model (i.e. using PartOf leads eventually to a SysdynModel).\r
140      *  \r
141      * @param control Control that is using this provider\r
142      * @param resource A resource that is part of a model\r
143      */\r
144     public VariableProposalProvider(final Control control, WidgetSupport support) {\r
145         this.proposals = new ArrayList<ChartVariable>();\r
146         support.register(this);\r
147         this.control = control;\r
148     }\r
149 \r
150     private Resource resource;\r
151     private Control control;\r
152     \r
153     @Override\r
154     public void setInput(ISessionContext context, Object input) {\r
155 \r
156         final Resource resource = AdaptionUtils.adaptToSingle(input, Resource.class);\r
157         if(resource == null)\r
158             return;\r
159         this.resource = resource;\r
160         /* Find the model resource. It can be found with PartOf \r
161         relations from series resource in a chart */\r
162        try {\r
163                 AllVariablesOfModel query = AllVariablesOfModel.withRandomResource(context, resource);\r
164                  SimanticsUI.getSession().asyncRequest(query\r
165                 , new Listener<Collection<ChartVariable>>() {\r
166 \r
167                     @Override\r
168                     public void execute(Collection<ChartVariable> result) {\r
169                         setProposals(result);\r
170                     }\r
171 \r
172                     @Override\r
173                     public void exception(Throwable t) {\r
174                         t.printStackTrace();\r
175                     }\r
176 \r
177                     @Override\r
178                     public boolean isDisposed() {\r
179                         return control == null || \r
180                                 control.isDisposed() || \r
181                                 !resource.equals(VariableProposalProvider.this.resource);\r
182                     }\r
183 \r
184                 }); \r
185         } catch (DatabaseException e) {\r
186                 // TODO Auto-generated catch block\r
187                 e.printStackTrace();\r
188         }\r
189         \r
190        \r
191         \r
192     }\r
193     \r
194 }\r