Search in sources :

Example 81 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-community by JetBrains.

the class ResourceBundleGrouper method modify.

@Override
@NotNull
public Collection<AbstractTreeNode> modify(@NotNull AbstractTreeNode parent, @NotNull final Collection<AbstractTreeNode> children, final ViewSettings settings) {
    if (parent instanceof ResourceBundleNode)
        return children;
    return ReadAction.compute(() -> {
        Map<ResourceBundle, Collection<PropertiesFile>> childBundles = new THashMap<>();
        for (AbstractTreeNode child : children) {
            Object f = child.getValue();
            if (f instanceof PsiFile) {
                PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile((PsiFile) f);
                if (propertiesFile != null) {
                    boolean isProcessed = false;
                    for (Collection<PropertiesFile> files : childBundles.values()) {
                        if (files.contains(propertiesFile)) {
                            isProcessed = true;
                            break;
                        }
                    }
                    if (isProcessed)
                        continue;
                    PropertiesImplUtil.ResourceBundleWithCachedFiles resourceBundleWithCachedFiles = PropertiesImplUtil.getResourceBundleWithCachedFiles(propertiesFile);
                    final ResourceBundle bundle = resourceBundleWithCachedFiles.getBundle();
                    Collection<PropertiesFile> files = childBundles.get(bundle);
                    if (files == null) {
                        files = new LinkedHashSet<>();
                        childBundles.put(bundle, files);
                    }
                    files.add(propertiesFile);
                    files.addAll(resourceBundleWithCachedFiles.getFiles());
                }
            }
        }
        List<AbstractTreeNode> result = new ArrayList<>();
        for (Map.Entry<ResourceBundle, Collection<PropertiesFile>> entry : childBundles.entrySet()) {
            ResourceBundle resourceBundle = entry.getKey();
            Collection<PropertiesFile> files = entry.getValue();
            if (files.size() != 1) {
                result.add(new ResourceBundleNode(myProject, resourceBundle, settings));
            }
        }
        for (AbstractTreeNode child : children) {
            Object f = child.getValue();
            if (f instanceof PsiFile) {
                PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile((PsiFile) f);
                if (propertiesFile != null) {
                    ResourceBundle bundle = null;
                    for (Map.Entry<ResourceBundle, Collection<PropertiesFile>> e : childBundles.entrySet()) {
                        if (e.getValue().contains(propertiesFile)) {
                            bundle = e.getKey();
                            break;
                        }
                    }
                    LOG.assertTrue(bundle != null);
                    if (childBundles.get(bundle).size() != 1) {
                        continue;
                    } else if (bundle instanceof CustomResourceBundle) {
                        final CustomResourceBundlePropertiesFileNode node = new CustomResourceBundlePropertiesFileNode(myProject, (PsiFile) f, settings);
                        result.add(node);
                    }
                }
            }
            result.add(child);
        }
        return result;
    });
}
Also used : AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) CustomResourceBundle(com.intellij.lang.properties.CustomResourceBundle) THashMap(gnu.trove.THashMap) PropertiesImplUtil(com.intellij.lang.properties.PropertiesImplUtil) ResourceBundle(com.intellij.lang.properties.ResourceBundle) CustomResourceBundle(com.intellij.lang.properties.CustomResourceBundle) PsiFile(com.intellij.psi.PsiFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) THashMap(gnu.trove.THashMap) NotNull(org.jetbrains.annotations.NotNull)

Example 82 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-community by JetBrains.

the class ResourceBundleEditor method recreateEditorsPanel.

