Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Same way as for Icon (for Overseer) : Preset list and browsable. #346

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ public interface ConfigManager {

IconBundleConfigurationService iconBundleConfiguration();

IconBundleConfigurationService pictureBundleConfiguration();

List<ProfileDescriptor> profiles();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class MercuryConfigManager implements ConfigManager, AsSubscriber {
private PlainConfigurationService<HotKeysSettingsDescriptor> hotKeyConfigurationService;
private ListConfigurationService<StashTabDescriptor> stashTabConfigurationService;
private IconBundleConfigurationService iconBundleConfigurationService;
private IconBundleConfigurationService pictureBundleConfigurationService;
private AdrConfigurationService adrConfigurationService;

private List<BaseConfigurationService> services = new ArrayList<>();
Expand Down Expand Up @@ -96,6 +97,11 @@ public IconBundleConfigurationService iconBundleConfiguration() {
return this.iconBundleConfigurationService;
}

@Override
public IconBundleConfigurationService pictureBundleConfiguration() {
return this.pictureBundleConfigurationService;
}

@Override
public PlainConfigurationService<HotKeysSettingsDescriptor> hotKeysConfiguration() {
return this.hotKeyConfigurationService;
Expand All @@ -112,9 +118,11 @@ public void load() {
File configFile = new File(dataSource.getConfigurationFilePath());
File configFolder = new File(dataSource.getConfigurationPath());
File iconFolder = new File(dataSource.getConfigurationPath() + "\\icons");
if (!configFolder.exists() || !configFile.exists() || !iconFolder.exists()) {
File pictureFolder = new File(dataSource.getConfigurationPath() + "\\pictures");
if (!configFolder.exists() || !configFile.exists() || !iconFolder.exists() || !pictureFolder.exists()) {
new File(dataSource.getConfigurationPath() + "\\temp").mkdir();
new File(dataSource.getConfigurationPath() + "\\icons").mkdir();
new File(dataSource.getConfigurationPath() + "\\pictures").mkdir();
new File(dataSource.getConfigurationFilePath()).createNewFile();

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Expand Down Expand Up @@ -155,6 +163,7 @@ public void load() {
this.hotKeyConfigurationService = new HotKeyConfigurationService(selectedProfile);
this.adrConfigurationService = new AdrConfigurationServiceMock(selectedProfile);
this.iconBundleConfigurationService = new IconBundleConfigurationServiceImpl(selectedProfile);
this.pictureBundleConfigurationService = new PictureBundleConfigurationServiceImpl(selectedProfile);

this.services.add((BaseConfigurationService) this.framesConfigurationService);
this.services.add((BaseConfigurationService) this.soundConfigurationService);
Expand All @@ -167,6 +176,7 @@ public void load() {
this.services.add((BaseConfigurationService) this.hotKeyConfigurationService);
this.services.add((BaseConfigurationService) this.adrConfigurationService);
this.services.add((BaseConfigurationService) this.iconBundleConfigurationService);
this.services.add((BaseConfigurationService) this.pictureBundleConfigurationService);

this.services.forEach(BaseConfigurationService::validate);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.mercury.platform.shared.config.configration.impl;

import com.mercury.platform.shared.config.configration.BaseConfigurationService;
import com.mercury.platform.shared.config.configration.IconBundleConfigurationService;
import com.mercury.platform.shared.config.descriptor.ProfileDescriptor;
import com.mercury.platform.shared.entity.message.MercuryError;
import com.mercury.platform.shared.store.MercuryStoreCore;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

public class PictureBundleConfigurationServiceImpl extends BaseConfigurationService<List<String>> implements IconBundleConfigurationService {
private static final String PICTURES_PATH = System.getenv("USERPROFILE") + "\\AppData\\Local\\MercuryTrade\\pictures\\";
private Map<String, URL> pictureBundle = new HashMap<>();

public PictureBundleConfigurationServiceImpl(ProfileDescriptor selectedProfile) {
super(selectedProfile);
}

@Override
public URL getIcon(String iconPath) {
URL url = this.pictureBundle.get(iconPath);
if (url == null) {
url = this.pictureBundle.get("default_syndicate.png");
}
return url;
}

@Override
public Map<String, URL> getIconBundle() {
return this.pictureBundle;
}

@Override
public void addIcon(String iconPath) {
try {
URL url = new URL("file:///" + iconPath);
String iconName = StringUtils.substringAfterLast(iconPath, "\\");

File file = new File(PICTURES_PATH + iconName);
BufferedImage image = ImageIO.read(url);
ImageIO.write(image, "png", file);
this.getEntities().add(iconName);
this.pictureBundle.put(iconName, url);
} catch (IOException e) {
MercuryStoreCore.errorHandlerSubject.onNext(
new MercuryError("Error while add icon: " + iconPath, e));
}
}

@Override
public void removeIcon(String iconPath) {
}

@Override
public List<String> getDefault() {
return new ArrayList<>();
}

@Override
public List<String> getDefaultBundle() {
return Arrays.stream(new String[]{
"default_syndicate.png",
"syndicate_colored.png",
"betrayal_reference_guide.png",
"map_drops.png",
}).collect(Collectors.toList());
}

@Override
public void toDefault() {
this.selectedProfile.setPictureBundleList(this.getDefault());
}

@Override
public List<String> getEntities() {
return this.selectedProfile.getPictureBundleList();
}

@Override
public void validate() {
if (this.selectedProfile.getPictureBundleList() == null) {
this.selectedProfile.setPictureBundleList(this.getDefault());
}
this.initPictureBundle(this.pictureBundle);
}

private void initPictureBundle(Map<String, URL> pictureBundle) {
this.getDefaultBundle().forEach(it -> {
URL resource = this.getClass().getClassLoader().getResource("app/adr/pictures/" + it);
if (resource != null) {
pictureBundle.put(it, resource);
}
});
this.getEntities().forEach(it -> {
try {
if (new File(PICTURES_PATH + it).exists()) {
URL resource = new URL("file:///" + PICTURES_PATH + it);
pictureBundle.put(it, resource);
} else {
URL resource = this.getClass().getClassLoader().getResource("app/adr/pictures/" + "default_syndicate.png");
pictureBundle.put(it, resource);
}
} catch (MalformedURLException e) {
MercuryStoreCore.errorHandlerSubject.onNext(
new MercuryError("Error while initializing icon: " + it, e));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public class ProfileDescriptor {
private List<StashTabDescriptor> stashTabDescriptors;
private List<AdrProfileDescriptor> adrProfileDescriptorList;
private List<String> iconBundleList;
private List<String> pictureBundleList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public class TaskBarDescriptor implements Serializable {
private String dndResponseText = "Response message";
private HotKeyDescriptor hideoutHotkey = new HotKeyDescriptor();
private HotKeyDescriptor helpIGHotkey = new HotKeyDescriptor();
private String helpIGPath = "...";
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ public class MercuryStoreCore {
public static final PublishSubject<PlainMessageDescriptor> newScannerMessageSubject = PublishSubject.create();
public static final PublishSubject<PlainMessageDescriptor> removeScannerNotificationSubject = PublishSubject.create();
public static final PublishSubject<NotificationDescriptor> expiredNotificationSubject = PublishSubject.create();

public static final PublishSubject<Boolean> pictureChange = PublishSubject.create();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.mercury.platform.ui.adr.dialog.AdrExportDialog;
import com.mercury.platform.ui.adr.dialog.AdrIconSelectDialog;
import com.mercury.platform.ui.adr.dialog.AdrNewProfileDialog;
import com.mercury.platform.ui.adr.dialog.AdrPictureSelectDialog;
import com.mercury.platform.ui.adr.routing.AdrPageDefinition;
import com.mercury.platform.ui.adr.routing.AdrPageState;
import com.mercury.platform.ui.components.fields.font.FontStyle;
Expand Down Expand Up @@ -38,6 +39,7 @@ public class AdrManagerFrame extends AbstractTitledComponentFrame {
private JComboBox profileSelector;
private AdrExportDialog exportDialog;
private AdrIconSelectDialog iconSelectDialog;
private AdrPictureSelectDialog pictureSelectDialog;
@Getter
private AdrProfileDescriptor selectedProfile;

Expand All @@ -52,6 +54,8 @@ public AdrManagerFrame(AdrProfileDescriptor selectedProfile) {
this.exportDialog = new AdrExportDialog(this, new ArrayList<>());
this.iconSelectDialog = new AdrIconSelectDialog();
this.iconSelectDialog.setLocationRelativeTo(null);
this.pictureSelectDialog = new AdrPictureSelectDialog();
this.pictureSelectDialog.setLocationRelativeTo(null);
FrameDescriptor frameDescriptor = this.framesConfig.get(this.getClass().getSimpleName());
this.setPreferredSize(frameDescriptor.getFrameSize());
UIManager.put("MenuItem.background", AppThemeColor.ADR_BG);
Expand Down Expand Up @@ -133,6 +137,10 @@ public void subscribe() {
this.iconSelectDialog.setCallback(callback);
this.iconSelectDialog.setVisible(true);
});
MercuryStoreUI.adrOpenPictureSelectSubject.subscribe(callback -> {
this.pictureSelectDialog.setCallback(callback);
this.pictureSelectDialog.setVisible(true);
});
}

public void setSelectedProfile(AdrProfileDescriptor profile) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mercury.platform.ui.adr.components.panel.ui;

import com.mercury.platform.shared.config.Configuration;
import com.mercury.platform.shared.config.configration.IconBundleConfigurationService;
import com.mercury.platform.ui.components.ComponentsFactory;
import com.mercury.platform.ui.misc.AppThemeColor;

import javax.swing.*;
import java.awt.*;


public class PicturesListCellRenderer implements ListCellRenderer<String> {
private ComponentsFactory componentsFactory = new ComponentsFactory();
private IconBundleConfigurationService config = Configuration.get().pictureBundleConfiguration();

@Override
public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel iconLabel = this.componentsFactory.getIconLabel(this.config.getIcon(value), 230);
iconLabel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
if (isSelected) {
iconLabel.setBorder(BorderFactory.createLineBorder(AppThemeColor.TEXT_MESSAGE));
}
return iconLabel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,17 @@


import com.mercury.platform.shared.config.Configuration;
import com.mercury.platform.shared.config.configration.IconBundleConfigurationService;
import com.mercury.platform.shared.store.MercuryStoreCore;
import com.mercury.platform.ui.adr.components.panel.ui.IconsListCellRenderer;
import com.mercury.platform.ui.components.fields.font.FontStyle;
import com.mercury.platform.ui.components.panel.VerticalScrollContainer;
import com.mercury.platform.ui.dialog.BaseDialog;
import com.mercury.platform.ui.misc.AppThemeColor;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.stream.Collectors;

public class AdrIconSelectDialog extends BaseDialog<String, String[]> {
private JList iconsList;
private IconBundleConfigurationService config;

public class AdrIconSelectDialog extends AdrSelectDialog {
public AdrIconSelectDialog() {
super(null, null, null);
super();
this.setTitle("Select icon");
}

Expand Down Expand Up @@ -51,79 +42,4 @@ protected void createView() {
this.add(scrollPane, BorderLayout.CENTER);
this.add(this.getBottomPanel(), BorderLayout.PAGE_END);
}

public void setSelectedIcon(String iconPath) {
this.iconsList.setSelectedValue(iconPath, true);
}

private JPanel getFilterPanel() {
JPanel root = this.componentsFactory.getJPanel(new BorderLayout());
root.add(this.componentsFactory.getTextLabel("Filter:"), BorderLayout.LINE_START);
JTextField filterField = this.componentsFactory.getTextField("Filter:", FontStyle.REGULAR, 15);
filterField.addActionListener(action -> {
List<String> entities = this.config.getDefaultBundle();
entities.addAll(this.config.getEntities());
if (filterField.getText().equals("")) {
this.iconsList.setListData(entities.toArray());
} else {
List<String> collect = entities.stream()
.filter(name -> StringUtils.containsIgnoreCase(name, filterField.getText()))
.collect(Collectors.toList());
this.iconsList.setListData(collect.toArray());
}
});
root.add(filterField, BorderLayout.CENTER);
root.setBackground(AppThemeColor.SLIDE_BG);
root.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(AppThemeColor.BORDER_DARK),
BorderFactory.createEmptyBorder(4, 0, 4, 4)));
JButton addIconButton = this.componentsFactory.getBorderedButton("Add icon");
addIconButton.setPreferredSize(new Dimension(100, 26));
addIconButton.addActionListener(action -> {
FileDialog dialog = new FileDialog(this, "Choose icon", FileDialog.LOAD);
dialog.setFile("*.png");
dialog.setVisible(true);
dialog.toFront();
if (this.isValidIconPath(dialog.getFile())) {
this.config.addIcon(dialog.getDirectory() + dialog.getFile());
Object selectedValue = this.iconsList.getSelectedValue();
List<String> entities = this.config.getDefaultBundle();
entities.addAll(this.config.getEntities());
this.iconsList.setListData(entities.toArray());
this.setSelectedIcon((String) selectedValue);
MercuryStoreCore.saveConfigSubject.onNext(true);
}
});
root.add(this.componentsFactory.wrapToSlide(addIconButton, AppThemeColor.ADR_BG), BorderLayout.LINE_END);
JPanel wrapper = this.componentsFactory.wrapToSlide(root);
wrapper.setBorder(BorderFactory.createEmptyBorder(4, 0, 4, 0));
return wrapper;
}

private JPanel getBottomPanel() {
JPanel root = this.componentsFactory.getJPanel(new FlowLayout(FlowLayout.CENTER));
JButton selectButton = this.componentsFactory.getBorderedButton("Select");
selectButton.setFont(this.componentsFactory.getFont(FontStyle.BOLD, 15f));
JButton cancelButton = componentsFactory.getButton(
FontStyle.BOLD,
AppThemeColor.FRAME_RGB,
BorderFactory.createLineBorder(AppThemeColor.BORDER),
"Cancel",
15f);
selectButton.setPreferredSize(new Dimension(128, 26));
cancelButton.setPreferredSize(new Dimension(128, 26));
selectButton.addActionListener(action -> {
this.callback.onAction((String) this.iconsList.getSelectedValue());
this.setVisible(false);
});
cancelButton.addActionListener(action -> this.setVisible(false));
root.add(selectButton);
root.add(cancelButton);
return root;
}

private boolean isValidIconPath(String name) {
return name != null && (name.endsWith(".png"));
}
}
Loading