1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt.contentassist;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.List;
19 import org.eclipse.jface.fieldassist.IContentProposal;
20 import org.eclipse.jface.fieldassist.IContentProposalProvider;
23 * @author Tuukka Lehtonen
27 public class NamedObjectContentProposalProvider<T extends INamedObject> implements IContentProposalProvider {
29 private final Collection<T> content;
31 public NamedObjectContentProposalProvider(T[] content) {
32 this(Arrays.asList(content));
35 public NamedObjectContentProposalProvider(Collection<T> content) {
36 this.content = content;
40 public IContentProposal[] getProposals(String contents, int position) {
41 String prefix = contents.substring(0, position).toLowerCase();
43 List<INamedObject> result = new ArrayList<INamedObject>(content.size());
44 for (INamedObject obj : content) {
45 if (obj.getName().toLowerCase().startsWith(prefix))
49 IContentProposal[] proposals = new IContentProposal[result.size()];
50 for (int i = 0; i < result.size(); ++i)
51 proposals[i] = createProposal(result.get(i), position);
56 public INamedObjectContentProposal createProposal(INamedObject object, int position) {
57 return new NamedObjectContentProposal((NamedObject<?>) object, position);