Search in sources :

Example 26 with ShortcutDTO

use of org.phoenicis.library.dto.ShortcutDTO in project POL-POM-5 by PlayOnLinux.

the class ShortcutManager method updateShortcut.

public void updateShortcut(ShortcutDTO shortcutDTO) {
    final String baseName = shortcutDTO.getId();
    final File shortcutDirectory = new File(this.shortcutDirectory);
    // backup icon if it didn't change (deleteShortcut will delete it -> icon lost after shortcut update)
    final File iconFile = new File(shortcutDirectory, baseName + ".icon");
    final File iconBackup = new File(shortcutDirectory, baseName + ".icon_backup");
    final URI shortcutIcon = shortcutDTO.getIcon();
    if (shortcutIcon != null && shortcutIcon.getPath() != null) {
        final boolean keepIcon = shortcutIcon.getPath().equals(iconFile.getPath());
        if (keepIcon) {
            try {
                Files.move(iconFile.toPath(), iconBackup.toPath());
                shortcutDTO = new ShortcutDTO.Builder(shortcutDTO).withIcon(iconBackup.toURI()).build();
            } catch (IOException e) {
                LOGGER.error("Could not backup icon.", e);
            }
        }
    }
    // backup category icon if it didn't change (deleteShortcut will delete it -> icon lost after shortcut update)
    final File categoryIconFile = new File(shortcutDirectory, baseName + "Category.icon");
    final File categoryIconBackup = new File(shortcutDirectory, baseName + "Category.icon_backup");
    final URI shortcutCategoryIcon = shortcutDTO.getCategoryIcon();
    if (shortcutCategoryIcon != null && shortcutCategoryIcon.getPath() != null) {
        final boolean keepIcon = shortcutCategoryIcon.getPath().equals(categoryIconFile.getPath());
        if (keepIcon) {
            try {
                Files.move(categoryIconFile.toPath(), categoryIconBackup.toPath());
                shortcutDTO = new ShortcutDTO.Builder(shortcutDTO).withCategoryIcon(categoryIconBackup.toURI()).build();
            } catch (IOException e) {
                LOGGER.error("Could not backup category icon.", e);
            }
        }
    }
    // backup miniature if it didn't change (deleteShortcut will delete it -> miniature lost after shortcut update)
    final File miniatureFile = new File(shortcutDirectory, baseName + ".miniature");
    final File miniatureBackup = new File(shortcutDirectory, baseName + ".miniature_backup");
    final URI shortcutMiniature = shortcutDTO.getMiniature();
    if (shortcutMiniature != null && shortcutMiniature.getPath() != null) {
        final boolean keepMiniature = shortcutMiniature.getPath().equals(miniatureFile.getPath());
        if (keepMiniature) {
            try {
                Files.move(miniatureFile.toPath(), miniatureBackup.toPath());
                shortcutDTO = new ShortcutDTO.Builder(shortcutDTO).withMiniature(miniatureBackup.toURI()).build();
            } catch (IOException e) {
                LOGGER.error("Could not backup miniature.", e);
            }
        }
    }
    deleteShortcut(shortcutDTO);
    createShortcut(shortcutDTO);
    // delete backups
    if (iconBackup.exists()) {
        iconBackup.delete();
    }
    if (miniatureBackup.exists()) {
        miniatureBackup.delete();
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) URI(java.net.URI) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO)

Example 27 with ShortcutDTO

use of org.phoenicis.library.dto.ShortcutDTO in project POL-POM-5 by PhoenicisOrg.

the class ShortcutManager method updateShortcut.

