+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.swt.contentassist;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.Collection;\r
+import java.util.List;\r
+\r
+import org.eclipse.jface.fieldassist.IContentProposal;\r
+import org.eclipse.jface.fieldassist.IContentProposalProvider;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ *\r
+ * @param <T>\r
+ */\r
+public class NamedObjectContentProposalProvider<T extends INamedObject> implements IContentProposalProvider {\r
+\r
+ private final Collection<T> content;\r
+\r
+ public NamedObjectContentProposalProvider(T[] content) {\r
+ this(Arrays.asList(content));\r
+ }\r
+\r
+ public NamedObjectContentProposalProvider(Collection<T> content) {\r
+ this.content = content;\r
+ }\r
+\r
+ @Override\r
+ public IContentProposal[] getProposals(String contents, int position) {\r
+ String prefix = contents.substring(0, position).toLowerCase();\r
+\r
+ List<INamedObject> result = new ArrayList<INamedObject>(content.size());\r
+ for (INamedObject obj : content) {\r
+ if (obj.getName().toLowerCase().startsWith(prefix))\r
+ result.add(obj);\r
+ }\r
+\r
+ IContentProposal[] proposals = new IContentProposal[result.size()];\r
+ for (int i = 0; i < result.size(); ++i)\r
+ proposals[i] = createProposal(result.get(i), position);\r
+\r
+ return proposals;\r
+ }\r
+\r
+ public INamedObjectContentProposal createProposal(INamedObject object, int position) {\r
+ return new NamedObjectContentProposal((NamedObject<?>) object, position);\r
+ }\r
+\r
+}\r