Search in sources :

Example 1 with AppResourceDescriptor

use of org.phoebus.framework.spi.AppResourceDescriptor in project phoebus by ControlSystemStudio.

the class AttachmentsPreviewController method initialize.

@FXML
public void initialize() {
    attachmentListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    attachmentListView.setCellFactory(view -> new AttachmentRow());
    attachmentListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<>() {

        /**
         * Shows preview of selected attachment.
         * @param observable
         * @param oldValue
         * @param newValue
         */
        @Override
        public void changed(ObservableValue<? extends Attachment> observable, Attachment oldValue, Attachment newValue) {
            selectedAttachment.set(newValue);
            showPreview();
        }
    });
    attachmentListView.setOnMouseClicked(me -> {
        if (me.getClickCount() == 2) {
            Attachment attachment = attachmentListView.getSelectionModel().getSelectedItem();
            if (!attachment.getContentType().startsWith("image")) {
                // First try to open the file with an internal Phoebus app
                AppResourceDescriptor defaultApp = ApplicationLauncherService.findApplication(attachment.getFile().toURI(), true, null);
                if (defaultApp != null) {
                    defaultApp.create(attachment.getFile().toURI());
                    return;
                }
                // If not internal apps are found look for external apps
                String fileName = attachment.getFile().getName();
                String[] parts = fileName.split("\\.");
                if (parts.length == 1 || !ApplicationService.getExtensionsHandledByExternalApp().contains(parts[parts.length - 1])) {
                    // If there is no app configured for the file type, show an error message and return.
                    ExceptionDetailsErrorDialog.openError(Messages.PreviewOpenErrorTitle, Messages.PreviewOpenErrorBody, null);
                    return;
                }
            }
            ApplicationLauncherService.openFile(attachment.getFile(), false, null);
        }
    });
    attachmentListView.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<>() {

        /**
         * Notifies listeners of list selection change.
         * @param change
         */
        @Override
        public void onChanged(Change<? extends Attachment> change) {
            selectedAttachments.setAll(change.getList());
            listSelectionChangeListeners.stream().forEach(l -> l.onChanged(change));
        }
    });
    attachmentListView.setOnContextMenuRequested((e) -> {
        ContextMenu contextMenu = new ContextMenu();
        MenuItem menuItem = new MenuItem(Messages.DownloadSelected);
        menuItem.setOnAction(actionEvent -> downloadSelectedAttachments());
        menuItem.disableProperty().bind(Bindings.createBooleanBinding(() -> selectedAttachments.isEmpty(), selectedAttachments));
        contextMenu.getItems().add(menuItem);
        URI selectedResource = !selectedAttachments.isEmpty() ? selectedAttachments.get(0).getFile().toURI() : null;
        if (selectedResource != null) {
            contextMenu.getItems().add(new SeparatorMenuItem());
            final List<AppResourceDescriptor> applications = ApplicationService.getApplications(selectedResource);
            applications.forEach(app -> {
                MenuItem appMenuItem = new MenuItem(app.getDisplayName());
                appMenuItem.setGraphic(ImageCache.getImageView(app.getIconURL()));
                appMenuItem.setOnAction(actionEvent -> app.create(selectedResource));
                contextMenu.getItems().add(appMenuItem);
            });
        }
        attachmentListView.setContextMenu(contextMenu);
    });
    imagePreview.fitWidthProperty().bind(previewPane.widthProperty());
    imagePreview.fitHeightProperty().bind(previewPane.heightProperty());
    imagePreview.hoverProperty().addListener((event) -> {
        if (((ReadOnlyBooleanProperty) event).get()) {
            splitPane.getScene().setCursor(Cursor.HAND);
        } else {
            splitPane.getScene().setCursor(Cursor.DEFAULT);
        }
    });
    imagePreview.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
        if (selectedAttachment.get() != null && selectedAttachment.get().getContentType().startsWith("image")) {
            ApplicationLauncherService.openFile(selectedAttachment.get().getFile(), false, null);
        }
        event.consume();
    });
}
Also used : JobManager(org.phoebus.framework.jobs.JobManager) javafx.scene.control(javafx.scene.control) MouseEvent(javafx.scene.input.MouseEvent) ReadOnlyBooleanProperty(javafx.beans.property.ReadOnlyBooleanProperty) FXCollections(javafx.collections.FXCollections) StackPane(javafx.scene.layout.StackPane) Bindings(javafx.beans.binding.Bindings) ApplicationService(org.phoebus.framework.workbench.ApplicationService) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ListChangeListener(javafx.collections.ListChangeListener) ApplicationLauncherService(org.phoebus.ui.application.ApplicationLauncherService) ImageIO(javax.imageio.ImageIO) URI(java.net.URI) Attachment(org.phoebus.logbook.Attachment) GridPane(javafx.scene.layout.GridPane) DirectoryChooser(javafx.stage.DirectoryChooser) ImageCache(org.phoebus.ui.javafx.ImageCache) BufferedImage(java.awt.image.BufferedImage) Files(java.nio.file.Files) IOException(java.io.IOException) Logger(java.util.logging.Logger) File(java.io.File) FXML(javafx.fxml.FXML) Cursor(javafx.scene.Cursor) List(java.util.List) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) ImageView(javafx.scene.image.ImageView) SwingFXUtils(javafx.embed.swing.SwingFXUtils) AppResourceDescriptor(org.phoebus.framework.spi.AppResourceDescriptor) ObservableValue(javafx.beans.value.ObservableValue) ObservableList(javafx.collections.ObservableList) ChangeListener(javafx.beans.value.ChangeListener) Image(javafx.scene.image.Image) AttachmentsViewController(org.phoebus.logbook.olog.ui.write.AttachmentsViewController) ExceptionDetailsErrorDialog(org.phoebus.ui.dialog.ExceptionDetailsErrorDialog) Attachment(org.phoebus.logbook.Attachment) URI(java.net.URI) AppResourceDescriptor(org.phoebus.framework.spi.AppResourceDescriptor) ReadOnlyBooleanProperty(javafx.beans.property.ReadOnlyBooleanProperty) FXML(javafx.fxml.FXML)

