Search in sources :

Example 21 with IDocumentListener

use of org.eclipse.jface.text.IDocumentListener in project liferay-ide by liferay.

the class AbstractKaleoEditorHelper method openEditor.

public void openEditor(ISapphirePart sapphirePart, Element modelElement, ValueProperty valueProperty) {
    try {
        Object content = modelElement.property(valueProperty).content();
        if (content == null) {
            content = "";
        }
        IProject project = sapphirePart.adapt(IProject.class);
        IEditorInput editorInput = modelElement.adapt(IEditorInput.class);
        String name = editorInput.getName();
        Node node = modelElement.nearest(Node.class);
        String nodeName = node.getName().content();
        HiddenFileEditorInput hiddenFileEditorInput = _getHiddenFileEditorInput(project, name, nodeName, content.toString());
        IEditorSite editorSite = sapphirePart.adapt(IEditorSite.class);
        IWorkbenchWindow wbWindow = editorSite.getWorkbenchWindow();
        IEditorPart editorPart = wbWindow.getActivePage().openEditor(hiddenFileEditorInput, _editorId);
        ITextEditor textEditor = (ITextEditor) editorPart.getAdapter(ITextEditor.class);
        IDocumentListener documentListener = new IDocumentListener() {

            public void documentAboutToBeChanged(DocumentEvent event) {
            }

            public void documentChanged(DocumentEvent event) {
                String contents = event.getDocument().get();
                modelElement.property(valueProperty).write(contents);
            }
        };
        IDocumentProvider documentProvider = textEditor.getDocumentProvider();
        documentProvider.getDocument(hiddenFileEditorInput).addDocumentListener(documentListener);
        IWorkbenchPartSite wbPartSite = editorPart.getSite();
        IPartListener partListener = new IPartListener() {

            public void partActivated(IWorkbenchPart part) {
            }

            public void partBroughtToTop(IWorkbenchPart part) {
            }

            public void partClosed(IWorkbenchPart part) {
                if ((part != null) && part.equals(editorPart)) {
                    new WorkspaceJob("delete temp editor file") {

                        @Override
                        public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
                            try {
                                IFile file = hiddenFileEditorInput.getFile();
                                file.getParent().delete(true, null);
                            } catch (CoreException ce) {
                            }
                            return Status.OK_STATUS;
                        }
                    }.schedule(100);
                }
            }

            public void partDeactivated(IWorkbenchPart part) {
            }

            public void partOpened(IWorkbenchPart part) {
            }
        };
        wbPartSite.getPage().addPartListener(partListener);
    } catch (Exception e) {
        KaleoUI.logError("Error opening editor.", e);
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IStatus(org.eclipse.core.runtime.IStatus) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IFile(org.eclipse.core.resources.IFile) IPartListener(org.eclipse.ui.IPartListener) Node(com.liferay.ide.kaleo.core.model.Node) IDocumentListener(org.eclipse.jface.text.IDocumentListener) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) IEditorPart(org.eclipse.ui.IEditorPart) DocumentEvent(org.eclipse.jface.text.DocumentEvent) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HiddenFileEditorInput(com.liferay.ide.kaleo.ui.editor.HiddenFileEditorInput) IWorkbenchPartSite(org.eclipse.ui.IWorkbenchPartSite) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) CoreException(org.eclipse.core.runtime.CoreException) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) IEditorInput(org.eclipse.ui.IEditorInput) IEditorSite(org.eclipse.ui.IEditorSite)

Example 22 with IDocumentListener

use of org.eclipse.jface.text.IDocumentListener in project hale by halestudio.

the class ValidatingSourceViewer method init.

/**
 * Initialize the Job and listener.
 */
protected void init() {
    validateJob = new Job("Source viewer validation") {

        @Override
        public boolean shouldRun() {
            return validationEnabled.get();
        }

        @Override
        public boolean shouldSchedule() {
            return validationEnabled.get();
        }

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            String content;
            changeLock.lock();
            try {
                if (!changed) {
                    return Status.OK_STATUS;
                }
                IDocument doc = getDocument();
                if (doc != null) {
                    content = doc.get();
                } else {
                    content = "";
                }
                changed = false;
            } finally {
                changeLock.unlock();
            }
            boolean success = false;
            try {
                // this is the potentially long running stuff
                success = validate(content);
            } catch (Exception e) {
                // ignore, but log
                log.warn("Error validating document content", e);
                success = false;
            }
            boolean notify = false;
            changeLock.lock();
            try {
                /*
					 * Only notify listeners if the document was not changed in
					 * the meantime and the valid state is different than
					 * before.
					 */
                notify = !changed && valid != success;
                if (notify) {
                    // set result
                    valid = success;
                }
            } finally {
                changeLock.unlock();
            }
            if (notify) {
                PropertyChangeEvent event = new PropertyChangeEvent(ValidatingSourceViewer.this, PROPERTY_VALID, !success, success);
                notifyOnPropertyChange(event);
            }
            return Status.OK_STATUS;
        }
    };
    validateJob.setUser(false);
    validateJob.setRule(new ExclusiveSchedulingRule(this));
    documentListener = new IDocumentListener() {

        @Override
        public void documentChanged(DocumentEvent event) {
            scheduleValidation();
        }

        @Override
        public void documentAboutToBeChanged(DocumentEvent event) {
        // do nothing
        }
    };
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) IDocumentListener(org.eclipse.jface.text.IDocumentListener) Job(org.eclipse.core.runtime.jobs.Job) DocumentEvent(org.eclipse.jface.text.DocumentEvent) IDocument(org.eclipse.jface.text.IDocument) ExclusiveSchedulingRule(eu.esdihumboldt.hale.ui.util.jobs.ExclusiveSchedulingRule)

