Skip to content

Commit 207878b

Browse files
authored
Merge pull request #70 from ybroeker/feat/filechooser
User intellij-FileChooser and add name
2 parents 94a1b3d + c6d0e06 commit 207878b

File tree

5 files changed

+68
-45
lines changed

5 files changed

+68
-45
lines changed

src/com/intellij/plugins/bodhi/pmd/PMDConfigurable.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
public class PMDConfigurable implements Configurable {
1212
private PMDConfigurationForm form;
1313
private PMDProjectComponent component;
14+
private final Project project;
1415

1516
public PMDConfigurable(Project project) {
17+
this.project=project;
1618
component = project.getComponent(PMDProjectComponent.class);
1719
}
1820

@@ -28,7 +30,7 @@ public String getHelpTopic() {
2830

2931
public JComponent createComponent() {
3032
if (form == null) {
31-
form = new PMDConfigurationForm();
33+
form = new PMDConfigurationForm(project);
3234
}
3335
return form.getRootPanel();
3436
}

src/com/intellij/plugins/bodhi/pmd/PMDConfigurationForm.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.intellij.plugins.bodhi.pmd;
22

3+
import com.intellij.openapi.fileChooser.FileChooser;
4+
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
5+
import com.intellij.openapi.vfs.VfsUtilCore;
6+
import com.intellij.openapi.vfs.VirtualFile;
7+
import com.intellij.openapi.project.Project;
38
import com.intellij.openapi.ui.DialogBuilder;
49
import com.intellij.openapi.ui.DialogWrapper;
510
import com.intellij.openapi.util.IconLoader;
@@ -36,13 +41,16 @@ public class PMDConfigurationForm {
3641
private JTable table1;
3742
private JPanel mainPanel;
3843
private JCheckBox skipTestsCheckBox;
44+
3945
private boolean isModified;
46+
private Project project;
4047

4148
private static final Object[] columnNames = new String[] {"Option", "Value"};
4249
private static final String[] optionNames = new String[] {"Target JDK", "Encoding"};
4350
private static final String[] defaultValues = new String[] {"1.8", ""};
4451

45-
public PMDConfigurationForm() {
52+
public PMDConfigurationForm(final Project project) {
53+
this.project = project;
4654
//Get the action group defined
4755
DefaultActionGroup actionGroup = (DefaultActionGroup) ActionManager.getInstance().getAction("PMDSettingsEdit");
4856
//Remove toolbar actions associated to previous form
@@ -132,7 +140,7 @@ private void modifyRuleSet(final String defaultValue, AnActionEvent e) {
132140
db.addOkAction();
133141
db.addCancelAction();
134142
db.setTitle("Select Custom RuleSet File or type URL");
135-
final BrowsePanel panel = new BrowsePanel(defaultValue, db);
143+
final BrowsePanel panel = new BrowsePanel(defaultValue, db, project);
136144
db.setOkActionEnabled(defaultValue != null && defaultValue.trim().length() > 0);
137145
db.show();
138146
//If ok is selected add the selected ruleset
@@ -282,8 +290,11 @@ static class BrowsePanel extends JPanel {
282290
private JTextField path;
283291
private JButton open;
284292

285-
public BrowsePanel(String defaultValue, final DialogBuilder db) {
293+
private Project project;
294+
295+
public BrowsePanel(String defaultValue, final DialogBuilder db, final Project project) {
286296
super();
297+
this.project = project;
287298
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
288299
label = new JLabel("Choose RuleSet: ");
289300
label.setMinimumSize(new Dimension(100, 20));
@@ -302,14 +313,15 @@ public BrowsePanel(String defaultValue, final DialogBuilder db) {
302313
open.setPreferredSize(new Dimension(80, 20));
303314
open.addActionListener(new ActionListener() {
304315
public void actionPerformed(ActionEvent e) {
305-
JFileChooser fc = new JFileChooser();
306-
fc.setFileFilter(PMDUtil.createFileExtensionFilter("xml", "XML Files"));
307-
final Component parent = SwingUtilities.getRoot(path);
308-
fc.showDialog(parent, "Open");
309-
File selected = fc.getSelectedFile();
310-
if (selected != null) {
311-
String newLocation = selected.getPath();
312-
path.setText(newLocation);
316+
final VirtualFile toSelect = project.getBaseDir();
317+
318+
final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, false, false, false, false);
319+
descriptor.withFileFilter(virtualFile -> virtualFile.getName().endsWith(".xml"));
320+
321+
final VirtualFile chosen = FileChooser.chooseFile(descriptor, BrowsePanel.this, project, toSelect);
322+
if (chosen != null) {
323+
final File newConfigFile = VfsUtilCore.virtualToIoFile(chosen);
324+
path.setText(newConfigFile.getAbsolutePath());
313325
}
314326
}
315327
});

src/com/intellij/plugins/bodhi/pmd/PMDProjectComponent.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.intellij.plugins.bodhi.pmd.core.PMDResultCollector;
2020
import com.intellij.ui.content.Content;
2121
import com.intellij.ui.content.ContentFactory;
22+
import net.sourceforge.pmd.RuleSet;
2223
import org.jetbrains.annotations.NonNls;
2324
import org.jetbrains.annotations.NotNull;
2425

@@ -97,23 +98,25 @@ private ActionGroup registerActions(String actionName) {
9798
void updateCustomRulesMenu() {
9899
PMDCustom actionGroup = (PMDCustom) ActionManager.getInstance().getAction("PMDCustom");
99100
for (final String rulePath : customRuleSetPaths) {
100-
String ruleName = PMDUtil.getRuleNameFromPath(rulePath);
101-
if (!customActionsMap.containsKey(rulePath)) {
102-
AnAction action = new AnAction(ruleName) {
101+
if (customActionsMap.containsKey(rulePath)) {
102+
continue;
103+
}
104+
try {
105+
RuleSet ruleSet = PMDResultCollector.loadRuleSet(rulePath);
106+
String ruleFileName = PMDUtil.getRuleNameFromPath(rulePath);
107+
AnAction action = new AnAction(String.format(Locale.ENGLISH, "%s (%s)",ruleSet.getName() , ruleFileName)) {
103108
public void actionPerformed(AnActionEvent e) {
104-
String err;
105-
if ( (err = PMDResultCollector.isValidRuleSet(rulePath)).length() > 0) {
106-
JOptionPane.showMessageDialog(resultPanel, "The ruleset file is not available or not a valid PMD ruleset: " + err,
107-
"Invalid File", JOptionPane.ERROR_MESSAGE);
108-
}
109-
else {
110-
PMDInvoker.getInstance().runPMD(e, rulePath, true);
111-
setLastRunActionAndRules(e, rulePath, true);
112-
}
109+
PMDInvoker.getInstance().runPMD(e, rulePath, true);
110+
setLastRunActionAndRules(e, rulePath, true);
113111
}
114112
};
115-
customActionsMap.put(rulePath, Pair.create(ruleName, action));
113+
customActionsMap.put(rulePath, Pair.create(ruleFileName, action));
116114
actionGroup.add(action);
115+
} catch (PMDResultCollector.InvalidRuleSetException e) {
116+
JOptionPane.showMessageDialog(resultPanel,
117+
"The ruleset file is not available or not a valid PMD ruleset:\n"
118+
+ e.getMessage(),
119+
"Invalid File", JOptionPane.ERROR_MESSAGE);
117120
}
118121
}
119122
for (Iterator<Map.Entry<String, Pair<String, AnAction>>> iterator = customActionsMap.entrySet().iterator(); iterator.hasNext();) {

src/com/intellij/plugins/bodhi/pmd/PMDResultPanel.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -469,24 +469,4 @@ public void actionPerformed(AnActionEvent e) {
469469
}
470470
}
471471

472-
private class ExportAction extends AnAction {
473-
private ExportAction() {
474-
super("Export", "Export the results to file", IconLoader.getIcon("/actions/export.png"));
475-
}
476-
477-
public void actionPerformed(AnActionEvent e) {
478-
DialogBuilder db = new DialogBuilder(PMDUtil.getProjectComponent(e).getCurrentProject());
479-
db.addOkAction();
480-
db.addCancelAction();
481-
db.setTitle("Export to File");
482-
final PMDConfigurationForm.BrowsePanel panel = new PMDConfigurationForm.BrowsePanel("", db);
483-
db.setOkActionEnabled(true);
484-
db.show();
485-
//If ok is selected add the selected ruleset
486-
if (db.getDialogWrapper().getExitCode() == DialogWrapper.OK_EXIT_CODE) {
487-
String fileName = panel.getText();
488-
System.out.println("Save to : " + fileName);
489-
}
490-
}
491-
}
492472
}

src/com/intellij/plugins/bodhi/pmd/core/PMDResultCollector.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,32 @@ public static String isValidRuleSet(String path) {
160160
return "Invalid File";
161161
}
162162

163+
public static RuleSet loadRuleSet(String path) throws InvalidRuleSetException {
164+
Thread.currentThread().setContextClassLoader(PMDResultCollector.class.getClassLoader());
165+
RuleSetFactory ruleSetFactory = new RuleSetFactory();
166+
try {
167+
RuleSet rs = ruleSetFactory.createRuleSet(path);
168+
if (rs.getRules().size() != 0) {
169+
return rs;
170+
}
171+
} catch (RuntimeException | RuleSetNotFoundException e) {
172+
throw new InvalidRuleSetException(e);
173+
}
174+
throw new InvalidRuleSetException("Invalid File");
175+
}
176+
177+
public static class InvalidRuleSetException extends Exception {
178+
179+
public InvalidRuleSetException(final String message) {
180+
super(message);
181+
}
182+
183+
public InvalidRuleSetException(final Throwable cause) {
184+
super(cause);
185+
}
186+
187+
}
188+
163189
private class PMDResultRenderer extends AbstractIncrementingRenderer {
164190

165191
private final List<DefaultMutableTreeNode> pmdResults;

0 commit comments

Comments
 (0)