From 8e8af6e3615c1b630f8207eb25f2fde64b7f7d2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20D=C3=B6bber?= <doebber@in.tum.de> Date: Mon, 12 Dec 2011 13:32:01 +0000 Subject: [PATCH] implemented simple code formatter and added generic field assist support refs 86 --- .../trunk/META-INF/MANIFEST.MF | 1 + .../base/ui/editor/TextEditorBase.java | 19 +- .../base/ui/fieldassist/FieldAssist.java | 171 ++++++++++++++++++ .../ui/fieldassist/IProposalProvider.java | 38 ++++ .../fieldassist/StaticProposalProvider.java | 44 +++++ 5 files changed, 261 insertions(+), 12 deletions(-) create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/fieldassist/FieldAssist.java create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/fieldassist/IProposalProvider.java create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/fieldassist/StaticProposalProvider.java diff --git a/org.fortiss.tooling.base.ui/trunk/META-INF/MANIFEST.MF b/org.fortiss.tooling.base.ui/trunk/META-INF/MANIFEST.MF index f62e17348..1b301674c 100644 --- a/org.fortiss.tooling.base.ui/trunk/META-INF/MANIFEST.MF +++ b/org.fortiss.tooling.base.ui/trunk/META-INF/MANIFEST.MF @@ -22,6 +22,7 @@ Export-Package: org.fortiss.tooling.base.ui, org.fortiss.tooling.base.ui.editpart.figure, org.fortiss.tooling.base.ui.editpart.policy, org.fortiss.tooling.base.ui.editpart.request, + org.fortiss.tooling.base.ui.fieldassist, org.fortiss.tooling.base.ui.layout, org.fortiss.tooling.base.ui.preferences, org.fortiss.tooling.base.ui.utils diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/TextEditorBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/TextEditorBase.java index 8f3df5280..85d61be2d 100644 --- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/TextEditorBase.java +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/TextEditorBase.java @@ -25,6 +25,7 @@ import org.eclipse.ui.IEditorSite; import org.eclipse.ui.PartInitException; import org.eclipse.ui.editors.text.TextEditor; import org.fortiss.tooling.kernel.service.ICommandStackService; +import org.fortiss.tooling.kernel.ui.extension.IModelEditor; import org.fortiss.tooling.kernel.ui.extension.IModelElementHandler; /** @@ -34,7 +35,8 @@ import org.fortiss.tooling.kernel.ui.extension.IModelElementHandler; * @version $Rev: 18709 $ * @ConQAT.Rating RED Hash: */ -public class TextEditorBase<T extends EObject> extends TextEditor { +public abstract class TextEditorBase<T extends EObject> extends TextEditor + implements IModelEditor<T> { /** * The object shown in this editor. This is valid as soon as @@ -49,22 +51,16 @@ public class TextEditorBase<T extends EObject> extends TextEditor { protected final EMFDataBindingContext dbc = new EMFDataBindingContext(); /** Returns the edited object. */ + @Override public T getEditedObject() { return editedObject; } - // /** {@inheritDoc} */ - // @Override - // protected void doSetInput(IEditorInput input) throws CoreException { - // super.doSetInput(new ModelElementEditorInput(getEditedObject())); - // } - /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void init(IEditorSite site, IEditorInput input) throws PartInitException { - // if (!(input instanceof ModelElementEditorInput)) { // throw new PartInitException("Expected input of type " // + ModelElementEditorInput.class); @@ -72,15 +68,14 @@ public class TextEditorBase<T extends EObject> extends TextEditor { // ModelElementEditorInput meInput = (ModelElementEditorInput) input; // // editedObject = (T) meInput.getModelElement(); - // if (editedObject == null) { // throw new PartInitException("Missing model element!"); // } // // handler = (IModelElementHandler<T>) meInput.getModelElementHandler(); - if (handler == null) { - throw new PartInitException("Missing model element handler!"); - } + // if (handler == null) { + // throw new PartInitException("Missing model element handler!"); + // } setSite(site); setInput(input); diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/fieldassist/FieldAssist.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/fieldassist/FieldAssist.java new file mode 100644 index 000000000..3e4947759 --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/fieldassist/FieldAssist.java @@ -0,0 +1,171 @@ +/*--------------------------------------------------------------------------+ +$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $ +| | +| Copyright 2011 ForTISS GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.base.ui.fieldassist; + +import org.eclipse.jface.bindings.keys.KeyStroke; +import org.eclipse.jface.bindings.keys.ParseException; +import org.eclipse.jface.fieldassist.ContentProposalAdapter; +import org.eclipse.jface.fieldassist.IContentProposal; +import org.eclipse.jface.fieldassist.IContentProposalListener; +import org.eclipse.jface.fieldassist.SimpleContentProposalProvider; +import org.eclipse.jface.fieldassist.TextContentAdapter; +import org.eclipse.swt.widgets.Text; + +/** + * This class provides easy access to auto completion in {@link Text} fields. + * Unlike the original this implementation takes care of proper in place + * insertion of proposals and provides ability to dynamically provide context + * sensitive proposals. + * + * This class is not yet thoroughly tested. + * + * @author doebber + * @author $Author: hoelzl $ + * @version $Rev: 18709 $ + * @ConQAT.Rating RED Hash: + */ +public class FieldAssist { + + /** The text field to assist. */ + private final Text field; + + /** + * ProposalProvider wrapper class that takes care of proper insertion. This + * is intended to be transparent to the implementor. + */ + private final AF3ContentProposalProvider provider; + + /** The keystroke the field assist is activated for. */ + private KeyStroke keystroke; + + /** + * Default keystroke for assist activation. Used if no other keystroke + * specified. + */ + public static final String DEFAULT_KEYSTROKE = "Ctrl+Space"; + + /** + * Array of chars the assist is activated for. A typical example in Java + * would be '.' as an auto activation char. + */ + private char[] autoActivationChars; + + /** + * Constructor. Sufficient for most applications. Initializes with default + * keystroke and no auto activation chars. + */ + public FieldAssist(Text field, IProposalProvider provider) { + this.field = field; + this.provider = new AF3ContentProposalProvider(provider); + initialize(); + } + + /** Constructor. Provides additional parameters which may be null. */ + public FieldAssist(Text field, IProposalProvider provider, + KeyStroke keystroke, char[] autoActivationChars) { + this.field = field; + this.provider = new AF3ContentProposalProvider(provider); + this.keystroke = keystroke; + this.autoActivationChars = autoActivationChars; + initialize(); + } + + /** Initializes the FieldAssist object. */ + private void initialize() { + provider.setFiltering(true); + if (keystroke == null) { + try { + keystroke = KeyStroke.getInstance(DEFAULT_KEYSTROKE); + } catch (ParseException e) { + // Should not happen + e.printStackTrace(); + } + } + ContentProposalAdapter adapter = new ContentProposalAdapter(field, + new TextContentAdapter(), provider, keystroke, + autoActivationChars); + adapter.setPropagateKeys(true); + adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_IGNORE); + adapter.addContentProposalListener(new IContentProposalListener() { + + @Override + /** {@inheritDoc} */ + public void proposalAccepted(IContentProposal proposal) { + int end = field.getCaretPosition(); + int start = findBeginningOfWord(field.getText(), end); + String newContents = field.getText().substring(0, + Math.max(0, start + 1)); + newContents = newContents.concat(proposal.getContent()); + newContents = newContents.concat(field.getText().substring( + Math.min(field.getText().length(), end))); + field.setText(newContents); + field.setSelection(start + proposal.getContent().length() + 1); + + } + }); + } + + /** + * Wrapper class transparent to the implementor that takes care of proper in + * place insertion. + */ + private final class AF3ContentProposalProvider extends + SimpleContentProposalProvider { + + /** + * Stores the custom proposal provider implemented for the concrete + * application. + */ + private IProposalProvider concreteProvider; + + /** + * Constructor for static proposals. Is never called but required by the + * compiler. + */ + public AF3ContentProposalProvider(String[] proposals) { + super(proposals); + this.concreteProvider = new StaticProposalProvider(proposals); + } + + /** Constructor. */ + public AF3ContentProposalProvider(IProposalProvider provider) { + super(new String[0]); + this.concreteProvider = provider; + } + + /** {@inheritDoc} */ + @Override + public IContentProposal[] getProposals(String contents, int position) { + int tmp = findBeginningOfWord(contents, position); + String currentWord = contents.substring( + Math.min(position, tmp + 1), position); + setProposals(concreteProvider.getProposals(currentWord)); + return super.getProposals(currentWord, position); + } + } + + /** Finds the beginning of the word the cursor is placed in. */ + private int findBeginningOfWord(String contents, int position) { + int tmp = Math.max(0, position - 1); + while (tmp > 0 && !Character.isWhitespace(contents.charAt(tmp))) { + // TODO include operators like '+' to be handled as whitespace + tmp--; + } + return tmp; + } +} diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/fieldassist/IProposalProvider.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/fieldassist/IProposalProvider.java new file mode 100644 index 000000000..f4fd88ad6 --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/fieldassist/IProposalProvider.java @@ -0,0 +1,38 @@ +/*--------------------------------------------------------------------------+ +$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $ +| | +| Copyright 2011 ForTISS GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.base.ui.fieldassist; + +/** + * Interface defining the {@link IProposalProvider} necessary to add custom + * field assist to text widgets. + * + * @author doebber + * @author $Author: hoelzl $ + * @version $Rev: 18709 $ + * @ConQAT.Rating RED Hash: + */ +public interface IProposalProvider { + + /** + * Returns the specific proposals. This method is called every time the + * assist window opens up. Implementors may generate and return dynamic + * proposals here. + */ + public String[] getProposals(String currentWord); + +} diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/fieldassist/StaticProposalProvider.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/fieldassist/StaticProposalProvider.java new file mode 100644 index 000000000..ba714341e --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/fieldassist/StaticProposalProvider.java @@ -0,0 +1,44 @@ +/*--------------------------------------------------------------------------+ +$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $ +| | +| Copyright 2011 ForTISS GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.base.ui.fieldassist; + +/** + * Implementation of an {@link IProposalProvider} with static proposals. + * + * @author doebber + * @author $Author: hoelzl $ + * @version $Rev: 18709 $ + * @ConQAT.Rating RED Hash: + */ +public final class StaticProposalProvider implements IProposalProvider { + + /** Stores the array of proposals. */ + private String[] proposals; + + /** Constructor. */ + public StaticProposalProvider(String[] proposals) { + this.proposals = proposals; + } + + /** {@inheritDoc} */ + @Override + public String[] getProposals(String currentWord) { + return proposals; + } + +} -- GitLab