summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/gui
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gui')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/ExtensionFileFilter.java4
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java72
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java43
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java32
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java8
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/SimpleTitledBorder.java1
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java4
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java12
8 files changed, 156 insertions, 20 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/ExtensionFileFilter.java b/BJC-Utils2/src/main/java/bjc/utils/gui/ExtensionFileFilter.java
index be1746f..e563530 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gui/ExtensionFileFilter.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/ExtensionFileFilter.java
@@ -49,6 +49,10 @@ public class ExtensionFileFilter extends FileFilter {
@Override
public boolean accept(File pathname) {
+ if (pathname == null) {
+ throw new NullPointerException("Pathname must not be null");
+ }
+
return extensions.anyMatch(pathname.getName()::endsWith);
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java b/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java
index 42c5761..4bbe6e7 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java
@@ -68,24 +68,60 @@ public class ListParameterPanel<E> extends JPanel {
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JPanel buttonPanel = new JPanel();
- buttonPanel.setLayout(new HLayout(3));
-
- JButton addParam = new JButton("Add...");
- JButton editParam = new JButton("Edit...");
- JButton removeParam = new JButton("Remove...");
-
- addParam.addActionListener(
- (event) -> ((DefaultListModel<E>) list.getModel())
- .addElement(addAction.get()));
- editParam.addActionListener(
- (event) -> editAction.accept(list.getSelectedValue()));
- removeParam.addActionListener((event) -> removeAction
- .accept(((DefaultListModel<E>) list.getModel())
- .remove(list.getSelectedIndex())));
-
- buttonPanel.add(addParam);
- buttonPanel.add(editParam);
- buttonPanel.add(removeParam);
+
+ int numButtons = 0;
+
+ if (addAction != null) {
+ numButtons++;
+ }
+
+ if (editAction != null) {
+ numButtons++;
+ }
+
+ if (removeAction != null) {
+ numButtons++;
+ }
+
+ buttonPanel.setLayout(new HLayout(numButtons));
+
+ JButton addParam = null;
+
+ if (addAction != null) {
+ addParam = new JButton("Add...");
+ addParam.addActionListener(
+ (event) -> ((DefaultListModel<E>) list.getModel())
+ .addElement(addAction.get()));
+ }
+
+ JButton editParam = null;
+
+ if (editAction != null) {
+ editParam = new JButton("Edit...");
+ editParam.addActionListener(
+ (event) -> editAction.accept(list.getSelectedValue()));
+ }
+
+ JButton removeParam = null;
+
+ if (removeAction != null) {
+ removeParam = new JButton("Remove...");
+ removeParam.addActionListener((event) -> removeAction
+ .accept(((DefaultListModel<E>) list.getModel())
+ .remove(list.getSelectedIndex())));
+ }
+
+ if (addAction != null) {
+ buttonPanel.add(addParam);
+ }
+
+ if (editAction != null) {
+ buttonPanel.add(editParam);
+ }
+
+ if (removeAction != null) {
+ buttonPanel.add(removeParam);
+ }
add(list);
add(buttonPanel);
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java
index fd7d05a..b09fbd8 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java
@@ -38,9 +38,10 @@ public class SimpleDialogs {
*/
public static int getBoundedInt(Component parent, String title,
String prompt, int lowerBound, int upperBound) {
- return getValue(parent, title, prompt, strang -> {
+ return getValue(parent, title, prompt, (strang) -> {
try {
int value = Integer.parseInt(strang);
+
return (value < upperBound) && (value > lowerBound);
} catch (NumberFormatException nfe) {
return false;
@@ -67,6 +68,15 @@ public class SimpleDialogs {
@SuppressWarnings("unchecked")
public static <E> E getChoice(Frame parent, String title,
String question, E... choices) {
+
+ if (parent == null) {
+ throw new NullPointerException("Parent must not be null");
+ } else if (title == null) {
+ throw new NullPointerException("Title must not be null");
+ } else if (question == null) {
+ throw new NullPointerException("Question must not be null");
+ }
+
JDialog mainDialog = new JDialog(parent, title, true);
mainDialog.setLayout(new VLayout(2));
@@ -134,6 +144,14 @@ public class SimpleDialogs {
*/
public static String getString(Component parent, String title,
String prompt) {
+ if (parent == null) {
+ throw new NullPointerException("Parent must not be null");
+ } else if (title == null) {
+ throw new NullPointerException("Title must not be null");
+ } else if (prompt == null) {
+ throw new NullPointerException("Prompt must not be null");
+ }
+
return JOptionPane.showInputDialog(parent, prompt, title,
JOptionPane.QUESTION_MESSAGE);
}
@@ -159,6 +177,12 @@ public class SimpleDialogs {
public static <E> E getValue(Component parent, String title,
String prompt, Predicate<String> inputValidator,
Function<String, E> inputTransformer) {
+ if (inputValidator == null) {
+ throw new NullPointerException("Validator must not be null");
+ } else if (inputTransformer == null) {
+ throw new NullPointerException("Transformer must not be null");
+ }
+
String inputString = getString(parent, title, prompt);
while (!inputValidator.test(inputString)) {
@@ -199,6 +223,14 @@ public class SimpleDialogs {
*/
public static boolean getYesNo(Component parent, String title,
String question) {
+ if (parent == null) {
+ throw new NullPointerException("Parent must not be null");
+ } else if (title == null) {
+ throw new NullPointerException("Title must not be null");
+ } else if (question == null) {
+ throw new NullPointerException("Question must not be null");
+ }
+
int dialogResult = JOptionPane.showConfirmDialog(parent, question,
title, JOptionPane.YES_NO_OPTION);
@@ -217,6 +249,15 @@ public class SimpleDialogs {
*/
public static void showError(Component parent, String title,
String errorMessage) {
+ if (parent == null) {
+ throw new NullPointerException("Parent must not be null");
+ } else if (title == null) {
+ throw new NullPointerException("Title must not be null");
+ } else if (errorMessage == null) {
+ throw new NullPointerException(
+ "Error message must not be null");
+ }
+
JOptionPane.showMessageDialog(parent, errorMessage, title,
JOptionPane.ERROR_MESSAGE);
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java
index 9648762..f39ff7c 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleFileChooser.java
@@ -18,6 +18,10 @@ import bjc.utils.exceptions.FileNotChosenException;
public class SimpleFileChooser {
private static File doOpenFile(Component parent, String title,
JFileChooser files) {
+ if (title == null) {
+ throw new NullPointerException("Title must not be null");
+ }
+
files.setDialogTitle(title);
boolean success = false;
@@ -38,6 +42,10 @@ public class SimpleFileChooser {
private static File doSaveFile(Component parent, String title,
JFileChooser files) {
+ if (title == null) {
+ throw new NullPointerException("Title must not be null");
+ }
+
files.setDialogTitle(title);
boolean success = false;
@@ -87,6 +95,7 @@ public class SimpleFileChooser {
public static File getOpenFile(Component parent, String title,
String... extensions) {
JFileChooser files = new JFileChooser();
+
files.addChoosableFileFilter(new ExtensionFileFilter(extensions));
return doOpenFile(parent, title, files);
@@ -121,6 +130,7 @@ public class SimpleFileChooser {
public static File getSaveFile(Component parent, String title,
String... extensions) {
JFileChooser files = new JFileChooser();
+
files.addChoosableFileFilter(new ExtensionFileFilter(extensions));
return doSaveFile(parent, title, files);
@@ -128,6 +138,13 @@ public class SimpleFileChooser {
private static void maybeDoOpenFile(Component parent,
JFileChooser files) throws FileNotChosenException {
+ if (parent == null) {
+ throw new NullPointerException("Parent must not be null");
+ } else if (files == null) {
+ throw new NullPointerException(
+ "File chooser must not be null");
+ }
+
int dialogResult = files.showSaveDialog(parent);
if (dialogResult != JFileChooser.APPROVE_OPTION) {
@@ -137,6 +154,13 @@ public class SimpleFileChooser {
private static void maybeDoSaveFile(Component parent,
JFileChooser files) throws FileNotChosenException {
+ if (parent == null) {
+ throw new NullPointerException("Parent must not be null");
+ } else if (files == null) {
+ throw new NullPointerException(
+ "File chooser must not be null");
+ }
+
int dialogResult = files.showSaveDialog(parent);
if (dialogResult != JFileChooser.APPROVE_OPTION) {
@@ -154,6 +178,10 @@ public class SimpleFileChooser {
* @return The file if the user chose one or null if they didn't.
*/
public static File maybeOpenFile(Component parent, String title) {
+ if (title == null) {
+ throw new NullPointerException("Title must not be null");
+ }
+
JFileChooser files = new JFileChooser();
files.setDialogTitle(title);
@@ -175,6 +203,10 @@ public class SimpleFileChooser {
* @return The file if the user chose one or null if they didn't.
*/
public static File maybeSaveFile(Component parent, String title) {
+ if (title == null) {
+ throw new NullPointerException("Title must not be null");
+ }
+
JFileChooser files = new JFileChooser();
files.setDialogTitle(title);
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java
index 4695318..4db5027 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java
@@ -22,6 +22,10 @@ public class SimpleJList {
* @return A JList populated with the elements from ls.
*/
public static <E> JList<E> buildFromList(Iterable<E> listSource) {
+ if (listSource == null) {
+ throw new NullPointerException("Source must not be null");
+ }
+
return new JList<>(buildModel(listSource));
}
@@ -36,6 +40,10 @@ public class SimpleJList {
* @return A list model populated with the elements from ls.
*/
public static <E> ListModel<E> buildModel(Iterable<E> listSource) {
+ if (listSource == null) {
+ throw new NullPointerException("Source must not be null");
+ }
+
DefaultListModel<E> defaultModel = new DefaultListModel<>();
listSource.forEach(defaultModel::addElement);
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleTitledBorder.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleTitledBorder.java
index ddf5492..e2fd390 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleTitledBorder.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleTitledBorder.java
@@ -22,5 +22,4 @@ public class SimpleTitledBorder extends TitledBorder {
public SimpleTitledBorder(String title) {
super(new EtchedBorder(), title);
}
-
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java
index 368f8be..7e2c2d2 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/ExtensionFileFilter.java
@@ -29,6 +29,10 @@ public class ExtensionFileFilter implements FilenameFilter {
* The extensions to show in this filter.
*/
public ExtensionFileFilter(List<String> exts) {
+ if (exts == null) {
+ throw new NullPointerException("Extensions must not be null");
+ }
+
extensions = new FunctionalList<>(exts);
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java
index a8df3b9..7617199 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java
@@ -42,6 +42,12 @@ public class SimpleFileDialog {
*/
public static File getOpenFile(Frame parent, String title,
String... extensions) {
+ if (parent == null) {
+ throw new NullPointerException("Parent must not be null");
+ } else if (title == null) {
+ throw new NullPointerException("Title must not be null");
+ }
+
FileDialog fileDialog =
new FileDialog(parent, title, FileDialog.LOAD);
@@ -87,6 +93,12 @@ public class SimpleFileDialog {
*/
public static File getSaveFile(Frame parent, String title,
String... extensions) {
+ if (parent == null) {
+ throw new NullPointerException("Parent must not be null");
+ } else if (title == null) {
+ throw new NullPointerException("Title must not be null");
+ }
+
FileDialog fileDialog =
new FileDialog(parent, title, FileDialog.SAVE);