From edb564214001da04e84fa2f8dd60a395ea67e58c Mon Sep 17 00:00:00 2001 From: jmillman Date: Thu, 19 May 2016 17:05:59 -0400 Subject: [PATCH] rename SwingMenuItemAdapter.java -> SwingFXMenuUtils.java and cleanup --- .../timeline/SwingMenuItemAdapter.java | 79 ------------ .../timeline/ui/listvew/ListTimeline.java | 5 +- .../timeline/ui/listvew/SwingFXMenuUtils.java | 117 ++++++++++++++++++ 3 files changed, 119 insertions(+), 82 deletions(-) delete mode 100644 Core/src/org/sleuthkit/autopsy/timeline/SwingMenuItemAdapter.java create mode 100644 Core/src/org/sleuthkit/autopsy/timeline/ui/listvew/SwingFXMenuUtils.java diff --git a/Core/src/org/sleuthkit/autopsy/timeline/SwingMenuItemAdapter.java b/Core/src/org/sleuthkit/autopsy/timeline/SwingMenuItemAdapter.java deleted file mode 100644 index c513c76830..0000000000 --- a/Core/src/org/sleuthkit/autopsy/timeline/SwingMenuItemAdapter.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Autopsy Forensic Browser - * - * Copyright 2013-14 Basis Technology Corp. - * Contact: carrier sleuthkit org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.sleuthkit.autopsy.timeline; - -import javafx.scene.control.Menu; -import javafx.scene.control.MenuItem; -import javafx.scene.control.SeparatorMenuItem; -import javax.swing.JMenu; -import javax.swing.JMenuItem; -import javax.swing.JPopupMenu; -import javax.swing.MenuElement; -import javax.swing.SwingUtilities; - -//TODO: move this into CoreUtils? -jm -public class SwingMenuItemAdapter extends MenuItem { - - SwingMenuItemAdapter(final JMenuItem jMenuItem) { - super(jMenuItem.getText()); - setOnAction(actionEvent -> SwingUtilities.invokeLater(jMenuItem::doClick)); - } - - public static MenuItem create(MenuElement jmenuItem) { - if (jmenuItem == null) { - return new SeparatorMenuItem(); - } else if (jmenuItem instanceof JMenu) { - return new SwingMenuAdapter((JMenu) jmenuItem); - } else if (jmenuItem instanceof JPopupMenu) { - return new SwingMenuAdapter((JPopupMenu) jmenuItem); - } else { - return new SwingMenuItemAdapter((JMenuItem) jmenuItem); - } - - } - - private static class SwingMenuAdapter extends Menu { - - SwingMenuAdapter(final JMenu jMenu) { - super(jMenu.getText()); - buildChildren(jMenu); - } - - SwingMenuAdapter(JPopupMenu jPopupMenu) { - super(jPopupMenu.getLabel()); - buildChildren(jPopupMenu); - } - - private void buildChildren(MenuElement jMenu) { - - for (MenuElement menuE : jMenu.getSubElements()) { - if (menuE == null) { - getItems().add(new SeparatorMenuItem()); - } else if (menuE instanceof JMenuItem) { - getItems().add(SwingMenuItemAdapter.create(menuE)); - } else if (menuE instanceof JPopupMenu) { - buildChildren(menuE); - } else { - throw new UnsupportedOperationException(); - } - } - } - } - -} diff --git a/Core/src/org/sleuthkit/autopsy/timeline/ui/listvew/ListTimeline.java b/Core/src/org/sleuthkit/autopsy/timeline/ui/listvew/ListTimeline.java index dd96f3c7a3..84dbc1de86 100644 --- a/Core/src/org/sleuthkit/autopsy/timeline/ui/listvew/ListTimeline.java +++ b/Core/src/org/sleuthkit/autopsy/timeline/ui/listvew/ListTimeline.java @@ -50,7 +50,6 @@ import org.openide.util.actions.Presenter; import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.ThreadConfined; import org.sleuthkit.autopsy.timeline.FXMLConstructor; -import org.sleuthkit.autopsy.timeline.SwingMenuItemAdapter; import org.sleuthkit.autopsy.timeline.TimeLineController; import org.sleuthkit.autopsy.timeline.datamodel.SingleEvent; import org.sleuthkit.autopsy.timeline.explorernodes.EventNode; @@ -295,9 +294,9 @@ class ListTimeline extends BorderPane { if (Arrays.asList("&Properties", "Tools").contains(actionName) == false) { if (element instanceof Presenter.Popup) { JMenuItem submenu = ((Presenter.Popup) element).getPopupPresenter(); - menuItems.add(SwingMenuItemAdapter.create(submenu)); + menuItems.add(SwingFXMenuUtils.createFXMenu(submenu)); } else { - menuItems.add(SwingMenuItemAdapter.create(new Actions.MenuItem(element, false))); + menuItems.add(SwingFXMenuUtils.createFXMenu(new Actions.MenuItem(element, false))); } } } diff --git a/Core/src/org/sleuthkit/autopsy/timeline/ui/listvew/SwingFXMenuUtils.java b/Core/src/org/sleuthkit/autopsy/timeline/ui/listvew/SwingFXMenuUtils.java new file mode 100644 index 0000000000..8468e8644e --- /dev/null +++ b/Core/src/org/sleuthkit/autopsy/timeline/ui/listvew/SwingFXMenuUtils.java @@ -0,0 +1,117 @@ +/* + * Autopsy Forensic Browser + * + * Copyright 2011-2016 Basis Technology Corp. + * Contact: carrier sleuthkit org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.sleuthkit.autopsy.timeline.ui.listvew; + +import javafx.scene.control.Menu; +import javafx.scene.control.MenuItem; +import javafx.scene.control.SeparatorMenuItem; +import javax.swing.JMenu; +import javax.swing.JMenuItem; +import javax.swing.JPopupMenu; +import javax.swing.MenuElement; +import javax.swing.SwingUtilities; + +/** + * Allows creation of JavaFX menus with the same structure as Swing menus and + * which invoke the same actions. + */ +public class SwingFXMenuUtils extends MenuItem { + + /** + * Factory method that creates a JavaFX MenuItem backed by a MenuElement + * + * @param jMenuElement The MenuElement to create a JavaFX menu for. + * + * @return a MenuItem for the given MenuElement + */ + public static MenuItem createFXMenu(MenuElement jMenuElement) { + if (jMenuElement == null) { + //Since null is sometime used to represenet a seperator, follow that convention. + return new SeparatorMenuItem(); + } else if (jMenuElement instanceof JMenu) { + return new MenuAdapter((JMenu) jMenuElement); + } else if (jMenuElement instanceof JPopupMenu) { + return new MenuAdapter((JPopupMenu) jMenuElement); + } else { + return new MenuItemAdapter((JMenuItem) jMenuElement); + } + } + + /** + * A JavaFX MenuItem that invokes the backing JMenuItem when clicked. + */ + private static class MenuItemAdapter extends MenuItem { + + private MenuItemAdapter(final JMenuItem jMenuItem) { + super(jMenuItem.getText()); + setOnAction(actionEvent -> SwingUtilities.invokeLater(jMenuItem::doClick)); + } + } + + /** + * A JavaFX Menu that has the same structure as a given Swing JMenu or + * JPopupMenu. + */ + private static class MenuAdapter extends Menu { + + /** + * Constructor for JMenu + * + * @param jMenu The JMenu to parallel in this Menu. + */ + MenuAdapter(final JMenu jMenu) { + super(jMenu.getText()); + populateSubMenus(jMenu); + } + + /** + * Constructor for JPopupMenu + * + * @param jPopupMenu The JPopupMenu to parallel in this Menu. + */ + MenuAdapter(JPopupMenu jPopupMenu) { + super(jPopupMenu.getLabel()); + populateSubMenus(jPopupMenu); + } + + /** + * Populate the sub menus of this menu. + * + * @param menu The MenuElement whose sub elements will be used to + * populate the sub menus of this menu. + */ + private void populateSubMenus(MenuElement menu) { + for (MenuElement menuElement : menu.getSubElements()) { + if (menuElement == null) { + //Since null is sometime used to represenet a seperator, follow that convention. + getItems().add(new SeparatorMenuItem()); + + } else if (menuElement instanceof JMenuItem) { + getItems().add(SwingFXMenuUtils.createFXMenu(menuElement)); + + } else if (menuElement instanceof JPopupMenu) { + populateSubMenus(menuElement); + + } else { + throw new UnsupportedOperationException(); + } + } + } + } +}