101101import org .phoebus .applications .saveandrestore .ui .snapshot .tag .TagWidget ;
102102import org .phoebus .framework .jobs .JobManager ;
103103import org .phoebus .framework .preferences .PhoebusPreferenceService ;
104+ import org .phoebus .framework .selection .SelectionService ;
104105import org .phoebus .framework .workbench .ApplicationService ;
105106import org .phoebus .security .tokens .ScopedAuthenticationToken ;
107+ import org .phoebus .ui .application .ContextMenuService ;
106108import org .phoebus .ui .dialog .DialogHelper ;
107109import org .phoebus .ui .dialog .ExceptionDetailsErrorDialog ;
108110import org .phoebus .ui .javafx .ImageCache ;
111+ import org .phoebus .ui .spi .ContextMenuEntry ;
109112
110113import javax .ws .rs .core .MultivaluedHashMap ;
111114import javax .ws .rs .core .MultivaluedMap ;
@@ -304,10 +307,13 @@ public Filter fromString(String s) {
304307 treeView .getRoot ().setValue (Node .builder ().uniqueId (Node .ROOT_FOLDER_UNIQUE_ID ).name (name ).build ());
305308 });
306309
307- JobManager .schedule ("Configure context menu" , monitor -> {
308- configureContextMenuItems ();
309- treeView .setContextMenu (contextMenu );
310- });
310+ //JobManager.schedule("Configure context menu", monitor -> {
311+ MenuItem addTagMenuItem = TagWidget .AddTagMenuItem ();
312+ addTagMenuItem .setOnAction (action -> addTagToSnapshots ());
313+ tagWithComment .getItems ().addAll (addTagMenuItem );
314+ //configureContextMenuItems();
315+ treeView .setContextMenu (contextMenu );
316+ //});
311317
312318 loadTreeData ();
313319 }
@@ -983,7 +989,7 @@ protected void addTagToSnapshots() {
983989 *
984990 * @param tagMenu The {@link Menu} subject to configuration.
985991 */
986- public void tag (final Menu tagMenu ) {
992+ public void configureTagContextMenu (final Menu tagMenu ) {
987993
988994 List <Node > selectedNodes =
989995 browserSelectionModel .getSelectedItems ().stream ().map (TreeItem ::getValue ).collect (Collectors .toList ());
@@ -1414,53 +1420,35 @@ private void openNode(String nodeId) {
14141420 });
14151421 }
14161422
1417- /**
1418- * Creates context menu items.
1419- */
1420- private void configureContextMenuItems () {
1421- contextMenu .getItems ().add (new LoginMenuItem (this , selectedItemsProperty ,
1422- unused -> ApplicationService .createInstance ("credentials_management" )));
1423- contextMenu .getItems ().add (new NewFolderMenuItem (this , selectedItemsProperty ,
1424- unused -> createNewFolder ()));
1425- contextMenu .getItems ().add (new NewConfigurationMenuItem (this , selectedItemsProperty ,
1426- unused -> createNewConfiguration ()));
1427- contextMenu .getItems ().add (new CreateSnapshotMenuItem (this , selectedItemsProperty ,
1428- unused -> openConfigurationForSnapshot ()));
1429- contextMenu .getItems ().add (new NewCompositeSnapshotMenuItem (this , selectedItemsProperty ,
1430- unused -> createNewCompositeSnapshot ()));
1431- contextMenu .getItems ().add (new SeparatorMenuItem ());
1432- contextMenu .getItems ().add (new EditCompositeMenuItem (this , selectedItemsProperty ,
1433- unused -> editCompositeSnapshot ()));
1434-
1435- contextMenu .getItems ().add (new RenameFolderMenuItem (this , selectedItemsProperty ,
1436- unused -> renameNode ()));
1437- contextMenu .getItems ().add (new CopyMenuItem (this , selectedItemsProperty , unused -> copySelectionToClipboard ()));
1438- contextMenu .getItems ().add (new PasteMenuItem (this , selectedItemsProperty ,
1439- unused -> pasteFromClipboard ()));
1440- contextMenu .getItems ().add (new DeleteNodeMenuItem (this , selectedItemsProperty ,
1441- unused -> deleteNodes ()));
1442- contextMenu .getItems ().add (new SeparatorMenuItem ());
1443-
1444- contextMenu .getItems ().add (new CompareSnapshotsMenuItem (this , selectedItemsProperty ,
1445- unused -> compareSnapshot ()));
1446- contextMenu .getItems ().add (new TagGoldenMenuItem (this , selectedItemsProperty , null ));
1447-
1448- MenuItem addTagMenuItem = TagWidget .AddTagMenuItem ();
1449- addTagMenuItem .setOnAction (action -> addTagToSnapshots ());
1450- tagWithComment .getItems ().addAll (addTagMenuItem );
1451- contextMenu .getItems ().add (tagWithComment );
1452-
1453-
1454- contextMenu .getItems ().add (new SeparatorMenuItem ());
1455- contextMenu .getItems ().add (new CopyUniqueIdToClipboardMenuItem (this , selectedItemsProperty ,
1456- unused -> copyUniqueNodeIdToClipboard ()));
1457- contextMenu .getItems ().add (new SeparatorMenuItem ());
1458- contextMenu .getItems ().add (new ImportFromCSVMenuItem (this , selectedItemsProperty ,
1459- unused -> importFromCSV ()));
1460- contextMenu .getItems ().add (new ExportToCSVMenuItem (this , selectedItemsProperty ,
1461- unused -> exportToCSV ()));
1462- }
1463-
1423+ List <MenuItem > menuItems = Arrays .asList (
1424+ new LoginMenuItem (this , selectedItemsProperty ,
1425+ unused -> ApplicationService .createInstance ("credentials_management" )),
1426+ new NewFolderMenuItem (this , selectedItemsProperty ,
1427+ unused -> createNewFolder ()),
1428+ new NewConfigurationMenuItem (this , selectedItemsProperty ,
1429+ unused -> createNewConfiguration ()),
1430+ new CreateSnapshotMenuItem (this , selectedItemsProperty ,
1431+ unused -> openConfigurationForSnapshot ()),
1432+ new NewCompositeSnapshotMenuItem (this , selectedItemsProperty ,
1433+ unused -> createNewCompositeSnapshot ()),
1434+ new SeparatorMenuItem (),
1435+ new EditCompositeMenuItem (this , selectedItemsProperty , unused -> editCompositeSnapshot ()),
1436+ new RenameFolderMenuItem (this , selectedItemsProperty , unused -> renameNode ()),
1437+ new CopyMenuItem (this , selectedItemsProperty , unused -> copySelectionToClipboard ()),
1438+ new PasteMenuItem (this , selectedItemsProperty , unused -> pasteFromClipboard ()),
1439+ new DeleteNodeMenuItem (this , selectedItemsProperty , unused -> deleteNodes ()),
1440+ new SeparatorMenuItem (),
1441+ new CompareSnapshotsMenuItem (this , selectedItemsProperty , unused -> compareSnapshot ()),
1442+ new TagGoldenMenuItem (this , selectedItemsProperty , null ),
1443+ tagWithComment ,
1444+ new SeparatorMenuItem (),
1445+ new CopyUniqueIdToClipboardMenuItem (this , selectedItemsProperty ,
1446+ unused -> copyUniqueNodeIdToClipboard ()),
1447+ new SeparatorMenuItem (),
1448+ new ImportFromCSVMenuItem (this , selectedItemsProperty , unused -> importFromCSV ()),
1449+ new ExportToCSVMenuItem (this , selectedItemsProperty , unused -> exportToCSV ()),
1450+ new SeparatorMenuItem ()
1451+ );
14641452
14651453 /**
14661454 * Called when user requests context menu. Updates the {@link #selectedItemsProperty}, and since
@@ -1471,17 +1459,44 @@ private void configureContextMenuItems() {
14711459 */
14721460 @ SuppressWarnings ("unused" )
14731461 @ FXML
1474- public void createContextMenu (ContextMenuEvent e ) {
1462+ public void configureContextMenu (ContextMenuEvent e ) {
14751463 ObservableList <? extends TreeItem <Node >> selectedItems = browserSelectionModel .getSelectedItems ();
14761464 selectedItemsProperty .setAll (selectedItems .stream ().map (TreeItem ::getValue ).toList ());
1465+
1466+ // Need to construct the selection carefully in order to be able to match an AdapterFactory to it.
1467+ List <List <Node >> selection = new ArrayList <>();
1468+ List <Node > nodes = new ArrayList <>();
1469+ nodes .addAll (selectedItemsProperty );
1470+ selection .add (nodes );
1471+ SelectionService .getInstance ().setSelection (SaveAndRestoreApplication .NAME , selection );
1472+
1473+ contextMenu .getItems ().clear ();
1474+ contextMenu .getItems ().addAll (menuItems );
1475+ List <ContextMenuEntry > supported = ContextMenuService .getInstance ().listSupportedContextMenuEntries ();
1476+
1477+ supported .stream ().forEach (action -> {
1478+ MenuItem menuItem = new MenuItem (action .getName (), new ImageView (action .getIcon ()));
1479+ menuItem .setOnAction ((ee ) -> {
1480+ try {
1481+ action .call (null , SelectionService .getInstance ().getSelection ());
1482+ } catch (Exception ex ) {
1483+ logger .log (Level .WARNING , "Failed to execute " + action .getName () + " from display builder." , ex );
1484+ }
1485+ });
1486+ menuItem .disableProperty ().set (selectedItemsProperty .isEmpty ());
1487+ contextMenu .getItems ().add (menuItem );
1488+ });
1489+
14771490 snapshotOrCompositeSnapshotOnlySelection .set (selectedItems .stream ().filter (t ->
14781491 !t .getValue ().getNodeType ().equals (NodeType .SNAPSHOT ) &&
14791492 !t .getValue ().getNodeType ().equals (NodeType .COMPOSITE_SNAPSHOT )).findFirst ().isEmpty ());
14801493 tagWithComment .disableProperty ().set (userIdentity .isNull ().get () ||
14811494 selectedItemsProperty .size () != 1 ||
14821495 (!selectedItemsProperty .get (0 ).getNodeType ().equals (NodeType .SNAPSHOT ) &&
14831496 !selectedItemsProperty .get (0 ).getNodeType ().equals (NodeType .COMPOSITE_SNAPSHOT )));
1484- tag (tagWithComment );
1497+ configureTagContextMenu (tagWithComment );
1498+
1499+
14851500 }
14861501
14871502 /**
0 commit comments