public void updateShortcut(ShortcutDTO shortcutDTO) {
    final String baseName = shortcutDTO.getId();
    final File shortcutDirectory = new File(this.shortcutDirectory);
    // backup icon if it didn't change (deleteShortcut will delete it -> icon lost after shortcut update)
    final File iconFile = new File(shortcutDirectory, baseName + ".icon");
    final File iconBackup = new File(shortcutDirectory, baseName + ".icon_backup");
    final URI shortcutIcon = shortcutDTO.getIcon();
    if (shortcutIcon != null && shortcutIcon.getPath() != null) {
        final boolean keepIcon = shortcutIcon.getPath().equals(iconFile.getPath());
        if (keepIcon) {
            try {
                Files.move(iconFile.toPath(), iconBackup.toPath());
                shortcutDTO = new ShortcutDTO.Builder(shortcutDTO).withIcon(iconBackup.toURI()).build();
            } catch (IOException e) {
                LOGGER.error("Could not backup icon.", e);
            }
        }
    }
    // backup category icon if it didn't change (deleteShortcut will delete it -> icon lost after shortcut update)
    final File categoryIconFile = new File(shortcutDirectory, baseName + "Category.icon");
    final File categoryIconBackup = new File(shortcutDirectory, baseName + "Category.icon_backup");
    final URI shortcutCategoryIcon = shortcutDTO.getCategoryIcon();
    if (shortcutCategoryIcon != null && shortcutCategoryIcon.getPath() != null) {
        final boolean keepIcon = shortcutCategoryIcon.getPath().equals(categoryIconFile.getPath());
        if (keepIcon) {
            try {
                Files.move(categoryIconFile.toPath(), categoryIconBackup.toPath());
                shortcutDTO = new ShortcutDTO.Builder(shortcutDTO).withCategoryIcon(categoryIconBackup.toURI()).build();
            } catch (IOException e) {
                LOGGER.error("Could not backup category icon.", e);
            }
        }
    }
    // backup miniature if it didn't change (deleteShortcut will delete it -> miniature lost after shortcut update)
    final File miniatureFile = new File(shortcutDirectory, baseName + ".miniature");
    final File miniatureBackup = new File(shortcutDirectory, baseName + ".miniature_backup");
    final URI shortcutMiniature = shortcutDTO.getMiniature();
    if (shortcutMiniature != null && shortcutMiniature.getPath() != null) {
        final boolean keepMiniature = shortcutMiniature.getPath().equals(miniatureFile.getPath());
        if (keepMiniature) {
            try {
                Files.move(miniatureFile.toPath(), miniatureBackup.toPath());
                shortcutDTO = new ShortcutDTO.Builder(shortcutDTO).withMiniature(miniatureBackup.toURI()).build();
            } catch (IOException e) {
                LOGGER.error("Could not backup miniature.", e);
            }
        }
    }
    deleteShortcut(shortcutDTO);
    createShortcut(shortcutDTO);
    // delete backups
    if (iconBackup.exists()) {
        iconBackup.delete();
    }
    if (miniatureBackup.exists()) {
        miniatureBackup.delete();
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) URI(java.net.URI) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO)

Example 28 with ShortcutDTO

use of org.phoenicis.library.dto.ShortcutDTO in project POL-POM-5 by PhoenicisOrg.

the class ShortcutEditingPanelSkin method updateProperties.

/**
 * Updates the shortcutProperties of the shortcut in the given {@link GridPane propertiesGrid}
 *
 * @param propertiesGrid The shortcutProperties grid
 */
