Search in sources :

Example 1 with HiddenFileEditorInput

use of com.liferay.ide.kaleo.ui.editor.HiddenFileEditorInput 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)

Aggregations

Node (com.liferay.ide.kaleo.core.model.Node)1 HiddenFileEditorInput (com.liferay.ide.kaleo.ui.editor.HiddenFileEditorInput)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 DocumentEvent (org.eclipse.jface.text.DocumentEvent)1 IDocumentListener (org.eclipse.jface.text.IDocumentListener)1 IEditorInput (org.eclipse.ui.IEditorInput)1 IEditorPart (org.eclipse.ui.IEditorPart)1 IEditorSite (org.eclipse.ui.IEditorSite)1 IPartListener (org.eclipse.ui.IPartListener)1 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)1 IWorkbenchPartSite (org.eclipse.ui.IWorkbenchPartSite)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1 PartInitException (org.eclipse.ui.PartInitException)1 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)1