Update server selector GUI

This commit is contained in:
Clisprail
2013-12-13 00:37:38 +01:00
parent e981a1a32e
commit 0e66ab2010
3 changed files with 117 additions and 64 deletions
@@ -3,12 +3,12 @@ package org.parabot.core.desc;
/**
*
* @author Everel
*
*
*/
public class ServerDescription {
public String serverName = null;
public String author = null;
public double revision = 0;
private String serverName = null;
private String author = null;
private double revision = 0;
public ServerDescription(final String serverName, final String author,
final double revision) {
@@ -16,10 +16,23 @@ public class ServerDescription {
this.author = author;
this.revision = revision;
}
public String getServerName() {
return this.serverName;
}
public String getAuthor() {
return this.author;
}
public double getRevision() {
return this.revision;
}
@Override
public String toString() {
return String.format("[Server: %s, Author: %s, Revision: %.1f]", this.serverName, this.author, this.revision);
return String.format("[Server: %s, Author: %s, Revision: %.2f]",
this.serverName, this.author, this.revision);
}
}
@@ -28,7 +28,7 @@ public class ServerSelector extends JFrame {
private static final long serialVersionUID = 5238720307271493899L;
private static ServerSelector instance = null;
private JPanel panel;
public static String initServer = null;
public static ServerSelector getInstance() {
@@ -39,10 +39,10 @@ public class ServerSelector extends JFrame {
}
public ServerSelector() {
Queue<ServerWidget> widgets = getServers();
if(initServer != null) {
if(runServer(widgets)) {
if (initServer != null) {
if (runServer(widgets)) {
initServer = null;
return;
}
@@ -53,23 +53,30 @@ public class ServerSelector extends JFrame {
this.setResizable(false);
this.panel = new JPanel(new BorderLayout());
this.panel.setPreferredSize(new Dimension(400, 200));
this.panel.setPreferredSize(new Dimension(600, 400));
JPanel interior = new JPanel(null);
interior.setPreferredSize(new Dimension(400, widgets.size() * 100));
int i = 0;
int y = 0;
while (widgets != null && !widgets.isEmpty()) {
final ServerWidget w = widgets.poll();
w.setSize(400, 100);
w.setLocation(0, i * 100);
w.setSize(300, 100);
if(i % 2 == 0 && i != 0) {
y += 100;
}
w.setLocation(i % 2 == 0 ? 0 : 300, y);
interior.add(w);
i++;
}
y += 100;
interior.setPreferredSize(new Dimension(300, y));
JScrollPane scrlInterior = new JScrollPane(interior);
scrlInterior.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrlInterior.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrlInterior
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrlInterior
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.panel.add(scrlInterior, BorderLayout.CENTER);
this.add(panel);
@@ -77,19 +84,20 @@ public class ServerSelector extends JFrame {
SwingUtil.finalize(this);
}
/**
* This method is called when -server argument is given
*
* @param widgets
*/
private boolean runServer(Queue<ServerWidget> widgets) {
// TODO: test this method
if(widgets == null || widgets.isEmpty()) {
if (widgets == null || widgets.isEmpty()) {
return false;
}
final String serverName = initServer.toLowerCase();
for(ServerWidget widget : widgets) {
if(widget.desc.serverName.toLowerCase().equals(serverName)) {
for (ServerWidget widget : widgets) {
if (widget.desc.getServerName().toLowerCase().equals(serverName)) {
Environment.load(widget.desc);
return true;
}
@@ -99,6 +107,7 @@ public class ServerSelector extends JFrame {
/**
* Fetches array of server widgets
*
* @return widgets array
*/
public Queue<ServerWidget> getServers() {
@@ -1,14 +1,15 @@
package org.parabot.core.ui.widgets;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import org.parabot.core.desc.ServerDescription;
@@ -22,49 +23,22 @@ import org.parabot.environment.Environment;
* @author Everel
*
*/
public class ServerWidget extends JPanel {
public class ServerWidget extends JPanel implements MouseListener,
MouseMotionListener {
private static final long serialVersionUID = 1L;
private String name = null;
public ServerDescription desc = null;
final ActionListener play = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
load(desc);
}
};
private boolean hovered = false;
public ServerWidget(final ServerDescription desc) {
this.desc = desc;
setLayout(null);
this.name = desc.serverName.replaceAll(" ", "");
JLabel l = new JLabel();
l.setFont(new Font("Arial", Font.BOLD, 16));
l.setForeground(Color.white);
l.setText(desc.serverName);
l.setBounds(10, 10, 200, 20);
add(l);
final Font f = new Font("Arial", Font.PLAIN, 12);
add(l);
l = new JLabel();
l.setFont(f);
l.setForeground(Color.white);
l.setBounds(10, 45, 100, 20);
l.setText("Author: " + desc.author);
add(l);
l = new JLabel();
l.setFont(f);
l.setForeground(Color.white);
l.setBounds(10, 60, 100, 20);
l.setText("Revision: " + desc.revision);
add(l);
final JButton b = new JButton("Start");
b.setFocusable(false);
b.setBounds(300, 70, 70, 20);
b.setOpaque(false);
b.addActionListener(play);
add(b);
this.name = desc.getServerName().replaceAll(" ", "");
addMouseListener(this);
addMouseMotionListener(this);
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
@Override
@@ -80,11 +54,28 @@ public class ServerWidget extends JPanel {
setOpaque(true);
int w = getWidth();
int h = getHeight();
Color color1 = new Color(41, 97, 105);
Color color2 = color1.darker();
GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);
g2d.setPaint(gp);
Color bgColor = Color.LIGHT_GRAY;
if (hovered) {
bgColor = Color.GRAY;
}
g2d.setColor(bgColor);
g2d.fillRect(0, 0, w, h);
g.setColor(Color.black);
Font title = new Font("Arial", Font.BOLD, 16);
g.setFont(title);
String serverName = desc.getServerName();
int sw = g.getFontMetrics().stringWidth(serverName);
g.drawString(serverName, (w / 2) - (sw / 2), 30);
Font normal = new Font("Arial", Font.PLAIN, 12);
g.setFont(normal);
FontMetrics fm = g.getFontMetrics();
String author = "Author: " + desc.getAuthor();
String revision = "Revision: " + desc.getRevision();
g.drawString(author, (w / 2) - (fm.stringWidth(author) / 2), 55);
g.drawString(revision, (w / 2) - (fm.stringWidth(revision) / 2), 70);
}
public void load(final ServerDescription desc) {
@@ -98,4 +89,44 @@ public class ServerWidget extends JPanel {
}
}).start();
}
@Override
public void mouseMoved(MouseEvent e) {
if (!hovered) {
hovered = true;
this.repaint();
}
}
@Override
public void mouseExited(MouseEvent e) {
if (hovered) {
hovered = false;
this.repaint();
}
}
@Override
public void mousePressed(MouseEvent e) {
if (hovered) {
load(desc);
}
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}