use of org.phoebus.framework.spi.AppResourceDescriptor in project phoebus by ControlSystemStudio.
the class FileBrowserController method createContextMenu.
@FXML
public void createContextMenu(ContextMenuEvent e) {
final ObservableList<TreeItem<FileInfo>> selectedItems = treeView.selectionModelProperty().getValue().getSelectedItems();
contextMenu.getItems().clear();
if (selectedItems.isEmpty()) {
// Create directory at root
contextMenu.getItems().addAll(new CreateDirectoryAction(treeView, treeView.getRoot()));
// Paste files at root
if (Clipboard.getSystemClipboard().hasFiles())
contextMenu.getItems().addAll(new PasteFiles(treeView, treeView.getRoot()));
} else {
// allMatch() would return true for empty, so only check if there are items
if (selectedItems.stream().allMatch(item -> item.isLeaf()))
contextMenu.getItems().add(open);
// If just one entry selected, check if there are multiple apps from which to select
if (selectedItems.size() == 1) {
final File file = selectedItems.get(0).getValue().file;
final URI resource = ResourceParser.getURI(file);
final List<AppResourceDescriptor> applications = ApplicationService.getApplications(resource);
if (applications.size() > 0) {
openWith.getItems().clear();
for (AppResourceDescriptor app : applications) {
final MenuItem open_app = new MenuItem(app.getDisplayName());
final URL icon_url = app.getIconURL();
if (icon_url != null)
open_app.setGraphic(new ImageView(icon_url.toExternalForm()));
open_app.setOnAction(event -> app.create(resource));
openWith.getItems().add(open_app);
}
contextMenu.getItems().add(openWith);
}
if (file.isDirectory()) {
contextMenu.getItems().add(new SetBaseDirectory(file, this::setRoot));
contextMenu.getItems().add(new SeparatorMenuItem());
}
SelectionService.getInstance().setSelection(this, Arrays.asList(file));
List<ContextMenuEntry> supported = ContextMenuService.getInstance().listSupportedContextMenuEntries();
supported.stream().forEach(action -> {
MenuItem menuItem = new MenuItem(action.getName(), new ImageView(action.getIcon()));
menuItem.setOnAction((ee) -> {
try {
action.call(SelectionService.getInstance().getSelection());
} catch (Exception ex) {
logger.log(Level.WARNING, "Failed to execute " + action.getName() + " from file browser.", ex);
}
});
contextMenu.getItems().add(menuItem);
});
if (!supported.isEmpty()) {
contextMenu.getItems().add(new SeparatorMenuItem());
}
}
contextMenu.getItems().add(new CopyPath(selectedItems));
contextMenu.getItems().add(new SeparatorMenuItem());
}
if (selectedItems.size() >= 1) {
final TreeItem<FileInfo> item = selectedItems.get(0);
final boolean is_file = item.isLeaf();
if (selectedItems.size() == 1) {
if (is_file) {
// Create directory within the _parent_
contextMenu.getItems().addAll(new CreateDirectoryAction(treeView, item.getParent()));
// Plain file can be duplicated, directory can't
contextMenu.getItems().add(new DuplicateAction(treeView, item));
} else
// Within a directory, a new directory can be created
contextMenu.getItems().addAll(new CreateDirectoryAction(treeView, item));
contextMenu.getItems().addAll(new RenameAction(treeView, selectedItems.get(0)));
if (Clipboard.getSystemClipboard().hasFiles())
contextMenu.getItems().addAll(new PasteFiles(treeView, selectedItems.get(0)));
}
contextMenu.getItems().add(new DeleteAction(treeView, selectedItems));
contextMenu.getItems().add(new SeparatorMenuItem());
if (is_file)
contextMenu.getItems().add(new RefreshAction(treeView, item.getParent()));
else
contextMenu.getItems().add(new RefreshAction(treeView, item));
}
if (selectedItems.size() == 1) {
contextMenu.getItems().addAll(new PropertiesAction(treeView, selectedItems.get(0)));
}
contextMenu.show(treeView.getScene().getWindow(), e.getScreenX(), e.getScreenY());
}
use of org.phoebus.framework.spi.AppResourceDescriptor in project phoebus by ControlSystemStudio.
the class DisplayNavigationViewController method createContextMenu.
private void createContextMenu(ContextMenuEvent e, final List<File> selectedItems, Control control) {
contextMenu.getItems().clear();
if (!selectedItems.isEmpty()) {
open.setOnAction(event -> {
selectedItems.forEach(item -> {
openResource(item, null);
});
});
copy.setOnAction(event -> {
final ClipboardContent content = new ClipboardContent();
content.putString(selectedItems.stream().map(f -> {
return f.getPath();
}).collect(Collectors.joining(System.getProperty("line.separator"))));
Clipboard.getSystemClipboard().setContent(content);
});
contextMenu.getItems().add(copy);
contextMenu.getItems().add(open);
}
// If just one entry selected, check if there are multiple apps from which to select
if (selectedItems.size() == 1) {
final File file = selectedItems.get(0);
final URI resource = ResourceParser.getURI(file);
final List<AppResourceDescriptor> applications = ApplicationService.getApplications(resource);
if (applications.size() > 0) {
openWith.getItems().clear();
for (AppResourceDescriptor app : applications) {
final MenuItem open_app = new MenuItem(app.getDisplayName());
final URL icon_url = app.getIconURL();
if (icon_url != null)
open_app.setGraphic(new ImageView(icon_url.toExternalForm()));
open_app.setOnAction(event -> app.create(resource));
openWith.getItems().add(open_app);
}
contextMenu.getItems().add(openWith);
}
}
contextMenu.show(control.getScene().getWindow(), e.getScreenX(), e.getScreenY());
}
use of org.phoebus.framework.spi.AppResourceDescriptor in project phoebus by ControlSystemStudio.
the class ContextMenuSupport method fillMenu.
/**
* Fill context menu with items for widget
*
* @param node
* @param widget
*/
private void fillMenu(final Node node, final Widget widget) {
final ObservableList<MenuItem> items = menu.getItems();
items.setAll(new WidgetInfoAction(widget));
// launch the info dialog for the entire OPI.
try {
Widget topWidget = widget.getTopDisplayModel();
if (!topWidget.equals(widget)) {
items.add(new SeparatorMenuItem());
items.add(new WidgetInfoAction(widget.getTopDisplayModel()));
}
} catch (Exception exception) {
logger.log(Level.WARNING, "Unable to get top display model", exception);
}
// Widget actions
for (ActionInfo info : widget.propActions().getValue().getActions()) {
if (info.getType() == ActionType.OPEN_DISPLAY) {
final OpenDisplayActionInfo open_info = (OpenDisplayActionInfo) info;
// Expand macros in action description
String desc;
try {
desc = MacroHandler.replace(widget.getEffectiveMacros(), open_info.getDescription());
} catch (Exception ex) {
logger.log(Level.WARNING, "Cannot expand macros in action description '" + open_info.getDescription() + "'", ex);
desc = open_info.getDescription();
}
// Add the requested target as default
final Target requestedTarget = open_info.getTarget();
items.add(createMenuItem(widget, new OpenDisplayActionInfo(desc, open_info.getFile(), open_info.getMacros(), requestedTarget)));
// Add variant for all the available Target types: Replace, new Tab, ...
for (Target target : Target.values()) {
if (target == Target.STANDALONE || target == requestedTarget)
continue;
// Mention non-default targets in the description
items.add(createMenuItem(widget, new OpenDisplayActionInfo(desc + " (" + target + ")", open_info.getFile(), open_info.getMacros(), target)));
}
} else
items.add(createMenuItem(widget, info));
}
// Actions of the widget runtime
final WidgetRuntime<Widget> runtime = RuntimeUtil.getRuntime(widget);
if (runtime == null)
throw new NullPointerException("Missing runtime for " + widget);
for (RuntimeAction info : runtime.getRuntimeActions()) {
// Load image for action from that action's class loader
final ImageView icon = ImageCache.getImageView(info.getClass(), info.getIconPath());
final MenuItem item = new MenuItem(info.getDescription(), icon);
item.setOnAction(event -> info.run());
items.add(item);
}
items.add(new SeparatorMenuItem());
// Does widget have a PV name?
final Optional<WidgetProperty<String>> name_prop = widget.checkProperty(CommonWidgetProperties.propPVName);
List<ProcessVariable> processVariables;
if (name_prop.isPresent()) {
processVariables = List.of(new ProcessVariable(name_prop.get().getValue()));
} else {
// Add all PVs referenced by the widget.
Collection<RuntimePV> runtimePvs = runtime.getPVs();
processVariables = runtimePvs.stream().map(runtimePV -> new ProcessVariable(runtimePV.getName())).collect(Collectors.toList());
}
if (!processVariables.isEmpty()) {
// Set the 'selection' to the PV of this widget
SelectionService.getInstance().setSelection(DisplayRuntimeApplication.NAME, processVariables);
// Add PV-based menu entries
ContextMenuHelper.addSupportedEntries(node, menu);
items.add(new SeparatorMenuItem());
}
// If toolbar is hidden, offer forward/backward navigation
if (!instance.isToolbarVisible()) {
boolean navigate = false;
final DisplayNavigation navigation = instance.getNavigation();
if (navigation.getBackwardDisplays().size() > 0) {
final MenuItem item = new MenuItem(Messages.NavigateBack_TT, new ImageView(NavigationAction.backward));
item.setOnAction(event -> instance.loadDisplayFile(navigation.goBackward(1)));
items.add(item);
navigate = true;
}
if (navigation.getForwardDisplays().size() > 0) {
final MenuItem item = new MenuItem(Messages.NavigateForward_TT, new ImageView(NavigationAction.forward));
item.setOnAction(event -> instance.loadDisplayFile(navigation.goForward(1)));
items.add(item);
navigate = true;
}
if (navigate)
items.add(new SeparatorMenuItem());
}
final Parent model_parent = instance.getRepresentation().getModelParent();
items.add(new PrintAction(model_parent));
items.add(new SaveSnapshotAction(model_parent));
try {
final DisplayModel model = widget.getDisplayModel();
// Add context menu actions based on the selection (i.e. email, logbook, etc...)
final Selection originalSelection = SelectionService.getInstance().getSelection();
final List<SelectionInfo> newSelection = Arrays.asList(SelectionInfo.forModel(model, model_parent));
SelectionService.getInstance().setSelection(DisplayRuntimeApplication.NAME, newSelection);
List<ContextMenuEntry> supported = ContextMenuService.getInstance().listSupportedContextMenuEntries();
supported.stream().forEach(action -> {
MenuItem menuItem = new MenuItem(action.getName(), new ImageView(action.getIcon()));
menuItem.setOnAction((e) -> {
try {
SelectionService.getInstance().setSelection(DisplayRuntimeApplication.NAME, newSelection);
action.call(model_parent, SelectionService.getInstance().getSelection());
} catch (Exception ex) {
logger.log(Level.WARNING, "Failed to execute " + action.getName() + " from display builder.", ex);
}
});
items.add(menuItem);
});
SelectionService.getInstance().setSelection(DisplayRuntimeApplication.NAME, originalSelection);
} catch (Exception ex) {
logger.log(Level.WARNING, "Failed to construct context menu actions", ex);
}
items.add(new SeparatorMenuItem());
items.add(new DisplayToolbarAction(instance));
// If the editor is available, add "Open in Editor"
final AppResourceDescriptor editor = ApplicationService.findApplication("display_editor");
if (editor != null && AuthorizationService.hasAuthorization("edit_display"))
items.add(new OpenInEditorAction(editor, widget));
items.add(new SeparatorMenuItem());
items.add(new ReloadDisplayAction(instance));
}
use of org.phoebus.framework.spi.AppResourceDescriptor in project phoebus by ControlSystemStudio.
the class LogPropertiesController method initialize.
@FXML
public void initialize() {
// Read the attribute types
String url = LogbookUIPreferences.log_attribute_desc;
if (url.isEmpty()) {
final URL resource = getClass().getResource("log_property_attributes.properties");
url = resource.toExternalForm();
}
try (InputStream input = new URL(url).openStream()) {
Properties prop = new Properties();
prop.load(input);
prop.stringPropertyNames().stream().forEach((p) -> {
attributeTypes.put(p.toLowerCase(), prop.getProperty(p).toLowerCase());
});
} catch (IOException ex) {
ex.printStackTrace();
}
// create the property trees
name.setMaxWidth(1f * Integer.MAX_VALUE * 40);
name.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<PropertyTreeNode, String>, ObservableValue<String>>() {
public ObservableValue<String> call(TreeTableColumn.CellDataFeatures<PropertyTreeNode, String> p) {
return p.getValue().getValue().nameProperty();
}
});
value.setMaxWidth(1f * Integer.MAX_VALUE * 60);
value.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<PropertyTreeNode, ?>, SimpleObjectProperty<PropertyTreeNode>>() {
public SimpleObjectProperty<PropertyTreeNode> call(TreeTableColumn.CellDataFeatures<PropertyTreeNode, ?> p) {
return p.getValue().getValue().nodeProperty();
}
});
value.setEditable(editable.getValue());
value.setCellFactory(new Callback<TreeTableColumn<PropertyTreeNode, Object>, TreeTableCell<PropertyTreeNode, Object>>() {
@Override
public TreeTableCell<PropertyTreeNode, Object> call(TreeTableColumn<PropertyTreeNode, Object> param) {
return new TreeTableCell<>() {
private TextField textField;
@Override
public void startEdit() {
super.startEdit();
if (textField == null) {
createTextField();
}
setText(null);
setGraphic(textField);
textField.selectAll();
}
@Override
public void cancelEdit() {
super.cancelEdit();
setText(((PropertyTreeNode) getItem()).getValue());
setGraphic(getTreeTableRow().getGraphic());
}
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
PropertyTreeNode propertyItem = ((PropertyTreeNode) item);
if (attributeTypes.containsKey(propertyItem.getFullQualifiedName().toLowerCase())) {
switch(attributeTypes.get(propertyItem.getFullQualifiedName().toLowerCase())) {
case "url":
final String url = propertyItem.getValue();
final URI uri = URI.create(url);
final Hyperlink urlLink = new Hyperlink(propertyItem.getValue());
// TODO open url in some webclient
urlLink.setOnContextMenuRequested((e) -> {
ContextMenu contextMenu = new ContextMenu();
final List<AppResourceDescriptor> applications = ApplicationService.getApplications(uri);
applications.forEach(app -> {
MenuItem menuItem = new MenuItem(app.getDisplayName());
menuItem.setGraphic(ImageCache.getImageView(app.getIconURL()));
menuItem.setOnAction(actionEvent -> app.create(uri));
contextMenu.getItems().add(menuItem);
});
contextMenu.getItems().add(new SeparatorMenuItem());
MenuItem copyMenuItem = new MenuItem("copy", copy);
copyMenuItem.setOnAction(event -> {
final ClipboardContent content = new ClipboardContent();
content.putString(uri.toString());
Clipboard.getSystemClipboard().setContent(content);
});
contextMenu.getItems().add(copyMenuItem);
urlLink.setContextMenu(contextMenu);
});
setGraphic(urlLink);
break;
case "resource":
final String resourceURL = propertyItem.getValue();
final URI resource = URI.create(resourceURL);
final Hyperlink resourceLink = new Hyperlink(resourceURL);
setGraphic(resourceLink);
// Open resource using the default application
resourceLink.setOnAction((e) -> {
final List<AppResourceDescriptor> applications = ApplicationService.getApplications(resource);
if (!applications.isEmpty()) {
applications.get(0).create(resource);
}
});
resourceLink.setOnContextMenuRequested((e) -> {
ContextMenu contextMenu = new ContextMenu();
final List<AppResourceDescriptor> applications = ApplicationService.getApplications(resource);
applications.forEach(app -> {
MenuItem menuItem = new MenuItem(app.getDisplayName());
menuItem.setGraphic(ImageCache.getImageView(app.getIconURL()));
menuItem.setOnAction(actionEvent -> app.create(resource));
contextMenu.getItems().add(menuItem);
});
contextMenu.getItems().add(new SeparatorMenuItem());
MenuItem copyMenuItem = new MenuItem("copy", copy);
copyMenuItem.setOnAction(event -> {
final ClipboardContent content = new ClipboardContent();
content.putString(resource.toString());
Clipboard.getSystemClipboard().setContent(content);
});
contextMenu.getItems().add(copyMenuItem);
resourceLink.setContextMenu(contextMenu);
});
break;
default:
setGraphic(new Label(propertyItem.getValue()));
}
} else {
setGraphic(new Label(propertyItem.getValue()));
}
}
}
private void createTextField() {
textField = new TextField(getString());
textField.setOnKeyReleased((KeyEvent t) -> {
if (t.getCode() == KeyCode.ENTER) {
((PropertyTreeNode) getItem()).setValue(textField.getText());
commitEdit(getItem());
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
});
}
private String getString() {
if (getItem() != null && ((PropertyTreeNode) getItem()).getValue() != null) {
return ((PropertyTreeNode) getItem()).getValue();
} else {
return "";
}
}
};
}
});
editable.addListener((observable, oldValue, newValue) -> {
value.setEditable(newValue);
});
// Hide the headers
treeTableView.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
// Get the table header
Pane header = (Pane) treeTableView.lookup("TableHeaderRow");
if (header != null && header.isVisible()) {
header.setMaxHeight(0);
header.setMinHeight(0);
header.setPrefHeight(0);
header.setVisible(false);
header.setManaged(false);
}
}
});
}
use of org.phoebus.framework.spi.AppResourceDescriptor in project phoebus by ControlSystemStudio.
the class PhoebusApplication method createTopResourcesMenu.
/**
* Fill the {@link #top_resources_menu} and {@link #top_resources_button}
*/
private void createTopResourcesMenu() {
// Create top resources menu items off UI thread
JobManager.schedule("Get top resources", monitor -> {
final TopResources tops = TopResources.parse(Preferences.top_resources);
final int N = tops.size();
if (N <= 0)
return;
final MenuItem[] menu_items = new MenuItem[N];
final MenuItem[] toolbar_items = new MenuItem[N];
for (int i = 0; i < N; ++i) {
final String description = tops.getDescription(i);
final URI resource = tops.getResource(i);
menu_items[i] = new MenuItem(description);
menu_items[i].setOnAction(event -> openResource(resource, false));
toolbar_items[i] = new MenuItem(description);
toolbar_items[i].setOnAction(event -> openResource(resource, false));
// Lookup application icon
final AppResourceDescriptor application = findApplication(resource, false);
if (application != null) {
final Image icon = ImageCache.getImage(application.getIconURL());
if (icon != null) {
menu_items[i].setGraphic(new ImageView(icon));
toolbar_items[i].setGraphic(new ImageView(icon));
}
}
}
// Back to UI thread to hook into menu
Platform.runLater(() -> {
top_resources_menu.getItems().setAll(menu_items);
top_resources_menu.setDisable(false);
top_resources_button.getItems().setAll(toolbar_items);
top_resources_button.setDisable(false);
});
});
}
Aggregations