void recreateEditorsPanel() {
    if (!myProject.isOpen() || myDisposed)
        return;
    myValuesPanel.removeAll();
    myValuesPanel.setLayout(new CardLayout());
    JPanel valuesPanelComponent = new MyJPanel(new GridBagLayout());
    myValuesPanel.add(new JBScrollPane(valuesPanelComponent) {

        @Override
        public void updateUI() {
            super.updateUI();
            getViewport().setBackground(UIUtil.getPanelBackground());
        }
    }, VALUES);
    myValuesPanel.add(myNoPropertySelectedPanel, NO_PROPERTY_SELECTED);
    final List<PropertiesFile> propertiesFiles = myResourceBundle.getPropertiesFiles();
    GridBagConstraints gc = new GridBagConstraints(0, 0, 0, 0, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, JBUI.insets(5), 0, 0);
    releaseAllEditors();
    myTitledPanels.clear();
    int y = 0;
    Editor previousEditor = null;
    Editor firstEditor = null;
    for (final PropertiesFile propertiesFile : propertiesFiles) {
        final EditorEx editor = createEditor();
        final Editor oldEditor = myEditors.put(propertiesFile.getVirtualFile(), editor);
        if (firstEditor == null) {
            firstEditor = editor;
        }
        if (previousEditor != null) {
            editor.putUserData(ChooseSubsequentPropertyValueEditorAction.PREV_EDITOR_KEY, previousEditor);
            previousEditor.putUserData(ChooseSubsequentPropertyValueEditorAction.NEXT_EDITOR_KEY, editor);
        }
        previousEditor = editor;
        if (oldEditor != null) {
            EditorFactory.getInstance().releaseEditor(oldEditor);
        }
        editor.setViewer(!propertiesFile.getVirtualFile().isWritable());
        editor.getContentComponent().addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                if (editor.isViewer()) {
                    editor.setViewer(ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(propertiesFile.getVirtualFile()).hasReadonlyFiles());
                }
            }
        });
        editor.addFocusListener(new FocusChangeListener() {

            @Override
            public void focusGained(final Editor editor) {
                mySelectedEditor = editor;
            }

            @Override
            public void focusLost(final Editor editor) {
                if (!editor.isViewer() && propertiesFile.getContainingFile().isValid()) {
                    writeEditorPropertyValue(null, editor, propertiesFile.getVirtualFile());
                    myVfsListener.flush();
                }
            }
        });
        gc.gridx = 0;
        gc.gridy = y++;
        gc.gridheight = 1;
        gc.gridwidth = GridBagConstraints.REMAINDER;
        gc.weightx = 1;
        gc.weighty = 1;
        gc.anchor = GridBagConstraints.CENTER;
        String title = propertiesFile.getName();
        title += PropertiesUtil.getPresentableLocale(propertiesFile.getLocale());
        JComponent comp = new JPanel(new BorderLayout()) {

            @Override
            public Dimension getPreferredSize() {
                Insets insets = getBorder().getBorderInsets(this);
                return new Dimension(100, editor.getLineHeight() * 4 + insets.top + insets.bottom);
            }
        };
        comp.add(editor.getComponent(), BorderLayout.CENTER);
        comp.setBorder(IdeBorderFactory.createTitledBorder(title, false));
        myTitledPanels.put(propertiesFile.getVirtualFile(), (JPanel) comp);
        valuesPanelComponent.add(comp, gc);
    }
    if (previousEditor != null) {
        previousEditor.putUserData(ChooseSubsequentPropertyValueEditorAction.NEXT_EDITOR_KEY, firstEditor);
        firstEditor.putUserData(ChooseSubsequentPropertyValueEditorAction.PREV_EDITOR_KEY, previousEditor);
    }
    gc.gridx = 0;
    gc.gridy = y;
    gc.gridheight = GridBagConstraints.REMAINDER;
    gc.gridwidth = GridBagConstraints.REMAINDER;
    gc.weightx = 10;
    gc.weighty = 1;
    valuesPanelComponent.add(new JPanel(), gc);
    selectionChanged();
    myValuesPanel.repaint();
    updateEditorsFromProperties(true);
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) KeyAdapter(java.awt.event.KeyAdapter) KeyEvent(java.awt.event.KeyEvent) XmlPropertiesFile(com.intellij.lang.properties.xml.XmlPropertiesFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) com.intellij.openapi.fileEditor(com.intellij.openapi.fileEditor) JBScrollPane(com.intellij.ui.components.JBScrollPane) FocusChangeListener(com.intellij.openapi.editor.ex.FocusChangeListener)

Example 83 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-community by JetBrains.

the class ResourceBundleEditor method installPropertiesChangeListeners.

private void installPropertiesChangeListeners() {
    final VirtualFileManager virtualFileManager = VirtualFileManager.getInstance();
    if (myVfsListener != null) {
        throw new AssertionError("Listeners can't be initialized twice");
    }
    myVfsListener = new ResourceBundleEditorFileListener(this);
    virtualFileManager.addVirtualFileListener(myVfsListener, this);
    PsiTreeChangeAdapter psiTreeChangeAdapter = new PsiTreeChangeAdapter() {

        @Override
        public void childAdded(@NotNull PsiTreeChangeEvent event) {
            final PsiFile file = event.getFile();
            if (file instanceof XmlFile) {
                final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(file);
                if (propertiesFile != null) {
                    final ResourceBundle bundle = propertiesFile.getResourceBundle();
                    if (bundle.equals(myResourceBundle) && !myEditors.containsKey(propertiesFile.getVirtualFile())) {
                        recreateEditorsPanel();
                    }
                }
            }
        }

        @Override
        public void childRemoved(@NotNull PsiTreeChangeEvent event) {
            final PsiFile file = event.getFile();
            final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(file);
            if (propertiesFile != null) {
                final ResourceBundle bundle = propertiesFile.getResourceBundle();
                if (bundle.equals(myResourceBundle) && myEditors.containsKey(propertiesFile.getVirtualFile())) {
                    final IProperty property = PropertiesImplUtil.getProperty(event.getParent());
                    if (property != null && Comparing.equal(property.getName(), getSelectedPropertyName())) {
                        updateEditorsFromProperties(false);
                    }
                }
            }
        }
    };
    PsiManager.getInstance(myProject).addPsiTreeChangeListener(psiTreeChangeAdapter, this);
}
Also used : VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) XmlFile(com.intellij.psi.xml.XmlFile) IProperty(com.intellij.lang.properties.IProperty) XmlPropertiesFile(com.intellij.lang.properties.xml.XmlPropertiesFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) ResourceBundle(com.intellij.lang.properties.ResourceBundle) NotNull(org.jetbrains.annotations.NotNull)

