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;
});
}
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);
}
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);
}
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();
}
}
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();
}
}
Aggregations