Skip to content

Commit

Permalink
Merge pull request #9 from skyrylyuk/master
Browse files Browse the repository at this point in the history
Custom prefix pattern and settings UI
  • Loading branch information
dmytrodanylyk committed Oct 30, 2015
2 parents 8fef6b2 + a491e85 commit d49404e
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 20 deletions.
5 changes: 5 additions & 0 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
<!-- Add your project components here -->
</project-components>

<extensions defaultExtensionNs="com.intellij">
<errorHandler implementation="com.intellij.diagnostic.ITNReporter"/>
<applicationConfigurable instance="com.dd.SettingConfigurable"/>
</extensions>

<actions>
<!-- Add your actions here -->
<action id="ComposeAction" class="com.dd.ComposeAction">
Expand Down
29 changes: 28 additions & 1 deletion src/com/dd/DirectoryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import com.intellij.ide.projectView.PresentationData;
import com.intellij.ide.projectView.ProjectViewNode;
import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.projectView.impl.nodes.PsiFileNode;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
Expand Down Expand Up @@ -34,12 +37,36 @@ public boolean contains(@NotNull VirtualFile file) {
return false;
}

public void addChildren(AbstractTreeNode treeNode) {
mChildNodeList.add(treeNode);
}

public void addAllChildren(List<AbstractTreeNode> treeNodeList) {
mChildNodeList.addAll(treeNodeList);
}

@NotNull
@Override
public List<AbstractTreeNode> getChildren() {
return mChildNodeList;
if (PropertiesComponent.getInstance().getBoolean(SettingConfigurable.PREFIX_HIDE, false)) {
final ArrayList<AbstractTreeNode> abstractTreeNodes = new ArrayList<>();
for (AbstractTreeNode fileNode : mChildNodeList) {
PsiFile psiFile = (PsiFile) fileNode.getValue();
final ViewSettings settings = ((PsiFileNode) fileNode).getSettings();
String shortName = psiFile.getName().substring(mName.length());
final int beginIndex = shortName.indexOf(ProjectStructureProvider.COMPOSE_BY_CHAR);
if (beginIndex != -1) {
shortName = shortName.substring(beginIndex + 1);
}
abstractTreeNodes.add(new FoldingNode(fileNode.getProject(), psiFile, settings, shortName));
}
return abstractTreeNodes;
} else {
return mChildNodeList;
}
}


