From 2b186b43405f0214b47ee3fbacc50e97ff864d16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20L=C3=B3pez=20Fern=C3=A1ndez?= Date: Thu, 16 Aug 2018 10:25:36 +0200 Subject: [PATCH] Allow disabling clear selected file action in JFileChooserPanel --- README.md | 4 +- gc4s-demo/pom.xml | 2 +- gc4s-genomebrowser-demo/pom.xml | 2 +- gc4s-genomebrowser/README.md | 2 +- gc4s-genomebrowser/pom.xml | 2 +- gc4s-heatmap-demo/pom.xml | 2 +- gc4s-heatmap/README.md | 2 +- gc4s-heatmap/pom.xml | 2 +- gc4s-jsparklines-factory-demo/pom.xml | 2 +- gc4s-jsparklines-factory/README.md | 2 +- gc4s-jsparklines-factory/pom.xml | 2 +- .../pom.xml | 2 +- .../README.md | 2 +- .../pom.xml | 2 +- gc4s-statistics-tests-table-demo/pom.xml | 2 +- gc4s-statistics-tests-table/README.md | 2 +- gc4s-statistics-tests-table/pom.xml | 2 +- gc4s/README.md | 2 +- gc4s/pom.xml | 2 +- .../input/filechooser/JFileChooserPanel.java | 35 +++++++++---- .../ui/menu/JPopupMenuActionsVisibility.java | 52 +++++++++++++++++++ pom.xml | 2 +- 22 files changed, 99 insertions(+), 30 deletions(-) create mode 100644 gc4s/src/main/java/org/sing_group/gc4s/ui/menu/JPopupMenuActionsVisibility.java diff --git a/README.md b/README.md index f3ec7f7..4f98019 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -GC4S [![Build Status](https://travis-ci.org/sing-group/GC4S.svg?branch=master)](https://travis-ci.org/hlfernandez/GC4S) [![license](https://img.shields.io/badge/LICENSE-LGPLv3-blue.svg)]() +GC4S [![Build Status](https://travis-ci.org/sing-group/GC4S.svg?branch=master)](https://travis-ci.org/hlfernandez/GC4S) [![license](https://img.shields.io/badge/LICENSE-LGPLv3-blue.svg)]() [![GitHub release](https://img.shields.io/github/release/sing-group/GC4S.svg)](https://github.com/sing-group/GC4S/releases) ======================== GC4S is an open-source library that provides a bioinformatics-oriented collection of GUI Components for (Java) Swing. @@ -38,7 +38,7 @@ Add the following repository and dependency declarations to your `pom.xml` in or org.sing_group gc4s - 1.2.1 + 1.2.2 ``` diff --git a/gc4s-demo/pom.xml b/gc4s-demo/pom.xml index 55a2c39..c3c6ae2 100644 --- a/gc4s-demo/pom.xml +++ b/gc4s-demo/pom.xml @@ -6,7 +6,7 @@ org.sing_group gc4s-all - 1.2.1 + 1.2.2 ../pom.xml diff --git a/gc4s-genomebrowser-demo/pom.xml b/gc4s-genomebrowser-demo/pom.xml index 47d2639..55ee705 100644 --- a/gc4s-genomebrowser-demo/pom.xml +++ b/gc4s-genomebrowser-demo/pom.xml @@ -4,7 +4,7 @@ org.sing_group gc4s-all - 1.2.1 + 1.2.2 ../pom.xml diff --git a/gc4s-genomebrowser/README.md b/gc4s-genomebrowser/README.md index dc5328d..725ce9e 100644 --- a/gc4s-genomebrowser/README.md +++ b/gc4s-genomebrowser/README.md @@ -23,7 +23,7 @@ Add the following repository and dependency declarations to your `pom.xml`: org.sing_group gc4s-genomebrowser - 1.2.1 + 1.2.2 ``` diff --git a/gc4s-genomebrowser/pom.xml b/gc4s-genomebrowser/pom.xml index b33c690..12698dc 100644 --- a/gc4s-genomebrowser/pom.xml +++ b/gc4s-genomebrowser/pom.xml @@ -6,7 +6,7 @@ org.sing_group gc4s-all - 1.2.1 + 1.2.2 ../pom.xml diff --git a/gc4s-heatmap-demo/pom.xml b/gc4s-heatmap-demo/pom.xml index 5230f64..76e1f68 100644 --- a/gc4s-heatmap-demo/pom.xml +++ b/gc4s-heatmap-demo/pom.xml @@ -4,7 +4,7 @@ org.sing_group gc4s-all - 1.2.1 + 1.2.2 ../pom.xml diff --git a/gc4s-heatmap/README.md b/gc4s-heatmap/README.md index 86d8445..1e2f2f1 100644 --- a/gc4s-heatmap/README.md +++ b/gc4s-heatmap/README.md @@ -23,7 +23,7 @@ Add the following repository and dependency declarations to your `pom.xml`: org.sing_group gc4s-heatmap - 1.2.1 + 1.2.2 ``` diff --git a/gc4s-heatmap/pom.xml b/gc4s-heatmap/pom.xml index d1e0a7b..d63edbe 100644 --- a/gc4s-heatmap/pom.xml +++ b/gc4s-heatmap/pom.xml @@ -6,7 +6,7 @@ org.sing_group gc4s-all - 1.2.1 + 1.2.2 ../pom.xml diff --git a/gc4s-jsparklines-factory-demo/pom.xml b/gc4s-jsparklines-factory-demo/pom.xml index ce0c761..3ccd71d 100644 --- a/gc4s-jsparklines-factory-demo/pom.xml +++ b/gc4s-jsparklines-factory-demo/pom.xml @@ -4,7 +4,7 @@ org.sing_group gc4s-all - 1.2.1 + 1.2.2 ../pom.xml diff --git a/gc4s-jsparklines-factory/README.md b/gc4s-jsparklines-factory/README.md index b8926ec..993ddfb 100644 --- a/gc4s-jsparklines-factory/README.md +++ b/gc4s-jsparklines-factory/README.md @@ -23,7 +23,7 @@ Add the following repository and dependency declarations to your `pom.xml`: org.sing_group gc4s-jsparklines-factory - 1.2.1 + 1.2.2 ``` diff --git a/gc4s-jsparklines-factory/pom.xml b/gc4s-jsparklines-factory/pom.xml index 4e09d15..3a537a3 100644 --- a/gc4s-jsparklines-factory/pom.xml +++ b/gc4s-jsparklines-factory/pom.xml @@ -6,7 +6,7 @@ org.sing_group gc4s-all - 1.2.1 + 1.2.2 ../pom.xml diff --git a/gc4s-multiple-sequence-alignment-viewer-demo/pom.xml b/gc4s-multiple-sequence-alignment-viewer-demo/pom.xml index a88ec0d..a8bcd9d 100644 --- a/gc4s-multiple-sequence-alignment-viewer-demo/pom.xml +++ b/gc4s-multiple-sequence-alignment-viewer-demo/pom.xml @@ -6,7 +6,7 @@ org.sing_group gc4s-all - 1.2.1 + 1.2.2 ../pom.xml diff --git a/gc4s-multiple-sequence-alignment-viewer/README.md b/gc4s-multiple-sequence-alignment-viewer/README.md index 8930275..6d4d3ba 100644 --- a/gc4s-multiple-sequence-alignment-viewer/README.md +++ b/gc4s-multiple-sequence-alignment-viewer/README.md @@ -23,7 +23,7 @@ Add the following repository and dependency declarations to your `pom.xml`: org.sing_group gc4s-multiple-sequence-alignment-viewer - 1.2.1 + 1.2.2 ``` diff --git a/gc4s-multiple-sequence-alignment-viewer/pom.xml b/gc4s-multiple-sequence-alignment-viewer/pom.xml index c7d125a..c476388 100644 --- a/gc4s-multiple-sequence-alignment-viewer/pom.xml +++ b/gc4s-multiple-sequence-alignment-viewer/pom.xml @@ -6,7 +6,7 @@ org.sing_group gc4s-all - 1.2.1 + 1.2.2 ../pom.xml diff --git a/gc4s-statistics-tests-table-demo/pom.xml b/gc4s-statistics-tests-table-demo/pom.xml index 33cadf4..caa31d4 100644 --- a/gc4s-statistics-tests-table-demo/pom.xml +++ b/gc4s-statistics-tests-table-demo/pom.xml @@ -4,7 +4,7 @@ org.sing_group gc4s-all - 1.2.1 + 1.2.2 ../pom.xml diff --git a/gc4s-statistics-tests-table/README.md b/gc4s-statistics-tests-table/README.md index b064236..3a20834 100644 --- a/gc4s-statistics-tests-table/README.md +++ b/gc4s-statistics-tests-table/README.md @@ -23,7 +23,7 @@ Add the following repository and dependency declarations to your `pom.xml`: org.sing_group gc4s-statistics-tests-table - 1.2.1 + 1.2.2 ``` diff --git a/gc4s-statistics-tests-table/pom.xml b/gc4s-statistics-tests-table/pom.xml index 339d9ee..9e4443e 100644 --- a/gc4s-statistics-tests-table/pom.xml +++ b/gc4s-statistics-tests-table/pom.xml @@ -6,7 +6,7 @@ org.sing_group gc4s-all - 1.2.1 + 1.2.2 ../pom.xml diff --git a/gc4s/README.md b/gc4s/README.md index 573810b..5a7cab6 100644 --- a/gc4s/README.md +++ b/gc4s/README.md @@ -19,7 +19,7 @@ Add the following repository and dependency declarations to your `pom.xml`: org.sing_group gc4s - 1.2.1 + 1.2.2 ``` diff --git a/gc4s/pom.xml b/gc4s/pom.xml index 06bfe0e..33367f6 100644 --- a/gc4s/pom.xml +++ b/gc4s/pom.xml @@ -6,7 +6,7 @@ org.sing_group gc4s-all - 1.2.1 + 1.2.2 ../pom.xml diff --git a/gc4s/src/main/java/org/sing_group/gc4s/input/filechooser/JFileChooserPanel.java b/gc4s/src/main/java/org/sing_group/gc4s/input/filechooser/JFileChooserPanel.java index eda2fa5..800ac76 100644 --- a/gc4s/src/main/java/org/sing_group/gc4s/input/filechooser/JFileChooserPanel.java +++ b/gc4s/src/main/java/org/sing_group/gc4s/input/filechooser/JFileChooserPanel.java @@ -48,6 +48,7 @@ import org.sing_group.gc4s.event.DocumentAdapter; import org.sing_group.gc4s.input.filechooser.event.FileChooserListener; import org.sing_group.gc4s.ui.icons.Icons; +import org.sing_group.gc4s.ui.menu.JPopupMenuActionsVisibility; import org.sing_group.gc4s.utilities.FileDrop; import org.sing_group.gc4s.utilities.FileDropListener; import org.sing_group.gc4s.utilities.builder.JButtonBuilder; @@ -92,6 +93,7 @@ public class JFileChooserPanel extends JPanel { private JFileChooser filechooser; private Mode mode; private AbstractAction browseAction; + private AbstractAction clearSelectionAction; private JButton btnBrowse; private JLabel lblFile; private String lblFileText; @@ -339,22 +341,26 @@ public void actionPerformed(ActionEvent e) { private JPopupMenu getPopupMenu() { if (this.popupMenu == null) { - this.popupMenu = new JPopupMenu(); + this.popupMenu = new JPopupMenuActionsVisibility(); this.popupMenu.add(getClearSelectionAction()); } - return this.popupMenu; } private Action getClearSelectionAction() { - return new AbstractAction("Clear selected file", DEFAULT_ICON_CLEAR) { - private static final long serialVersionUID = 1L; + if (this.clearSelectionAction == null) { + this.clearSelectionAction = new AbstractAction( + "Clear selected file", DEFAULT_ICON_CLEAR) { + private static final long serialVersionUID = 1L; + + @Override + public void actionPerformed(ActionEvent e) { + clearSelectedFile(); + } + }; + } - @Override - public void actionPerformed(ActionEvent e) { - clearSelectedFile(); - } - }; + return this.clearSelectionAction; } private void fileNameUpdated() { @@ -555,6 +561,17 @@ public File getSelectedFile() { public void setUseSharedLastFileFilter(boolean use) { this.useSharedLastFileFilter = use; } + + /** + * Establishes whether the clear selected file action should be enabled or + * not. + * + * @param enabled whether the clear selected file action should be enabled + * or not. + */ + public void setClearSelectedFileActionEnabled(boolean enabled) { + this.getClearSelectionAction().setEnabled(enabled); + } /** * Clears the last file filter shared with other instances. diff --git a/gc4s/src/main/java/org/sing_group/gc4s/ui/menu/JPopupMenuActionsVisibility.java b/gc4s/src/main/java/org/sing_group/gc4s/ui/menu/JPopupMenuActionsVisibility.java new file mode 100644 index 0000000..a892baa --- /dev/null +++ b/gc4s/src/main/java/org/sing_group/gc4s/ui/menu/JPopupMenuActionsVisibility.java @@ -0,0 +1,52 @@ +/* + * #%L + * GC4S components + * %% + * Copyright (C) 2014 - 2018 Hugo López-Fernández, Daniel Glez-Peña, Miguel Reboiro-Jato, + * Florentino Fdez-Riverola, Rosalía Laza-Fidalgo, Reyes Pavón-Rial + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ +package org.sing_group.gc4s.ui.menu; + +import java.util.stream.Stream; + +import javax.swing.JMenuItem; +import javax.swing.JPopupMenu; + +/** + * This component extends {@code JPopupMenu} so that it only becomes visible if + * there is at least one {@code JMenuItem} enabled. Otherwise, it will not + * become visible. + * + * @author hlfernandez + * + */ +public class JPopupMenuActionsVisibility extends JPopupMenu { + private static final long serialVersionUID = 1L; + + @Override + public void setVisible(boolean b) { + if(b) { + b = Stream.of(this.getComponents()) + .filter(c -> c instanceof JMenuItem) + .filter(c -> { + return ((JMenuItem) c).isEnabled(); + }).findAny().isPresent(); + } + super.setVisible(b); + } +} diff --git a/pom.xml b/pom.xml index 35ab924..b264548 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.sing_group gc4s-all - 1.2.1 + 1.2.2 GC4S GUI Components for Java Swing.