blob: 432bd09acddad38a2dfa596c9082603b4d43e8d2 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2009, 2020 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.internal.ltk.ui;
import java.net.URL;
import java.util.regex.Pattern;
import org.osgi.framework.Bundle;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
import org.eclipse.statet.jcommons.lang.Nullable;
import org.eclipse.statet.ecommons.ui.SharedUIResources;
import org.eclipse.statet.ltk.ui.sourceediting.assist.ContentAssistComputerRegistry;
@NonNullByDefault
public class AdvancedExtensionsInternal {
public static final String CONTENTASSIST_EXTENSIONPOINT_ID= "org.eclipse.statet.ltk.AdvancedContentAssist"; //$NON-NLS-1$
public static final String INFOHOVER_EXTENSIONPOINT_ID= "org.eclipse.statet.ltk.AdvancedInfoHover"; //$NON-NLS-1$
/** The extension schema name of the contribution id attribute. */
public static final String CONFIG_ID_ATTRIBUTE_NAME= "id"; //$NON-NLS-1$
/** The extension schema name of the contribution name attribute. */
public static final String CONFIG_NAME_ATTRIBUTE_NAME= "name"; //$NON-NLS-1$
/** The extension schema name of the content type id attribute referencing to a content type id. */
public static final String CONFIG_CONTENT_TYPE_ID_ATTRIBUTE_NAME= "contentTypeId"; //$NON-NLS-1$
/** The extension schema name of the category id attribute referencing to a category id. */
public static final String CONFIG_CATEGORY_ID_ATTRIBUTE_NAME= "categoryId"; //$NON-NLS-1$
/** The extension schema name of the partition type element. */
public static final String CONFIG_PARTITION_ELEMENT_NAME= "partition"; //$NON-NLS-1$
/** The extension schema name of the computer type element. */
public static final String CONFIG_COMPUTER_ELEMENT_NAME= "computer"; //$NON-NLS-1$
/** The extension schema name of the category type element. */
public static final String CONFIG_CATEGORY_ELEMENT_NAME= "category"; //$NON-NLS-1$
/** The extension schema name of the content type id attribute of the partition. */
public static final String CONFIG_CONTENTTYPE_ID_ELEMENT_NAME= "partitionType"; //$NON-NLS-1$
/** The extension schema name of the class attribute. */
public static final String CONFIG_CLASS_ATTRIBUTE_NAME= "class"; //$NON-NLS-1$
/** The extension schema name of the icon resource attribute. */
public static final String CONFIG_ICON_ATTRIBUTE_NAME= "icon"; //$NON-NLS-1$
private static final Pattern MODIFIERS_PATTERN= Pattern.compile("\\s*\\+\\s*"); //$NON-NLS-1$
public static final String getCheckedString(final IConfigurationElement element, final String attrName) throws CoreException {
final String s= element.getAttribute(attrName);
if (s == null || s.length() == 0) {
throw new CoreException(new Status(IStatus.ERROR, SharedUIResources.BUNDLE_ID, -1,
NLS.bind("missing value for attribute ''{0}''", attrName), null));
}
return s;
}
public static final @Nullable ImageDescriptor getImageDescriptor(final IConfigurationElement element, final String attrName) throws CoreException {
final String imagePath= element.getAttribute(attrName);
if (imagePath != null) {
final Bundle bundle= ContentAssistComputerRegistry.getBundle(element);
final URL url= FileLocator.find(bundle, new Path(imagePath), null);
if (url != null) {
return ImageDescriptor.createFromURL(url);
}
}
return null;
}
/**
* Computes the state mask for the given modifier string.
*
* @param modifiers the string with the modifiers, separated by '+'
* @return the state mask or -1 if the input is invalid
*/
public static int computeStateMask(final @Nullable String modifiers) {
if (modifiers == null) {
return -1;
}
if (modifiers.isEmpty()) {
return SWT.NONE;
}
final String[] mods= MODIFIERS_PATTERN.split(modifiers);
int stateMask= 0;
for (final String modifier : mods) {
final String key= modifier.toUpperCase();
if (key.equals("CTRL")) { //$NON-NLS-1$
stateMask |= SWT.CTRL;
}
else if (key.equals("ALT")) { //$NON-NLS-1$
stateMask |= SWT.ALT;
}
else if (key.equals("SHIFT")) { //$NON-NLS-1$
stateMask |= SWT.SHIFT;
}
else if (key.equals("COMMAND")) { //$NON-NLS-1$
stateMask |= SWT.COMMAND;
}
else if (key.equals("M1")) { //$NON-NLS-1$
stateMask |= SWT.MOD1;
}
else if (key.equals("M2")) { //$NON-NLS-1$
stateMask |= SWT.MOD2;
}
else if (key.equals("M3")) { //$NON-NLS-1$
stateMask |= SWT.MOD3;
}
else if (key.equals("M4")) { //$NON-NLS-1$
stateMask |= SWT.MOD4;
}
else {
return -1;
}
}
return stateMask;
}
public static String createUnifiedStateMaskString(final int stateMask) {
if (stateMask != 0) {
final StringBuilder sb= new StringBuilder();
if ((stateMask & SWT.MOD1) == SWT.MOD1) {
sb.append("M1+"); //$NON-NLS-1$
}
if ((stateMask & SWT.MOD2) == SWT.MOD2) {
sb.append("M2+"); //$NON-NLS-1$
}
if ((stateMask & SWT.MOD3) == SWT.MOD3) {
sb.append("M3+"); //$NON-NLS-1$
}
if (SWT.MOD4 != 0 && (stateMask & SWT.MOD4) == SWT.MOD4) {
sb.append("M4+"); //$NON-NLS-1$
}
if (sb.length() > 0) {
return sb.substring(0, sb.length()-1);
}
}
return ""; //$NON-NLS-1$
}
}