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