Example 23 with IDocumentListener

use of org.eclipse.jface.text.IDocumentListener in project hale by halestudio.

the class XMLStylePage3 method createControl.

/**
 * @see IDialogPage#createControl(Composite)
 */
@Override
public void createControl(Composite parent) {
    changed = false;
    final Display display = parent.getDisplay();
    FillLayout fillLayout = new FillLayout();
    fillLayout.type = SWT.VERTICAL;
    parent.setLayout(fillLayout);
    CompositeRuler ruler = new CompositeRuler(3);
    LineNumberRulerColumn lineNumbers = new LineNumberRulerColumn();
    // SWT.COLOR_INFO_BACKGROUND));
    lineNumbers.setBackground(display.getSystemColor(SWT.COLOR_GRAY));
    // SWT.COLOR_INFO_FOREGROUND));
    lineNumbers.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
    lineNumbers.setFont(JFaceResources.getTextFont());
    ruler.addDecorator(0, lineNumbers);
    viewer = new SourceViewer(parent, ruler, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    viewer.getTextWidget().setFont(JFaceResources.getTextFont());
    SourceViewerConfiguration conf = new SourceViewerConfiguration();
    viewer.configure(conf);
    SLDTransformer trans = new SLDTransformer();
    trans.setIndentation(2);
    String xml;
    try {
        xml = trans.transform(getParent().getStyle());
    } catch (TransformerException e) {
        // $NON-NLS-1$
        xml = "Error: " + e.getMessage();
    }
    IDocument doc = new Document();
    doc.set(xml);
    doc.addDocumentListener(new IDocumentListener() {

        @Override
        public void documentChanged(DocumentEvent event) {
            changed = true;
        }

        @Override
        public void documentAboutToBeChanged(DocumentEvent event) {
        // ignore
        }
    });
    viewer.setInput(doc);
    setControl(viewer.getControl());
}
Also used : LineNumberRulerColumn(org.eclipse.jface.text.source.LineNumberRulerColumn) SourceViewer(org.eclipse.jface.text.source.SourceViewer) IDocumentListener(org.eclipse.jface.text.IDocumentListener) CompositeRuler(org.eclipse.jface.text.source.CompositeRuler) FillLayout(org.eclipse.swt.layout.FillLayout) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DocumentEvent(org.eclipse.jface.text.DocumentEvent) SourceViewerConfiguration(org.eclipse.jface.text.source.SourceViewerConfiguration) SLDTransformer(org.geotools.xml.styling.SLDTransformer) TransformerException(javax.xml.transform.TransformerException) IDocument(org.eclipse.jface.text.IDocument) Display(org.eclipse.swt.widgets.Display)

Example 24 with IDocumentListener

use of org.eclipse.jface.text.IDocumentListener in project mylyn.docs by eclipse.

the class MarkupViewerPreferencePage method createContents.

@Override
protected Control createContents(Composite parent) {
    colorRegistry = new Colors();
    colorRegistry.put(WHITE, new RGB(255, 255, 255));
    Composite composite = new Composite(parent, SWT.NULL);
    GridLayoutFactory.fillDefaults().margins(5, 5).numColumns(1).applyTo(composite);
    Label label = new Label(composite, SWT.WRAP);
    label.setText(Messages.MarkupViewerPreferencePage_appearanceInfo);
    GridDataFactory.fillDefaults().applyTo(label);
    Preferences preferences = WikiTextUiPlugin.getDefault().getPreferences();
    Composite viewerContainer = new Composite(composite, SWT.BORDER);
    GridLayoutFactory.fillDefaults().margins(0, 0).numColumns(1).applyTo(viewerContainer);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(viewerContainer);
    {
        sourceViewer = new SourceViewer(viewerContainer, new VerticalRuler(0), SWT.WRAP | SWT.V_SCROLL);
        GridDataFactory.fillDefaults().grab(true, true).applyTo(sourceViewer.getControl());
        Document document = new Document(preferences.getMarkupViewerCss());
        CssPartitioner partitioner = new CssPartitioner();
        partitioner.connect(document);
        document.setDocumentPartitioner(partitioner);
        sourceViewer.setDocument(document);
        CssConfiguration configuration = new CssConfiguration(colorRegistry);
        sourceViewer.configure(configuration);
    }
    label = new Label(composite, SWT.WRAP);
    label.setText(Messages.MarkupViewerPreferencePage_preview);
    GridDataFactory.fillDefaults().applyTo(label);
    applyDialogFont(composite);
    Composite previewViewerContainer = new Composite(composite, SWT.BORDER);
    GridLayoutFactory.fillDefaults().margins(0, 0).numColumns(1).applyTo(previewViewerContainer);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(previewViewerContainer);
    {
        previewViewer = new HtmlViewer(previewViewerContainer, new VerticalRuler(0), SWT.WRAP | SWT.V_SCROLL);
        previewViewer.getTextWidget().setBackground(colorRegistry.get(WHITE));
        GridDataFactory.fillDefaults().grab(true, true).applyTo(previewViewer.getControl());
        htmlViewerConfiguration = new HtmlViewerConfiguration(previewViewer);
        previewViewer.configure(htmlViewerConfiguration);
        previewViewer.getTextWidget().setEditable(false);
        previewViewer.setStylesheet(preferences.getStylesheet());
        if (JFaceResources.getFontRegistry().hasValueFor(WikiTextTasksUiPlugin.FONT_REGISTRY_KEY_DEFAULT_FONT)) {
            previewViewer.getTextWidget().setFont(JFaceResources.getFontRegistry().get(WikiTextTasksUiPlugin.FONT_REGISTRY_KEY_DEFAULT_FONT));
        }
        if (JFaceResources.getFontRegistry().hasValueFor(WikiTextTasksUiPlugin.FONT_REGISTRY_KEY_MONOSPACE_FONT)) {
            previewViewer.setDefaultMonospaceFont(JFaceResources.getFontRegistry().get(WikiTextTasksUiPlugin.FONT_REGISTRY_KEY_MONOSPACE_FONT));
        }
        previewViewer.setHtml(createPreviewHtml());
        sourceViewer.getDocument().addDocumentListener(new IDocumentListener() {

            public void documentAboutToBeChanged(DocumentEvent event) {
            }

            public void documentChanged(DocumentEvent event) {
                schedulePreviewUpdate();
            }
        });
    }
    return composite;
}
Also used : VerticalRuler(org.eclipse.jface.text.source.VerticalRuler) CssConfiguration(org.eclipse.mylyn.internal.wikitext.ui.util.css.editor.CssConfiguration) SourceViewer(org.eclipse.jface.text.source.SourceViewer) Composite(org.eclipse.swt.widgets.Composite) HtmlViewerConfiguration(org.eclipse.mylyn.wikitext.ui.viewer.HtmlViewerConfiguration) IDocumentListener(org.eclipse.jface.text.IDocumentListener) Label(org.eclipse.swt.widgets.Label) CssPartitioner(org.eclipse.mylyn.internal.wikitext.ui.util.css.editor.CssPartitioner) RGB(org.eclipse.swt.graphics.RGB) Document(org.eclipse.jface.text.Document) DocumentEvent(org.eclipse.jface.text.DocumentEvent) Preferences(org.eclipse.mylyn.internal.wikitext.ui.editor.preferences.Preferences) HtmlViewer(org.eclipse.mylyn.wikitext.ui.viewer.HtmlViewer)

Example 25 with IDocumentListener

use of org.eclipse.jface.text.IDocumentListener in project ow by vtst.

the class AbstractEditorRegistry method removeEditor.

private synchronized void removeEditor(ITextEditor textEditor) {
    IFile file = editorToFile.get(textEditor);
    if (file != null)
        fileToEditors.remove(file, textEditor);
    IDocument document = editorToDocument.get(textEditor);
    if (document != null) {
        if (documentToEditors.remove(document, textEditor)) {
            IDocumentListener listener = documentToListener.remove(document);
            if (listener != null)
                document.removeDocumentListener(listener);
            documentToFile.remove(document);
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IDocumentListener(org.eclipse.jface.text.IDocumentListener) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

IDocumentListener (org.eclipse.jface.text.IDocumentListener)28 DocumentEvent (org.eclipse.jface.text.DocumentEvent)27 IDocument (org.eclipse.jface.text.IDocument)11 ArrayList (java.util.ArrayList)6 Document (org.eclipse.jface.text.Document)5 IDocumentExtension3 (org.eclipse.jface.text.IDocumentExtension3)4 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)4 FastPartitioner (org.eclipse.jface.text.rules.FastPartitioner)4 Composite (org.eclipse.swt.widgets.Composite)4 List (java.util.List)3 SourceViewer (org.eclipse.jface.text.source.SourceViewer)3 PropertyChangeEvent (org.eclipse.jface.util.PropertyChangeEvent)3 StyledText (org.eclipse.swt.custom.StyledText)3 RGB (org.eclipse.swt.graphics.RGB)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 IFile (org.eclipse.core.resources.IFile)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IStatus (org.eclipse.core.runtime.IStatus)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2