Skip to content

Commit

Permalink
switched from javafx to swing, implemented persistance layer
Browse files Browse the repository at this point in the history
  • Loading branch information
s1mpl3x committed Apr 29, 2014
1 parent 6323ac9 commit a9593c1
Show file tree
Hide file tree
Showing 10 changed files with 555 additions and 99 deletions.
45 changes: 30 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>over9000</groupId>
<artifactId>Skadi</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Skadi</name>
<description>livestreamer java gui</description>

<dependencies><dependency>
<groupId>javafx</groupId>
<artifactId>jfxrt</artifactId>
<version>${java.version}</version>
<scope>system</scope>
<systemPath>${java.home}/lib/jfxrt.jar</systemPath>
</dependency></dependencies>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>over9000</groupId>
<artifactId>Skadi</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Skadi</name>
<description>livestreamer java gui</description>

<organization>
<name>s1mpl3x</name>
</organization>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
49 changes: 37 additions & 12 deletions src/main/java/eu/over9000/skadi/SkadiMain.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package eu.over9000.skadi;

import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;

import javafx.application.Application;
import eu.over9000.skadi.channel.ChannelInstance;
import eu.over9000.skadi.gui.SkadiGUI;
import eu.over9000.skadi.io.PersistenceManager;
Expand All @@ -12,7 +11,10 @@ public class SkadiMain {

private static SkadiMain instance;

private final Map<String, ChannelInstance> channels = PersistenceManager.getInstance().loadChannels();
private TreeMap<String, ChannelInstance> channels = new TreeMap<>();

public String livestreamer_exec = "livestreamer";
public String chrome_exec = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";

public static SkadiMain getInstance() {
if (SkadiMain.instance == null) {
Expand All @@ -27,26 +29,34 @@ public static void main(final String[] args) {

private void runInit(final String[] args) {
this.addShutdownHook();
Application.launch(SkadiGUI.class, args);
PersistenceManager.getInstance().loadData();
SkadiGUI.create();
}

private void addShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

@Override
public void run() {
System.out.println("BEGIN HOOK");
System.out.println("KILLING STREAMS/CHATS..");
for (final ChannelInstance instance : SkadiMain.this.channels.values()) {
instance.closeStreamAndChat();
}
System.out.println("END HOOK");
System.out.println("SAVING DATA..");
PersistenceManager.getInstance().saveData();
System.out.println("SHUTDOWN COMPLETE");

}
}));
}

public void addAndOpenNewChannel(String url) {
public void addNewChannel(String url) {
if (!url.endsWith("/")) {
url = url + "/";
}

if (!SkadiMain.validateURL(url)) {
System.out.println("invalid url given");
return;
}
if (url.startsWith("twitch.tv/")) {
Expand All @@ -56,21 +66,36 @@ public void addAndOpenNewChannel(String url) {
url = "http://" + url;
}

if (!url.endsWith("/")) {
url = url + "/";
if (this.channels.containsKey(url)) {
System.out.println("Channel already in list");
return;
}

final ChannelInstance newChannel = new ChannelInstance(url, "best");

this.channels.put(url, newChannel);

newChannel.openStreamAndChat();

System.out.println("ADDED AND OPENED STREAM AND CHAT FOR URL " + url);

SkadiGUI.handleChannelListUpdate();

}

private static boolean validateURL(final String url) {
return Pattern.matches("(http://)?(www\\.)?(twitch\\.tv/)[0-9a-zA-Z]+/", url);
return Pattern.matches("(http://)?(www\\.)?(twitch\\.tv/)[A-Za-z0-9_]+/", url);

}

public TreeMap<String, ChannelInstance> getChannels() {
return this.channels;
}

public void setChannels(final TreeMap<String, ChannelInstance> newChannels) {
this.channels = newChannels;
}

public void deleteChannel(final ChannelInstance channel) {
this.channels.remove(channel.getURL());
channel.closeStreamAndChat();
}
}
8 changes: 8 additions & 0 deletions src/main/java/eu/over9000/skadi/channel/ChannelInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ public void closeStreamAndChat() {
this.closeStream();
this.closeChat();
}

public String getURL() {
return this.url;
}

public String getQuality() {
return this.quality;
}
}
29 changes: 29 additions & 0 deletions src/main/java/eu/over9000/skadi/gui/ChannelDataListModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package eu.over9000.skadi.gui;

import javax.swing.AbstractListModel;

import eu.over9000.skadi.SkadiMain;
import eu.over9000.skadi.channel.ChannelInstance;

public class ChannelDataListModel extends AbstractListModel<ChannelInstance> {

/**
* serialVersionUID
*/
private static final long serialVersionUID = 7576558662916716790L;

@Override
public int getSize() {
return SkadiMain.getInstance().getChannels().size();
}

@Override
public ChannelInstance getElementAt(final int index) {
return SkadiMain.getInstance().getChannels().values().toArray(new ChannelInstance[0])[index];
}

public void handleUpdate() {
this.fireContentsChanged(this, 0, this.getSize());
}

}
52 changes: 52 additions & 0 deletions src/main/java/eu/over9000/skadi/gui/ChannelDataListRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package eu.over9000.skadi.gui;

import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

import eu.over9000.skadi.channel.ChannelInstance;

public class ChannelDataListRenderer implements ListCellRenderer<ChannelInstance> {

private final ChannelDataListDisplayer displayer = new ChannelDataListDisplayer();

@Override
public Component getListCellRendererComponent(final JList<? extends ChannelInstance> list,
final ChannelInstance value, final int index, final boolean isSelected, final boolean cellHasFocus) {
return this.displayer.renderFor(list, value, isSelected);
}

private class ChannelDataListDisplayer extends JPanel {

/**
* serialVersionUID
*/
private static final long serialVersionUID = 3574128645388761068L;

private final JLabel labelURL;

public ChannelDataListDisplayer() {
this.labelURL = new JLabel();

this.add(this.labelURL);
}

public Component renderFor(final JList<? extends ChannelInstance> list, final ChannelInstance value,
final boolean isSelected) {
this.labelURL.setText(value.getURL());

if (isSelected) {
this.setBackground(list.getSelectionBackground());
this.setForeground(list.getSelectionForeground());
} else {
this.setBackground(list.getBackground());
this.setForeground(list.getForeground());
}
return this;
}

}
}
Loading

0 comments on commit a9593c1

Please sign in to comment.