Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
schuemie committed May 4, 2015
1 parent 43d90f1 commit 0ce5fa4
Show file tree
Hide file tree
Showing 10 changed files with 1,379 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="lib/json-simple-1.1.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TradeRouteOptimizer</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
100 changes: 100 additions & 0 deletions src/AutocompleteJComboBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.JComboBox;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;

public class AutocompleteJComboBox extends JComboBox<String> {
private static final long serialVersionUID = 742581679282073435L;
private List<String> items;

public void setItems(List<String> items) {
this.items = items;
}

public AutocompleteJComboBox(List<String> s) {
super();
this.items = s;
setEditable(true);
Component c = getEditor().getEditorComponent();
if (c instanceof JTextComponent) {
final JTextComponent tc = (JTextComponent) c;
tc.getDocument().addDocumentListener(new DocumentListener() {

@Override
public void changedUpdate(DocumentEvent arg0) {
}

@Override
public void insertUpdate(DocumentEvent arg0) {
update();
}

@Override
public void removeUpdate(DocumentEvent arg0) {
update();
}

public void update() {

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
List<String> founds = search(items, tc.getText());
Set<String> foundSet = new HashSet<String>();
for (String s : founds)
foundSet.add(s.toLowerCase());
setEditable(false);
removeAllItems();

if (!foundSet.contains(tc.getText().toLowerCase())) {
addItem(tc.getText());
}

for (String s : founds) {
addItem(s);
}
setEditable(true);
setPopupVisible(true);
tc.requestFocus();
}

private List<String> search(List<String> searchable, String text) {
List<String> result = new ArrayList<String>();
for (String string : searchable)
if (string.toLowerCase().contains(text.toLowerCase()))
result.add(string);
return result;
}
});
}
});

tc.addFocusListener(new FocusListener() {

@Override
public void focusGained(FocusEvent arg0) {
if (tc.getText().length() > 0) {
setPopupVisible(true);
}
}

@Override
public void focusLost(FocusEvent arg0) {
}
});

} else {
throw new IllegalStateException("Editing component is not a JTextComponent!");
}
}
}
48 changes: 48 additions & 0 deletions src/Console.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.io.IOException;
import java.io.OutputStream;

import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;

public class Console extends OutputStream {

private StringBuffer buffer = new StringBuffer();
private JTextArea textArea;

public void println(String string) {
textArea.append(string + "\n");
textArea.repaint();
System.out.println(string);
}

public void setTextArea(JTextArea textArea) {
this.textArea = textArea;
}

public String getText() {
try {
return textArea.getDocument().getText(0, textArea.getDocument().getLength());
} catch (BadLocationException e) {
e.printStackTrace();
}
return null;
}

@Override
public void write(int b) throws IOException {
buffer.append((char) b);
if ((char) b == '\n') {
if (textArea != null) {
textArea.append(buffer.toString());
textArea.setCaretPosition(textArea.getDocument().getLength());
}
buffer = new StringBuffer();
}
}

public void clear() {
textArea.setText("");

}

}
48 changes: 48 additions & 0 deletions src/DataLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;

/**
* Class for downloading the data from eddb.io
*/
public class DataLoader {

public static String baseUrl = "http://eddb.io/archive/v3/";
public static String[] fileNames = new String[] { "commodities.json", "systems.json", "stations.json" };

/**
* Download all necessary files from eddb.io, and store in the working folder.
*
* @param folder
* Absolute path to the working folder
*/
public static void download(String folder) {
for (String fileName : fileNames) {
System.out.format("Downloading %s\n", fileName);
downloadFile(baseUrl + fileName, folder + "/" + fileName);
}
System.out.println("Finished downloading");
}

private static void downloadFile(String url, String filename) {
try {
FileOutputStream out = new FileOutputStream(filename);
URL sourceURL = new URL(url);
HttpURLConnection sourceConnection = (HttpURLConnection) sourceURL.openConnection();
sourceConnection.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
sourceConnection.connect();
GZIPInputStream resultingInputStream = new GZIPInputStream(sourceConnection.getInputStream());
BufferedInputStream in = new BufferedInputStream(resultingInputStream);
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
out.write(data, 0, count);
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit 0ce5fa4

Please sign in to comment.