]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
c32297479c334dc58d853d3b8ae02bf05383a5e4
[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.management.ISessionContext;\r
26 import org.simantics.db.procedure.Listener;\r
27 import org.simantics.ui.SimanticsUI;\r
28 import org.simantics.utils.ui.AdaptionUtils;\r
29 \r
30 /**\r
31  * \r
32  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
33  *\r
34  */\r
35 public class VariableProposalProvider implements IContentProposalProvider, Widget {\r
36 \r
37         /*\r
38          * The proposals provided.\r
39          */\r
40         private Collection<ChartVariable> proposals;\r
41 \r
42         /*\r
43          * The proposals mapped to IContentProposal. Cached for speed in the case\r
44          * where filtering is not used.\r
45          */\r
46         private IContentProposal[] contentProposals;\r
47 \r
48         /*\r
49          * Boolean that tracks whether filtering is used.\r
50          */\r
51         private boolean filterProposals = false;\r
52 \r
53 \r
54         private boolean compareRVI = false;\r
55         /**\r
56          * Return an array of Objects representing the valid content proposals for a\r
57          * field. \r
58          * \r
59          * @param contents\r
60          *            the current contents of the field (only consulted if filtering\r
61          *            is set to <code>true</code>)\r
62          * @param position\r
63          *            the current cursor position within the field (ignored)\r
64          * @return the array of Objects that represent valid proposals for the field\r
65          *         given its current content.\r
66          */\r
67         @SuppressWarnings("unchecked")\r
68         public IContentProposal[] getProposals(String contents, int position) {\r
69                 if (filterProposals) {\r
70                         @SuppressWarnings("rawtypes")\r
71             ArrayList list = new ArrayList();\r
72                         if (compareRVI) {\r
73                                 for (ChartVariable proposal : proposals) {\r
74                                         if (proposal.getRvi().length() >= contents.length() && proposal.getRvi().substring(0, contents.length()).equalsIgnoreCase(contents)) {\r
75                                                 if (proposal.getLabel() != null)\r
76                                                         list.add(new ContentProposal(proposal.getRvi(),proposal.getLabel(), null));\r
77                                                 else\r
78                                                         list.add(new ContentProposal(proposal.getRvi()));\r
79                                         } else if (proposal.getLabel() != null && proposal.getLabel().length() >= contents.length() && proposal.getLabel().substring(0, contents.length()).equalsIgnoreCase(contents)) {\r
80                                                 list.add(new ContentProposal(proposal.getRvi(),proposal.getLabel(), null));\r
81                                         }\r
82                                 }\r
83                         } else {\r
84                                 for (ChartVariable proposal : proposals) {\r
85                                         if (proposal.getLabel() != null && proposal.getLabel().length() >= contents.length() && proposal.getLabel().substring(0, contents.length()).equalsIgnoreCase(contents)) {\r
86                                                 list.add(new ContentProposal(proposal.getRvi(),proposal.getLabel(), null));\r
87                                         }\r
88                                 }\r
89                         }\r
90                         \r
91                         return (IContentProposal[]) list.toArray(new IContentProposal[list\r
92                                         .size()]);\r
93                 }\r
94                 if (contentProposals == null) {\r
95                         contentProposals = new IContentProposal[proposals.size()];\r
96                         Iterator<ChartVariable> iter = proposals.iterator();\r
97                         for (int i = 0; i < proposals.size(); i++) {\r
98                                 ChartVariable proposal = iter.next();\r
99                                 if (proposal.getLabel() != null)\r
100                                         contentProposals[i] = new ContentProposal(proposal.getRvi(),proposal.getLabel(),null);\r
101                                 else\r
102                                         contentProposals[i] = new ContentProposal(proposal.getRvi());\r
103                         }\r
104                 }\r
105                 return contentProposals;\r
106         }\r
107 \r
108         /**\r
109          * Set the Strings to be used as content proposals.\r
110          * \r
111          * @param items\r
112          *            the array of Strings to be used as proposals.\r
113          */\r
114         public void setProposals(Collection<ChartVariable> items) {\r
115                 this.proposals = items;\r
116                 contentProposals = null;\r
117         }\r
118 \r
119         /**\r
120          * Set the boolean that controls whether proposals are filtered according to\r
121          * the current field content.\r
122          * \r
123          * @param filterProposals\r
124          *            <code>true</code> if the proposals should be filtered to\r
125          *            show only those that match the current contents of the field,\r
126          *            and <code>false</code> if the proposals should remain the\r
127          *            same, ignoring the field content.\r
128          * @since 3.3\r
129          */\r
130         public void setFiltering(boolean filterProposals) {\r
131                 this.filterProposals = filterProposals;\r
132                 // Clear any cached proposals.\r
133                 contentProposals = null;\r
134         }\r
135         \r
136     /**\r
137      * Provides all variables a model contains. Given resource needs to be\r
138      * part of a model (i.e. using PartOf leads eventually to a SysdynModel).\r
139      *  \r
140      * @param control Control that is using this provider\r
141      * @param resource A resource that is part of a model\r
142      */\r
143     public VariableProposalProvider(final Control control, WidgetSupport support) {\r
144         this.proposals = new ArrayList<ChartVariable>();\r
145         support.register(this);\r
146         this.control = control;\r
147     }\r
148 \r
149     private Resource resource;\r
150     private Control control;\r
151     \r
152     @Override\r
153     public void setInput(ISessionContext context, Object input) {\r
154 \r
155         final Resource resource = AdaptionUtils.adaptToSingle(input, Resource.class);\r
156         if(resource == null)\r
157             return;\r
158         this.resource = resource;\r
159         \r
160         SimanticsUI.getSession().asyncRequest(\r
161                 new AllVariablesOfModel(resource)\r
162         , new Listener<Collection<ChartVariable>>() {\r
163 \r
164             @Override\r
165             public void execute(Collection<ChartVariable> result) {\r
166                 setProposals(result);\r
167             }\r
168 \r
169             @Override\r
170             public void exception(Throwable t) {\r
171                 t.printStackTrace();\r
172             }\r
173 \r
174             @Override\r
175             public boolean isDisposed() {\r
176                 return control == null || \r
177                         control.isDisposed() || \r
178                         !resource.equals(VariableProposalProvider.this.resource);\r
179             }\r
180 \r
181         }); \r
182         \r
183     }\r
184     \r
185 }\r