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);
}
}
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);
}
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;
}
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);
}
}
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;
}
Aggregations