2 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
5 package com
.intellij
.util
.xml
.ui
;
7 import com
.intellij
.openapi
.util
.UserDataHolderBase
;
8 import com
.intellij
.openapi
.fileEditor
.*;
9 import com
.intellij
.openapi
.project
.Project
;
10 import com
.intellij
.openapi
.editor
.Document
;
11 import com
.intellij
.openapi
.editor
.ScrollType
;
12 import com
.intellij
.openapi
.vfs
.VirtualFile
;
13 import com
.intellij
.util
.xml
.DomElement
;
14 import com
.intellij
.util
.xml
.DomManager
;
15 import com
.intellij
.psi
.xml
.XmlTag
;
16 import com
.intellij
.psi
.xml
.XmlFile
;
17 import com
.intellij
.psi
.PsiFile
;
18 import com
.intellij
.psi
.PsiDocumentManager
;
19 import com
.intellij
.psi
.PsiManager
;
20 import com
.intellij
.psi
.PsiElement
;
21 import com
.intellij
.psi
.util
.PsiTreeUtil
;
22 import com
.intellij
.codeHighlighting
.BackgroundEditorHighlighter
;
23 import com
.intellij
.ide
.structureView
.StructureViewBuilder
;
25 import java
.beans
.PropertyChangeListener
;
28 * User: Sergey.Vasiliev
30 abstract public class PerspectiveFileEditor
extends UserDataHolderBase
implements FileEditor
{
31 private final XmlFile myFile
;
32 private FileEditorManagerAdapter myFileEditorManagerAdapter
;
34 private Project myProject
;
37 protected PerspectiveFileEditor(final Project project
, final VirtualFile file
) {
40 myFile
= (XmlFile
)PsiManager
.getInstance(project
).findFile(file
);
42 myFileEditorManagerAdapter
= new FileEditorManagerAdapter() {
43 public void selectionChanged(FileEditorManagerEvent event
) {
44 if (PerspectiveFileEditor
.this.equals(event
.getOldEditor())) {
46 if (event
.getNewEditor() instanceof TextEditor
) {
47 setSelectionInTextEditor((TextEditor
)event
.getNewEditor(), getSelectedDomElement());
50 else if (PerspectiveFileEditor
.this.equals(event
.getNewEditor())) {
52 if (event
.getOldEditor() instanceof TextEditor
) {
53 setSelectedDomElement(getSelectedDomElementFromTextEditor((TextEditor
)event
.getOldEditor()));
54 } else if (event
.getOldEditor() instanceof PerspectiveFileEditor
) {
55 setSelectedDomElement(((PerspectiveFileEditor
)event
.getOldEditor()).getSelectedDomElement());
60 FileEditorManager
.getInstance(myProject
).addFileEditorManagerListener(myFileEditorManagerAdapter
);
63 abstract protected DomElement
getSelectedDomElement();
64 abstract protected void setSelectedDomElement(DomElement domElement
);
66 protected DomElement
getSelectedDomElementFromTextEditor(final TextEditor textEditor
) {
67 final PsiElement psiElement
= myFile
.findElementAt(textEditor
.getEditor().getCaretModel().getOffset());
69 if(psiElement
== null) return null;
71 final XmlTag xmlTag
= PsiTreeUtil
.getParentOfType(psiElement
, XmlTag
.class);
73 return DomManager
.getDomManager(myFile
.getProject()).getDomElement(xmlTag
);
76 public void setSelectionInTextEditor(final TextEditor textEditor
, final DomElement element
) {
77 if(element
!= null && element
.isValid()&& element
.getXmlTag()!=null) {
78 final XmlTag tag
= element
.getXmlTag();
79 final PsiFile file
= tag
.getContainingFile();
80 if (file
== null) return;
82 final Document document
= PsiDocumentManager
.getInstance(file
.getProject()).getDocument(file
);
83 if (document
== null || !document
.equals(textEditor
.getEditor().getDocument())) {
87 textEditor
.getEditor().getCaretModel().moveToOffset(tag
.getTextOffset());
88 textEditor
.getEditor().getScrollingModel().scrollToCaret(ScrollType
.CENTER
);
92 public XmlFile
getXmlFile() {
96 public void dispose() {
97 FileEditorManager
.getInstance(myProject
).removeFileEditorManagerListener(myFileEditorManagerAdapter
);
100 public boolean isModified() {
101 return FileDocumentManager
.getInstance().isFileModified(myFile
.getVirtualFile());
104 public final boolean isValid() {
105 return myFile
.isValid();
108 public void selectNotify() {
111 public void deselectNotify() {
114 public void addPropertyChangeListener(PropertyChangeListener listener
) {
117 public void removePropertyChangeListener(PropertyChangeListener listener
) {
120 public BackgroundEditorHighlighter
getBackgroundHighlighter() {
124 public FileEditorLocation
getCurrentLocation() {
125 return new FileEditorLocation() {
126 public FileEditor
getEditor() {
127 return PerspectiveFileEditor
.this;
130 public int compareTo(final FileEditorLocation fileEditorLocation
) {
136 public StructureViewBuilder
getStructureViewBuilder() {