Skip to content

Commit

Permalink
New Feature: Import routes from system names/ids in routing tool (Issue
Browse files Browse the repository at this point in the history
#475)

Merge develop
  • Loading branch information
GoldenGnu committed Sep 17, 2024
2 parents 9c3cca1 + efcbb2e commit 67a7ff4
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public JTextAreaPlaceholder(String placeholder) {
/**
* Create a textfield with hint.
*
* @param text
* @param placeholder Text displayed when empty
*/
public JTextAreaPlaceholder(String text, String placeholder) {
Expand Down Expand Up @@ -192,8 +193,12 @@ private void updatePlaceholderBorder() {
}

private void updateShown() {
updateShown(false);
}

private void updateShown(boolean forceRepaint) {
boolean paint = getText().isEmpty() && placeholderText != null && !placeholderText.isEmpty();
boolean repaint = paint != paintPlaceholder;
boolean repaint = forceRepaint || paint != paintPlaceholder;
getMock().setText(placeholderText);
paintPlaceholder = paint;
if (repaint) {
Expand All @@ -214,7 +219,7 @@ private void updateSize() {

public void setPlaceholderText(String placeholder) {
this.placeholderText = placeholder;
updateShown();
updateShown(true);
}

public void setPlaceholderForeground(Color fg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package net.nikr.eve.jeveasset.gui.shared.components;

import java.awt.Color;
import java.awt.Component;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Expand All @@ -30,12 +31,19 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.GroupLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import net.nikr.eve.jeveasset.Program;
import net.nikr.eve.jeveasset.data.settings.Colors;
import net.nikr.eve.jeveasset.gui.images.Images;
Expand All @@ -46,6 +54,7 @@
public class JTextDialog extends JDialogCentered {

private enum TextDialogAction {
IMPORT_TYPE,
TO_CLIPBOARD,
TO_FILE,
FROM_CLIPBOARD,
Expand All @@ -55,6 +64,7 @@ private enum TextDialogAction {
}

private final JTextAreaPlaceholder jText;
private final JComboBox<TextImport> jImportTypes;
private final JButton jToClipboard;
private final JButton jFromClipboard;
private final JButton jToFile;
Expand All @@ -75,6 +85,11 @@ public JTextDialog(Window window) {

ListenerClass listener = new ListenerClass();

jImportTypes = new JComboBox<>();
jImportTypes.setRenderer(new TextImportListRenderer());
jImportTypes.setActionCommand(TextDialogAction.IMPORT_TYPE.name());
jImportTypes.addActionListener(listener);

jToClipboard = new JButton(GuiShared.get().textToClipboard(), Images.EDIT_COPY.getIcon());
jToClipboard.setActionCommand(TextDialogAction.TO_CLIPBOARD.name());
jToClipboard.addActionListener(listener);
Expand Down Expand Up @@ -112,6 +127,7 @@ public JTextDialog(Window window) {
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jImportTypes)
.addComponent(jToClipboard)
.addComponent(jToFile)
.addComponent(jFromClipboard)
Expand All @@ -127,6 +143,7 @@ public JTextDialog(Window window) {
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(jImportTypes, Program.getButtonsHeight(), Program.getButtonsHeight(), Program.getButtonsHeight())
.addComponent(jToClipboard, Program.getButtonsHeight(), Program.getButtonsHeight(), Program.getButtonsHeight())
.addComponent(jToFile, Program.getButtonsHeight(), Program.getButtonsHeight(), Program.getButtonsHeight())
.addComponent(jFromClipboard, Program.getButtonsHeight(), Program.getButtonsHeight(), Program.getButtonsHeight())
Expand Down Expand Up @@ -169,7 +186,40 @@ public String importText(String text) {
}

public String importText(String text, String example) {
return importText(text, example, null, null).getText();
}

public <E extends TextImport> TextReturn<E> importText(E[] imports) {
return importText("", "", imports, null);
}

public <E extends TextImport> TextReturn<E> importText(E[] imports, E selected) {
return importText("", "", imports, selected);
}

public <E extends TextImport> TextReturn<E> importText(String text, E[] imports) {
return importText(text, "", imports, null);
}

public <E extends TextImport> TextReturn<E> importText(String text, E[] imports, E selected) {
return importText(text, "", imports, selected);
}

public <E extends TextImport> TextReturn<E> importText(String text, String example, E[] imports, E selected) {
getDialog().setTitle(GuiShared.get().textImport());
if (imports == null || imports.length < 1) {
jImportTypes.removeAllItems();
jImportTypes.setVisible(false);
} else {
jImportTypes.setModel(new DefaultComboBoxModel<>(imports));
if (selected != null) {
jImportTypes.setSelectedItem(selected);
} else {
jImportTypes.setSelectedIndex(0);
}
example = jImportTypes.getItemAt(jImportTypes.getSelectedIndex()).getExample();
jImportTypes.setVisible(true);
}
jText.setEditable(true);
jText.setOpaque(true);
jText.setBackground(importColor);
Expand All @@ -186,7 +236,11 @@ public String importText(String text, String example) {
jToFile.setVisible(false);
returnValue = null;
setVisible(true);
return returnValue;
if (imports == null) {
return new TextReturn<>(returnValue, null);
} else {
return new TextReturn<>(returnValue, imports[jImportTypes.getSelectedIndex()]);
}
}

public void exportText(String text) {
Expand Down Expand Up @@ -253,29 +307,70 @@ private void fromFile() {
}
}

public static interface TextImport {
public String getExample();
public Icon getIcon();
public String getType();
}

public static class TextReturn<E extends TextImport> {
private final String text;
private final E type;

public TextReturn(String text, E type) {
this.text = text;
this.type = type;
}

public String getText() {
return text;
}

public E getType() {
return type;
}
}

private class ListenerClass implements ActionListener {
@Override
public void actionPerformed(final ActionEvent e) {
if (TextDialogAction.TO_CLIPBOARD.name().equals(e.getActionCommand())) {
CopyHandler.toClipboard(jText.getText());
}
if (TextDialogAction.TO_FILE.name().equals(e.getActionCommand())) {
} else if (TextDialogAction.TO_FILE.name().equals(e.getActionCommand())) {
toFile();
}
if (TextDialogAction.FROM_CLIPBOARD.name().equals(e.getActionCommand())) {
} else if (TextDialogAction.FROM_CLIPBOARD.name().equals(e.getActionCommand())) {
CopyHandler.paste(jText);
}
if (TextDialogAction.FROM_FILE.name().equals(e.getActionCommand())) {
} else if (TextDialogAction.FROM_FILE.name().equals(e.getActionCommand())) {
fromFile();
}
if (TextDialogAction.OK.name().equals(e.getActionCommand())) {
} else if (TextDialogAction.OK.name().equals(e.getActionCommand())) {
returnValue = jText.getText();
setVisible(false);
}
if (TextDialogAction.CANCEL.name().equals(e.getActionCommand())) {
} else if (TextDialogAction.CANCEL.name().equals(e.getActionCommand())) {
returnValue = null;
setVisible(false);
} else if (TextDialogAction.IMPORT_TYPE.name().equals(e.getActionCommand())) {
TextImport textImport = jImportTypes.getItemAt(jImportTypes.getSelectedIndex());
jText.setPlaceholderText(textImport.getExample());
}
}
}

class TextImportListRenderer implements ListCellRenderer<TextImport> {

private final DefaultListCellRenderer renderer;

public TextImportListRenderer() {
renderer = new DefaultListCellRenderer();
}

@Override
public Component getListCellRendererComponent(JList<? extends TextImport> list, TextImport value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) renderer.getListCellRendererComponent(list, value.getType(), index, isSelected, cellHasFocus);
// Set icon to display for value
label.setIcon(value.getIcon());
return label;
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultListModel;
Expand Down Expand Up @@ -157,11 +158,11 @@ private boolean recalculateRoutes() {
Route last = null;
for (Route route : list) {
if (last != null) {
jumps = jumps + distanceBetween(last, route);
jumps = jumps + distanceBetween(systemCache, filteredGraph, last, route);
}
last = route;
}
jumps = jumps + distanceBetween(last, list.get(0));
jumps = jumps + distanceBetween(systemCache, filteredGraph, last, list.get(0));
calculateInfo(jumps);
return true;
} catch (DisconnectedGraphException ex) {
Expand All @@ -186,11 +187,12 @@ private void calculateInfo(int jumps) {
jJumps.setText(TabsRouting.get().resultEditJumps(jumps));
}

private List<SolarSystem> routeBetween(Route from, Route to) {
private static List<SolarSystem> routeBetween(Map<Long, SolarSystem> systemCache, Graph<SolarSystem> filteredGraph, Route from, Route to) {
return filteredGraph.routeBetween(systemCache.get(from.getSystemID()), systemCache.get(to.getSystemID()));
}

private int distanceBetween(Route from, Route to) {

private static int distanceBetween(Map<Long, SolarSystem> systemCache, Graph<SolarSystem> filteredGraph, Route from, Route to) {
return filteredGraph.distanceBetween(systemCache.get(from.getSystemID()), systemCache.get(to.getSystemID()));
}

Expand Down Expand Up @@ -238,26 +240,34 @@ protected void save() {
list.add(model.get(i));
}
try {
List<List<SolarSystem>> routes = new ArrayList<>();
int jumps = 0;
Route last = null;
for (Route route : list) {
if (last != null) {
jumps = jumps + distanceBetween(last, route);
routes.add(routeBetween(last, route));
}
last = route;
}
jumps = jumps + distanceBetween(last, list.get(0));
routes.add(routeBetween(last, list.get(0)));
returnResult = new RouteResult(routes, routeResult.getStations(), routeResult.getWaypoints(), TabsRouting.get().resultEdited(), 0, jumps, program.getRoutingTab().getAvoidString(), program.getRoutingTab().getSecurityString());
returnResult = makeRouteResult(program, systemCache, filteredGraph, list, TabsRouting.get().resultEdited(), routeResult.getStations());
} catch (DisconnectedGraphException ex) {
returnResult = null;
}
setVisible(false);
}

private static class Route implements Serializable {
public static RouteResult makeRouteResult(Program program, Map<Long, SolarSystem> systemCache, Graph<SolarSystem> filteredGraph, List<Route> list, String algorithmName) throws DisconnectedGraphException {
return makeRouteResult(program, systemCache, filteredGraph, list, algorithmName, new HashMap<>());
}

private static RouteResult makeRouteResult(Program program, Map<Long, SolarSystem> systemCache, Graph<SolarSystem> filteredGraph, List<Route> list, String algorithmName, Map<Long, List<SolarSystem>> stations) throws DisconnectedGraphException {
List<List<SolarSystem>> routes = new ArrayList<>();
int jumps = 0;
Route last = null;
for (Route route : list) {
if (last != null) {
jumps = jumps + distanceBetween(systemCache, filteredGraph, last, route);
routes.add(routeBetween(systemCache, filteredGraph, last, route));
}
last = route;
}
jumps = jumps + distanceBetween(systemCache, filteredGraph, last, list.get(0));
routes.add(routeBetween(systemCache, filteredGraph, last, list.get(0)));
return new RouteResult(routes, stations, routes.size(), algorithmName, 0, jumps, program.getRoutingTab().getAvoidString(), program.getRoutingTab().getSecurityString());
}

public static class Route implements Serializable {
private final long systemID;
private final String system;

Expand Down
Loading

0 comments on commit 67a7ff4

Please sign in to comment.