Example 2 with AppResourceDescriptor

use of org.phoebus.framework.spi.AppResourceDescriptor in project phoebus by ControlSystemStudio.

the class PhoebusApplication method findApplication.

/**
 * @param resource Resource
 * @param prompt   Prompt if there are multiple applications, or use first one?
 * @return Application for opening resource, or <code>null</code> if none found
 */
private AppResourceDescriptor findApplication(final URI resource, final boolean prompt) {
    // Does resource request a specific application?
    final String app_name = ResourceParser.getAppName(resource);
    if (app_name != null) {
        final AppDescriptor app = ApplicationService.findApplication(app_name);
        if (app == null) {
            logger.log(Level.WARNING, "Unknown application '" + app_name + "'");
            return null;
        }
        if (app instanceof AppResourceDescriptor)
            return (AppResourceDescriptor) app;
        else {
            logger.log(Level.WARNING, "'" + app_name + "' application does not handle resources");
            return null;
        }
    }
    // Check all applications
    final List<AppResourceDescriptor> applications = ApplicationService.getApplications(resource);
    if (applications.isEmpty()) {
        logger.log(Level.WARNING, "No application found for opening " + resource);
        return null;
    }
    // Only one app?
    if (applications.size() == 1)
        return applications.get(0);
    // Pick default application based on preference setting?
    if (!prompt) {
        for (AppResourceDescriptor app : applications) for (String part : Preferences.default_apps) if (app.getName().contains(part))
            return app;
        // , not just the first one, which may be undefined
        logger.log(Level.WARNING, "No default application found for opening " + resource + ", using first one");
        return applications.get(0);
    }
    // Prompt user which application to use for this resource
    final List<String> options = applications.stream().map(app -> app.getDisplayName()).collect(Collectors.toList());
    final Dialog<String> which = new ListPickerDialog(main_stage.getScene().getRoot(), options, default_application);
    which.setTitle(Messages.OpenTitle);
    which.setHeaderText(Messages.OpenHdr + resource);
    which.setWidth(300);
    which.setHeight(300);
    final Optional<String> result = which.showAndWait();
    if (!result.isPresent())
        return null;
    default_application = result.get();
    return applications.get(options.indexOf(result.get()));
}
Also used : Button(javafx.scene.control.Button) JobManager(org.phoebus.framework.jobs.JobManager) Arrays(java.util.Arrays) CheckMenuItem(javafx.scene.control.CheckMenuItem) DockPaneListener(org.phoebus.ui.docking.DockPaneListener) VBox(javafx.scene.layout.VBox) KeyCombination(javafx.scene.input.KeyCombination) Preferences(org.phoebus.ui.Preferences) Application(javafx.application.Application) FullScreenAction(org.phoebus.ui.javafx.FullScreenAction) MenuTreeNode(org.phoebus.ui.application.MenuEntryService.MenuTreeNode) AlertType(javafx.scene.control.Alert.AlertType) Map(java.util.Map) DockPane(org.phoebus.ui.docking.DockPane) URI(java.net.URI) Alert(javafx.scene.control.Alert) MenuEntry(org.phoebus.ui.spi.MenuEntry) ImageCache(org.phoebus.ui.javafx.ImageCache) ResponsivenessMonitor(org.phoebus.ui.monitoring.ResponsivenessMonitor) MenuItem(javafx.scene.control.MenuItem) OpenHelp(org.phoebus.ui.help.OpenHelp) Collection(java.util.Collection) AppDescriptor(org.phoebus.framework.spi.AppDescriptor) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Platform(javafx.application.Platform) SeparatorMenuItem(javafx.scene.control.SeparatorMenuItem) List(java.util.List) SubJobMonitor(org.phoebus.framework.jobs.SubJobMonitor) MenuButton(javafx.scene.control.MenuButton) AppResourceDescriptor(org.phoebus.framework.spi.AppResourceDescriptor) Optional(java.util.Optional) BorderPane(javafx.scene.layout.BorderPane) Welcome(org.phoebus.ui.welcome.Welcome) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) DialogHelper(org.phoebus.ui.dialog.DialogHelper) ButtonType(javafx.scene.control.ButtonType) MouseEvent(javafx.scene.input.MouseEvent) JobMonitor(org.phoebus.framework.jobs.JobMonitor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AuthorizationService(org.phoebus.security.authorization.AuthorizationService) ApplicationService(org.phoebus.framework.workbench.ApplicationService) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) StatusBar(org.phoebus.ui.statusbar.StatusBar) ResourceParser(org.phoebus.framework.util.ResourceParser) OpenFileDialog(org.phoebus.ui.dialog.OpenFileDialog) WeakReference(java.lang.ref.WeakReference) Tooltip(javafx.scene.control.Tooltip) DockItemWithInput(org.phoebus.ui.docking.DockItemWithInput) MementoHelper(org.phoebus.ui.internal.MementoHelper) KeyCode(javafx.scene.input.KeyCode) ListPickerDialog(org.phoebus.ui.dialog.ListPickerDialog) Dialog(javafx.scene.control.Dialog) MenuBar(javafx.scene.control.MenuBar) PlatformInfo(org.phoebus.ui.javafx.PlatformInfo) Iterator(java.util.Iterator) ToolBar(javafx.scene.control.ToolBar) DockStage(org.phoebus.ui.docking.DockStage) Node(javafx.scene.Node) MementoTree(org.phoebus.framework.persistence.MementoTree) DockItem(org.phoebus.ui.docking.DockItem) OpenAbout(org.phoebus.ui.help.OpenAbout) FileInputStream(java.io.FileInputStream) File(java.io.File) Menu(javafx.scene.control.Menu) KeyCodeCombination(javafx.scene.input.KeyCodeCombination) TimeUnit(java.util.concurrent.TimeUnit) ActionEvent(javafx.event.ActionEvent) Stage(javafx.stage.Stage) XMLMementoTree(org.phoebus.framework.persistence.XMLMementoTree) ImageView(javafx.scene.image.ImageView) Window(javafx.stage.Window) Locations(org.phoebus.framework.workbench.Locations) Comparator(java.util.Comparator) Image(javafx.scene.image.Image) AppDescriptor(org.phoebus.framework.spi.AppDescriptor) AppResourceDescriptor(org.phoebus.framework.spi.AppResourceDescriptor) ListPickerDialog(org.phoebus.ui.dialog.ListPickerDialog)

