Search in sources :

Example 1 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project dbeaver by serge-rider.

the class ProjectExportWizard method saveResourceProperties.

private void saveResourceProperties(IResource resource, XMLBuilder xml) throws CoreException, IOException {
    if (resource instanceof IFile) {
        final IContentDescription contentDescription = ((IFile) resource).getContentDescription();
        if (contentDescription != null && contentDescription.getCharset() != null) {
            xml.addAttribute(ExportConstants.ATTR_CHARSET, contentDescription.getCharset());
        //xml.addAttribute(ExportConstants.ATTR_CHARSET, contentDescription.getContentType());
        }
    } else if (resource instanceof IFolder) {
        xml.addAttribute(ExportConstants.ATTR_DIRECTORY, true);
    }
    for (Object entry : resource.getPersistentProperties().entrySet()) {
        Map.Entry<?, ?> propEntry = (Map.Entry<?, ?>) entry;
        xml.startElement(ExportConstants.TAG_ATTRIBUTE);
        final QualifiedName attrName = (QualifiedName) propEntry.getKey();
        xml.addAttribute(ExportConstants.ATTR_QUALIFIER, attrName.getQualifier());
        xml.addAttribute(ExportConstants.ATTR_NAME, attrName.getLocalName());
        xml.addAttribute(ExportConstants.ATTR_VALUE, (String) propEntry.getValue());
        xml.endElement();
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) QualifiedName(org.eclipse.core.runtime.QualifiedName) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.

the class FileStoreTextFileBuffer method cacheEncodingState.

protected void cacheEncodingState() {
    fEncoding = fExplicitEncoding;
    fHasBOM = false;
    fIsCacheUpdated = true;
    try (InputStream stream = getFileContents(fFileStore)) {
        if (stream == null)
            return;
        QualifiedName[] options = new QualifiedName[] { IContentDescription.CHARSET, IContentDescription.BYTE_ORDER_MARK };
        IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(stream, fFileStore.getName(), options);
        if (description != null) {
            fHasBOM = description.getProperty(IContentDescription.BYTE_ORDER_MARK) != null;
            if (fEncoding == null)
                fEncoding = description.getCharset();
        }
    } catch (CoreException e) {
    // do nothing
    } catch (IOException e) {
    // do nothing
    }
    // Use global default
    if (fEncoding == null)
        fEncoding = fManager.getDefaultEncoding();
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) QualifiedName(org.eclipse.core.runtime.QualifiedName) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 3 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.

the class ResourceTextFileBufferManager method isTextFileLocation.

@Override
public boolean isTextFileLocation(IPath location, boolean strict) {
    Assert.isNotNull(location);
    location = FileBuffers.normalizeLocation(location);
    IFile file = FileBuffers.getWorkspaceFileAtLocation(location, true);
    if (file != null) {
        if (file.exists()) {
            try {
                IContentDescription description = file.getContentDescription();
                if (description != null) {
                    IContentType type = description.getContentType();
                    if (type != null)
                        return type.isKindOf(TEXT_CONTENT_TYPE);
                }
            } catch (CoreException x) {
            // ignore: API specification tells return true if content type can't be determined
            }
        } else {
            IContentTypeManager manager = Platform.getContentTypeManager();
            IContentType[] contentTypes = manager.findContentTypesFor(file.getName());
            if (contentTypes != null && contentTypes.length > 0) {
                for (IContentType contentType : contentTypes) if (contentType.isKindOf(TEXT_CONTENT_TYPE))
                    return true;
                return false;
            }
        }
        return !strict;
    }
    return isTextFileLocation(FileBuffers.getFileStoreAtLocation(location), strict);
}
Also used : IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentType(org.eclipse.core.runtime.content.IContentType) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 4 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.

the class TextFileBufferManager method isTextFileLocation.

/**
 * Returns whether a file store at the given location is or can be considered a
 * text file. If the file store exists, the concrete content type of the file store is
 * checked. If the concrete content type for the existing file store can not be
 * determined, this method returns <code>!strict</code>. If the file store does
 * not exist, it is checked whether a text content type is associated with
 * the given location. If no content type is associated with the location,
 * this method returns <code>!strict</code>.
 * <p>
 * The provided location is either a full path of a workspace resource or an
 * absolute path in the local file system. The file buffer manager does not
 * resolve the location of workspace resources in the case of linked
 * resources.
 * </p>
 *
 * @param fileStore	file store to check
 * @param strict	<code>true</code> if a file with unknown content type
 * 					is not treated as text file, <code>false</code> otherwise
 * @return <code>true</code> if the location is a text file location
 * @since 3.3
 */
protected boolean isTextFileLocation(IFileStore fileStore, boolean strict) {
    if (fileStore == null)
        return false;
    IContentTypeManager manager = Platform.getContentTypeManager();
    IFileInfo fileInfo = fileStore.fetchInfo();
    if (fileInfo.exists()) {
        try (InputStream is = fileStore.openInputStream(EFS.NONE, null)) {
            IContentDescription description = manager.getDescriptionFor(is, fileStore.getName(), IContentDescription.ALL);
            if (description != null) {
                IContentType type = description.getContentType();
                if (type != null)
                    return type.isKindOf(TEXT_CONTENT_TYPE);
            }
        } catch (CoreException ex) {
        // ignore: API specification tells return true if content type can't be determined
        } catch (IOException ex) {
        // ignore: API specification tells return true if content type can't be determined
        }
        return !strict;
    }
    IContentType[] contentTypes = manager.findContentTypesFor(fileStore.getName());
    if (contentTypes != null && contentTypes.length > 0) {
        for (IContentType contentType : contentTypes) if (contentType.isKindOf(TEXT_CONTENT_TYPE))
            return true;
        return false;
    }
    return !strict;
}
Also used : IFileInfo(org.eclipse.core.filesystem.IFileInfo) CoreException(org.eclipse.core.runtime.CoreException) InputStream(java.io.InputStream) IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 5 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project liferay-ide by liferay.

the class CodePropertyEditorRenderer method createContents.

@Override
protected void createContents(Composite parent) {
    PropertyEditorPart part = getPart();
    Composite scriptEditorParent = createMainComposite(parent, new CreateMainCompositeDelegate(part) {

        @Override
        public boolean canScaleVertically() {
            return true;
        }
    });
    context.adapt(scriptEditorParent);
    int textFieldParentColumns = 1;
    SapphireToolBarActionPresentation toolBarActionsPresentation = new SapphireToolBarActionPresentation(getActionPresentationManager());
    boolean actionsToolBarNeeded = toolBarActionsPresentation.hasActions();
    if (actionsToolBarNeeded || true)
        textFieldParentColumns++;
    scriptEditorParent.setLayout(glayout(textFieldParentColumns, 0, 0, 0, 0));
    Composite nestedComposite = new Composite(scriptEditorParent, SWT.NONE);
    nestedComposite.setLayoutData(gdfill());
    nestedComposite.setLayout(glspacing(glayout(2, 0, 0), 2));
    this.context.adapt(nestedComposite);
    PropertyEditorAssistDecorator decorator = createDecorator(nestedComposite);
    decorator.control().setLayoutData(gdvalign(gd(), SWT.TOP));
    decorator.addEditorControl(nestedComposite);
    /*
		 * scriptEditorParent.setLayout( new FillLayout( SWT.HORIZONTAL ) );
		 * scriptEditorParent.setLayoutData( gdhhint( gdfill(), 300 ) );
		 */
    PropertyEditorInput editorInput = new PropertyEditorInput(part.getLocalModelElement(), (ValueProperty) part.getProperty());
    try {
        ScriptLanguageType scriptLang = context.getPart().getLocalModelElement().nearest(IScriptable.class).getScriptLanguage().getContent(false);
        String fileName = scriptLang.getClass().getField(scriptLang.toString()).getAnnotation(DefaultValue.class).text();
        IContentDescription contentDescription = ContentTypeManager.getInstance().getDescriptionFor(editorInput.getStorage().getContents(), fileName, IContentDescription.ALL);
        EditorDescriptor defaultEditor = (EditorDescriptor) PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(editorInput.getName(), contentDescription.getContentType());
        _textEditor = (ITextEditor) defaultEditor.createEditor();
    } catch (Exception e1) {
    }
    IEditorReference ref = new ScriptEditorReference(_textEditor, editorInput);
    IEditorSite site = new ScriptEditorSite(ref, _textEditor, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage());
    try {
        _textEditor.init(site, editorInput);
        _textEditor.getDocumentProvider().getDocument(editorInput).addDocumentListener(new IDocumentListener() {

            public void documentAboutToBeChanged(DocumentEvent event) {
            }

            public void documentChanged(DocumentEvent event) {
                String content = event.getDocument().get();
                part.getLocalModelElement().write(((ValueProperty) part.getProperty()), content);
            }
        });
        ModelPropertyListener modelListener = new ModelPropertyListener() {

            @Override
            public void handlePropertyChangedEvent(ModelPropertyChangeEvent event) {
                CodePropertyEditorRenderer.this.textEditor.getDocumentProvider().getDocument(editorInput).set(part.getLocalModelElement().read(getProperty()).getText());
            }
        };
        part.getLocalModelElement().addListener(modelListener, part.getProperty().getName());
    } catch (PartInitException pie) {
        pie.printStackTrace();
    }
    Control[] prevChildren = scriptEditorParent.getChildren();
    // _textEditor.createPartControl( scriptEditorParent );
    new Label(scriptEditorParent, SWT.NONE);
    Control[] newChildren = scriptEditorParent.getChildren();
    decorator.addEditorControl(newChildren[prevChildren.length], true);
    if (actionsToolBarNeeded) {
        ToolBar toolbar = new ToolBar(scriptEditorParent, SWT.FLAT | SWT.HORIZONTAL);
        toolbar.setLayoutData(gdvfill());
        toolBarActionsPresentation.setToolBar(toolbar);
        toolBarActionsPresentation.render();
        context.adapt(toolbar);
        decorator.addEditorControl(toolbar);
    // relatedControls.add( toolbar );
    }
}
Also used : Label(org.eclipse.swt.widgets.Label) ScriptLanguageType(com.liferay.ide.kaleo.core.model.ScriptLanguageType) PropertyEditorAssistDecorator(org.eclipse.sapphire.ui.assist.internal.PropertyEditorAssistDecorator) DefaultValue(org.eclipse.sapphire.modeling.annotations.DefaultValue) Control(org.eclipse.swt.widgets.Control) IEditorReference(org.eclipse.ui.IEditorReference) PartInitException(org.eclipse.ui.PartInitException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) PropertyEditorPart(org.eclipse.sapphire.ui.PropertyEditorPart) ModelPropertyChangeEvent(org.eclipse.sapphire.modeling.ModelPropertyChangeEvent) Composite(org.eclipse.swt.widgets.Composite) IDocumentListener(org.eclipse.jface.text.IDocumentListener) DocumentEvent(org.eclipse.jface.text.DocumentEvent) EditorDescriptor(org.eclipse.ui.internal.registry.EditorDescriptor) PartInitException(org.eclipse.ui.PartInitException) SapphireToolBarActionPresentation(org.eclipse.sapphire.ui.swt.renderer.SapphireToolBarActionPresentation) ModelPropertyListener(org.eclipse.sapphire.modeling.ModelPropertyListener) ToolBar(org.eclipse.swt.widgets.ToolBar) IEditorSite(org.eclipse.ui.IEditorSite)

Aggregations

IContentDescription (org.eclipse.core.runtime.content.IContentDescription)58 CoreException (org.eclipse.core.runtime.CoreException)27 IOException (java.io.IOException)24 IContentType (org.eclipse.core.runtime.content.IContentType)22 InputStream (java.io.InputStream)19 IFile (org.eclipse.core.resources.IFile)14 QualifiedName (org.eclipse.core.runtime.QualifiedName)13 IContentTypeManager (org.eclipse.core.runtime.content.IContentTypeManager)8 Reader (java.io.Reader)6 IStatus (org.eclipse.core.runtime.IStatus)6 Status (org.eclipse.core.runtime.Status)6 BufferedReader (java.io.BufferedReader)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStreamReader (java.io.InputStreamReader)4 IStructuredFormatProcessor (org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor)4 SequenceInputStream (java.io.SequenceInputStream)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 IResource (org.eclipse.core.resources.IResource)3