added file chooser

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@2884 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Brant Gurganus
2004-08-12 21:02:06 +00:00
parent aa91a7b0f2
commit d78261a706
10 changed files with 417 additions and 16 deletions

View File

@@ -70,8 +70,14 @@
<artifactId>pmd</artifactId>
<version>1.9</version>
<url>http://pmd.sourceforge.net/</url>
</dependency>
</dependency>-->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.0</version>
<url>http://jakarta.apache.org/commons/cli/</url>
</dependency>
<!--<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1-beta-4</version>

View File

@@ -0,0 +1,269 @@
package net.sourceforge.pmd.swingui;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
/**
* This is a file locator. On the graphics end, it provides a listing of files
* as well as list modification controls. On the programming end, it provides
* a means for retrieving the list of selected files.
*
* @author Brant Gurganus
* @version 0.1
* @since 0.1
*/
public class FileLocator extends TitledPanel {
/**
* This is the resource bundle for this UI component.
*/
private static final ResourceBundle UI_STRINGS = ResourceBundle
.getBundle("net.sourceforge.pmd.swingui.l10n.FileLocator");
private static final Logger LOGGER = Logger.getLogger(FileLocator.class
.getName(),
"net.sourceforge.pmd.swingui.l10n.Logging");
/**
* This is the location list.
*/
private JList locationList;
/**
* This is the location list scroller.
*/
private JScrollPane locationScroller;
/**
* These are the chosen locations.
*/
private Vector locations;
/**
* This is the reset button.
*/
private JButton resetButton;
/**
* This is the reset action.
*/
private AbstractAction resetAction;
/**
* This is the add button.
*/
private JButton addButton;
/**
* This is the add action.
*/
private AbstractAction addAction;
/**
* This is the control panel.
*/
private JPanel controlPanel;
/**
* Creates a file locator with default title.
*/
public FileLocator() {
this(UI_STRINGS.getString("title"));
}
/**
* Creates a file locator titled with <code>title</code>.
*
* @param title title for file locator
*/
public FileLocator(final String title) {
super(title);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
locationScrollerInit();
add(locationScroller);
controlPanelInit();
add(controlPanel);
}
/**
* Initializes the location list scroller.
*/
private void locationScrollerInit() {
LOGGER.entering(getClass().getName(), "locationScrollerInit");
assert locationScroller == null :
"locationScroller already initialized.";
locationListInit();
locationScroller = new JScrollPane(locationList);
LOGGER.exiting(getClass().getName(), "locationScrollerInit");
}
/**
* Initializes the location list.
*
* @todo determine if an initial capacity or increment should be used
*/
private void locationListInit() {
LOGGER.entering(getClass().getName(), "locationListInit");
assert locationList == null : "locationList already initalized.";
locations = new Vector();
locationList = new JList(locations);
LOGGER.exiting(getClass().getName(), "locationListInit");
}
/**
* Initializes the control panel.
*/
private void controlPanelInit() {
LOGGER.entering(getClass().getName(), "controlPanelInit");
assert controlPanel == null : "controlPanel already initialized.";
controlPanel = new JPanel();
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS));
resetButtonInit();
controlPanel.add(resetButton);
addButtonInit();
controlPanel.add(addButton);
LOGGER.exiting(getClass().getName(), "controlPanelInit");
}
/**
* Initializes the add button.
*/
private void addButtonInit() {
LOGGER.entering(getClass().getName(), "addButtonInit");
assert addButton == null : "addButton already initialized.";
addActionInit();
addButton = new JButton(addAction);
LOGGER.exiting(getClass().getName(), "addButtonInit");
}
/**
* Initializes the add action.
*/
private void addActionInit() {
LOGGER.entering(getClass().getName(), "addActionInit");
assert addAction == null : "addAction already initialized.";
addAction = new AbstractAction(UI_STRINGS.getString("add")) {
public void actionPerformed(ActionEvent e) {
LOGGER.entering(getClass().getName(), "actionPerformed", e);
assert e.getActionCommand().equals(getValue(ACTION_COMMAND_KEY))
: "event source unexpected";
promptForFiles();
LOGGER.exiting(getClass().getName(), "actionPerformed");
}
};
addAction.putValue(addAction.ACTION_COMMAND_KEY, "add");
addAction.putValue(addAction.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(UI_STRINGS
.getString("add.accelerator")));
addAction.putValue(addAction.MNEMONIC_KEY, Integer
.valueOf(PMDViewer.translateKey(UI_STRINGS
.getString("add.mnemonic"))));
addAction.putValue(addAction.SHORT_DESCRIPTION, UI_STRINGS
.getString("add.tooltip"));
addAction.putValue(addAction.LONG_DESCRIPTION, UI_STRINGS
.getString("add.description"));
LOGGER.exiting(getClass().getName(), "addActionInit");
}
/**
* Prompts for files.
*/
private void promptForFiles() {
LOGGER.entering(getClass().getName(), "promptForFiles");
assert locations != null : "locations not initialized.";
assert locationList != null : "locationList not initialized.";
assert resetAction != null : "resetAction not initialized.";
final JFileChooser fileChooser = new JFileChooser();
final JavaFileFilter filter = new JavaFileFilter();
fileChooser.setFileFilter(filter);
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setAcceptAllFileFilterUsed(false);
final int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
final File selectedFile = fileChooser.getSelectedFile();
locations.add(selectedFile);
locationList.setListData(locations);
resetAction.setEnabled(true);
}
LOGGER.exiting(getClass().getName(), "promptForFiles");
}
/**
* Initializes the reset button.
*/
private void resetButtonInit() {
LOGGER.entering(getClass().getName(), "resetButtonInit");
assert resetButton == null : "resetButton already initialized.";
resetActionInit();
resetButton = new JButton(resetAction);
LOGGER.exiting(getClass().getName(), "resetButtonInit");
}
/**
* Initializes the reset action.
*/
private void resetActionInit() {
LOGGER.entering(getClass().getName(), "resetActionInit");
assert resetAction == null : "resetAction already initialized.";
resetAction = new AbstractAction(UI_STRINGS.getString("reset")) {
public void actionPerformed(ActionEvent e) {
LOGGER.entering(getClass().getName(), "actionPerformed", e);
assert e.getActionCommand().equals(getValue(ACTION_COMMAND_KEY))
: "event source unexpected";
resetList();
LOGGER.exiting(getClass().getName(), "actionPerformed");
}
};
resetAction.putValue(resetAction.ACTION_COMMAND_KEY, "reset");
resetAction.putValue(resetAction.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(UI_STRINGS
.getString("reset.accelerator")));
resetAction.putValue(resetAction.MNEMONIC_KEY, Integer
.valueOf(PMDViewer.translateKey(UI_STRINGS
.getString("reset.mnemonic"))));
resetAction.putValue(resetAction.SHORT_DESCRIPTION, UI_STRINGS
.getString("reset.tooltip"));
resetAction.putValue(resetAction.LONG_DESCRIPTION, UI_STRINGS
.getString("reset.description"));
resetAction.setEnabled(false);
LOGGER.exiting(getClass().getName(), "resetActionInit");
}
/**
* Gets the list of files.
*
* @return copy of list of files
*/
public Vector getList() {
LOGGER.entering(getClass().getName(), "getList");
final Vector returnedList = (Vector) locations.clone();
LOGGER.exiting(getClass().getName(), "getList", returnedList);
return returnedList;
}
/**
* Resets the list.
*/
private void resetList() {
LOGGER.entering(getClass().getName(), "resetList");
assert locations != null : "locations is not initialized.";
assert locationList != null : "locationList is not initialized.";
assert resetAction != null : "resetAction is not initialized.";
locations.removeAllElements();
locationList.setListData(locations);
resetAction.setEnabled(false);
LOGGER.exiting(getClass().getName(), "resetList");
}
}