Example 3 with AppResourceDescriptor

use of org.phoebus.framework.spi.AppResourceDescriptor in project phoebus by ControlSystemStudio.

the class PhoebusApplication method openResource.

/**
 * @param resource Resource received as command line argument
 * @param prompt   Prompt if there are multiple applications, or use first one?
 */
private void openResource(final URI resource, final boolean prompt) {
    final AppResourceDescriptor application = findApplication(resource, prompt);
    if (application == null)
        return;
    final String query = resource.getQuery();
    if (query != null) {
        // Query could contain _anything_, to be used by the application.
        // Perform a simplistic search for "target=window" or "target=pane_name".
        final int i = query.indexOf("target=");
        if (i >= 0) {
            int end = query.indexOf('&', i + 7);
            if (end < 0)
                end = query.length();
            final String target = query.substring(i + 7, end);
            if (!target.equals("window")) {
                // Should the new panel open in a specific, named pane?
                final DockPane existing = DockStage.getDockPaneByName(target);
                if (existing != null)
                    DockPane.setActiveDockPane(existing);
                else {
                    // Open new Stage with pane for that name
                    final Stage new_stage = new Stage();
                    DockStage.configureStage(new_stage);
                    new_stage.show();
                    DockPane.getActiveDockPane().setName(target);
                }
            }
        }
    }
    logger.log(Level.INFO, "Opening " + resource + " with " + application.getName());
    application.create(resource);
}
Also used : DockPane(org.phoebus.ui.docking.DockPane) DockStage(org.phoebus.ui.docking.DockStage) Stage(javafx.stage.Stage) AppResourceDescriptor(org.phoebus.framework.spi.AppResourceDescriptor)

