| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| * |
| * This copyright notice shows up in the generated Java code |
| * |
| */ |
| |
| package org.eclipse.osbp.xtext.menu.ui.contentassist |
| |
| import com.google.inject.Inject |
| import org.eclipse.emf.ecore.EObject |
| import org.eclipse.jface.viewers.StyledString |
| import org.eclipse.osbp.xtext.basic.ui.contentassist.BasicDSLProposalProviderHelper |
| import org.eclipse.osbp.xtext.menu.MenuEntry |
| import org.eclipse.osbp.xtext.menu.ui.MenuDSLDocumentationTranslator |
| import org.eclipse.swt.layout.FillLayout |
| import org.eclipse.swt.widgets.Shell |
| import org.eclipse.xtext.Assignment |
| import org.eclipse.xtext.Keyword |
| import org.eclipse.xtext.RuleCall |
| import org.eclipse.xtext.common.ui.contentassist.TerminalsProposalProvider |
| import org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal |
| import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext |
| import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor |
| import org.eclipse.xtext.ui.editor.contentassist.ReplacementTextApplier |
| import org.mihalis.opal.imageSelector.ImageSelectorDialog |
| |
| class ImageFileNameTextApplier extends ReplacementTextApplier { |
| var ContentAssistContext context |
| var String[] extensions |
| |
| def setContext(ContentAssistContext context) { |
| this.context = context |
| } |
| |
| def setExtensions(String[] fileExtensions) { |
| extensions = fileExtensions |
| } |
| |
| // this will inject a file dialog when selecting the file picker proposal |
| override getActualReplacementString(ConfigurableCompletionProposal proposal) { |
| var display = context.getViewer().getTextWidget().getDisplay(); |
| var shell = new Shell(display); |
| shell.setLayout(new FillLayout()); |
| var imageSelectorDialog = new ImageSelectorDialog(shell, 16); |
| imageSelectorDialog.setFilterExtensions(extensions) |
| var imageFileName = imageSelectorDialog.open(true); |
| return if (imageFileName !== null) "\"".concat(imageFileName).concat("\"") else ""; |
| } |
| } |
| |
| /** |
| * see http://www.eclipse.org/Xtext/documentation.html#contentAssist on how to customize content assistant |
| */ |
| class MenuDslProposalProvider extends AbstractMenuDslProposalProvider { |
| var imageSelectionPrompt = "Select image file..." |
| |
| @Inject TerminalsProposalProvider provider |
| @Inject BasicDSLProposalProviderHelper providerHelper |
| |
| override protected StyledString getKeywordDisplayString(Keyword keyword) { |
| return BasicDSLProposalProviderHelper.getKeywordDisplayString(keyword, MenuDSLDocumentationTranslator.instance()) |
| } |
| |
| /** |
| * This override will enable 1 length non letter characters as keyword. |
| */ |
| override protected boolean isKeywordWorthyToPropose(Keyword keyword) { |
| return true |
| } |
| |
| override completeMenuEntry_Icon(EObject model, Assignment assignment, ContentAssistContext context, |
| ICompletionProposalAcceptor acceptor) { |
| imageFilePickerProposal(model, assignment, context, acceptor, ".png") |
| } |
| |
| override completeMenuPackage_CollapsedImage(EObject model, Assignment assignment, ContentAssistContext context, |
| ICompletionProposalAcceptor acceptor) { |
| imageFilePickerProposal(model, assignment, context, acceptor, ".png") |
| } |
| |
| override completeMenuPackage_ExpandedImage(EObject model, Assignment assignment, ContentAssistContext context, |
| ICompletionProposalAcceptor acceptor) { |
| imageFilePickerProposal(model, assignment, context, acceptor, ".png") |
| } |
| |
| def imageFilePickerProposal(EObject model, Assignment assignment, ContentAssistContext context, |
| ICompletionProposalAcceptor acceptor, String fileExtensions) { |
| var fileName = createCompletionProposal(imageSelectionPrompt, context) as ConfigurableCompletionProposal |
| if (fileName != null) { |
| var applier = new ImageFileNameTextApplier() |
| applier.setExtensions(fileExtensions.split(",")) |
| applier.setContext(context) |
| fileName.setTextApplier = applier |
| } |
| acceptor.accept(fileName) |
| } |
| |
| /** |
| * This method decides which proposals will be valid. |
| */ |
| override protected boolean isValidProposal(String proposal, String prefix, ContentAssistContext context) { |
| var result = super.isValidProposal(proposal, prefix, context) |
| if (context.getCurrentModel() instanceof MenuEntry) { |
| return isEntryValidProposal(context, proposal, result) |
| } |
| return result |
| } |
| |
| def isEntryValidProposal(ContentAssistContext context, String proposal, boolean result) { |
| if (isGrammarToken(proposal)) { |
| return true |
| } |
| return true |
| } |
| |
| def isGrammarToken(String proposal) { |
| |
| if (imageSelectionPrompt.equals(proposal)) { |
| return true |
| } |
| if ("dialog".equals(proposal)) { |
| return true |
| } |
| if ("image".equals(proposal)) { |
| return true |
| } |
| if ("table".equals(proposal)) { |
| return true |
| } |
| if ("perspective".equals(proposal)) { |
| return true |
| } |
| if ("process".equals(proposal)) { |
| return true |
| } |
| if ("showBadge".equals(proposal)) { |
| return true |
| } |
| if ("described by".equals(proposal)) { |
| return true |
| } |
| if ("entry".equals(proposal)) { |
| return true |
| } |
| if ("expandedImage".equals(proposal)) { |
| return true |
| } |
| if ("collapsedImage".equals(proposal)) { |
| return true |
| } |
| if ("width".equals(proposal)) { |
| return true |
| } |
| } |
| |
| override public void complete_QualifiedName(EObject model, RuleCall ruleCall, ContentAssistContext context, |
| ICompletionProposalAcceptor acceptor) { |
| providerHelper.complete_PackageName(model, ruleCall, context, acceptor, this) |
| } |
| |
| // override public void complete_MenuQualifiedNameWithWildCard(EObject model, RuleCall ruleCall, ContentAssistContext context, |
| // ICompletionProposalAcceptor acceptor) { |
| // providerHelper.complete_PackageName(model, ruleCall, context, acceptor, this) |
| // } |
| |
| // ------------------------ delegates to TerminalsProposalProvider ----------------- |
| override public void complete_TRANSLATABLESTRING(EObject model, RuleCall ruleCall, ContentAssistContext context, |
| ICompletionProposalAcceptor acceptor) { |
| provider.complete_STRING(model, ruleCall, context, acceptor) |
| } |
| |
| override public void complete_TRANSLATABLEID(EObject model, RuleCall ruleCall, ContentAssistContext context, |
| ICompletionProposalAcceptor acceptor) { |
| provider.complete_ID(model, ruleCall, context, acceptor) |
| } |
| |
| override public void completeMenuPackage_Width(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { |
| provider.complete_STRING(model, (assignment.getTerminal() as RuleCall), context, acceptor) |
| } |
| |
| } |