Search in sources :

Example 21 with ShortcutDTO

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

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 22 with ShortcutDTO

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

the class LibraryFeaturePanelSkin method createContent.

/**
 * {@inheritDoc}
 */
@Override
public ObjectExpression<Node> createContent() {
    final CombinedListWidget<ShortcutDTO> combinedListWidget = createCombinedListWidget();
    final Tab installedApplicationsTab = new Tab(tr("My applications"), combinedListWidget);
    installedApplicationsTab.setClosable(false);
    final TabPane container = new TabPane();
    container.getStyleClass().add("rightPane");
    getControl().selectedTabProperty().addListener((Observable invalidation) -> {
        final Tab selectedTab = getControl().getSelectedTab();
        if (selectedTab != null) {
            container.getSelectionModel().select(selectedTab);
        } else {
            container.getSelectionModel().selectFirst();
        }
    });
    container.getSelectionModel().selectedItemProperty().addListener((Observable invalidation) -> getControl().setSelectedTab(container.getSelectionModel().getSelectedItem()));
    Bindings.bindContentBidirectional(container.getTabs(), getControl().getTabs());
    container.getTabs().add(installedApplicationsTab);
    return new SimpleObjectProperty<>(container);
}
Also used : TabPane(javafx.scene.control.TabPane) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) Tab(javafx.scene.control.Tab) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) Observable(javafx.beans.Observable)

Example 23 with ShortcutDTO

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

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 24 with ShortcutDTO

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

the class LibraryManager method fetchShortcutDTO.

private ShortcutDTO fetchShortcutDTO(File shortcutDirectory, File file) {
    final String baseName = FilenameUtils.getBaseName(file.getName());
    final File infoFile = new File(shortcutDirectory, baseName + ".info");
    final File iconFile = new File(shortcutDirectory, baseName + ".icon");
    final File categoryIconFile = new File(shortcutDirectory, baseName + "Category.icon");
    final File miniatureFile = new File(shortcutDirectory, baseName + ".miniature");
    final ShortcutInfoDTO.Builder shortcutInfoDTOBuilder;
    if (infoFile.exists()) {
        final ShortcutInfoDTO shortcutInfoDTOFromJsonFile = unSerializeShortcutInfo(infoFile);
        shortcutInfoDTOBuilder = new ShortcutInfoDTO.Builder(shortcutInfoDTOFromJsonFile);
    } else {
        shortcutInfoDTOBuilder = new ShortcutInfoDTO.Builder();
        shortcutInfoDTOBuilder.withName(baseName);
    }
    if (StringUtils.isBlank(shortcutInfoDTOBuilder.getName())) {
        shortcutInfoDTOBuilder.withName(baseName);
    }
    if (StringUtils.isBlank(shortcutInfoDTOBuilder.getCategory())) {
        shortcutInfoDTOBuilder.withCategory("Other");
    }
    final ShortcutInfoDTO shortcutInfoDTO = shortcutInfoDTOBuilder.build();
    try {
        final URI icon = iconFile.exists() ? iconFile.toURI() : getClass().getResource("phoenicis.png").toURI();
        final URI categoryIcon = categoryIconFile.exists() ? categoryIconFile.toURI() : getClass().getResource("phoenicis.png").toURI();
        final URI miniature = miniatureFile.exists() ? miniatureFile.toURI() : getClass().getResource("defaultMiniature.png").toURI();
        return new ShortcutDTO.Builder().withId(baseName).withInfo(shortcutInfoDTO).withScript(IOUtils.toString(new FileInputStream(file), "UTF-8")).withIcon(icon).withCategoryIcon(categoryIcon).withMiniature(miniature).build();
    } catch (URISyntaxException | IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ShortcutInfoDTO(org.phoenicis.library.dto.ShortcutInfoDTO) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) File(java.io.File) URI(java.net.URI) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) FileInputStream(java.io.FileInputStream)

Example 25 with ShortcutDTO

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

the class LibraryManager method fetchShortcuts.

public List<ShortcutCategoryDTO> fetchShortcuts() {
    final File shortcutDirectoryFile = new File(this.shortcutDirectory);
    if (!shortcutDirectoryFile.exists()) {
        shortcutDirectoryFile.mkdirs();
        return Collections.emptyList();
    }
    final File[] directoryContent = shortcutDirectoryFile.listFiles();
    if (directoryContent == null) {
        return Collections.emptyList();
    }
    HashMap<String, List<ShortcutDTO>> categoryMap = new HashMap<>();
    for (File file : directoryContent) {
        if ("shortcut".equals(FilenameUtils.getExtension(file.getName()))) {
            ShortcutDTO shortcut = fetchShortcutDTO(shortcutDirectoryFile, file);
            String categoryId = shortcut.getInfo().getCategory();
            if (!categoryMap.containsKey(categoryId)) {
                categoryMap.put(categoryId, new ArrayList<>());
            }
            categoryMap.get(categoryId).add(shortcut);
        }
    }
    List<ShortcutCategoryDTO> shortcuts = new ArrayList<>();
    for (Map.Entry<String, List<ShortcutDTO>> entry : categoryMap.entrySet()) {
        entry.getValue().sort(ShortcutDTO.nameComparator());
        ShortcutCategoryDTO category = new ShortcutCategoryDTO.Builder().withId(entry.getKey()).withName(entry.getKey()).withShortcuts(entry.getValue()).withIcon(// choose one category icon
        entry.getValue().get(0).getCategoryIcon()).build();
        shortcuts.add(tr(category));
    }
    return shortcuts;
}
Also used : ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) File(java.io.File)

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