Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
SwingFXMenuUtils.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.timeline.ui.listvew;
20 
21 import javafx.scene.control.Menu;
22 import javafx.scene.control.MenuItem;
23 import javafx.scene.control.SeparatorMenuItem;
24 import javax.swing.JMenu;
25 import javax.swing.JMenuItem;
26 import javax.swing.JPopupMenu;
27 import javax.swing.MenuElement;
28 import javax.swing.SwingUtilities;
29 
34 class SwingFXMenuUtils {
35 
36  private SwingFXMenuUtils() {
37  }
38 
47  public static MenuItem createFXMenu(MenuElement jMenuElement) {
48  if (jMenuElement == null) {
49  //Since null is sometime used to represenet a seperator, follow that convention.
50  return new SeparatorMenuItem();
51  } else if (jMenuElement instanceof JMenu) {
52  return new MenuAdapter((JMenu) jMenuElement);
53  } else if (jMenuElement instanceof JPopupMenu) {
54  return new MenuAdapter((JPopupMenu) jMenuElement);
55  } else {
56  return new MenuItemAdapter((JMenuItem) jMenuElement);
57  }
58  }
59 
63  private static class MenuItemAdapter extends MenuItem {
64 
65  private MenuItemAdapter(final JMenuItem jMenuItem) {
66  super(jMenuItem.getText());
67  setDisable(jMenuItem.isEnabled() == false);
68  setOnAction(actionEvent -> SwingUtilities.invokeLater(jMenuItem::doClick));
69  }
70  }
71 
76  private static class MenuAdapter extends Menu {
77 
83  MenuAdapter(final JMenu jMenu) {
84  super(jMenu.getText());
85  setDisable(jMenu.isEnabled() == false);
86  populateSubMenus(jMenu);
87  }
88 
94  MenuAdapter(JPopupMenu jPopupMenu) {
95  super(jPopupMenu.getLabel());
96  setDisable(jPopupMenu.isEnabled() == false);
97  populateSubMenus(jPopupMenu);
98  }
99 
106  private void populateSubMenus(MenuElement menu) {
107  for (MenuElement menuElement : menu.getSubElements()) {
108  if (menuElement == null) {
109  //Since null is sometime used to represenet a seperator, follow that convention.
110  getItems().add(new SeparatorMenuItem());
111 
112  } else if (menuElement instanceof JMenuItem) {
113  getItems().add(SwingFXMenuUtils.createFXMenu(menuElement));
114 
115  } else if (menuElement instanceof JPopupMenu) {
116  populateSubMenus(menuElement);
117  } else {
118  throw new UnsupportedOperationException("Unown MenuElement subclass: " + menuElement.getClass().getName());
119  }
120  }
121  }
122  }
123 }

Copyright © 2012-2022 Basis Technology. Generated on: Tue Jun 27 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.