1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.jfreechart.chart.properties;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.Iterator;
\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.ui.utils.AdaptionUtils;
\r
32 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
35 public class VariableProposalProvider implements IContentProposalProvider, Widget {
\r
38 * The proposals provided.
\r
40 private Collection<ChartVariable> proposals;
\r
43 * The proposals mapped to IContentProposal. Cached for speed in the case
\r
44 * where filtering is not used.
\r
46 private IContentProposal[] contentProposals;
\r
49 * Boolean that tracks whether filtering is used.
\r
51 private boolean filterProposals = false;
\r
54 private boolean compareRVI = false;
\r
56 * Return an array of Objects representing the valid content proposals for a
\r
60 * the current contents of the field (only consulted if filtering
\r
61 * is set to <code>true</code>)
\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
67 @SuppressWarnings("unchecked")
\r
68 public IContentProposal[] getProposals(String contents, int position) {
\r
69 if (filterProposals) {
\r
70 ArrayList list = new ArrayList();
\r
72 for (ChartVariable proposal : proposals) {
\r
73 if (proposal.getRvi().length() >= contents.length() && proposal.getRvi().substring(0, contents.length()).equalsIgnoreCase(contents)) {
\r
74 if (proposal.getLabel() != null)
\r
75 list.add(new ContentProposal(proposal.getRvi(),proposal.getLabel(), null));
\r
77 list.add(new ContentProposal(proposal.getRvi()));
\r
78 } else if (proposal.getLabel() != null && proposal.getLabel().length() >= contents.length() && proposal.getLabel().substring(0, contents.length()).equalsIgnoreCase(contents)) {
\r
79 list.add(new ContentProposal(proposal.getRvi(),proposal.getLabel(), null));
\r
83 for (ChartVariable proposal : proposals) {
\r
84 if (proposal.getLabel() != null && proposal.getLabel().length() >= contents.length() && proposal.getLabel().substring(0, contents.length()).equalsIgnoreCase(contents)) {
\r
85 list.add(new ContentProposal(proposal.getRvi(),proposal.getLabel(), null));
\r
90 return (IContentProposal[]) list.toArray(new IContentProposal[list
\r
93 if (contentProposals == null) {
\r
94 contentProposals = new IContentProposal[proposals.size()];
\r
95 Iterator<ChartVariable> iter = proposals.iterator();
\r
96 for (int i = 0; i < proposals.size(); i++) {
\r
97 ChartVariable proposal = iter.next();
\r
98 if (proposal.getLabel() != null)
\r
99 contentProposals[i] = new ContentProposal(proposal.getRvi(),proposal.getLabel(),null);
\r
101 contentProposals[i] = new ContentProposal(proposal.getRvi());
\r
104 return contentProposals;
\r
108 * Set the Strings to be used as content proposals.
\r
111 * the array of Strings to be used as proposals.
\r
113 public void setProposals(Collection<ChartVariable> items) {
\r
114 this.proposals = items;
\r
115 contentProposals = null;
\r
119 * Set the boolean that controls whether proposals are filtered according to
\r
120 * the current field content.
\r
122 * @param filterProposals
\r
123 * <code>true</code> if the proposals should be filtered to
\r
124 * show only those that match the current contents of the field,
\r
125 * and <code>false</code> if the proposals should remain the
\r
126 * same, ignoring the field content.
\r
129 public void setFiltering(boolean filterProposals) {
\r
130 this.filterProposals = filterProposals;
\r
131 // Clear any cached proposals.
\r
132 contentProposals = null;
\r
136 * Provides all variables a model contains. Given resource needs to be
\r
137 * part of a model (i.e. using PartOf leads eventually to a SysdynModel).
\r
139 * @param control Control that is using this provider
\r
140 * @param resource A resource that is part of a model
\r
142 public VariableProposalProvider(final Control control, WidgetSupport support) {
\r
143 this.proposals = new ArrayList<ChartVariable>();
\r
144 support.register(this);
\r
145 this.control = control;
\r
148 private Resource resource;
\r
149 private Control control;
\r
152 public void setInput(ISessionContext context, Object input) {
\r
154 final Resource resource = AdaptionUtils.adaptToSingle(input, Resource.class);
\r
155 if(resource == null)
\r
157 this.resource = resource;
\r
159 SimanticsUI.getSession().asyncRequest(
\r
160 new AllVariablesOfModel(resource)
\r
161 , new Listener<Collection<ChartVariable>>() {
\r
164 public void execute(Collection<ChartVariable> result) {
\r
165 setProposals(result);
\r
169 public void exception(Throwable t) {
\r
170 t.printStackTrace();
\r
174 public boolean isDisposed() {
\r
175 return control == null ||
\r
176 control.isDisposed() ||
\r
177 !resource.equals(VariableProposalProvider.this.resource);
\r