diff --git a/pmd-gel/src/net/sourceforge/pmd/gel/PMDPlugin.java b/pmd-gel/src/net/sourceforge/pmd/gel/PMDPlugin.java index e6ab803929..e6dc1f6f51 100644 --- a/pmd-gel/src/net/sourceforge/pmd/gel/PMDPlugin.java +++ b/pmd-gel/src/net/sourceforge/pmd/gel/PMDPlugin.java @@ -1,95 +1,259 @@ +/* + * @(#)PMDPlugin.java $Revision$ ($Date$) + * Copyright (c) 2004 + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ package net.sourceforge.pmd.gel; -import java.awt.Color; -import java.awt.BorderLayout; -import java.util.List; +import java.io.File; +import java.io.FileReader; +import java.io.FilenameFilter; +import java.io.Reader; +import java.io.StringReader; + +import java.util.ArrayList; import java.util.Iterator; -import java.util.Vector; -import java.io.*; -import javax.swing.*; -import com.gexperts.gel.*; -import net.sourceforge.pmd.*; -import net.sourceforge.pmd.cpd.*; +import java.util.List; -public class PMDPlugin implements GelAction { +import javax.swing.ProgressMonitor; - // GelAction - public boolean isActive(Gel p0) { - return true; - } +import com.gexperts.gel.Gel; +import com.gexperts.gel.GelAction; - public void perform(Gel p0) { - try { - PMD pmd = new PMD(); - RuleContext ctx = new RuleContext(); - RuleSetFactory rsf = new RuleSetFactory(); - RuleSet ruleSet = new RuleSet(); - ruleSet.addRuleSet(rsf.createRuleSet("rulesets/unusedcode.xml,rulesets/basic.xml")); - ctx.setReport(new Report()); - if (p0.getProject() == null) { - String code = p0.getEditor().getContents(); - String name = p0.getEditor().getFileName(); - if (name == null) { - name = "Unnamed.java"; - } - ctx.setSourceCodeFilename(name); - Reader reader = new StringReader(code); - pmd.processFile(reader, ruleSet, ctx); - } else { - for (Iterator iter = p0.getProject().getSourcePaths().iterator(); iter.hasNext();) { - String srcDir = (String)iter.next(); - FileFinder ff = new FileFinder(); - List files = ff.findFilesFrom(srcDir, new JavaLanguage.JavaFileOrDirectoryFilter(), true); - for (Iterator fileIter = files.iterator(); fileIter.hasNext();) { - File fileName = (File)fileIter.next(); - ctx.setSourceCodeFilename(fileName.getAbsolutePath()); - Reader reader = new FileReader(fileName); - pmd.processFile(reader, ruleSet, ctx); - } - } - } - if (ctx.getReport().isEmpty()) { - JOptionPane.showMessageDialog(null, "No problems found", "PMD", JOptionPane.INFORMATION_MESSAGE); - } else { - createProblemFrame(ctx.getReport()); - } - } catch (Exception e) { - JOptionPane.showMessageDialog(null, "ERROR " + e.getClass().getName() + ":" + e.getMessage()); - e.printStackTrace(); - } - } +import net.sourceforge.pmd.PMD; +import net.sourceforge.pmd.Report; +import net.sourceforge.pmd.ReportListener; +import net.sourceforge.pmd.Rule; +import net.sourceforge.pmd.RuleContext; +import net.sourceforge.pmd.RuleSet; +import net.sourceforge.pmd.RuleSetFactory; +import net.sourceforge.pmd.RuleViolation; +import net.sourceforge.pmd.cpd.FileFinder; +import net.sourceforge.pmd.cpd.JavaLanguage; +import net.sourceforge.pmd.stat.Metric; - public String getName() { - return "PMD"; - } - // GelAction - private JFrame createProblemFrame(Report report) { - JFrame newFrame = new JFrame(); - JDialog dialog = new JDialog(newFrame, report.size() + " problems found"); - dialog.setContentPane(createProblemListPanel(report)); - dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - dialog.pack(); - dialog.setLocationRelativeTo(newFrame); - dialog.setVisible(true); - return newFrame; - } +/** + * PMD plugin for Gel. + * + * @author Andrey Lumyanski + * @version $Revision$ ($Date$) + */ +public class PMDPlugin implements GelAction, Runnable, ReportListener { + private Gel gel; + private Report report; + private ArrayList listOfFiles; + private ProgressMonitor progressMonitor; + private RuleSet arrayRuleSet[]; - private JPanel createProblemListPanel(Report report) { - Vector v = new Vector(); - for (Iterator i = report.iterator(); i.hasNext();) { - RuleViolation rv = (RuleViolation)i.next(); - String msg = rv.getFilename() + " - line " + (rv.getLine()-1) + " - " + rv.getDescription(); - v.add(msg); + /** + * Creates a PMDPlugin object. + */ + public PMDPlugin() { + super(); + gel = null; + report = null; + listOfFiles = null; + progressMonitor = null; + arrayRuleSet = null; + } - } - JList list = new JList(v); - list.setForeground(Color.red); - list.setBackground(Color.white); - list.setVisibleRowCount(20); + /** + * Returns true if the plugin is active. + * + * @param gel a Gel instance. + * + * @return true if the plugin is active. + */ + public boolean isActive(Gel gel) { + return gel.getEditor() != null; + } - JPanel container = new JPanel(new BorderLayout()); - container.add(list, BorderLayout.CENTER); - return container; - } + /** + * Performs action. + * + * @param gel Gel a Gel instance. + */ + public void perform(Gel gel) { + this.gel = gel; + report = new Report(); + report.addListener(this); + gel.clearMessages(); + gel.addMessage("PMD started"); + + try { + RuleSetFactory rsf = new RuleSetFactory(); + Iterator it = rsf.getRegisteredRuleSets(); + ArrayList listOfRuleSet = new ArrayList(); + while (it.hasNext()) { + listOfRuleSet.add((RuleSet)it.next()); + } + arrayRuleSet = new RuleSet[listOfRuleSet.size()]; + for (int i = 0; i < arrayRuleSet.length; ++i) { + arrayRuleSet[i] = (RuleSet) listOfRuleSet.get(i); + } + + RuleSetDialog dlgRuleSet = new RuleSetDialog(arrayRuleSet); + dlgRuleSet.show(); + if (dlgRuleSet.isCanceled()) { + gel.addMessage("PMD canceled."); + return; + } + + if (gel.getProject() != null) { + listOfFiles = new ArrayList(); + + it = gel.getProject().getSourcePaths().iterator(); + FileFinder ff = new FileFinder(); + FilenameFilter filter = + new JavaLanguage.JavaFileOrDirectoryFilter(); + + while (it.hasNext()) { + String srcDir = (String) it.next(); + List files = ff.findFilesFrom(srcDir, filter, true); + + if (files != null) { + listOfFiles.addAll(files); + } + } + } else { + String name = gel.getEditor().getFileName(); + + if (name == null) { + name = "Untitled"; + } + + listOfFiles.add(new File(name)); + } + + if (listOfFiles != null) { + progressMonitor = + new ProgressMonitor(null, "PMD", "", 0, listOfFiles.size()); + } + } catch (Exception e) { + StringBuffer msg = new StringBuffer(); + msg.append("PMD error:"); + msg.append(e.getClass().getName()); + msg.append(":"); + msg.append(e.getLocalizedMessage()); + gel.showMessage(msg.toString()); + } + + Thread threadPMD = new Thread(this); + threadPMD.start(); + } + + /** + * Returns a plugin's name. + * + * @return a plugin's name. + */ + public String getName() { + return "PMD"; + } + + /** + * A Runnable interface implementation. + */ + public void run() { + try { + PMD pmd = new PMD(); + RuleContext ctx = new RuleContext(); + RuleSet ruleSet = new RuleSet(); + Iterator it; + Rule rule; + for (int i = 0; i < arrayRuleSet.length; ++i) { + if (arrayRuleSet[i].include()) { + it = arrayRuleSet[i].getRules().iterator(); + while (it.hasNext()) { + rule = (Rule) it.next(); + if (rule.include()) { + ruleSet.addRule(rule); + } + } + } + } + if (ruleSet.size() > 0) { + ctx.setReport(report); + + if (gel.getProject() == null) { + String code = gel.getEditor().getContents(); + String name = gel.getEditor().getFileName(); + + if (name == null) { + name = "Untitled"; + } + + ctx.setSourceCodeFilename(name); + progressMonitor.setNote(name); + Reader reader = new StringReader(code); + pmd.processFile(reader, ruleSet, ctx); + progressMonitor.setProgress(1); + } else { + it = listOfFiles.iterator(); + int index = 0; + + while (it.hasNext()) { + File file = (File) it.next(); + ctx.setSourceCodeFilename(file.getAbsolutePath()); + ++index; + progressMonitor.setNote(file.getAbsolutePath()); + Reader reader = new FileReader(file); + pmd.processFile(reader, ruleSet, ctx); + progressMonitor.setProgress(index); + if (progressMonitor.isCanceled()) { + gel.addMessage("PMD canceled."); + return; + } + } + } + + if (report.isEmpty()) { + gel.addMessage("No problems found"); + } else { + StringBuffer msg = new StringBuffer(); + msg.append(report.size()); + msg.append(" problems found"); + gel.addMessage(msg.toString()); + } + } else { + gel.addMessage("It is not chosen any rule!"); + } + } catch (Exception e) { + StringBuffer msg = new StringBuffer(); + msg.append("PMD error:"); + msg.append(e.getClass().getName()); + msg.append(":"); + msg.append(e.getLocalizedMessage()); + gel.showMessage(msg.toString()); + } + + gel.addMessage("PMD finished"); + } + + /** + * Processes a RuleViolation adding. + * + * @param rv a RuleViolation + */ + public void ruleViolationAdded(RuleViolation rv) { + StringBuffer msg = new StringBuffer(); + msg.append("["); + msg.append(rv.getRule().getName()); + msg.append("]: "); + msg.append(rv.getFilename()); + msg.append(":"); + msg.append(rv.getLine()); + msg.append(": "); + msg.append(rv.getDescription()); + gel.addMessage(msg.toString()); + } + + /** + * Processes a Metric adding. + * + * @param metric aMetric + */ + public void metricAdded(Metric metric) { + } } \ No newline at end of file diff --git a/pmd-gel/src/net/sourceforge/pmd/gel/RuleSetDialog.java b/pmd-gel/src/net/sourceforge/pmd/gel/RuleSetDialog.java new file mode 100644 index 0000000000..0b9747ee13 --- /dev/null +++ b/pmd-gel/src/net/sourceforge/pmd/gel/RuleSetDialog.java @@ -0,0 +1,289 @@ +/* + * @(#)RuleSetDialog.java $Revision$ ($Date$) + * Copyright (c) 2004 + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.gel; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.Frame; +import java.awt.GridLayout; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import java.util.Properties; + +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.JTextArea; +import javax.swing.ListSelectionModel; +import javax.swing.UIManager; + +import javax.swing.border.Border; +import javax.swing.border.EmptyBorder; + +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import javax.swing.event.TableModelEvent; +import javax.swing.event.TableModelListener; + +import net.sourceforge.pmd.RuleSet; + +import net.sourceforge.pmd.gel.data.RuleSetsTableModel; +import net.sourceforge.pmd.gel.data.RuleSetTableModel; + +/** + * A RuleSet choose dialog. + * + * @author Andrey Lumyanski + * @version $Revision$ ($Date$) + */ +public class RuleSetDialog extends JDialog + implements ActionListener, ListSelectionListener, TableModelListener { + + private JButton buttonProcess; + private JButton buttonCancel; + private JTable tableRuleSets; + private JTextArea textareaRSDescription; + private JLabel labelRules; + private JTable tableRules; + private JTextArea textareaRuleDescription; + private JTextArea textareaCodeExample; + private boolean isCanceled; + + private RuleSet ruleSet[]; + private RuleSetsTableModel dmRuleSets; + private RuleSetTableModel dmRules; + + /** + * Creates a RuleSetDialog object. + * @param rs RuleSet[] a array of RuleSet + */ + public RuleSetDialog(RuleSet rs[]) { + super((Frame)null, "PMD: select rules", true); + + ruleSet = rs; + dmRuleSets = new RuleSetsTableModel(ruleSet); + dmRules = new RuleSetTableModel(); + + Border border1 = new EmptyBorder(2, 10, 2, 10); + Border border2 = new EmptyBorder(2, 10, 2, 10); + + JPanel panelRS = new JPanel(new BorderLayout()); + + Dimension size = new Dimension(20, 20); + + Font font = new Font("Helvetica", Font.PLAIN, 10); + Color color = new Color(0x00, 0x00, 0x80); + if (font != null) { + UIManager.put("Label.font", font); + UIManager.put("Button.font", font); + } + if (color != null) { + UIManager.put("Label.foreground", color); + UIManager.put("Button.foreground", color); + } + + JLabel labelRuleSets = new JLabel("Rule sets:"); + panelRS.add(labelRuleSets, BorderLayout.NORTH); + + tableRuleSets = new JTable(dmRuleSets); + tableRuleSets.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + tableRuleSets.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); + tableRuleSets.getColumnModel().getColumn(0).setMinWidth(50); + tableRuleSets.getColumnModel().getColumn(0).setMaxWidth(50); + tableRuleSets.getSelectionModel().addListSelectionListener(this); + dmRuleSets.addTableModelListener(this); + panelRS.add(new JScrollPane(tableRuleSets)); + + JPanel panelRSDescription = new JPanel(new BorderLayout()); + JLabel labelRSDescription = new JLabel("Rule set description:"); + textareaRSDescription = new JTextArea(); + textareaRSDescription.setLineWrap(true); + textareaRSDescription.setWrapStyleWord(true); + textareaRSDescription.setEditable(false); + panelRSDescription.add(labelRSDescription, BorderLayout.NORTH); + panelRSDescription.add(new JScrollPane(textareaRSDescription)); + size = new Dimension(300, 100); + panelRSDescription.setPreferredSize(size); + panelRSDescription.setBorder(border2); + panelRS.add(panelRSDescription, BorderLayout.EAST); + + JPanel panelRules = new JPanel(new BorderLayout()); + labelRules = new JLabel("Rules:"); + panelRules.add(labelRules, BorderLayout.NORTH); + + tableRules = new JTable(dmRules); + tableRules.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + tableRules.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); + tableRules.getColumnModel().getColumn(0).setMinWidth(50); + tableRules.getColumnModel().getColumn(0).setMaxWidth(50); + tableRules.getSelectionModel().addListSelectionListener(this); + + JPanel panelRule = new JPanel(new BorderLayout()); + + JPanel panelRD = new JPanel(new BorderLayout()); + JLabel labelRuleDescription = new JLabel("Rule description:"); + panelRD.add(labelRuleDescription, BorderLayout.NORTH); + textareaRuleDescription = new JTextArea(); + textareaRuleDescription.setLineWrap(true); + textareaRuleDescription.setWrapStyleWord(true); + textareaRuleDescription.setEditable(false); + panelRD.add(new JScrollPane(textareaRuleDescription)); + + size = new Dimension(100, 100); + panelRD.setPreferredSize(size); + panelRule.add(panelRD, BorderLayout.NORTH); + + JPanel panelRuleExample = new JPanel(new BorderLayout()); + JLabel labelCodeExample = new JLabel("Code example:"); + panelRuleExample.add(labelCodeExample, BorderLayout.NORTH); + textareaCodeExample = new JTextArea(); + textareaCodeExample.setEditable(false); + panelRuleExample.add(new JScrollPane(textareaCodeExample)); + + panelRule.add(panelRuleExample); + panelRule.setBorder(border2); + size = new Dimension(300, 100); + panelRule.setPreferredSize(size); + + panelRules.add(new JScrollPane(tableRules)); + panelRules.add(panelRule, BorderLayout.EAST); + + JPanel panelButtons = new JPanel(new GridLayout(1, 2, 5, 5)); + buttonProcess = new JButton("Process"); + buttonProcess.addActionListener(this); + panelButtons.add(buttonProcess); + buttonProcess.setDefaultCapable(true); + + buttonCancel = new JButton("Cancel"); + buttonCancel.addActionListener(this); + panelButtons.add(buttonCancel); + + JPanel panelFooter = new JPanel(new BorderLayout()); + size = new Dimension(300, 20); + panelButtons.setPreferredSize(size); + panelButtons.setBorder(border2); + panelFooter.add(panelButtons, BorderLayout.EAST); + + String version = null; + try { + Properties props = new Properties(); + props.load(getClass().getResourceAsStream("plugin.properties")); + version = props.getProperty("PMDPlugin.version", ""); + } catch (Exception e) { + version = ""; + } + + if (version.length() > 0) { + version = " Ver. " + version; + } + + JLabel labelInfo = + new JLabel("PMD for Gel" + version + ". Author: Andrey Lumyanski"); + panelFooter.add(labelInfo); + + size = new Dimension(240, 100); + panelRS.setPreferredSize(size); + panelRS.setBorder(border1); + getContentPane().add(panelRS, BorderLayout.NORTH); + + panelRules.setBorder(border1); + getContentPane().add(panelRules); + + size = new Dimension(300, 30); + panelFooter.setPreferredSize(size); + panelFooter.setBorder(border1); + getContentPane().add(panelFooter, BorderLayout.SOUTH); + + setSize(640, 480); + + isCanceled = true; + } + + /** + * Processes action from child controls. + * Note! Do not invoke this method manually. + * + * @param evt ActionEvent event info + */ + public void actionPerformed(ActionEvent evt) { + if (evt.getSource() == buttonProcess) { + isCanceled = false; + } + dispose(); + } + + /** + * Processes selection event from child JTable controls. + * Note! Do not invoke this method manually. + * + * @param evt ListSelectionEvent event info + */ + public void valueChanged(ListSelectionEvent evt) { + if (evt.getSource() == tableRuleSets.getSelectionModel()) { + int selectedRow = tableRuleSets.getSelectedRow(); + if (selectedRow > -1) { + labelRules.setText(ruleSet[selectedRow].getName() + " rules:"); + textareaRSDescription.setText( + ruleSet[selectedRow].getDescription()); + dmRules.setData(ruleSet[selectedRow]); + } else { + labelRules.setText("Rules:"); + textareaRSDescription.setText(""); + dmRules.setData(null); + } + return; + } + if (evt.getSource() == tableRules.getSelectionModel()) { + int selectedRow = tableRules.getSelectedRow(); + if (selectedRow > -1) { + textareaRuleDescription.setText( + dmRules.getRule(selectedRow).getDescription()); + textareaCodeExample.setText( + dmRules.getRule(selectedRow).getExample()); + } else { + textareaRuleDescription.setText(""); + textareaCodeExample.setText(""); + } + return; + } + } + + /** + * Processes tableChanged event from child JTable control. + * Note! Do not invoke this method manually. + * + * @param evt TableModelEvent event info + */ + public void tableChanged(TableModelEvent evt) { + if (evt.getSource() == dmRuleSets) { + if (evt.getType() == TableModelEvent.UPDATE) { + if (ruleSet[evt.getFirstRow()].include()) { + tableRules.tableChanged( + new TableModelEvent( + dmRules, 0, dmRules.getRowCount()-1, 0)); + } + } + } + + } + + /** + * Returns true if dialog was canceled. + * Otherwise returns false. + * + * @return true if dialog was canceled + */ + public boolean isCanceled() { + return isCanceled; + } +} \ No newline at end of file diff --git a/pmd-gel/src/net/sourceforge/pmd/gel/data/RuleSetTableModel.java b/pmd-gel/src/net/sourceforge/pmd/gel/data/RuleSetTableModel.java new file mode 100644 index 0000000000..b59d9254d3 --- /dev/null +++ b/pmd-gel/src/net/sourceforge/pmd/gel/data/RuleSetTableModel.java @@ -0,0 +1,168 @@ +/* + * @(#)RuleSetTableModel.java $Revision$ ($Date$) + * Copyright (c) 2004 + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.gel.data; + +import java.util.Iterator; + +import javax.swing.table.AbstractTableModel; + +import net.sourceforge.pmd.Rule; +import net.sourceforge.pmd.RuleSet; + + +/** + * A RuleSet TableModel. + * + * @author Andrey Lumyanski + * @version $Revision$ ($Date$) + */ +public class RuleSetTableModel extends AbstractTableModel { + private static final String[] columnName = { + "Include", "Name" + }; + private static final Class[] columnClass = { + Boolean.class, String.class + }; + + private RuleSet ruleSet; + private Rule rule[]; + private int ruleCount; + + /** + * Creates a RuleSetTableModel object. + */ + public RuleSetTableModel() { + super(); + ruleSet = null; + rule = null; + ruleCount = 0; + } + + /** + * Returns the columns count. + * + * @return the columns count. + */ + public int getColumnCount() { + return columnName.length; + } + + /** + * Always returns String.class + * + * @param index int the column index + * + * @return String.class + */ + public Class getColumnClass(int index) { + return columnClass[index]; + } + + /** + * Returns the column name. + * + * @param index int the column index + * + * @return the column name + */ + public String getColumnName(int index) { + return columnName[index]; + } + + /** + * Returns the number of rows in the model. + * + * @return the number of rows in the model. + */ + public int getRowCount() { + return ruleCount; + } + + /** + * Returns true for column #0 and false for all + * other columns. + * + * @param rowIndex - the row whose value to be queried + * @param columnIndex - the column whose value to be queried + * + * @return true if the cell is editable + */ + public boolean isCellEditable(int rowIndex, int columnIndex) { + return columnIndex == 0; + } + + /** + * Returns the value for the cell at columnIndex and + * rowIndex. + * + * @param rowIndex the row whose value is to be queried + * @param columnIndex the column whose value is to be queried + * + * @return the value Object at the specified cell + */ + public Object getValueAt(int rowIndex, int columnIndex) { + Object value = null; + + switch (columnIndex) { + case 0: + value = Boolean.valueOf(rule[rowIndex].include()); + break; + case 1: + value = rule[rowIndex].getName(); + break; + } + + return value; + } + + /** + * Sets the value in the cell at columnIndex + * and rowIndex to value. + * + * @param value the new value + * @param rowIndex the row whose value is to be changed + * @param columnIndex the column whose value is to be changed + */ + public void setValueAt(Object value, int rowIndex, int columnIndex) { + if (columnIndex == 0) { + rule[rowIndex].setInclude(((Boolean)value).booleanValue()); + } + } + + /** + * Sets a data for table model. + * Fires tableDataChanged event. + * + * @param rs RuleSet a RuleSet instance + */ + public void setData(RuleSet rs) { + if (rs != ruleSet) { + ruleSet = rs; + ruleCount = (ruleSet == null) ? 0 : ruleSet.size(); + if (rule == null || ruleCount > rule.length) { + rule = new Rule[ruleCount]; + } + if (ruleSet != null) { + Iterator it = ruleSet.getRules().iterator(); + int i = 0; + while (it.hasNext()) { + rule[i++] = (Rule) it.next(); + } + } + fireTableDataChanged(); + } + } + + /** + * Returns a Rule by the index. + * + * @param index int a rule index + * @returna Rule by the index. + */ + public Rule getRule(int index) { + return rule[index]; + } +} \ No newline at end of file diff --git a/pmd-gel/src/net/sourceforge/pmd/gel/data/RuleSetsTableModel.java b/pmd-gel/src/net/sourceforge/pmd/gel/data/RuleSetsTableModel.java new file mode 100644 index 0000000000..35219bbae8 --- /dev/null +++ b/pmd-gel/src/net/sourceforge/pmd/gel/data/RuleSetsTableModel.java @@ -0,0 +1,143 @@ +/* + * @(#)RuleSetsTableModel.java $Revision$ ($Date$) + * Copyright (c) 2004 + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.gel.data; + +import java.util.Iterator; + +import javax.swing.table.AbstractTableModel; + +import net.sourceforge.pmd.Rule; +import net.sourceforge.pmd.RuleSet; + + +/** + * An array of RuleSet TableModel. + * + * @author Andrey Lumyanski + * @version $Revision$ ($Date$) + */ +public class RuleSetsTableModel extends AbstractTableModel { + private static final String[] columnName = { + "Include", "Name", "File" + }; + private static final Class[] columnClass = { + Boolean.class, String.class, String.class + }; + private RuleSet[] ruleSet; + + /** + * Creates a RuleSetsTableModel object. + * + * @param rs RuleSet[] a array of RuleSet + */ + public RuleSetsTableModel(RuleSet[] rs) { + super(); + ruleSet = rs; + } + + /** + * Returns the columns count. + * + * @return the columns count. + */ + public int getColumnCount() { + return columnName.length; + } + + /** + * Always returns String.class + * + * @param index int the column index + * + * @return String.class + */ + public Class getColumnClass(int index) { + return columnClass[index]; + } + + /** + * Returns the column name. + * + * @param index int the column index + * + * @return the column name + */ + public String getColumnName(int index) { + return columnName[index]; + } + + /** + * Returns the number of rows in the model. + * + * @return the number of rows in the model. + */ + public int getRowCount() { + return ruleSet.length; + } + + /** + * Returns true for column #0 and false for all + * other columns. + * + * @param rowIndex - the row whose value to be queried + * @param columnIndex - the column whose value to be queried + * + * @return true if the cell is editable + */ + public boolean isCellEditable(int rowIndex, int columnIndex) { + return columnIndex == 0; + } + + /** + * Returns the value for the cell at columnIndex and + * rowIndex. + * + * @param rowIndex the row whose value is to be queried + * @param columnIndex the column whose value is to be queried + * + * @return the value Object at the specified cell + */ + public Object getValueAt(int rowIndex, int columnIndex) { + Object value = null; + + switch (columnIndex) { + case 0: + value = Boolean.valueOf(ruleSet[rowIndex].include()); + break; + case 1: + value = ruleSet[rowIndex].getName(); + break; + case 2: + value = ruleSet[rowIndex].getFileName(); + break; + } + + return value; + } + + /** + * Sets the value in the cell at columnIndex + * and rowIndex to value. + * + * @param value the new value + * @param rowIndex the row whose value is to be changed + * @param columnIndex the column whose value is to be changed + */ + public void setValueAt(Object value, int rowIndex, int columnIndex) { + if (columnIndex == 0) { + ruleSet[rowIndex].setInclude(((Boolean)value).booleanValue()); + if (ruleSet[rowIndex].include()) { + Iterator it = ruleSet[rowIndex].getRules().iterator(); + Rule rule = null; + while (it.hasNext()) { + rule = (Rule) it.next(); + rule.setInclude(true); + } + } + fireTableCellUpdated(rowIndex, columnIndex); + } + } +} \ No newline at end of file diff --git a/pmd-gel/src/net/sourceforge/pmd/gel/plugin.properties b/pmd-gel/src/net/sourceforge/pmd/gel/plugin.properties new file mode 100644 index 0000000000..f30c638280 --- /dev/null +++ b/pmd-gel/src/net/sourceforge/pmd/gel/plugin.properties @@ -0,0 +1,2 @@ +#Mon May 24 19:52:41 EEST 2004 +PMDPlugin.version=1.0