Search in sources :

Example 11 with ShortcutDTO

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

the class ListWidgetElement method create.

public static ListWidgetElement<ContainerDTO> create(ContainerDTO container) {
    final List<BufferedImage> miniatures = new ArrayList<>();
    // do not use too many segments (cannot recognize the miniature if the segment is too small)
    final int maxSegments = 4;
    int currentSegment = 0;
    for (ShortcutDTO shortcutDTO : container.getInstalledShortcuts()) {
        if (currentSegment >= maxSegments) {
            break;
        }
        try {
            miniatures.add(ImageIO.read(shortcutDTO.getMiniature().toURL()));
            currentSegment++;
        } catch (IOException e) {
            LOGGER.warn(String.format("Could not read miniature for shortcut \"%s\"", shortcutDTO.getInfo().getName()), e);
        }
    }
    final BufferedImage segmentedMiniature = createSegmentedMiniature(miniatures);
    final Optional<URI> shortcutMiniature = saveBufferedImage(segmentedMiniature, container.getName());
    return new ListWidgetElement<>(container, shortcutMiniature, CONTAINER_MINIATURE, container.getName(), Collections.emptyList(), Collections.emptyList());
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) URI(java.net.URI) BufferedImage(java.awt.image.BufferedImage) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO)

Example 12 with ShortcutDTO

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

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)

Example 13 with ShortcutDTO

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

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)

Example 14 with ShortcutDTO

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

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

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

the class GenericContainersManager method fetchContainers.

/**
 * fetches all containers in a given directory
 *
 * @param directory
 * @return found containers
 */
private List<ContainerDTO> fetchContainers(File directory) {
    final List<ContainerDTO> containers = new ArrayList<>();
    final File[] containerDirectories = directory.listFiles();
    if (containerDirectories != null) {
        for (File containerDirectory : containerDirectories) {
            if (!containerDirectory.isHidden()) {
                final ConfigFile configFile = compatibleConfigFileFormatFactory.open(new File(containerDirectory, "phoenicis.cfg"));
                final String containerPath = containerDirectory.getAbsolutePath();
                final String containerName = containerPath.substring(containerPath.lastIndexOf('/') + 1);
                // find shortcuts which use this container
                List<ShortcutDTO> shortcutDTOS = libraryManager.fetchShortcuts().stream().flatMap(shortcutCategory -> shortcutCategory.getShortcuts().stream()).filter(shortcut -> {
                    boolean toAdd = false;
                    try {
                        final Map<String, Object> shortcutProperties = objectMapper.readValue(shortcut.getScript(), new TypeReference<Map<String, Object>>() {
                        });
                        toAdd = shortcutProperties.get("winePrefix").equals(containerName);
                    } catch (IOException e) {
                        LOGGER.warn("Could not parse shortcut script JSON", e);
                    }
                    return toAdd;
                }).collect(Collectors.toList());
                if (directory.getName().equals("wineprefix")) {
                    containers.add(new WinePrefixContainerDTO.Builder().withName(containerDirectory.getName()).withPath(containerPath).withInstalledShortcuts(shortcutDTOS).withArchitecture(configFile.readValue("wineArchitecture", "")).withDistribution(configFile.readValue("wineDistribution", "")).withVersion(configFile.readValue("wineVersion", "")).build());
                }
            }
        }
        containers.sort(ContainerDTO.nameComparator());
    }
    return containers;
}
Also used : ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) ContainerCategoryDTO(org.phoenicis.containers.dto.ContainerCategoryDTO) LoggerFactory(org.slf4j.LoggerFactory) ShortcutReader(org.phoenicis.library.ShortcutReader) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) Map(java.util.Map) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ContainerDTO(org.phoenicis.containers.dto.ContainerDTO) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) InteractiveScriptSession(org.phoenicis.scripts.session.InteractiveScriptSession) Consumer(java.util.function.Consumer) List(java.util.List) ShortcutManager(org.phoenicis.library.ShortcutManager) ConfigFile(org.phoenicis.tools.config.ConfigFile) Safe(org.phoenicis.configuration.security.Safe) CompatibleConfigFileFormatFactory(org.phoenicis.tools.config.CompatibleConfigFileFormatFactory) CollectionUtils(org.springframework.util.CollectionUtils) WinePrefixContainerDTO(org.phoenicis.containers.dto.WinePrefixContainerDTO) LibraryManager(org.phoenicis.library.LibraryManager) Collections(java.util.Collections) ConfigFile(org.phoenicis.tools.config.ConfigFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ContainerDTO(org.phoenicis.containers.dto.ContainerDTO) WinePrefixContainerDTO(org.phoenicis.containers.dto.WinePrefixContainerDTO) ShortcutDTO(org.phoenicis.library.dto.ShortcutDTO) TypeReference(com.fasterxml.jackson.core.type.TypeReference) File(java.io.File) ConfigFile(org.phoenicis.tools.config.ConfigFile) Map(java.util.Map)

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