From c88ca0d43b5564a8ebc085bc5c3016a6c7ce1364 Mon Sep 17 00:00:00 2001 From: Sergey Vasiliev Date: Tue, 14 Mar 2006 17:59:23 +0300 Subject: [PATCH] implemented navigation providers for graph views and dom UI controls --- .../intellij/javaee/ui/CompositeCommittable.java | 4 + openapi/src/com/intellij/util/xml/DomUtil.java | 8 ++ .../src/com/intellij/util/xml/ui/BaseControl.java | 8 ++ .../intellij/util/xml/ui/DomCollectionControl.java | 9 ++ .../src/com/intellij/util/xml/ui/DomUIControl.java | 3 + .../util/xml/ui/DomUINavigationProvider.java | 70 ++++++++++ .../util/xml/ui/PerspectiveFileEditor.java | 141 +++++++++++++++++++++ 7 files changed, 243 insertions(+) create mode 100644 openapi/src/com/intellij/util/xml/ui/DomUINavigationProvider.java create mode 100644 source/com/intellij/util/xml/ui/PerspectiveFileEditor.java diff --git a/J2EE/openapi/com/intellij/javaee/ui/CompositeCommittable.java b/J2EE/openapi/com/intellij/javaee/ui/CompositeCommittable.java index a6fd70954a..0a753446a0 100644 --- a/J2EE/openapi/com/intellij/javaee/ui/CompositeCommittable.java +++ b/J2EE/openapi/com/intellij/javaee/ui/CompositeCommittable.java @@ -59,4 +59,8 @@ public class CompositeCommittable implements Committable { } return result; } + + public List getChildren() { + return myComponents; + } } diff --git a/openapi/src/com/intellij/util/xml/DomUtil.java b/openapi/src/com/intellij/util/xml/DomUtil.java index 825b3d5f9f..38ac87dc93 100644 --- a/openapi/src/com/intellij/util/xml/DomUtil.java +++ b/openapi/src/com/intellij/util/xml/DomUtil.java @@ -4,7 +4,15 @@ package com.intellij.util.xml; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.fileEditor.TextEditor; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.ScrollType; import com.intellij.util.containers.ContainerUtil; +import com.intellij.psi.xml.XmlTag; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiDocumentManager; +import com.intellij.psi.PsiElement; +import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.Nullable; import java.lang.annotation.Annotation; diff --git a/openapi/src/com/intellij/util/xml/ui/BaseControl.java b/openapi/src/com/intellij/util/xml/ui/BaseControl.java index 66cabc7e57..251c4681f8 100644 --- a/openapi/src/com/intellij/util/xml/ui/BaseControl.java +++ b/openapi/src/com/intellij/util/xml/ui/BaseControl.java @@ -156,6 +156,14 @@ public abstract class BaseControl implements DomUIC } } + + public boolean canNavigate(DomElement element) { + return false; + } + + public void navigate(DomElement element) { + } + protected abstract T getValue(Bound component); protected abstract void setValue(Bound component, T value); diff --git a/openapi/src/com/intellij/util/xml/ui/DomCollectionControl.java b/openapi/src/com/intellij/util/xml/ui/DomCollectionControl.java index 29d1464947..dd28af5ed9 100644 --- a/openapi/src/com/intellij/util/xml/ui/DomCollectionControl.java +++ b/openapi/src/com/intellij/util/xml/ui/DomCollectionControl.java @@ -93,6 +93,15 @@ public class DomCollectionControl implements DomUIControl myDispatcher.removeListener(listener); } + + public boolean canNavigate(DomElement element) { + return false; + } + + public void navigate(DomElement element) { + + } + protected void initialize(final DomCollectionPanel boundComponent) { if (boundComponent == null) { myCollectionPanel = new DomCollectionPanel(); diff --git a/openapi/src/com/intellij/util/xml/ui/DomUIControl.java b/openapi/src/com/intellij/util/xml/ui/DomUIControl.java index 6752507918..d03e3da180 100644 --- a/openapi/src/com/intellij/util/xml/ui/DomUIControl.java +++ b/openapi/src/com/intellij/util/xml/ui/DomUIControl.java @@ -25,4 +25,7 @@ public interface DomUIControl extends CommittablePanel { void removeCommitListener(CommitListener listener); + boolean canNavigate(DomElement element); + + void navigate(DomElement element); } diff --git a/openapi/src/com/intellij/util/xml/ui/DomUINavigationProvider.java b/openapi/src/com/intellij/util/xml/ui/DomUINavigationProvider.java new file mode 100644 index 0000000000..0b600e6063 --- /dev/null +++ b/openapi/src/com/intellij/util/xml/ui/DomUINavigationProvider.java @@ -0,0 +1,70 @@ +/* + * Copyright 2000-2006 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.intellij.util.xml.ui; + +import com.intellij.util.xml.DomElementNavigateProvider; +import com.intellij.util.xml.DomElement; +import com.intellij.javaee.ui.Committable; +import com.intellij.javaee.ui.CompositeCommittable; + +import java.util.List; + +/** + * User: Sergey.Vasiliev + */ +public class DomUINavigationProvider implements DomElementNavigateProvider { + public static String DOM_UI_NAVIGATION_PROVIDER_NAME = "DOM_UI_NAVIGATION_PROVIDER_NAME"; + + private AbstractDomElementComponent myComponent; + + protected DomUINavigationProvider(final AbstractDomElementComponent component) { + myComponent = component; + } + + + public String getProviderName() { + return DOM_UI_NAVIGATION_PROVIDER_NAME; + } + + public void navigate(DomElement domElement, boolean requestFocus) { + final DomUIControl domUIControl = getNavigationControl(myComponent, domElement); + if(domUIControl != null) { + domUIControl.navigate(domElement); + } + } + + public boolean canNavigate(DomElement domElement) { + return getNavigationControl(myComponent, domElement) != null; + } + + private DomUIControl getNavigationControl(final CompositeCommittable compositCommitable, final DomElement domElement) { + //todo implement visitor for (Composite)Commitable ? + final List list = compositCommitable.getChildren(); + for (Committable committable : list) { + if (committable instanceof DomUIControl) { + if(((DomUIControl)committable).canNavigate(domElement)) { + return (DomUIControl)committable; + } + } else if (committable instanceof CompositeCommittable) { + final DomUIControl control = getNavigationControl((CompositeCommittable)committable, domElement); + if(control != null) return control; + } + } + return null; + } +} diff --git a/source/com/intellij/util/xml/ui/PerspectiveFileEditor.java b/source/com/intellij/util/xml/ui/PerspectiveFileEditor.java new file mode 100644 index 0000000000..e725ebd33e --- /dev/null +++ b/source/com/intellij/util/xml/ui/PerspectiveFileEditor.java @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved. + */ + +package com.intellij.util.xml.ui; + +import com.intellij.openapi.util.UserDataHolderBase; +import com.intellij.openapi.fileEditor.*; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.ScrollType; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.xml.DomElement; +import com.intellij.util.xml.DomManager; +import com.intellij.psi.xml.XmlTag; +import com.intellij.psi.xml.XmlFile; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiDocumentManager; +import com.intellij.psi.PsiManager; +import com.intellij.psi.PsiElement; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.codeHighlighting.BackgroundEditorHighlighter; +import com.intellij.ide.structureView.StructureViewBuilder; + +import java.beans.PropertyChangeListener; + +/** + * User: Sergey.Vasiliev + */ +abstract public class PerspectiveFileEditor extends UserDataHolderBase implements FileEditor { + private final XmlFile myFile; + private FileEditorManagerAdapter myFileEditorManagerAdapter; + + private Project myProject; + + + protected PerspectiveFileEditor(final Project project, final VirtualFile file) { + myProject = project; + + myFile = (XmlFile)PsiManager.getInstance(project).findFile(file); + + myFileEditorManagerAdapter = new FileEditorManagerAdapter() { + public void selectionChanged(FileEditorManagerEvent event) { + if (PerspectiveFileEditor.this.equals(event.getOldEditor())) { + deselectNotify(); + if (event.getNewEditor() instanceof TextEditor) { + setSelectionInTextEditor((TextEditor)event.getNewEditor(), getSelectedDomElement()); + } + } + else if (PerspectiveFileEditor.this.equals(event.getNewEditor())) { + selectNotify(); + if (event.getOldEditor() instanceof TextEditor) { + setSelectedDomElement(getSelectedDomElementFromTextEditor((TextEditor)event.getOldEditor())); + } else if (event.getOldEditor() instanceof PerspectiveFileEditor) { + setSelectedDomElement(((PerspectiveFileEditor)event.getOldEditor()).getSelectedDomElement()); + } + } + } + }; + FileEditorManager.getInstance(myProject).addFileEditorManagerListener(myFileEditorManagerAdapter); + } + + abstract protected DomElement getSelectedDomElement(); + abstract protected void setSelectedDomElement(DomElement domElement); + + protected DomElement getSelectedDomElementFromTextEditor(final TextEditor textEditor) { + final PsiElement psiElement = myFile.findElementAt(textEditor.getEditor().getCaretModel().getOffset()); + + if(psiElement == null) return null; + + final XmlTag xmlTag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class); + + return DomManager.getDomManager(myFile.getProject()).getDomElement(xmlTag); + } + + public void setSelectionInTextEditor(final TextEditor textEditor, final DomElement element) { + if(element != null && element.isValid()&& element.getXmlTag()!=null) { + final XmlTag tag = element.getXmlTag(); + final PsiFile file = tag.getContainingFile(); + if (file == null) return; + + final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file); + if (document == null || !document.equals(textEditor.getEditor().getDocument())) { + return; + } + + textEditor.getEditor().getCaretModel().moveToOffset(tag.getTextOffset()); + textEditor.getEditor().getScrollingModel().scrollToCaret(ScrollType.CENTER); + } + } + + public XmlFile getXmlFile() { + return myFile; + } + + public void dispose() { + FileEditorManager.getInstance(myProject).removeFileEditorManagerListener(myFileEditorManagerAdapter); + } + + public boolean isModified() { + return FileDocumentManager.getInstance().isFileModified(myFile.getVirtualFile()); + } + + public final boolean isValid() { + return myFile.isValid(); + } + + public void selectNotify() { + } + + public void deselectNotify() { + } + + public void addPropertyChangeListener(PropertyChangeListener listener) { + } + + public void removePropertyChangeListener(PropertyChangeListener listener) { + } + + public BackgroundEditorHighlighter getBackgroundHighlighter() { + return null; + } + + public FileEditorLocation getCurrentLocation() { + return new FileEditorLocation() { + public FileEditor getEditor() { + return PerspectiveFileEditor.this; + } + + public int compareTo(final FileEditorLocation fileEditorLocation) { + return 0; + } + }; + } + + public StructureViewBuilder getStructureViewBuilder() { + return null; + } + + +} -- 2.11.4.GIT