Example 4 with AppResourceDescriptor

use of org.phoebus.framework.spi.AppResourceDescriptor in project phoebus by ControlSystemStudio.

the class MementoHelper method restoreApplication.

private static void restoreApplication(final MementoTree item_memento, final DockPane pane, final AppDescriptor app) {
    DockPane.setActiveDockPane(pane);
    final AppInstance instance;
    if (app instanceof AppResourceDescriptor) {
        final AppResourceDescriptor app_res = (AppResourceDescriptor) app;
        final String input = item_memento.getString(INPUT_URI).orElse(null);
        instance = input == null ? app_res.create() : app_res.create(URI.create(input));
    } else
        instance = app.create();
    instance.restore(item_memento);
}
Also used : AppInstance(org.phoebus.framework.spi.AppInstance) AppResourceDescriptor(org.phoebus.framework.spi.AppResourceDescriptor)

Example 5 with AppResourceDescriptor

use of org.phoebus.framework.spi.AppResourceDescriptor in project phoebus by ControlSystemStudio.

the class ApplicationLauncherService method findApplication.

/**
 * Locate suitable application
 *
 * @param resource
 *            Resource {@link URI}
 * @param prompt
 *            Prompt if there are multiple applications, or use first one?
 * @param stage
 *            If prompt is enabled, a selection dialog will be launched
 *            positioned next to the provided stage. If <code>null</code> then the
 *            default or first application will be used
 * @return Application for opening resource, or <code>null</code> if none
 *         found
 */