private void updateProperties(final GridPane propertiesGrid) {
    propertiesGrid.getChildren().clear();
    // add miniature
    final Label miniatureLabel = new Label(tr("Miniature:"));
    miniatureLabel.getStyleClass().add("captionTitle");
    GridPane.setValignment(miniatureLabel, VPos.TOP);
    final TextField miniaturePathField = new TextField(Optional.ofNullable(getControl().getShortcut()).map(ShortcutDTO::getMiniature).map(URI::getPath).orElse(""));
    HBox.setHgrow(miniaturePathField, Priority.ALWAYS);
    final Button openBrowser = new Button(tr("Browse..."));
    openBrowser.setOnAction(event -> {
        final URI miniatureURI = Optional.ofNullable(getControl().getShortcut()).map(ShortcutDTO::getMiniature).orElseThrow(() -> new IllegalStateException("The shortcut is null"));
        final FileChooser chooser = new FileChooser();
        chooser.setSelectedExtensionFilter(new FileChooser.ExtensionFilter(tr("Images"), "*.miniature, *.png"));
        final File defaultFile = new File(miniatureURI);
        chooser.setInitialDirectory(defaultFile.getParentFile());
        Optional.ofNullable(chooser.showOpenDialog(getControl().getScene().getWindow())).ifPresent(newMiniature -> {
            miniaturePathField.setText(newMiniature.toString());
            getControl().setShortcut(new ShortcutDTO.Builder(getControl().getShortcut()).withMiniature(newMiniature.toURI()).build());
        });
    });
    final HBox miniatureContainer = new HBox(miniaturePathField, openBrowser);
    propertiesGrid.addRow(0, miniatureLabel, miniatureContainer);
    for (Map.Entry<String, Object> entry : shortcutProperties.entrySet()) {
        final int row = propertiesGrid.getRowCount();
        if (!"environment".equals(entry.getKey())) {
            final Label keyLabel = new Label(tr(decamelize(entry.getKey())) + ":");
            keyLabel.getStyleClass().add("captionTitle");
            GridPane.setValignment(keyLabel, VPos.TOP);
            final TextArea valueLabel = new TextArea(entry.getValue().toString());
            valueLabel.setWrapText(true);
            valueLabel.setPrefRowCount(entry.getValue().toString().length() / 25);
            valueLabel.focusedProperty().addListener((observable, oldValue, newValue) -> {
                // update shortcut if TextArea looses focus (doesn't save yet)
                if (!newValue) {
                    shortcutProperties.replace(entry.getKey(), valueLabel.getText());
                    try {
                        final ShortcutDTO shortcut = getControl().getShortcut();
                        final String json = new ObjectMapper().writeValueAsString(shortcutProperties);
                        getControl().setShortcut(new ShortcutDTO.Builder(shortcut).withScript(json).build());
                    } catch (JsonProcessingException e) {
                        LOGGER.error("Creating new shortcut String failed.", e);
                    }
                }
            });
            propertiesGrid.addRow(row, keyLabel, valueLabel);
        }
    }
    // set the environment
    this.environmentAttributes.clear();
    if (shortcutProperties.containsKey("environment")) {
        final Map<String, String> environment = (Map<String, String>) shortcutProperties.get("environment");
        this.environmentAttributes.putAll(environment);
    }
}
Also used : TextArea(javafx.scene.control.TextArea) Label(javafx.scene.control.Label) URI(java.net.URI) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) Button(javafx.scene.control.Button) FileChooser(javafx.stage.FileChooser) TextField(javafx.scene.control.TextField) File(java.io.File) Map(java.util.Map) ObservableMap(javafx.collections.ObservableMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 29 with ShortcutDTO

use of org.phoenicis.library.dto.ShortcutDTO in project POL-POM-5 by PhoenicisOrg.

the class ShortcutEditingPanelSkin method createKeyAttributeList.

private KeyAttributeList createKeyAttributeList() {
    final KeyAttributeList keyAttributeList = new KeyAttributeList();
    keyAttributeList.setEditable(true);
    keyAttributeList.setOnChange(environment -> {
        // update shortcut if a part of the environment list has changed
        shortcutProperties.replace("environment", environment);
        try {
            final ShortcutDTO shortcut = getControl().getShortcut();
            final String json = new ObjectMapper().writeValueAsString(shortcutProperties);
            getControl().setShortcut(new ShortcutDTO.Builder(shortcut).withScript(json).build());
        } catch (JsonProcessingException e) {
            LOGGER.error("Creating new shortcut String failed.", e);
        }
    });
    this.environmentAttributes.addListener((Observable invalidated) -> keyAttributeList.setAttributeMap(this.environmentAttributes));
    keyAttributeList.setAttributeMap(this.environmentAttributes);
    return keyAttributeList;
}
Also used : KeyAttributeList(org.phoenicis.javafx.components.common.control.KeyAttributeList) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Observable(javafx.beans.Observable)

Example 30 with ShortcutDTO

use of org.phoenicis.library.dto.ShortcutDTO in project POL-POM-5 by PhoenicisOrg.

the class LibraryFeaturePanelSkin method createCombinedListWidget.

private CombinedListWidget<ShortcutDTO> createCombinedListWidget() {
    final FilteredList<ShortcutDTO> filteredShortcuts = ConcatenatedList.create(new MappedList<>(getControl().getCategories().sorted(Comparator.comparing(ShortcutCategoryDTO::getName)), ShortcutCategoryDTO::getShortcuts)).filtered(getControl().getFilter()::filter);
    filteredShortcuts.predicateProperty().bind(Bindings.createObjectBinding(() -> getControl().getFilter()::filter, getControl().getFilter().searchTermProperty(), getControl().getFilter().selectedShortcutCategoryProperty()));
    final SortedList<ShortcutDTO> sortedShortcuts = filteredShortcuts.sorted(Comparator.comparing(shortcut -> shortcut.getInfo().getName()));
    final ObservableList<ListWidgetElement<ShortcutDTO>> listWidgetEntries = new MappedList<>(sortedShortcuts, ListWidgetElement::create);
    final CombinedListWidget<ShortcutDTO> combinedListWidget = new CombinedListWidget<>(listWidgetEntries, this.selectedListWidget);
    // bind direction: controller property -> skin property
    getControl().selectedShortcutProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue != null) {
            combinedListWidget.select(newValue);
        } else {
            combinedListWidget.deselect();
        }
    });
    // bind direction: skin property -> controller properties
    combinedListWidget.selectedElementProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue != null) {
            final ShortcutDTO selectedItem = newValue.getItem();
            final MouseEvent event = newValue.getEvent();
            getControl().setSelectedShortcut(selectedItem);
            getControl().setOpenedDetailsPanel(new ShortcutInformation(selectedItem));
            if (event.getClickCount() == 2) {
                getControl().runShortcut(selectedItem);
            }
        } else {
            getControl().setSelectedShortcut(null);
            getControl().setOpenedDetailsPanel(new None());
        }
    });
    return combinedListWidget;
}
Also used : CombinedListWidget(org.phoenicis.javafx.components.common.widgets.control.CombinedListWidget) ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) StringBindings(org.phoenicis.javafx.utils.StringBindings) ObjectExpression(javafx.beans.binding.ObjectExpression) MappedList(org.phoenicis.javafx.collections.MappedList) MouseEvent(javafx.scene.input.MouseEvent) ShortcutEditing(org.phoenicis.javafx.components.library.panelstates.ShortcutEditing) Bindings(javafx.beans.binding.Bindings) SidebarBase(org.phoenicis.javafx.components.common.control.SidebarBase) org.phoenicis.javafx.components.library.control(org.phoenicis.javafx.components.library.control) ListWidgetElement(org.phoenicis.javafx.components.common.widgets.utils.ListWidgetElement) ShortcutInformation(org.phoenicis.javafx.components.library.panelstates.ShortcutInformation) ShortcutCreation(org.phoenicis.javafx.components.library.panelstates.ShortcutCreation) TabPane(javafx.scene.control.TabPane) None(org.phoenicis.javafx.components.common.panelstates.None) FeaturePanelSkin(org.phoenicis.javafx.components.common.skin.FeaturePanelSkin) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) SwitchBinding(org.phoenicis.javafx.utils.SwitchBinding) SortedList(javafx.collections.transformation.SortedList) ListWidgetType(org.phoenicis.javafx.components.common.widgets.utils.ListWidgetType) ObjectProperty(javafx.beans.property.ObjectProperty) Node(javafx.scene.Node) FilteredList(javafx.collections.transformation.FilteredList) Localisation.tr(org.phoenicis.configuration.localisation.Localisation.tr) Observable(javafx.beans.Observable) JavaFxSettingsManager(org.phoenicis.javafx.settings.JavaFxSettingsManager) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) Tab(javafx.scene.control.Tab) OpenDetailsPanel(org.phoenicis.javafx.components.common.panelstates.OpenDetailsPanel) DetailsPanel(org.phoenicis.javafx.components.common.control.DetailsPanel) Optional(java.util.Optional) ObjectBindings(org.phoenicis.javafx.utils.ObjectBindings) ObservableList(javafx.collections.ObservableList) ConcatenatedList(org.phoenicis.javafx.collections.ConcatenatedList) Comparator(java.util.Comparator) CombinedListWidget(org.phoenicis.javafx.components.common.widgets.control.CombinedListWidget) MouseEvent(javafx.scene.input.MouseEvent) ListWidgetElement(org.phoenicis.javafx.components.common.widgets.utils.ListWidgetElement) ShortcutInformation(org.phoenicis.javafx.components.library.panelstates.ShortcutInformation) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) MappedList(org.phoenicis.javafx.collections.MappedList) ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) None(org.phoenicis.javafx.components.common.panelstates.None)

Aggregations

ShortcutDTO (org.phoenicis.library.dto.ShortcutDTO)30 File (java.io.File)18 IOException (java.io.IOException)17 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 URI (java.net.URI)12 ShortcutCategoryDTO (org.phoenicis.library.dto.ShortcutCategoryDTO)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 Consumer (java.util.function.Consumer)8 List (java.util.List)7 ScriptInterpreter (org.phoenicis.scripts.interpreter.ScriptInterpreter)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 Observable (javafx.beans.Observable)6 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)6 Safe (org.phoenicis.configuration.security.Safe)6 TypeReference (com.fasterxml.jackson.core.type.TypeReference)5 Collections (java.util.Collections)5 Collectors (java.util.stream.Collectors)5 ContainerCategoryDTO (org.phoenicis.containers.dto.ContainerCategoryDTO)5 ContainerDTO (org.phoenicis.containers.dto.ContainerDTO)5