Search in sources :

Example 1 with AbstractSource

use of org.eclipse.wb.internal.core.nls.model.AbstractSource in project windowbuilder by eclipse.

the class StringPropertyDialog method okPressed.

@Override
protected void okPressed() {
    final String value = m_valueText.getText();
    if (m_component != null) {
        ExecutionUtils.run(m_component, new RunnableEx() {

            @Override
            public void run() throws Exception {
                if (m_genericProperty != null) {
                    // replace with StringLiteral
                    if (!m_nlsButton.getSelection() && m_initialSource != null) {
                        m_initialSource.replace_toStringLiteral(m_genericProperty, value);
                        return;
                    }
                    // use different key in different source
                    if (m_nlsButton.getSelection()) {
                        AbstractSource selectedSource = m_support.getAttachedSource(m_editableSupport, m_selectedEditSource);
                        selectedSource.useKey(m_genericProperty, m_selectedKey);
                        if (isValueDifferentThanInSource()) {
                            m_property.setValue(value);
                        }
                        return;
                    }
                }
                m_property.setValue(value);
            }

            /**
             * @return <code>true</code> if value in NLS source and in editor is not same, so user
             *         changed it and we should update NLS source.
             */
            private boolean isValueDifferentThanInSource() {
                String valueInSource = m_selectedEditSource.getValue(m_locale, m_selectedKey);
                return !value.equals(valueInSource);
            }
        });
    } else {
        ExecutionUtils.runLog(new RunnableEx() {

            @Override
            public void run() throws Exception {
                m_property.setValue(value);
            }
        });
    }
    // close dialog
    super.okPressed();
}
Also used : AbstractSource(org.eclipse.wb.internal.core.nls.model.AbstractSource) RunnableEx(org.eclipse.wb.internal.core.utils.execution.RunnableEx)

Example 2 with AbstractSource

use of org.eclipse.wb.internal.core.nls.model.AbstractSource in project windowbuilder by eclipse.

the class GenericPropertyImpl method process_NLSSupport_specialFunctionality.

private boolean process_NLSSupport_specialFunctionality(final Object value) throws Exception {
    if (value == UNKNOWN_VALUE) {
        return false;
    }
    if (!NlsSupport.isStringProperty(this)) {
        return false;
    }
    final NlsSupport support = NlsSupport.get(m_javaInfo);
    // check if key name is used
    if (value instanceof String) {
        String stringValue = (String) value;
        String keyPrefix = m_javaInfo.getDescription().getToolkit().getPreferences().getString(IPreferenceConstants.P_NLS_KEY_AS_VALUE_PREFIX);
        if (!StringUtils.isEmpty(keyPrefix) && stringValue.startsWith(keyPrefix)) {
            final String key = stringValue.substring(keyPrefix.length());
            final AbstractSource source = support.getKeySource(key);
            if (source != null) {
                ExecutionUtils.run(m_javaInfo, new RunnableEx() {

                    @Override
                    public void run() throws Exception {
                        source.useKey(m_this, key);
                    }
                });
                return true;
            }
        }
    }
    // check for externalized String property
    {
        final Expression expression = getExpression();
        if (expression != null) {
            if (support.isExternalized(expression)) {
                ExecutionUtils.run(m_javaInfo, new RunnableEx() {

                    @Override
                    public void run() throws Exception {
                        String string = value == UNKNOWN_VALUE ? null : (String) value;
                        support.setValue(expression, string);
                    }
                });
                return true;
            }
        }
    }
    // no NLS
    return false;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) AbstractSource(org.eclipse.wb.internal.core.nls.model.AbstractSource) RunnableEx(org.eclipse.wb.internal.core.utils.execution.RunnableEx) NlsSupport(org.eclipse.wb.internal.core.nls.NlsSupport)

Example 3 with AbstractSource

use of org.eclipse.wb.internal.core.nls.model.AbstractSource in project windowbuilder by eclipse.

the class ModernEclipseSource method getPossibleSources.

/**
 * Return "possible" sources that exist in given package.
 *
 * "Possible" source is source that exists in current package, but is not used in current unit. We
 * show "possible" sources only if there are no "real" sources.
 */