public static AppResourceDescriptor findApplication(final URI resource, final boolean prompt, final Stage stage) {
    // Does resource request a specific application?
    final String app_name = ResourceParser.getAppName(resource);
    if (app_name != null) {
        final AppDescriptor app = ApplicationService.findApplication(app_name);
        if (app == null) {
            logger.log(Level.WARNING, "Unknown application '" + app_name + "'");
            return null;
        }
        if (app instanceof AppResourceDescriptor)
            return (AppResourceDescriptor) app;
        else {
            logger.log(Level.WARNING, "'" + app_name + "' application does not handle resources");
            return null;
        }
    }
    // Check all applications
    final List<AppResourceDescriptor> applications = ApplicationService.getApplications(resource);
    if (applications.isEmpty()) {
        logger.log(Level.INFO, "No application found for opening " + resource);
        return null;
    }
    // Only one app?
    if (applications.size() == 1)
        return applications.get(0);
    // Pick default application based on preference setting?
    if (!prompt || stage == null) {
        for (AppResourceDescriptor app : applications) for (String part : Preferences.default_apps) if (app.getName().contains(part))
            return app;
        // , not just the first one, which may be undefined
        final AppResourceDescriptor first = applications.get(0);
        logger.log(Level.WARNING, "No default application found for opening " + resource + ", using first one, \"" + first.getDisplayName() + "\"");
        return first;
    }
    // Prompt user which application to use for this resource
    final List<String> options = applications.stream().map(app -> app.getDisplayName()).collect(Collectors.toList());
    final Dialog<String> which = new ListPickerDialog(stage.getScene().getRoot(), options, null);
    which.setTitle(Messages.OpenTitle);
    which.setHeaderText(Messages.OpenHdr + resource);
    which.setWidth(300);
    which.setHeight(300);
    final Optional<String> result = which.showAndWait();
    if (!result.isPresent())
        return null;
    return applications.get(options.indexOf(result.get()));
}
Also used : ListPickerDialog(org.phoebus.ui.dialog.ListPickerDialog) Dialog(javafx.scene.control.Dialog) AppDescriptor(org.phoebus.framework.spi.AppDescriptor) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) ApplicationService(org.phoebus.framework.workbench.ApplicationService) File(java.io.File) Level(java.util.logging.Level) Preferences(org.phoebus.ui.Preferences) List(java.util.List) Stage(javafx.stage.Stage) ResourceParser(org.phoebus.framework.util.ResourceParser) AppResourceDescriptor(org.phoebus.framework.spi.AppResourceDescriptor) Optional(java.util.Optional) URI(java.net.URI) AppDescriptor(org.phoebus.framework.spi.AppDescriptor) AppResourceDescriptor(org.phoebus.framework.spi.AppResourceDescriptor) ListPickerDialog(org.phoebus.ui.dialog.ListPickerDialog)

Aggregations

AppResourceDescriptor (org.phoebus.framework.spi.AppResourceDescriptor)12 URI (java.net.URI)7 ImageView (javafx.scene.image.ImageView)7 File (java.io.File)6 Logger (java.util.logging.Logger)5 MenuItem (javafx.scene.control.MenuItem)5 List (java.util.List)4 Level (java.util.logging.Level)4 SeparatorMenuItem (javafx.scene.control.SeparatorMenuItem)4 Image (javafx.scene.image.Image)4 FileInputStream (java.io.FileInputStream)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 FXML (javafx.fxml.FXML)3 Stage (javafx.stage.Stage)3 ApplicationService (org.phoebus.framework.workbench.ApplicationService)3 IOException (java.io.IOException)2 Optional (java.util.Optional)2 Dialog (javafx.scene.control.Dialog)2