Example 84 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-community by JetBrains.

the class ResourceBundleEditor method updateEditorsFromProperties.

void updateEditorsFromProperties(final boolean checkIsUnderUndoRedoAction) {
    String propertyName = getSelectedPropertyName();
    ((CardLayout) myValuesPanel.getLayout()).show(myValuesPanel, propertyName == null ? NO_PROPERTY_SELECTED : VALUES);
    if (propertyName == null)
        return;
    final UndoManagerImpl undoManager = (UndoManagerImpl) UndoManager.getInstance(myProject);
    for (final PropertiesFile propertiesFile : myResourceBundle.getPropertiesFiles()) {
        final EditorEx editor = myEditors.get(propertiesFile.getVirtualFile());
        if (editor == null)
            continue;
        final IProperty property = propertiesFile.findPropertyByKey(propertyName);
        final Document document = editor.getDocument();
        CommandProcessor.getInstance().executeCommand(null, () -> ApplicationManager.getApplication().runWriteAction(() -> {
            if (!checkIsUnderUndoRedoAction || !undoManager.isActive() || !(undoManager.isRedoInProgress() || undoManager.isUndoInProgress())) {
                updateDocumentFromPropertyValue(getPropertyEditorValue(property), document, propertiesFile.getVirtualFile());
            }
        }), "", this);
        JPanel titledPanel = myTitledPanels.get(propertiesFile.getVirtualFile());
        ((TitledBorder) titledPanel.getBorder()).setTitleColor(property == null ? JBColor.RED : UIUtil.getLabelTextForeground());
        titledPanel.repaint();
    }
}
Also used : UndoManagerImpl(com.intellij.openapi.command.impl.UndoManagerImpl) EditorEx(com.intellij.openapi.editor.ex.EditorEx) IProperty(com.intellij.lang.properties.IProperty) XmlPropertiesFile(com.intellij.lang.properties.xml.XmlPropertiesFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) TitledBorder(javax.swing.border.TitledBorder)

Example 85 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-community by JetBrains.

the class ResourceBundlePropertiesUpdateManager method deletePropertyIfExist.

public void deletePropertyIfExist(String key, PropertiesFile file) {
    final IProperty property = file.findPropertyByKey(key);
    if (property != null && myKeysOrder != null) {
        boolean keyExistInOtherPropertiesFiles = false;
        for (PropertiesFile propertiesFile : myResourceBundle.getPropertiesFiles()) {
            if (!propertiesFile.equals(file) && propertiesFile.findPropertyByKey(key) != null) {
                keyExistInOtherPropertiesFiles = true;
                break;
            }
        }
        if (!keyExistInOtherPropertiesFiles) {
            myKeysOrder.remove(key);
        }
    }
    if (property != null) {
        PsiElement anElement = property.getPsiElement();
        if (anElement instanceof PomTargetPsiElement) {
            final PomTarget xmlProperty = ((PomTargetPsiElement) anElement).getTarget();
            LOG.assertTrue(xmlProperty instanceof XmlProperty);
            anElement = ((XmlProperty) xmlProperty).getNavigationElement();
        }
        anElement.delete();
    }
}
Also used : PomTarget(com.intellij.pom.PomTarget) IProperty(com.intellij.lang.properties.IProperty) XmlProperty(com.intellij.lang.properties.xml.XmlProperty) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)143 IProperty (com.intellij.lang.properties.IProperty)44 PsiFile (com.intellij.psi.PsiFile)42 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 ResourceBundle (com.intellij.lang.properties.ResourceBundle)19 PsiElement (com.intellij.psi.PsiElement)19 NotNull (org.jetbrains.annotations.NotNull)19 Nullable (org.jetbrains.annotations.Nullable)18 Property (com.intellij.lang.properties.psi.Property)15 Project (com.intellij.openapi.project.Project)10 XmlPropertiesFile (com.intellij.lang.properties.xml.XmlPropertiesFile)9 PsiDirectory (com.intellij.psi.PsiDirectory)8 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 THashSet (gnu.trove.THashSet)8 ArrayList (java.util.ArrayList)7 Module (com.intellij.openapi.module.Module)6 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)6 HashSet (com.intellij.util.containers.HashSet)6 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5