@Override
protected void update(PresentationData presentation) {
presentation.setPresentableText(mName);
Expand Down
23 changes: 23 additions & 0 deletions src/com/dd/FoldingNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.dd;

import com.intellij.ide.projectView.PresentationData;
import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.projectView.impl.nodes.PsiFileNode;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;

class FoldingNode extends PsiFileNode {
private String mName;

public FoldingNode(Project project, PsiFile value, ViewSettings viewSettings, String shortName) {
super(project, value, viewSettings);
mName = shortName;
}

@Override
protected void updateImpl(PresentationData presentationData) {
super.updateImpl(presentationData);

presentationData.setPresentableText(mName);
}
}
73 changes: 54 additions & 19 deletions src/com/dd/ProjectStructureProvider.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dd;

import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
Expand All @@ -12,10 +13,12 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProjectStructureProvider implements com.intellij.ide.projectView.TreeStructureProvider {

private static final char COMPOSE_BY_CHAR = '_';
public static final char COMPOSE_BY_CHAR = '_';
private static final String OTHER_NODE = "other";

@Nullable
Expand All @@ -27,7 +30,7 @@ public Object getData(Collection<AbstractTreeNode> collection, String s) {
@NotNull
@Override
public Collection<AbstractTreeNode> modify(@NotNull AbstractTreeNode parent, @NotNull Collection<AbstractTreeNode> children, ViewSettings viewSettings) {
List<AbstractTreeNode> resultList = new ArrayList<AbstractTreeNode>();
List<AbstractTreeNode> resultList = new ArrayList<>();
if (parent.getValue() instanceof PsiDirectory) {
PsiDirectory directory = (PsiDirectory) parent.getValue();
String path = directory.getVirtualFile().getPath();
Expand All @@ -45,36 +48,50 @@ public Collection<AbstractTreeNode> modify(@NotNull AbstractTreeNode parent, @No

@NotNull
private List<AbstractTreeNode> createComposedFiles(@NotNull Collection<AbstractTreeNode> fileNodes, ViewSettings viewSettings) {
List<AbstractTreeNode> resultList = new ArrayList<AbstractTreeNode>();
List<AbstractTreeNode> resultList = new ArrayList<>();
Project project = Utils.getCurrentProject();
if (project != null) {
HashSet<String> composedDirNameSet = new HashSet<String>();
List<AbstractTreeNode> notComposedFileNodes = new ArrayList<AbstractTreeNode>();
HashSet<String> composedDirNameSet = new HashSet<>();
List<AbstractTreeNode> notComposedFileNodes = new ArrayList<>();

final boolean customPrefix = PropertiesComponent.getInstance().getBoolean(SettingConfigurable.PREFIX_CUSTOM_USE, false);

Pattern pattern = customPrefix ? Pattern.compile(PropertiesComponent.getInstance().getValue(SettingConfigurable.PREFIX_PATTERN, SettingConfigurable.DEFAULT_PATTERN)) : null;

for (AbstractTreeNode fileNode : fileNodes) {
if (fileNode.getValue() instanceof PsiFile) {
PsiFile psiFile = (PsiFile) fileNode.getValue();
String fileName = psiFile.getName();
int endIndex = fileName.indexOf(COMPOSE_BY_CHAR);
if (endIndex != -1) {
String composedDirName = fileName.substring(0, endIndex);
composedDirNameSet.add(composedDirName);
if (customPrefix) {
Matcher m = pattern.matcher(fileName);
if (m.find()) {
String composedDirName = m.group(0);
composedDirNameSet.add(composedDirName);
} else {
notComposedFileNodes.add(fileNode);
}
} else {
notComposedFileNodes.add(fileNode);
int endIndex = fileName.indexOf(COMPOSE_BY_CHAR);
if (endIndex != -1) {
String composedDirName = fileName.substring(0, endIndex);
composedDirNameSet.add(composedDirName);
} else {
notComposedFileNodes.add(fileNode);
}
}
}
}

for (String composedDirName : composedDirNameSet) {
DirectoryNode composedDirNode = new DirectoryNode(project, viewSettings, composedDirName);
List<AbstractTreeNode> composedFileNodes = filterByDirName(fileNodes, composedDirName);
composedDirNode.getChildren().addAll(composedFileNodes);
composedDirNode.addAllChildren(composedFileNodes);
resultList.add(composedDirNode);
}

if(!notComposedFileNodes.isEmpty()) {
if (!notComposedFileNodes.isEmpty()) {
DirectoryNode composedDirNode = new DirectoryNode(project, viewSettings, OTHER_NODE);
composedDirNode.getChildren().addAll(notComposedFileNodes);
composedDirNode.addAllChildren(notComposedFileNodes);
resultList.add(composedDirNode);
}
}
Expand All @@ -83,16 +100,34 @@ private List<AbstractTreeNode> createComposedFiles(@NotNull Collection<AbstractT

@NotNull
private List<AbstractTreeNode> filterByDirName(Collection<AbstractTreeNode> fileNodes, String token) {
List<AbstractTreeNode> resultList = new ArrayList<AbstractTreeNode>();
List<AbstractTreeNode> resultList = new ArrayList<>();

final boolean customPrefix = PropertiesComponent.getInstance().getBoolean(SettingConfigurable.PREFIX_CUSTOM_USE, false);

Pattern pattern = customPrefix ? Pattern.compile(PropertiesComponent.getInstance().getValue(SettingConfigurable.PREFIX_PATTERN, SettingConfigurable.DEFAULT_PATTERN)) : null;

for (AbstractTreeNode fileNode : fileNodes) {
if (fileNode.getValue() instanceof PsiFile) {
PsiFile psiFile = (PsiFile) fileNode.getValue();
String fileName = psiFile.getName();
int endIndex = fileName.indexOf(COMPOSE_BY_CHAR);
if (endIndex != -1) {
String composedDirName = fileName.substring(0, endIndex);
if(composedDirName.equals(token)) {
resultList.add(fileNode);

if (customPrefix) {
Matcher m = pattern.matcher(fileName);
if (m.find()) {

String composedDirName = m.group(0);
if (composedDirName.equals(token)) {
resultList.add(fileNode);
}
}
} else {

int endIndex = fileName.indexOf(COMPOSE_BY_CHAR);
if (endIndex != -1) {
String composedDirName = fileName.substring(0, endIndex);
if (composedDirName.equals(token)) {
resultList.add(fileNode);
}
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/com/dd/SettingConfigurable.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.dd.SettingConfigurable">
<grid id="27dc6" binding="mPanel" default-binding="true" layout-manager="FormLayout">
<rowspec value="center:d:noGrow"/>
<rowspec value="top:3dlu:noGrow"/>
<rowspec value="center:max(d;4px):noGrow"/>
<colspec value="fill:d:noGrow"/>
<colspec value="left:4dlu:noGrow"/>
<colspec value="fill:d:grow"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="77c02" class="javax.swing.JCheckBox" binding="useCustomPatternCheckBox" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<forms/>
</constraints>
<properties>
<text value="Use Custom Pattern"/>
</properties>
</component>
<component id="e1fec" class="javax.swing.JTextField" binding="customPattern">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
<forms defaultalign-horz="false"/>
</constraints>
<properties>
<enabled value="false"/>
</properties>
</component>
<component id="5d684" class="javax.swing.JCheckBox" binding="hideFoldingPrefix">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<forms/>
</constraints>
<properties>
<selected value="false"/>
<text value="Hide Folding Prefix"/>
</properties>
</component>
</children>
</grid>
</form>
118 changes: 118 additions & 0 deletions src/com/dd/SettingConfigurable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.dd;

import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* Created by skyrylyuk on 10/15/15.
*/
public class SettingConfigurable implements Configurable {
public static final String PREFIX_PATTERN = "folding_plugin_prefix_pattern";
public static final String PREFIX_CUSTOM_USE = "folding_plugin_prefix_custom_use";
public static final String PREFIX_HIDE = "folding_plugin_prefix_hide";

public static final String DEFAULT_PATTERN = "[^_]{1,}(?=_)";
public static final String DEFAULT_PATTERN_DOUBLE = "[^_]{1,}_[^_]{1,}(?=_)";

private JPanel mPanel;
private JCheckBox useCustomPatternCheckBox;
private JTextField customPattern;
private JCheckBox hideFoldingPrefix;
private boolean isModified = false;

@Nls
@Override
public String getDisplayName() {
return "Android Folding";
}

@Nullable
@Override
public String getHelpTopic() {
return "null:";
}

@Nullable
@Override
public JComponent createComponent() {

useCustomPatternCheckBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
boolean selected = getCheckBoxStatus(actionEvent);

customPattern.setEnabled(selected);
isModified = true;
}
});

customPattern.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
isModified = true;
}

@Override
public void removeUpdate(DocumentEvent e) {
isModified = true;
}

@Override
public void changedUpdate(DocumentEvent e) {
isModified = true;
}
});

hideFoldingPrefix.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
isModified = true;
}
});

reset();

return mPanel;
}

private boolean getCheckBoxStatus(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
return abstractButton.getModel().isSelected();
}


@Override
public boolean isModified() {

return isModified;
}

@Override
public void apply() throws ConfigurationException {
PropertiesComponent.getInstance().setValue(PREFIX_CUSTOM_USE, Boolean.valueOf(useCustomPatternCheckBox.isSelected()).toString());
PropertiesComponent.getInstance().setValue(PREFIX_PATTERN, customPattern.getText());
PropertiesComponent.getInstance().setValue(PREFIX_HIDE, Boolean.valueOf(hideFoldingPrefix.isSelected()).toString());

isModified = false;
}

@Override
public void reset() {
final boolean customPrefix = PropertiesComponent.getInstance().getBoolean(PREFIX_CUSTOM_USE, false);
useCustomPatternCheckBox.setSelected(customPrefix);
customPattern.setEnabled(customPrefix);
customPattern.setText(PropertiesComponent.getInstance().getValue(PREFIX_PATTERN, DEFAULT_PATTERN_DOUBLE));
hideFoldingPrefix.getModel().setSelected(PropertiesComponent.getInstance().getBoolean(PREFIX_HIDE, false));
}

@Override
public void disposeUIResources() {
}
}

0 comments on commit d49404e

Please sign in to comment.