From 1c8bc7132d980c1ff2dbd6b9af579c3b2fd8c63e Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 3 Apr 2016 19:22:48 -0400 Subject: General code refactoring and maintenance --- .../java/bjc/utils/gui/ExtensionFileFilter.java | 4 ++ .../java/bjc/utils/gui/ListParameterPanel.java | 72 ++++++++++++++++------ .../src/main/java/bjc/utils/gui/SimpleDialogs.java | 43 ++++++++++++- .../main/java/bjc/utils/gui/SimpleFileChooser.java | 32 ++++++++++ .../src/main/java/bjc/utils/gui/SimpleJList.java | 8 +++ .../java/bjc/utils/gui/SimpleTitledBorder.java | 1 - .../bjc/utils/gui/awt/ExtensionFileFilter.java | 4 ++ .../java/bjc/utils/gui/awt/SimpleFileDialog.java | 12 ++++ 8 files changed, 156 insertions(+), 20 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/gui') 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 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) list.getModel()) - .addElement(addAction.get())); - editParam.addActionListener( - (event) -> editAction.accept(list.getSelectedValue())); - removeParam.addActionListener((event) -> removeAction - .accept(((DefaultListModel) 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) 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) 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 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 getValue(Component parent, String title, String prompt, Predicate inputValidator, Function 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 JList buildFromList(Iterable 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 ListModel buildModel(Iterable listSource) { + if (listSource == null) { + throw new NullPointerException("Source must not be null"); + } + DefaultListModel 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 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); -- cgit v1.2.3