/******************************************************************************* | |
* Copyright (c) 2004-2008 Istvan Rath and Daniel Varro | |
* All rights reserved. This program and the accompanying materials | |
* are made available under the terms of the Eclipse Public License v1.0 | |
* which accompanies this distribution, and is available at | |
* http://www.eclipse.org/legal/epl-v10.html | |
* | |
* Contributors: | |
* Istvan Rath - initial API and implementation | |
*******************************************************************************/ | |
package org.eclipse.viatra2.treeeditor.properties.descriptors; | |
import java.util.ArrayList; | |
import org.eclipse.jface.fieldassist.ContentProposalAdapter; | |
import org.eclipse.jface.fieldassist.IContentProposal; | |
import org.eclipse.jface.fieldassist.IContentProposalProvider; | |
import org.eclipse.jface.fieldassist.TextContentAdapter; | |
import org.eclipse.jface.viewers.CellEditor; | |
import org.eclipse.jface.viewers.TextCellEditor; | |
import org.eclipse.swt.widgets.Composite; | |
import org.eclipse.swt.widgets.Control; | |
import org.eclipse.ui.views.properties.TextPropertyDescriptor; | |
import org.eclipse.viatra2.core.IModelElement; | |
/** | |
* Property descriptor for VPM FQNs. | |
* @author Istvan Rath | |
* | |
*/ | |
public class FQNPropertyDescriptor extends TextPropertyDescriptor | |
{ | |
IModelElement iElement; | |
public FQNPropertyDescriptor(Object id, String displayName, IModelElement element) { | |
super(id, displayName); | |
iElement = element; | |
} | |
@Override | |
public CellEditor createPropertyEditor(Composite parent) | |
{ | |
return new FQNCellEditor(parent); | |
} | |
public class FQNCellEditor extends TextCellEditor | |
{ | |
public FQNCellEditor(Composite parent) | |
{ | |
super(parent); | |
} | |
@Override | |
protected Control createControl(Composite parent) { | |
Control c = super.createControl(parent); | |
try | |
{ | |
// set up content assist | |
@SuppressWarnings("unused") | |
ContentProposalAdapter a = new ContentProposalAdapter( | |
c, | |
new TextContentAdapter(), | |
//new SimpleContentProposalProvider(new String[]{"A","B","C"}), | |
new FQNContentProposalProvider(), | |
null,//KeyStroke.getInstance("Ctrl+Space"), | |
null | |
); | |
} | |
catch (Exception e) { } | |
return c; | |
} | |
} | |
public class FQNContentProposal implements IContentProposal | |
{ | |
public FQNContentProposal(IModelElement aE, int origPos) | |
{ | |
e = aE; | |
origPosition = origPos; | |
} | |
IModelElement e; | |
int origPosition; | |
public String getContent() { | |
return e.getFullyQualifiedName().substring(origPosition); | |
} | |
public int getCursorPosition() { | |
return getContent().length(); // place cursor at the very end | |
} | |
public String getDescription() { | |
return "A fully qualified name whose prefix matches the content."; | |
} | |
public String getLabel() { | |
return e.getFullyQualifiedName(); | |
} | |
} | |
public class FQNContentProposalProvider implements IContentProposalProvider | |
{ | |
public IContentProposal[] getProposals(String contents, int position) | |
{ | |
String validFQN = contents.substring(0,Math.max(0, contents.lastIndexOf("."))); | |
String remainder = contents.substring(contents.lastIndexOf(".")+1); | |
IModelElement validRoot = iElement.getModelSpace().getModelManager().getElementByName(validFQN); | |
if (validRoot!=null) | |
{ | |
ArrayList<IContentProposal> r = new ArrayList<IContentProposal>(); | |
for (IModelElement e : validRoot.getElementsInNamespace()) | |
{ | |
if (e.getName().startsWith(remainder)) | |
r.add(new FQNContentProposal(e,position)); | |
} | |
return r.toArray(new IContentProposal[]{}); | |
} | |
return null; | |
} | |
} | |
} |