View File

@@ -23,7 +23,7 @@ public class JARFileFilter extends FileFilter {
* This is the logger for the filter.
*/
private static final Logger LOGGER = Logger.getLogger(
"net.sourceforge.pmd.swingui.JARFileFilter",
JARFileFilter.class.getName(),
"net.sourceforge.pmd.swingui.l10n.Logging");
/**
@@ -41,8 +41,10 @@ public class JARFileFilter extends FileFilter {
*/
public boolean accept(File f) {
LOGGER.entering(getClass().getName(), "accept", f);
final boolean acceptable =
f.getName().endsWith(".jar") || f.isDirectory();
final boolean acceptableFile = f.getName().endsWith(".jar") &&
!f.isDirectory();
final boolean acceptableDir = f.isDirectory();
final boolean acceptable = acceptableFile || acceptableDir;
LOGGER.exiting(getClass().getName(), "accept",
Boolean.valueOf(acceptable));
return acceptable;

View File

@@ -0,0 +1,67 @@
package net.sourceforge.pmd.swingui;
import java.io.File;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import javax.swing.filechooser.FileFilter;
/**
* This is a Java file filter.
*
* @author Brant Gurganus
* @since 0.1
* @version 0.1
*/
public class JavaFileFilter extends FileFilter implements java.io.FileFilter {
/**
* This is the resource bundle for the filter.
*/
private static final ResourceBundle UI_STRINGS = ResourceBundle
.getBundle("net.sourceforge.pmd.swingui.l10n.JavaFileFilter");
/**
* This is the logger for the filter.
*/
private static final Logger LOGGER = Logger.getLogger(
JavaFileFilter.class.getName(),
"net.sourceforge.pmd.swingui.l10n.Logging");
/**
* Creates the filter.
*/
public JavaFileFilter() {
super();
}
/**
* Indicates whether the file is accepted by the filter.
*
* @param f file
* @return acceptability
*/
public boolean accept(File f) {
LOGGER.entering(getClass().getName(), "accept", f);
final boolean acceptableFile =
!f.isDirectory() && f.getName().endsWith(".java");
System.out.println(f.isDirectory());
System.out.println(f);
final boolean acceptableDir =
f.isDirectory() && f.listFiles(this).length > 0;
final boolean acceptable = acceptableFile || acceptableDir;
LOGGER.exiting(getClass().getName(), "accept",
Boolean.valueOf(acceptable));
return acceptable;
}
/**
* Gets the description of the filter.
*
* @return description of filter
*/
public String getDescription() {
LOGGER.entering(getClass().getName(), "getDescription");
final String description = UI_STRINGS.getString("description");
LOGGER.exiting(getClass().getName(), "getDescription", description);
return description;
}
}

View File

@@ -24,25 +24,25 @@ import javax.swing.border.TitledBorder;
* @version 0.1
* @since 0.1
*/
class PMDLocator extends JPanel {
class PMDLocator extends TitledPanel {
/**
* This is the resource bundle for this UI component.
*/
private ResourceBundle UI_STRINGS =
private static final ResourceBundle UI_STRINGS =
ResourceBundle.getBundle("net.sourceforge.pmd.swingui.l10n.PMDLocator");
/**
* These are the preferences for this component.
*/
private Preferences PREFS =
private static final Preferences PREFS =
Preferences.userNodeForPackage(PMDLocator.class);
/**
* This is the logger for the PMD Locator.
*/
private Logger LOGGER =
Logger.getLogger("net.sourceforge.pmd.swingui.PMDLocator",
"net.sourceforge.pmd.swingui.l10n.Logging");
private static final Logger LOGGER = Logger.getLogger(PMDLocator.class
.getName(),
"net.sourceforge.pmd.swingui.l10n.Logging");
/**
* This is the label.
@@ -68,9 +68,8 @@ class PMDLocator extends JPanel {
* This creates the PMD Locator.
*/
public PMDLocator() {
super();
super(UI_STRINGS.getString("title"));
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setBorder(new TitledBorder(UI_STRINGS.getString("title")));
locationFieldInit();
locationLabelInit();
locationButtonInit();

View File

@@ -33,9 +33,9 @@ class PMDViewer extends JFrame {
/**
* This is the logger.
*/
private static final Logger LOGGER = Logger.getLogger(
"net.sourceforge.pmd.swingui.PMDViewer",
"net.sourceforge.pmd.swingui.l10n.Logging");
private static final Logger LOGGER = Logger.getLogger(PMDViewer.class
.getName(),
"net.sourceforge.pmd.swingui.l10n.Logging");
/**
* This is the application menu bar.
@@ -67,6 +67,11 @@ class PMDViewer extends JFrame {
*/
private PMDLocator pmdLocator;
/**
* This is the file locator.
*/
private FileLocator fileLocator;
/**
* Creates the PMD Viewer.
*/
@@ -101,9 +106,22 @@ class PMDViewer extends JFrame {
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
pmdLocatorInit();
contentPane.add(pmdLocator);
fileLocatorInit();
contentPane.add(fileLocator);
pack();
LOGGER.exiting(getClass().getName(), "contentPaneInit");
}
/**
* Initializes the file locator.
*/
private void fileLocatorInit() {
LOGGER.entering(getClass().getName(), "fileLocatorInit");
assert fileLocator == null : "fileLocator already initialized.";
fileLocator = new FileLocator();
LOGGER.exiting(getClass().getName(), "fileLocatorInit");
}
/**
* Initializes the PMD Locator.
*/

View File

@@ -0,0 +1,23 @@
package net.sourceforge.pmd.swingui;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
/**
* This is a titled panel.
*
* @author Brant Gurganus
* @version 0.1
* @since 0.1
*/
public class TitledPanel extends JPanel {
/**
* Creates the titled panel.
*
* @param title title of panel
*/
public TitledPanel(String title) {
super();
setBorder(new TitledBorder(title));
}
}

View File

@@ -0,0 +1,12 @@
# These are localized strings for the File Locator.
add = Add
add.accelerator = ctrl typed A
add.mnemonic = A
add.tooltip = Add files to the list.
add.description = Prompts for a file or directory to add to the list.
reset = Reset
reset.accelerator = ctrl typed R
reset.mnemonic = R
reset.tooltip = Resets the list.
reset.description = Clears the location list of all entries.
title = Locate Files

View File

@@ -0,0 +1,2 @@
# These are localized strings for the JARFileFilter.
description = Java Source Code

View File

@@ -9,6 +9,9 @@
<action dev="brantgurga" type="add">
Initial working PMD Viewer, not complete.
</action>
<action dev="brantgurga" type="add">
Added a file chooser.
</action>
</release>
</body>
</document>
</document>