public static List<AbstractSource> getPossibleSources(JavaInfo root, IPackageFragment pkg) throws Exception {
    List<AbstractSource> sources = Lists.newArrayList();
    // check that there is "NLS" type
    IType typeNLS = root.getEditor().getJavaProject().findType("org.eclipse.osgi.util.NLS");
    if (typeNLS != null) {
        IJavaElement[] packageElements = pkg.getChildren();
        for (int i = 0; i < packageElements.length; i++) {
            ICompilationUnit unit = (ICompilationUnit) packageElements[i];
            IType type = unit.findPrimaryType();
            if (type != null) {
                // check that type is is successor of NLS
                if (CodeUtils.isSuccessorOf(type, typeNLS)) {
                    try {
                        String accessorClassName = type.getFullyQualifiedName();
                        AbstractSource source = new ModernEclipseSource(root, accessorClassName, null);
                        sources.add(source);
                    } catch (Throwable e) {
                        DesignerPlugin.log(e);
                    }
                }
            }
        }
    }
    return sources;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AbstractSource(org.eclipse.wb.internal.core.nls.model.AbstractSource) IType(org.eclipse.jdt.core.IType)

Example 4 with AbstractSource

use of org.eclipse.wb.internal.core.nls.model.AbstractSource in project windowbuilder by eclipse.

the class DirectSource method getPossibleSources.

/**
 * Return "possible" sources that exist in given package.
 *
 * "Possible" source is source that exists in current package, but is not used in current unit. We
 * show "possible" sources only if there are no "real" sources.
 */
public static List<AbstractSource> getPossibleSources(JavaInfo root, IPackageFragment pkg) throws Exception {
    List<AbstractSource> sources = Lists.newArrayList();
    // 
    Object[] nonJavaResources = pkg.getNonJavaResources();
    for (int i = 0; i < nonJavaResources.length; i++) {
        Object o = nonJavaResources[i];
        if (o instanceof IFile) {
            IFile file = (IFile) o;
            String fileName = file.getName();
            // we need .properties files
            if (!fileName.endsWith(".properties")) {
                continue;
            }
            // we need only main (default) bundles
            if (fileName.indexOf('_') != -1) {
                continue;
            }
            // check first line for required comment
            InputStream is = file.getContents(true);
            String firstLine = IOUtils2.readFirstLine(is);
            if (firstLine == null || !firstLine.startsWith("#" + BUNDLE_COMMENT)) {
                continue;
            }
            // OK, this is probably correct source
            try {
                String bundleName = pkg.getElementName() + "." + StringUtils.substring(fileName, 0, -".properties".length());
                AbstractSource source = new DirectSource(root, bundleName);
                sources.add(source);
            } catch (Throwable e) {
                DesignerPlugin.log(e);
            }
        }
    }
    // 
    return sources;
}
Also used : IFile(org.eclipse.core.resources.IFile) InputStream(java.io.InputStream) AbstractSource(org.eclipse.wb.internal.core.nls.model.AbstractSource)

Example 5 with AbstractSource

use of org.eclipse.wb.internal.core.nls.model.AbstractSource in project windowbuilder by eclipse.

the class NlsSupport method getValue.

/**
 * Supports conversion of {@link Expression} into NLS value during parsing, i.e. when no root
 * {@link JavaInfo} is known.
 *
 * @return the {@link String} value or <code>null</code> if given {@link Expression} does not
 *         represent any known NLS pattern.
 */
public static String getValue(JavaInfo component, Expression expression) throws Exception {
    for (SourceDescription sourceDescription : getSourceDescriptions(component)) {
        try {
            List<AbstractSource> sources = Lists.newArrayList();
            AbstractSource source = sourceDescription.getSource(component, null, expression, sources);
            if (source != null) {
                setSource(expression, source);
                return source.getValue(expression);
            }
        } catch (Throwable e) {
        }
    }
    return null;
}
Also used : AbstractSource(org.eclipse.wb.internal.core.nls.model.AbstractSource)

Aggregations

AbstractSource (org.eclipse.wb.internal.core.nls.model.AbstractSource)20 NlsSupport (org.eclipse.wb.internal.core.nls.NlsSupport)5 GenericProperty (org.eclipse.wb.internal.core.model.property.GenericProperty)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 IJavaElement (org.eclipse.jdt.core.IJavaElement)3 IType (org.eclipse.jdt.core.IType)3 Expression (org.eclipse.jdt.core.dom.Expression)3 IEditableSupport (org.eclipse.wb.internal.core.nls.edit.IEditableSupport)3 ContainerInfo (org.eclipse.wb.internal.swing.model.component.ContainerInfo)3 InputStream (java.io.InputStream)2 IFile (org.eclipse.core.resources.IFile)2 IField (org.eclipse.jdt.core.IField)2 IMethod (org.eclipse.jdt.core.IMethod)2 JavaInfo (org.eclipse.wb.core.model.JavaInfo)2 Property (org.eclipse.wb.internal.core.model.property.Property)2 EditableSupport (org.eclipse.wb.internal.core.nls.edit.EditableSupport)2 IEditableSource (org.eclipse.wb.internal.core.nls.edit.IEditableSource)2 RunnableEx (org.eclipse.wb.internal.core.utils.execution.RunnableEx)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1