diff --git a/app/alarm/model/src/main/java/org/phoebus/applications/alarm/client/AlarmClientLeaf.java b/app/alarm/model/src/main/java/org/phoebus/applications/alarm/client/AlarmClientLeaf.java index 2c0260bfe9..ed50d91567 100644 --- a/app/alarm/model/src/main/java/org/phoebus/applications/alarm/client/AlarmClientLeaf.java +++ b/app/alarm/model/src/main/java/org/phoebus/applications/alarm/client/AlarmClientLeaf.java @@ -52,9 +52,9 @@ public AlarmClientLeaf(final String parent_path, final String name) * a detached copy is used to send the request. * @return {@link AlarmClientLeaf} with same configuration, but no parent */ - public AlarmClientLeaf createDetachedCopy() + public AlarmClientLeaf createDetachedCopy(final String newName) { - final AlarmClientLeaf pv = new AlarmClientLeaf(null, getName()); + final AlarmClientLeaf pv = new AlarmClientLeaf(null, newName); pv.setDescription(getDescription()); pv.setEnabled(getEnabled()); pv.setLatching(isLatching()); @@ -69,6 +69,14 @@ public AlarmClientLeaf createDetachedCopy() return pv; } + /** When requesting a configuration update, + * a detached copy is used to send the request. + * @return {@link AlarmClientLeaf} with same configuration, but no parent + */ + public AlarmClientLeaf createDetachedCopy() { + return createDetachedCopy(getName()); + } + /** @return Return description */ @Override public String getDescription() diff --git a/app/alarm/model/src/main/java/org/phoebus/applications/alarm/client/AlarmClientNode.java b/app/alarm/model/src/main/java/org/phoebus/applications/alarm/client/AlarmClientNode.java index e5ec601250..315238ab63 100644 --- a/app/alarm/model/src/main/java/org/phoebus/applications/alarm/client/AlarmClientNode.java +++ b/app/alarm/model/src/main/java/org/phoebus/applications/alarm/client/AlarmClientNode.java @@ -7,6 +7,7 @@ *******************************************************************************/ package org.phoebus.applications.alarm.client; +import java.util.ArrayList; import java.util.concurrent.CopyOnWriteArrayList; import org.phoebus.applications.alarm.model.AlarmTreeItemWithState; @@ -27,4 +28,17 @@ public AlarmClientNode(final String parent_path, final String name) super(parent_path, name, new CopyOnWriteArrayList<>()); state = new BasicState(SeverityLevel.OK); } + + /** @return {@link AlarmClientNode} with same configuration, but no parent or children */ + public AlarmClientNode createDetachedCopy() + { + final AlarmClientNode node = new AlarmClientNode(null, getName()); + + node.setGuidance(new ArrayList<>(getGuidance())); + node.setDisplays(new ArrayList<>(getDisplays())); + node.setCommands(new ArrayList<>(getCommands())); + node.setActions(new ArrayList<>(getActions())); + + return node; + } } diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/Messages.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/Messages.java index 1a8d67bb5e..d9d3f45a83 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/Messages.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/Messages.java @@ -22,6 +22,8 @@ public class Messages public static String Annunciator; public static String commands; public static String configure; + public static String cutPasteAcrossConfigs; + public static String cutPasteIntoChild; public static String delayTooltip0; public static String delayTooltip1; public static String delayTooltip2; @@ -36,6 +38,8 @@ public class Messages public static String displays; public static String enableAlarmFailed; public static String enableAlarms; + public static String enterNameForDuplicate; + public static String enterNewName; public static String guidance; public static String headerAlreadyDisabled; public static String headerAlreadyEnabled; @@ -45,7 +49,16 @@ public class Messages public static String indefinitely; public static String MenuPath; public static String moveItemFailed; + public static String nameCannotBeEmpty; + public static String nodeAlreadyExists; + public static String nodePvsAlreadyExist; + public static String pasteItem; + public static String pasteItemFailed; public static String promptTitle; + public static String pvAlreadyExists; + public static String renameItem; + public static String renameDialogLabel; + public static String renameDialogTitle; public static String removeComponentFailed; public static String renameItemFailed; public static String totalPVs; diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelper.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelper.java index 25e77114a6..858e50b399 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelper.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelper.java @@ -18,10 +18,7 @@ import org.phoebus.applications.alarm.model.AlarmTreeItem; import org.phoebus.applications.alarm.model.AlarmTreePath; import org.phoebus.applications.alarm.ui.Messages; -import org.phoebus.ui.dialog.DialogHelper; -import javafx.scene.control.TextInputDialog; -import javafx.scene.control.TreeView; import org.phoebus.util.time.TimestampFormats; /** @author Evan Smith @@ -56,28 +53,6 @@ public static void rebuildTree(AlarmClient model, AlarmTreeItem parent, Strin } } - /** Prompt the screen with a dialog box. - * @param title Title string of the dialog box. - * @param header Header string of the dialog box. - * @param default_text Default text contents of dialog box. - * @param node Node where the dialog box will be located. - * @return String The entered string or null if the entry was empty. - */ - public static String prompt(String title, String header, final String default_text, final TreeView> node) - { - // Prompt for new name - final TextInputDialog prompt = new TextInputDialog(default_text); - DialogHelper.positionDialog(prompt, node, -200, -100); - prompt.setTitle(title); - prompt.setHeaderText(header); - prompt.getDialogPane().setPrefWidth(500); - prompt.setResizable(true); - final String input = prompt.showAndWait().orElse(""); - if (input.isEmpty()) - return null; - return input; - } - /** Validate the passed path string by checking if the path name is valid. * *

A path name is considered valid if the path to the item's new location exists in the tree, @@ -153,6 +128,18 @@ private static Stream streamLeafItems(final AlarmTreeItem al } } + /** + * Collects the names of {@link AlarmClientLeaf}s items. + * @param root The start node from where to get {@link AlarmClientLeaf}s. If this is an {@link AlarmClientLeaf}, it + * will be returned as the sole item in the {@link Set} + * @return A {@link Set} of the names of all {@link AlarmClientLeaf}s. + */ + public static Set getLeafNames(final AlarmTreeItem root) { + return streamLeafItems(root) + .map(AlarmTreeItem::getName) + .collect(Collectors.toSet()); + } + /** * * @param items {@link List} of {@link AlarmTreeItem}s that may be a mix of leaves and non-leaves, e.g. a user diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeView.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeView.java index 3671f35bed..4b41cb2867 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeView.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeView.java @@ -24,6 +24,8 @@ import java.util.logging.Level; import java.util.stream.Collectors; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleObjectProperty; import javafx.collections.FXCollections; import org.phoebus.applications.alarm.AlarmSystem; import org.phoebus.applications.alarm.client.AlarmClient; @@ -149,6 +151,9 @@ public class AlarmTreeView extends BorderPane implements AlarmClientListener items.remove(changing); }); + private static final SimpleObjectProperty> clipboard = new SimpleObjectProperty<>(null); + private static final SimpleBooleanProperty isClipboardCut = new SimpleBooleanProperty(false); + // Javadoc for TreeItem shows example for overriding isLeaf() and getChildren() // to dynamically create TreeItem as TreeView requests information. // @@ -450,6 +455,11 @@ public void itemRemoved(final AlarmTreeItem item) { indicateChange(); + // Clear clipboard if we are removing clipboard's item + if (clipboard.get() != null && clipboard.get().getPathName().equals(item.getPathName())) { + clipboard.set(null); + } + // Remove item and all sub-items from model2ui final TreeItem> view_item = removeViewItems(item); if (view_item == null) @@ -608,6 +618,13 @@ else if (selection.size() == 1) menu_items.add(new ConfigureComponentAction(tree_view, model, item)); menu_items.add(new SeparatorMenuItem()); + menu_items.add(new CopyItemAction(tree_view, clipboard, isClipboardCut, item)); + menu_items.add(new CutItemAction(tree_view, clipboard, isClipboardCut, item)); + + final AlarmTreeItem parent = item instanceof AlarmClientNode ? item : item.getParent(); + final TreeItem> viewParent = path2view.get(parent.getPathName()); + menu_items.add(new PasteItemAction(tree_view, model, viewParent, clipboard, isClipboardCut.get())); + if (item instanceof AlarmClientNode) menu_items.add(new AddComponentAction(tree_view, model, item)); @@ -618,6 +635,7 @@ else if (selection.size() == 1) menu_items.add(new MoveTreeItemAction(tree_view, model, item)); } + if (selection.size() >= 1) { menu_items.add(new EnableComponentAction(tree_view, model, selection)); diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/CopyItemAction.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/CopyItemAction.java new file mode 100644 index 0000000000..7c8a9ff00e --- /dev/null +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/CopyItemAction.java @@ -0,0 +1,28 @@ +package org.phoebus.applications.alarm.ui.tree; + +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleObjectProperty; +import javafx.scene.Node; +import javafx.scene.control.MenuItem; +import org.phoebus.applications.alarm.AlarmSystem; +import org.phoebus.applications.alarm.model.AlarmTreeItem; +import org.phoebus.ui.javafx.ImageCache; + + +@SuppressWarnings("nls") +public class CopyItemAction extends MenuItem +{ + /** @param node Node to position dialog + * @param clipboard Property containing the clipboard + * @param isCut Property indicating whether the clipboard item should be removed when pasted + * @param item Parent item in alarm tree + */ + public CopyItemAction(final Node node, final SimpleObjectProperty> clipboard, final SimpleBooleanProperty isCut, final AlarmTreeItem item) + { + super("Copy Item", ImageCache.getImageView(AlarmSystem.class, "/icons/copy.png")); + setOnAction(event -> { + clipboard.set(item); + isCut.set(false); + }); + } +} diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/CutItemAction.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/CutItemAction.java new file mode 100644 index 0000000000..4a7057f56a --- /dev/null +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/CutItemAction.java @@ -0,0 +1,28 @@ +package org.phoebus.applications.alarm.ui.tree; + +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleObjectProperty; +import javafx.scene.Node; +import javafx.scene.control.MenuItem; +import org.phoebus.applications.alarm.AlarmSystem; +import org.phoebus.applications.alarm.model.AlarmTreeItem; +import org.phoebus.ui.javafx.ImageCache; + + +@SuppressWarnings("nls") +public class CutItemAction extends MenuItem +{ + /** @param node Node to position dialog + * @param clipboard Property containing the clipboard + * @param isCut Property indicating whether the clipboard item should be removed when pasted + * @param item Parent item in alarm tree + */ + public CutItemAction(final Node node, final SimpleObjectProperty> clipboard, final SimpleBooleanProperty isCut, final AlarmTreeItem item) + { + super("Cut Item", ImageCache.getImageView(AlarmSystem.class, "/icons/cut.png")); + setOnAction(event -> { + clipboard.set(item); + isCut.set(true); + }); + } +} diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/DuplicatePVAction.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/DuplicatePVAction.java index d93214df08..1e76f8ef61 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/DuplicatePVAction.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/DuplicatePVAction.java @@ -12,6 +12,7 @@ import org.phoebus.applications.alarm.client.AlarmClient; import org.phoebus.applications.alarm.client.AlarmClientLeaf; import org.phoebus.applications.alarm.model.AlarmTreePath; +import org.phoebus.applications.alarm.ui.Messages; import org.phoebus.framework.jobs.JobManager; import org.phoebus.ui.dialog.DialogHelper; import org.phoebus.ui.dialog.ExceptionDetailsErrorDialog; @@ -19,7 +20,6 @@ import javafx.scene.Node; import javafx.scene.control.MenuItem; -import javafx.scene.control.TextInputDialog; import java.util.logging.Level; import java.util.logging.Logger; @@ -40,27 +40,15 @@ public DuplicatePVAction(final Node node, final AlarmClient model, final AlarmCl setOnAction(event -> { // Prompt for new PV name - final TextInputDialog dialog = new TextInputDialog(); - dialog.setTitle(getText()); - dialog.setHeaderText("Enter name for duplicate of\n" + original.getPathName()); + final RenameDialog dialog = new RenameDialog(model, original.getParent(), original); + dialog.setHeaderText(Messages.enterNameForDuplicate); DialogHelper.positionDialog(dialog, node, -100, -50); final String new_name = dialog.showAndWait().orElse(null); - if (new_name == null || new_name.isEmpty()) + if (new_name == null) return; // Create near copy of original PV, but with new name - final AlarmClientLeaf template = new AlarmClientLeaf(null, new_name); - template.setDescription(original.getDescription()); - template.setEnabled(original.isEnabled()); - template.setLatching(original.isLatching()); - template.setAnnunciating(original.isAnnunciating()); - template.setDelay(original.getDelay()); - template.setCount(original.getCount()); - template.setFilter(original.getFilter()); - template.setGuidance(original.getGuidance()); - template.setDisplays(original.getDisplays()); - template.setCommands(original.getCommands()); - template.setActions(original.getActions()); + final AlarmClientLeaf template = original.createDetachedCopy(new_name); // Request adding new PV JobManager.schedule(getText(), monitor -> { diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/MoveTreeItemAction.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/MoveTreeItemAction.java index 93ac6d881b..6ccf660968 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/MoveTreeItemAction.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/MoveTreeItemAction.java @@ -9,6 +9,7 @@ import org.phoebus.applications.alarm.AlarmSystem; import org.phoebus.applications.alarm.client.AlarmClient; +import org.phoebus.applications.alarm.client.AlarmClientNode; import org.phoebus.applications.alarm.model.AlarmTreeItem; import org.phoebus.applications.alarm.model.AlarmTreePath; import org.phoebus.applications.alarm.ui.Messages; @@ -70,20 +71,22 @@ public MoveTreeItemAction(TreeView> node, return; } - // Tree view keeps the selection indices, which will point to wrong content - // after those items have been removed. - node.getSelectionModel().clearSelection(); + // Tree view keeps the selection indices, which will point to wrong content + // after those items have been removed + node.getSelectionModel().clearSelection(); final String new_path = path; // On a background thread, send the item configuration updates for the item to be moved and all its children. JobManager.schedule(getText(), monitor -> { try { - // Move the item. + // Add the new item model.sendItemConfigurationUpdate(new_path, item); - // Move the item's children. - AlarmTreeHelper.rebuildTree(model, item, new_path); - // Delete the old item. This deletes the old item's children as well. + // ...and rebuild all its children + if (item instanceof AlarmClientNode) { + AlarmTreeHelper.rebuildTree(model, item, new_path); + } + // Remove the item and all its children model.removeComponent(item); } catch (Exception e) { ExceptionDetailsErrorDialog.openError(Messages.error, diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/PasteItemAction.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/PasteItemAction.java new file mode 100644 index 0000000000..033b4f1781 --- /dev/null +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/PasteItemAction.java @@ -0,0 +1,160 @@ +package org.phoebus.applications.alarm.ui.tree; + +import javafx.beans.property.SimpleObjectProperty; +import javafx.scene.Node; +import javafx.scene.control.Alert; +import javafx.scene.control.Label; +import javafx.scene.control.MenuItem; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.TreeItem; +import javafx.scene.layout.VBox; +import org.phoebus.applications.alarm.AlarmSystem; +import org.phoebus.applications.alarm.client.AlarmClient; +import org.phoebus.applications.alarm.client.AlarmClientNode; +import org.phoebus.applications.alarm.model.AlarmTreeItem; +import org.phoebus.applications.alarm.model.AlarmTreePath; +import org.phoebus.applications.alarm.ui.Messages; +import org.phoebus.framework.jobs.JobManager; +import org.phoebus.ui.dialog.DialogHelper; +import org.phoebus.ui.dialog.ExceptionDetailsErrorDialog; +import org.phoebus.ui.javafx.ImageCache; + +import java.text.MessageFormat; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + + +@SuppressWarnings("nls") +public class PasteItemAction extends MenuItem +{ + + /** @param node Node to position dialog + * @param model {@link AlarmClient} + * @param parent Parent to paste the item into + * @param clipboard Property containing the clipboard + * @param isCut Property indicating whether the clipboard item should be removed when pasted + */ + public PasteItemAction( + final Node node, + final AlarmClient model, + final TreeItem> viewParent, + final SimpleObjectProperty> clipboard, + final boolean isCut) + { + super(Messages.pasteItem, ImageCache.getImageView(AlarmSystem.class, "/icons/paste.png")); + + final AlarmTreeItem parent = viewParent.getValue(); + + setDisable(clipboard.get() == null || isCut && parent.getPathName().equals(clipboard.get().getParent().getPathName())); + + setOnAction(event -> { + final AlarmTreeItem item = clipboard.get(); + final String itemName = item.getName(); + + final String itemConfig = AlarmTreePath.splitPath(item.getPathName())[0]; + final String modelConfig = AlarmTreePath.splitPath(model.getRoot().getPathName())[0]; + final boolean isCrossConfig = !itemConfig.equals(modelConfig); + + // Cross-config cut/paste is forbidden. (Why? User cuts from config A -> switches to + // config B -> pastes to config B -> config A is no longer loaded so how can we remove + // the old item?) + if (isCut && isCrossConfig) { + final Alert alert = new Alert(Alert.AlertType.ERROR); + DialogHelper.positionDialog(alert, node, -100, -50); + alert.setTitle(Messages.pasteItemFailed); + alert.setHeaderText(Messages.cutPasteAcrossConfigs); + alert.show(); + return; + } + + // Cutting+pasting a node into one of its children is forbidden for obvious reasons + final boolean isPasteIntoSelf = parent.getPathName().equals(item.getPathName()); + final boolean isPasteIntoChild = parent.getPathName().startsWith(item.getPathName() + "/"); + if (isCut && (isPasteIntoSelf || isPasteIntoChild)) { + final Alert alert = new Alert(Alert.AlertType.ERROR); + DialogHelper.positionDialog(alert, node, -100, -50); + alert.setTitle(Messages.pasteItemFailed); + alert.setHeaderText(Messages.cutPasteIntoChild); + alert.show(); + return; + } + + // Figure out if this item needs renaming + final boolean needsRename; + final Set allPvsInNewConfig = AlarmTreeHelper.getLeafNames(model.getRoot()); + if (item instanceof AlarmClientNode) { + // Needs to be renamed if new name matches any child + needsRename = parent.getChildren().stream().anyMatch(child -> item.getName().equals(child.getName())); + } else { + // Needs to be renamed if pasting would result in a duplicate PV + needsRename = !isCut && allPvsInNewConfig.contains(item.getName()); + } + + // Get final name + final String finalName; + if (needsRename) { + final RenameDialog renameDialog = new RenameDialog(model, parent, item); + renameDialog.setHeaderText(MessageFormat.format(Messages.enterNewName, parent.getName(), item.getName())); + DialogHelper.positionDialog(renameDialog, node, -100, -50); + final Optional name = renameDialog.showAndWait(); + if (!name.isPresent()) { + return; + } + finalName = name.get(); + } else { + finalName = item.getName(); + } + + // Special case: if a node is pasted across configs, some of the PVs in that node + // may already exist. But the user still may have intended to copy across all the + // PVs and so we should not just fail silently. Best-effort copying is unlikely + // to be helpful here, and likely to cause unintended behaviour, so we do not offer + // that option and just display an error instead. + if (isCrossConfig && item instanceof AlarmClientNode) { + final Set duplicatePvs = AlarmTreeHelper.getLeafNames(item); + duplicatePvs.retainAll(allPvsInNewConfig); + + if (!duplicatePvs.isEmpty()) { + final Alert alert = new Alert(Alert.AlertType.ERROR); + DialogHelper.positionDialog(alert, node, -100, -50); + alert.setTitle(Messages.pasteItemFailed); + alert.setHeaderText(Messages.nodePvsAlreadyExist); + + final VBox pvPane = new VBox(); + for (final String pv : duplicatePvs) { + pvPane.getChildren().add(new Label(pv)); + } + final ScrollPane scrollPane = new ScrollPane(pvPane); + scrollPane.setMaxHeight(400.0); + + alert.getDialogPane().setContent(scrollPane); + alert.show(); + return; + } + } + + // Expand the node which we're adding to in anticipation of new children being added + viewParent.setExpanded(true); + + // Finally paste the item (on a separate thread of course) + final AlarmTreeItem finalItem = item; // lambda variables must be final + JobManager.schedule("Paste alarm tree item", _ -> { + try { + final String newPath = AlarmTreePath.makePath(parent.getPathName(), finalName); + model.sendItemConfigurationUpdate(newPath, item); + if (isCut) { + // If we're cutting, there's never any risk of duplicate PVs so we can copy all the children too + AlarmTreeHelper.rebuildTree(model, item, newPath); + model.removeComponent(item); + } else if (isCrossConfig && item instanceof AlarmClientNode) { + // Cross-config pasting should copy all the children + AlarmTreeHelper.rebuildTree(model, item, newPath); + } + } catch (final Exception ex) { + ExceptionDetailsErrorDialog.openError(Messages.error, Messages.pasteItemFailed, ex); + } + }); + }); + } +} diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/RenameDialog.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/RenameDialog.java new file mode 100644 index 0000000000..ce10dacf43 --- /dev/null +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/RenameDialog.java @@ -0,0 +1,77 @@ +package org.phoebus.applications.alarm.ui.tree; + +import javafx.geometry.Pos; +import javafx.scene.control.ButtonType; +import javafx.scene.control.Dialog; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Priority; +import javafx.scene.layout.VBox; +import javafx.scene.paint.Color; +import org.phoebus.applications.alarm.client.AlarmClient; +import org.phoebus.applications.alarm.client.AlarmClientLeaf; +import org.phoebus.applications.alarm.client.AlarmClientNode; +import org.phoebus.applications.alarm.model.AlarmTreeItem; +import org.phoebus.applications.alarm.ui.Messages; + +import java.util.HashSet; +import java.util.Set; +import java.util.function.Consumer; +import java.util.stream.Collectors; + + +public class RenameDialog extends Dialog { + + public RenameDialog(final AlarmClient model, final AlarmTreeItem parent, final AlarmTreeItem item) { + super(); + + setTitle(Messages.renameDialogTitle); + + final Label renameLabel = new Label(Messages.renameDialogLabel); + + final TextField input = new TextField(); + input.setPrefWidth(600.0); + HBox.setHgrow(input, Priority.ALWAYS); + + final HBox inputBox = new HBox(renameLabel, input); + inputBox.setAlignment(Pos.CENTER_LEFT); + + final Label errorLabel = new Label(Messages.nodeAlreadyExists); + errorLabel.setTextFill(Color.RED); + errorLabel.setMaxWidth(Double.MAX_VALUE); + + final Consumer updateError = newError -> { + errorLabel.setText(newError); + getDialogPane().lookupButton(ButtonType.OK).setDisable(newError != null); + }; + + final Set allPvs = AlarmTreeHelper.getLeafNames(model.getRoot()); + input.textProperty().addListener(_ -> { + // Show error and disable OK if text is empty + if (input.getText().isEmpty()) { + updateError.accept(Messages.nameCannotBeEmpty); + return; + } + + if (item instanceof AlarmClientNode) { + // Show error and disable OK if the new name matches any child + final boolean childExists = parent.getChildren().stream().anyMatch(child -> input.getText().equals(child.getName())); + updateError.accept(childExists ? Messages.nodeAlreadyExists : null); + } else { + // SHow error and disable OK if the PV already exists in the config + final boolean pvExists = allPvs.contains(input.getText()); + updateError.accept(pvExists ? Messages.pvAlreadyExists : null); + } + }); + + + getDialogPane().setContent(new VBox(inputBox, errorLabel)); + getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL); + + setResultConverter(buttonType -> buttonType == ButtonType.OK ? input.getText() : null); + + input.setText(item.getName()); + errorLabel.setText(""); // It can be confusing if an error is shown immediately when the UI loads + } +} diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/RenameTreeItemAction.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/RenameTreeItemAction.java index 40e5f13191..e4fa3cf6db 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/RenameTreeItemAction.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/RenameTreeItemAction.java @@ -15,6 +15,7 @@ import org.phoebus.applications.alarm.model.BasicState; import org.phoebus.applications.alarm.ui.Messages; import org.phoebus.framework.jobs.JobManager; +import org.phoebus.ui.dialog.DialogHelper; import org.phoebus.ui.dialog.ExceptionDetailsErrorDialog; import org.phoebus.ui.javafx.ImageCache; @@ -36,68 +37,42 @@ public RenameTreeItemAction(final TreeView> node, final AlarmClient model, final AlarmTreeItem item) { - super("Rename Item", ImageCache.getImageView(AlarmSystem.class, "/icons/rename.png")); + super(Messages.renameItem, ImageCache.getImageView(AlarmSystem.class, "/icons/rename.png")); - if (item instanceof AlarmClientNode) - { - setOnAction(event -> - { - //Prompt for new name - final String new_name = AlarmTreeHelper.prompt(getText(), "Enter new name for Node", item.getName(), node); - if (null == new_name) - return; - // Tree view keeps the selection indices, which will point to wrong content - // after those items have been removed - if (node instanceof TreeView) - ((TreeView) node).getSelectionModel().clearSelection(); + setOnAction(event -> + { + // Prompt for new name + final RenameDialog dialog = new RenameDialog(model, item.getParent(), item); + dialog.setHeaderText(Messages.enterNewName); + DialogHelper.positionDialog(dialog, node, -100, -50); + final String new_name = dialog.showAndWait().orElse(null); + if (new_name == null) + return; - // Delete old, rebuild child links, and add new name in background thread. - JobManager.schedule(getText(), monitor -> - { - final AlarmTreeItem parent = item.getParent(); - // Remove the item and all its children. - // Add the new item, and then rebuild all its children. - try { - final String new_path = AlarmTreePath.makePath(parent.getPathName(), new_name); - model.sendItemConfigurationUpdate(new_path, item); - AlarmTreeHelper.rebuildTree(model, item, new_path); - model.removeComponent(item); - } catch (Exception e) { - ExceptionDetailsErrorDialog.openError(Messages.error, - Messages.renameItemFailed, - e); - } - }); - }); - } - else - { - setOnAction(event -> - { - //Prompt for new name - final String new_name = AlarmTreeHelper.prompt(getText(), "Enter new name for PV", item.getName(), node); - if (null == new_name) - return; - // Tree view keeps the selection indices, which will point to wrong content - // after those items have been removed - if (node instanceof TreeView) - ((TreeView) node).getSelectionModel().clearSelection(); + // Tree view keeps the selection indices, which will point to wrong content + // after those items have been removed + node.getSelectionModel().clearSelection(); - // Delete old, add new name in background thread - JobManager.schedule(getText(), monitor -> - { - final AlarmTreeItem parent = item.getParent(); - try { - final String new_path = AlarmTreePath.makePath(parent.getPathName(), new_name); - model.sendItemConfigurationUpdate(new_path, item); - model.removeComponent(item); - } catch (Exception e) { - ExceptionDetailsErrorDialog.openError(Messages.error, - Messages.renameItemFailed, - e); + // Delete old, rebuild child links, and add new name in background thread. + JobManager.schedule(getText(), monitor -> + { + final AlarmTreeItem parent = item.getParent(); + try { + final String new_path = AlarmTreePath.makePath(parent.getPathName(), new_name); + // Add the new item + model.sendItemConfigurationUpdate(new_path, item); + // ...and rebuild all its children + if (item instanceof AlarmClientNode) { + AlarmTreeHelper.rebuildTree(model, item, new_path); } - }); - }); - } + // Remove the item and all its children + model.removeComponent(item); + } catch (final Exception ex) { + ExceptionDetailsErrorDialog.openError(Messages.error, + Messages.renameItemFailed, + ex); + } + }); + }); } } diff --git a/app/alarm/ui/src/main/resources/icons/cut.png b/app/alarm/ui/src/main/resources/icons/cut.png new file mode 100644 index 0000000000..e2a59bc600 Binary files /dev/null and b/app/alarm/ui/src/main/resources/icons/cut.png differ diff --git a/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/messages.properties b/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/messages.properties index 2d6d863eff..ce5acaaca2 100644 --- a/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/messages.properties +++ b/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/messages.properties @@ -33,6 +33,8 @@ behavior=Behavior: behaviorTooltip=Enable alarms? See also 'Enabling Filter' commands=Commands: configure=Configure +cutPasteAcrossConfigs=You cannot cut and paste a component across configs.\nInstead, copy the component across and then delete the original. +cutPasteIntoChild=You cannot cut and paste a component into itself or one of its children dateTimePickerTooltip=Select a date until which the alarm should be disabled delay=Delay delayTooltip0=Alarms are indicated when they persist for at least this long.\n{0} @@ -57,6 +59,8 @@ enablingFilterTooltip=Optional expression for enabling the alarm error=Error enableAlarmFailed=Failed to enable alarm enableAlarms=Enable Alarms +enterNameForDuplicate=Enter name for duplicate +enterNewName=Enter new name guidance=Guidance: headerAlreadyDisabled=All PVs in the selected section are already disabled headerAlreadyEnabled=All PVs in the selected section are already enabled @@ -70,13 +74,22 @@ latch=Latch latchTooltip=Latch alarm until acknowledged? MenuPath=Alarm moveItemFailed=Failed to move item +nameCannotBeEmpty=Name cannot be empty +nodePvsAlreadyExist=The node you're pasting has some PVs which already exist in the config: +nodeAlreadyExists=Node already exists option=Option partlyDisabled2=(Partly disabled) +pasteItem=Paste Item +pasteItemFailed=Failed to paste item path=Path: promptTitle=Absolute Date is set to a point in time in the past +pvAlreadyExists=PV already exists in this config relativeDateLabel=Relative Date: relativeDateTooltip=Select a predefined duration for disabling the alarm removeComponentFailed=Failed to remove component +renameDialogTitle=Rename +renameDialogLabel=Rename: +renameItem=Rename item renameItemFailed=Failed to rename item timer=Timer title=Title