From 779baf0b1db9352ea63cc31c97841809d5b5c525 Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Thu, 19 Jul 2012 12:29:39 +0400 Subject: [PATCH] MapExternalResourceDialog --- .../impl/quickfix/BaseExtResourceAction.java | 8 +- .../ManuallySetupExtResourceAction.java | 28 +-- .../intellij/javaee/EditLocationDialog.java | 33 --- .../javaee/ExternalResourceConfigurable.java | 55 ++--- .../javaee/MapExternalResourceDialog.form | 80 +++++++ .../javaee/MapExternalResourceDialog.java | 218 ++++++++++++++++++ .../com/intellij/javaee/NameLocationPair.java | 53 +++++ .../xml/config/ConfigFileSearcher.java | 26 ++- .../xml/config/ConfigFilesTreeBuilder.java | 27 ++- .../xml/index/IndexedRelevantResource.java | 10 +- .../intellij/xml/index/XmlNamespaceIndex.java | 9 +- .../src/com/intellij/xml/util/XmlUtil.java | 2 +- 12 files changed, 433 insertions(+), 116 deletions(-) create mode 100644 xml/impl/src/com/intellij/javaee/MapExternalResourceDialog.form create mode 100644 xml/impl/src/com/intellij/javaee/MapExternalResourceDialog.java create mode 100644 xml/impl/src/com/intellij/javaee/NameLocationPair.java rename xml/{dom-openapi/src/com/intellij/util => impl/src/com/intellij}/xml/config/ConfigFileSearcher.java (57%) rename xml/{dom-openapi/src/com/intellij/util => impl/src/com/intellij}/xml/config/ConfigFilesTreeBuilder.java (88%) diff --git a/xml/impl/src/com/intellij/codeInsight/daemon/impl/quickfix/BaseExtResourceAction.java b/xml/impl/src/com/intellij/codeInsight/daemon/impl/quickfix/BaseExtResourceAction.java index b08ccd2fb287..9feb95a5db14 100644 --- a/xml/impl/src/com/intellij/codeInsight/daemon/impl/quickfix/BaseExtResourceAction.java +++ b/xml/impl/src/com/intellij/codeInsight/daemon/impl/quickfix/BaseExtResourceAction.java @@ -26,7 +26,6 @@ import com.intellij.psi.impl.source.resolve.reference.impl.providers.URLReferenc import com.intellij.psi.xml.XmlFile; import com.intellij.util.IncorrectOperationException; import com.intellij.xml.XmlBundle; -import com.intellij.xml.util.XmlUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -40,13 +39,8 @@ abstract class BaseExtResourceAction extends BaseIntentionAction { int offset = editor.getCaretModel().getOffset(); String uri = findUri(file, offset); + if (uri == null || !isAcceptableUri(uri)) return false; - - if (uri == null) return false; - - XmlFile xmlFile = XmlUtil.findNamespaceByLocation(file, uri); - if (xmlFile != null) return false; - if (!isAcceptableUri(uri)) return false; setText(XmlBundle.message(getQuickFixKeyId())); return true; } diff --git a/xml/impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ManuallySetupExtResourceAction.java b/xml/impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ManuallySetupExtResourceAction.java index 6558a45e209f..06f98655f90c 100644 --- a/xml/impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ManuallySetupExtResourceAction.java +++ b/xml/impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ManuallySetupExtResourceAction.java @@ -15,12 +15,10 @@ */ package com.intellij.codeInsight.daemon.impl.quickfix; -import com.intellij.javaee.ExternalResourceConfigurable; +import com.intellij.javaee.MapExternalResourceDialog; import com.intellij.javaee.ExternalResourceManager; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.options.ShowSettingsUtil; -import com.intellij.openapi.project.Project; import com.intellij.psi.PsiFile; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; @@ -35,19 +33,17 @@ public class ManuallySetupExtResourceAction extends BaseExtResourceAction { } protected void doInvoke(@NotNull final PsiFile file, final int offset, @NotNull final String uri, final Editor editor) throws IncorrectOperationException { - ApplicationManager.getApplication().runWriteAction(new Runnable() { - public void run() { - ExternalResourceManager.getInstance().addResource(uri, ""); - } - }); - - final Project project = file.getProject(); - final ExternalResourceConfigurable component = new ExternalResourceConfigurable(project); - ShowSettingsUtil.getInstance().editConfigurable(project, component, new Runnable() { - public void run() { - component.selectResource(uri); - } - }); + final MapExternalResourceDialog dialog = new MapExternalResourceDialog(uri, file.getProject(), file, null); + dialog.show(); + if (dialog.isOK()) { + ApplicationManager.getApplication().runWriteAction(new Runnable() { + @Override + public void run() { + String location = dialog.getResourceLocation(); + ExternalResourceManager.getInstance().addResource(dialog.getUri(), location); + } + }); + } } public boolean startInWriteAction() { diff --git a/xml/impl/src/com/intellij/javaee/EditLocationDialog.java b/xml/impl/src/com/intellij/javaee/EditLocationDialog.java index f3e7fffdea52..443e0d6c42c8 100644 --- a/xml/impl/src/com/intellij/javaee/EditLocationDialog.java +++ b/xml/impl/src/com/intellij/javaee/EditLocationDialog.java @@ -141,37 +141,4 @@ public class EditLocationDialog extends DialogWrapper { myTfPath.setText(origin.myLocation); myTfShared = origin.myShared; } - - public static class NameLocationPair implements Comparable { - String myName; - String myLocation; - boolean myShared; - - public NameLocationPair(String name, String location, boolean shared) { - myName = name; - myLocation = location; - myShared = shared; - } - - public int compareTo(Object o) { - return myName.compareTo(((NameLocationPair)o).myName); - } - - public boolean equals(Object obj) { - if (! (obj instanceof NameLocationPair)) return false; - return compareTo(obj) == 0; - } - - public int hashCode() { - return myName.hashCode(); - } - - public String getName() { - return myName; - } - - public String getLocation(){ - return myLocation; - } - } } diff --git a/xml/impl/src/com/intellij/javaee/ExternalResourceConfigurable.java b/xml/impl/src/com/intellij/javaee/ExternalResourceConfigurable.java index 55bdf84d4f02..c0009cd8fd13 100644 --- a/xml/impl/src/com/intellij/javaee/ExternalResourceConfigurable.java +++ b/xml/impl/src/com/intellij/javaee/ExternalResourceConfigurable.java @@ -46,20 +46,20 @@ import java.util.List; public class ExternalResourceConfigurable extends BaseConfigurable implements SearchableConfigurable, OptionalConfigurable, Configurable.NoScroll { private JPanel myPanel; - private List myPairs; + private List myPairs; private List myIgnoredUrls; private String myDefaultHtmlDoctype; - private AddEditRemovePanel myExtPanel; + private AddEditRemovePanel myExtPanel; private AddEditRemovePanel myIgnorePanel; private HtmlLanguageLevelForm myHtmlLanguageLevelForm; @Nullable private final Project myProject; - private final List myNewPairs; + private final List myNewPairs; public ExternalResourceConfigurable(@Nullable Project project) { - this(project, Collections.emptyList()); + this(project, Collections.emptyList()); } - public ExternalResourceConfigurable(@Nullable Project project, List newResources) { + public ExternalResourceConfigurable(@Nullable Project project, List newResources) { myProject = project; myNewPairs = newResources; } @@ -75,17 +75,17 @@ public class ExternalResourceConfigurable extends BaseConfigurable implements Se } }; - myExtPanel = new AddEditRemovePanel(new ExtUrlsTableModel(), myPairs, XmlBundle.message("label.edit.external.resource.configure.external.resources")) { - protected EditLocationDialog.NameLocationPair addItem() { + myExtPanel = new AddEditRemovePanel(new ExtUrlsTableModel(), myPairs, XmlBundle.message("label.edit.external.resource.configure.external.resources")) { + protected NameLocationPair addItem() { return addExtLocation(); } - protected boolean removeItem(EditLocationDialog.NameLocationPair o) { + protected boolean removeItem(NameLocationPair o) { setModified(true); return true; } - protected EditLocationDialog.NameLocationPair editItem(EditLocationDialog.NameLocationPair o) { + protected NameLocationPair editItem(NameLocationPair o) { return editExtLocation(o); } }; @@ -161,7 +161,7 @@ public class ExternalResourceConfigurable extends BaseConfigurable implements Se manager.clearAllResources(myProject); } for (Object myPair : myPairs) { - EditLocationDialog.NameLocationPair pair = (EditLocationDialog.NameLocationPair)myPair; + NameLocationPair pair = (NameLocationPair)myPair; String s = FileUtil.toSystemIndependentName(pair.myLocation); if (myProject == null || pair.myShared) { manager.addResource(pair.myName, s); @@ -186,19 +186,19 @@ public class ExternalResourceConfigurable extends BaseConfigurable implements Se public void reset() { - myPairs = new ArrayList(myNewPairs); + myPairs = new ArrayList(myNewPairs); ExternalResourceManagerEx manager = ExternalResourceManagerEx.getInstanceEx(); String[] urls = manager.getAvailableUrls(); for (String url : urls) { String loc = myProject == null ? manager.getResourceLocation(url, (String)null) : manager.getResourceLocation(url, myProject); - myPairs.add(new EditLocationDialog.NameLocationPair(url, FileUtil.toSystemDependentName(loc), true)); + myPairs.add(new NameLocationPair(url, FileUtil.toSystemDependentName(loc), true)); } if (myProject != null) { urls = manager.getAvailableUrls(myProject); for (String url : urls) { String loc = manager.getResourceLocation(url, myProject); - myPairs.add(new EditLocationDialog.NameLocationPair(url, FileUtil.toSystemDependentName(loc), false)); + myPairs.add(new NameLocationPair(url, FileUtil.toSystemDependentName(loc), false)); } } @@ -216,7 +216,7 @@ public class ExternalResourceConfigurable extends BaseConfigurable implements Se if (!myNewPairs.isEmpty()) { ListSelectionModel selectionModel = myExtPanel.getTable().getSelectionModel(); selectionModel.clearSelection(); - for (EditLocationDialog.NameLocationPair newPair : myNewPairs) { + for (NameLocationPair newPair : myNewPairs) { int index = myPairs.indexOf(newPair); selectionModel.addSelectionInterval(index, index); } @@ -243,25 +243,24 @@ public class ExternalResourceConfigurable extends BaseConfigurable implements Se } @Nullable - private EditLocationDialog.NameLocationPair addExtLocation() { - EditLocationDialog dialog = new EditLocationDialog(null, true); + private NameLocationPair addExtLocation() { + MapExternalResourceDialog dialog = new MapExternalResourceDialog(null, myProject, null, null); dialog.show(); if (!dialog.isOK()) return null; setModified(true); - return dialog.getPair(); + return new NameLocationPair(dialog.getUri(), dialog.getResourceLocation(), false); } @Nullable - private EditLocationDialog.NameLocationPair editExtLocation(Object o) { - EditLocationDialog dialog = new EditLocationDialog(null, true); - final EditLocationDialog.NameLocationPair pair = (EditLocationDialog.NameLocationPair)o; - dialog.init(pair); + private NameLocationPair editExtLocation(Object o) { + NameLocationPair pair = (NameLocationPair)o; + MapExternalResourceDialog dialog = new MapExternalResourceDialog(pair.getName(), myProject, null, pair.getLocation()); dialog.show(); if (!dialog.isOK()) { return null; } setModified(true); - return dialog.getPair(); + return new NameLocationPair(dialog.getUri(), dialog.getResourceLocation(), pair.myShared); } @Nullable @@ -276,17 +275,13 @@ public class ExternalResourceConfigurable extends BaseConfigurable implements Se @Nullable private String editIgnoreLocation(Object o) { EditLocationDialog dialog = new EditLocationDialog(null, false); - dialog.init(new EditLocationDialog.NameLocationPair(o.toString(), null, false)); + dialog.init(new NameLocationPair(o.toString(), null, false)); dialog.show(); if (!dialog.isOK()) return null; setModified(true); return dialog.getPair().myName; } - public void selectResource(final String uri) { - myExtPanel.setSelected(new EditLocationDialog.NameLocationPair(uri, null, false)); - } - private static class PathRenderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { final Component rendererComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); @@ -335,7 +330,7 @@ public class ExternalResourceConfigurable extends BaseConfigurable implements Se } } - private class ExtUrlsTableModel extends AddEditRemovePanel.TableModel { + private class ExtUrlsTableModel extends AddEditRemovePanel.TableModel { final String[] myNames; { @@ -352,7 +347,7 @@ public class ExternalResourceConfigurable extends BaseConfigurable implements Se return myNames.length; } - public Object getField(EditLocationDialog.NameLocationPair pair, int columnIndex) { + public Object getField(NameLocationPair pair, int columnIndex) { switch (columnIndex) { case 0: return pair.myName; @@ -373,7 +368,7 @@ public class ExternalResourceConfigurable extends BaseConfigurable implements Se return column == 2; } - public void setValue(Object aValue, EditLocationDialog.NameLocationPair data, int columnIndex) { + public void setValue(Object aValue, NameLocationPair data, int columnIndex) { data.myShared = !((Boolean)aValue).booleanValue(); } diff --git a/xml/impl/src/com/intellij/javaee/MapExternalResourceDialog.form b/xml/impl/src/com/intellij/javaee/MapExternalResourceDialog.form new file mode 100644 index 000000000000..4a36005f155d --- /dev/null +++ b/xml/impl/src/com/intellij/javaee/MapExternalResourceDialog.form @@ -0,0 +1,80 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/xml/impl/src/com/intellij/javaee/MapExternalResourceDialog.java b/xml/impl/src/com/intellij/javaee/MapExternalResourceDialog.java new file mode 100644 index 000000000000..38c7035131a0 --- /dev/null +++ b/xml/impl/src/com/intellij/javaee/MapExternalResourceDialog.java @@ -0,0 +1,218 @@ +/* + * Copyright 2000-2012 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.javaee; + +import com.intellij.openapi.editor.colors.EditorColorsManager; +import com.intellij.openapi.editor.colors.EditorFontType; +import com.intellij.openapi.fileChooser.FileChooserDescriptor; +import com.intellij.openapi.fileChooser.FileSystemTree; +import com.intellij.openapi.fileChooser.ex.FileSystemTreeImpl; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleUtilCore; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiManager; +import com.intellij.ui.ColoredTreeCellRenderer; +import com.intellij.ui.DocumentAdapter; +import com.intellij.ui.ScrollPaneFactory; +import com.intellij.ui.components.JBTabbedPane; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.ui.tree.TreeUtil; +import com.intellij.xml.config.ConfigFileSearcher; +import com.intellij.xml.config.ConfigFilesTreeBuilder; +import com.intellij.xml.index.IndexedRelevantResource; +import com.intellij.xml.index.XmlNamespaceIndex; +import com.intellij.xml.index.XsdNamespaceBuilder; +import com.intellij.xml.util.XmlUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.TreeSelectionEvent; +import javax.swing.event.TreeSelectionListener; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.TreePath; +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @author Dmitry Avdeev + * Date: 7/17/12 + */ +public class MapExternalResourceDialog extends DialogWrapper { + + private JTextField myUri; + private JPanel myMainPanel; + private JTree mySchemasTree; + private JPanel myExplorerPanel; + private JBTabbedPane myTabs; + private final FileSystemTreeImpl myExplorer; + private String myLocation; + + + public MapExternalResourceDialog(String uri, @NotNull Project project, @Nullable PsiFile file, @Nullable String location) { + super(project); + setTitle("Map External Resource"); + myUri.setText(uri); + + DefaultMutableTreeNode root = new DefaultMutableTreeNode(); + mySchemasTree.setModel(new DefaultTreeModel(root)); + ConfigFileSearcher searcher = new ConfigFileSearcher(file == null ? null : ModuleUtilCore.findModuleForPsiElement(file), project) { + @Override + public Set search(@Nullable Module module, @NotNull Project project) { + List> resources = XmlNamespaceIndex.getAllResources(module, project, null); + + HashSet files = new HashSet(); + PsiManager psiManager = PsiManager.getInstance(project); + for (IndexedRelevantResource resource : resources) { + VirtualFile file = resource.getFile(); + PsiFile psiFile = psiManager.findFile(file); + ContainerUtil.addIfNotNull(files, psiFile); + } + return files; + } + }; + searcher.search(); + new ConfigFilesTreeBuilder(mySchemasTree).buildTree(searcher, root); + TreeUtil.expandAll(mySchemasTree); + mySchemasTree.setRootVisible(false); + mySchemasTree.setShowsRootHandles(true); + + ColoredTreeCellRenderer renderer = new ColoredTreeCellRenderer() { + @Override + public void customizeCellRenderer(JTree tree, + Object value, + boolean selected, + boolean expanded, + boolean leaf, + int row, + boolean hasFocus) { + ConfigFilesTreeBuilder.renderNode(value, expanded, this); + } + }; + renderer.setFont(EditorColorsManager.getInstance().getGlobalScheme().getFont(EditorFontType.PLAIN)); + + mySchemasTree.setCellRenderer(renderer); + MouseAdapter mouseAdapter = new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() > 1 && isOKActionEnabled()) { + doOKAction(); + } + } + }; + mySchemasTree.addMouseListener(mouseAdapter); + + myUri.getDocument().addDocumentListener(new DocumentAdapter() { + @Override + protected void textChanged(DocumentEvent e) { + validateInput(); + } + }); + mySchemasTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() { + @Override + public void valueChanged(TreeSelectionEvent e) { + validateInput(); + } + }); + + myExplorer = new FileSystemTreeImpl(project, new FileChooserDescriptor(true, false, false, false, true, false)); + + myExplorer.addListener(new FileSystemTree.Listener() { + @Override + public void selectionChanged(List selection) { + validateInput(); + } + }, myExplorer); + myExplorer.getTree().addMouseListener(mouseAdapter); + + myExplorerPanel.add(ScrollPaneFactory.createScrollPane(myExplorer.getTree()), BorderLayout.CENTER); + + PsiFile schema = null; + if (file != null) { + schema = XmlUtil.findNamespaceByLocation(file, uri); + } + else if (location != null) { + VirtualFile virtualFile = VfsUtil.findRelativeFile(location, null); + if (virtualFile != null) { + schema = PsiManager.getInstance(project).findFile(virtualFile); + } + } + + if (schema != null) { + DefaultMutableTreeNode node = TreeUtil.findNodeWithObject(root, schema); + if (node != null) { + TreeUtil.selectNode(mySchemasTree, node); + } + myExplorer.select(schema.getVirtualFile(), null); + } + + init(); + } + + @Override + protected void processDoNotAskOnOk(int exitCode) { + super.processDoNotAskOnOk(exitCode); + // store it since explorer will be disposed + myLocation = getResourceLocation(); + } + + private void validateInput() { + setOKActionEnabled(!StringUtil.isEmpty(myUri.getText()) && getResourceLocation() != null); + } + + @Override + protected JComponent createCenterPanel() { + return myMainPanel; + } + + @Override + public JComponent getPreferredFocusedComponent() { + return StringUtil.isEmpty(myUri.getText()) ? myUri : mySchemasTree; + } + + public String getUri() { + return myUri.getText(); + } + + @Nullable + public String getResourceLocation() { + if (myLocation != null) return myLocation; + + if (myTabs.getSelectedIndex() == 0) { + TreePath path = mySchemasTree.getSelectionPath(); + if (path == null) return null; + Object object = ((DefaultMutableTreeNode)path.getLastPathComponent()).getUserObject(); + if (!(object instanceof PsiFile)) return null; + return FileUtil.toSystemIndependentName(((PsiFile)object).getVirtualFile().getPath()); + } + else { + VirtualFile file = myExplorer.getSelectedFile(); + return file == null ? null : FileUtil.toSystemIndependentName(file.getPath()); + } + } +} diff --git a/xml/impl/src/com/intellij/javaee/NameLocationPair.java b/xml/impl/src/com/intellij/javaee/NameLocationPair.java new file mode 100644 index 000000000000..724d2a557136 --- /dev/null +++ b/xml/impl/src/com/intellij/javaee/NameLocationPair.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2012 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.javaee; + +/** +* @author Dmitry Avdeev +* Date: 7/18/12 +*/ +public class NameLocationPair implements Comparable { + String myName; + String myLocation; + boolean myShared; + + public NameLocationPair(String name, String location, boolean shared) { + myName = name; + myLocation = location; + myShared = shared; + } + + public int compareTo(Object o) { + return myName.compareTo(((NameLocationPair)o).myName); + } + + public boolean equals(Object obj) { + if (! (obj instanceof NameLocationPair)) return false; + return compareTo(obj) == 0; + } + + public int hashCode() { + return myName.hashCode(); + } + + public String getName() { + return myName; + } + + public String getLocation(){ + return myLocation; + } +} diff --git a/xml/dom-openapi/src/com/intellij/util/xml/config/ConfigFileSearcher.java b/xml/impl/src/com/intellij/xml/config/ConfigFileSearcher.java similarity index 57% rename from xml/dom-openapi/src/com/intellij/util/xml/config/ConfigFileSearcher.java rename to xml/impl/src/com/intellij/xml/config/ConfigFileSearcher.java index 0094735bc532..20317e8aa4b7 100644 --- a/xml/dom-openapi/src/com/intellij/util/xml/config/ConfigFileSearcher.java +++ b/xml/impl/src/com/intellij/xml/config/ConfigFileSearcher.java @@ -1,33 +1,37 @@ -package com.intellij.util.xml.config; +package com.intellij.xml.config; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleUtilCore; +import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.JarFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiManager; import com.intellij.util.containers.MultiMap; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.List; import java.util.Set; public abstract class ConfigFileSearcher { private final MultiMap myFiles = new MultiMap(); private final MultiMap myJars = new MultiMap(); - private final List myVirtualFiles = new ArrayList(); - private final @NotNull Module myModule; + private final MultiMap myVirtualFiles = new MultiMap(); + private final @Nullable Module myModule; + @NotNull private final Project myProject; - public ConfigFileSearcher(@NotNull Module module) { + public ConfigFileSearcher(@Nullable Module module, @NotNull Project project) { myModule = module; + myProject = project; } public void search() { myFiles.clear(); myJars.clear(); - for (PsiFile file : search(myModule)) { + PsiManager psiManager = PsiManager.getInstance(myProject); + for (PsiFile file : search(myModule, myProject)) { VirtualFile jar = JarFileSystem.getInstance().getVirtualFileForJar(file.getVirtualFile()); if (jar != null) { myJars.putValue(jar, file); @@ -37,11 +41,15 @@ public abstract class ConfigFileSearcher { if (module != null) { myFiles.putValue(module, file); } + else { + VirtualFile virtualFile = file.getVirtualFile(); + myVirtualFiles.putValue(virtualFile.getParent(), psiManager.findFile(virtualFile)); + } } } } - public abstract Set search(@NotNull Module module); + public abstract Set search(@Nullable Module module, @NotNull Project project); public MultiMap getFilesByModules() { return myFiles; @@ -51,7 +59,7 @@ public abstract class ConfigFileSearcher { return myJars; } - public List getVirtualFiles() { + public MultiMap getVirtualFiles() { return myVirtualFiles; } } diff --git a/xml/dom-openapi/src/com/intellij/util/xml/config/ConfigFilesTreeBuilder.java b/xml/impl/src/com/intellij/xml/config/ConfigFilesTreeBuilder.java similarity index 88% rename from xml/dom-openapi/src/com/intellij/util/xml/config/ConfigFilesTreeBuilder.java rename to xml/impl/src/com/intellij/xml/config/ConfigFilesTreeBuilder.java index a55e50c40d80..ec42d5b611b6 100644 --- a/xml/dom-openapi/src/com/intellij/util/xml/config/ConfigFilesTreeBuilder.java +++ b/xml/impl/src/com/intellij/xml/config/ConfigFilesTreeBuilder.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.intellij.util.xml.config; +package com.intellij.xml.config; import com.intellij.ide.presentation.VirtualFilePresentation; import com.intellij.openapi.module.Module; @@ -21,7 +21,6 @@ import com.intellij.openapi.module.ModuleType; import com.intellij.openapi.vfs.JarFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; -import com.intellij.ui.CheckedTreeNode; import com.intellij.ui.ColoredTreeCellRenderer; import com.intellij.ui.SimpleTextAttributes; import com.intellij.ui.TreeSpeedSearch; @@ -44,6 +43,7 @@ public class ConfigFilesTreeBuilder { public ConfigFilesTreeBuilder(JTree tree) { myTree = tree; + installSearch(tree); } public Set buildTree(ConfigFileSearcher searcher, DefaultMutableTreeNode root) { @@ -51,10 +51,21 @@ public class ConfigFilesTreeBuilder { final MultiMap files = searcher.getFilesByModules(); final MultiMap jars = searcher.getJars(); final Set psiFiles = buildModuleNodes(files, jars, root); - final List virtualFiles = searcher.getVirtualFiles(); + final MultiMap virtualFiles = searcher.getVirtualFiles(); - for (VirtualFile virtualFile : virtualFiles) { - addFile(virtualFile); + for (Map.Entry> entry : virtualFiles.entrySet()) { + DefaultMutableTreeNode node = createFileNode(entry.getKey()); + List list = new ArrayList(entry.getValue()); + Collections.sort(list, new Comparator() { + @Override + public int compare(PsiFile o1, PsiFile o2) { + return o1.getName().compareToIgnoreCase(o2.getName()); + } + }); + for (PsiFile file : list) { + node.add(createFileNode(file)); + } + root.add(node); } return psiFiles; } @@ -80,8 +91,7 @@ public class ConfigFilesTreeBuilder { } }); for (Module module: modules) { - CheckedTreeNode moduleNode = new CheckedTreeNode(module); - moduleNode.setChecked(false); + DefaultMutableTreeNode moduleNode = createFileNode(module); root.add(moduleNode); if (files.containsKey(module)) { List moduleFiles = new ArrayList(files.get(module)); @@ -97,8 +107,7 @@ public class ConfigFilesTreeBuilder { final List list = new ArrayList(jars.get(file)); final PsiFile jar = list.get(0).getManager().findFile(file); if (jar != null) { - final CheckedTreeNode jarNode = new CheckedTreeNode(jar); - jarNode.setChecked(false); + final DefaultMutableTreeNode jarNode = createFileNode(jar); root.add(jarNode); Collections.sort(list, FILE_COMPARATOR); for (PsiFile psiFile: list) { diff --git a/xml/impl/src/com/intellij/xml/index/IndexedRelevantResource.java b/xml/impl/src/com/intellij/xml/index/IndexedRelevantResource.java index 075b32d8bdf2..a1db3e22731a 100644 --- a/xml/impl/src/com/intellij/xml/index/IndexedRelevantResource.java +++ b/xml/impl/src/com/intellij/xml/index/IndexedRelevantResource.java @@ -58,13 +58,13 @@ public class IndexedRelevantResource implements Compara } public static List> getAllResources(ID indexId, - @NotNull final Module module, - @Nullable NullableFunction>, IndexedRelevantResource> chooser) { + @Nullable final Module module, + @NotNull Project project, + @Nullable NullableFunction>, IndexedRelevantResource> chooser) { ArrayList> all = new ArrayList>(); - Collection allKeys = FileBasedIndex.getInstance().getAllKeys(indexId, module.getProject()); + Collection allKeys = FileBasedIndex.getInstance().getAllKeys(indexId, project); for (K key : allKeys) { - List> resources = getResources(indexId, key, module, module.getProject(), - null); + List> resources = getResources(indexId, key, module, project, null); if (!resources.isEmpty()) { if (chooser == null) { all.add(resources.get(0)); diff --git a/xml/impl/src/com/intellij/xml/index/XmlNamespaceIndex.java b/xml/impl/src/com/intellij/xml/index/XmlNamespaceIndex.java index a01971a0dc9d..4fe063b4caae 100644 --- a/xml/impl/src/com/intellij/xml/index/XmlNamespaceIndex.java +++ b/xml/impl/src/com/intellij/xml/index/XmlNamespaceIndex.java @@ -54,13 +54,10 @@ public class XmlNamespaceIndex extends XmlIndex { return resources; } - public static List> getAllResources(@NotNull final Module module) { - return getAllResources(module, null); - } - - public static List> getAllResources(@NotNull final Module module, + public static List> getAllResources(@Nullable final Module module, + @NotNull Project project, @Nullable NullableFunction>, IndexedRelevantResource> chooser) { - return IndexedRelevantResource.getAllResources(NAME, module, chooser); + return IndexedRelevantResource.getAllResources(NAME, module, project, chooser); } private static final ID NAME = ID.create("XmlNamespaces"); diff --git a/xml/impl/src/com/intellij/xml/util/XmlUtil.java b/xml/impl/src/com/intellij/xml/util/XmlUtil.java index a4f710f21066..0c36beb61375 100644 --- a/xml/impl/src/com/intellij/xml/util/XmlUtil.java +++ b/xml/impl/src/com/intellij/xml/util/XmlUtil.java @@ -246,7 +246,7 @@ public class XmlUtil { } @Nullable - public static XmlFile findNamespaceByLocation(PsiFile base, @NotNull String nsLocation) { + public static XmlFile findNamespaceByLocation(@NotNull PsiFile base, @NotNull String nsLocation) { final String location = ExternalResourceManager.getInstance().getResourceLocation(nsLocation, base.getProject()); return findXmlFile(base, location); }