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 * @author Everel
* *
*/ */
public class ServerDescription { public class ServerDescription {
public String serverName = null; private String serverName = null;
public String author = null; private String author = null;
public double revision = 0; private double revision = 0;
public ServerDescription(final String serverName, final String author, public ServerDescription(final String serverName, final String author,
final double revision) { final double revision) {
@@ -16,10 +16,23 @@ public class ServerDescription {
this.author = author; this.author = author;
this.revision = revision; this.revision = revision;
} }
public String getServerName() {
return this.serverName;
}
public String getAuthor() {
return this.author;
}
public double getRevision() {
return this.revision;
}
@Override @Override
public String toString() { 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 final long serialVersionUID = 5238720307271493899L;
private static ServerSelector instance = null; private static ServerSelector instance = null;
private JPanel panel; private JPanel panel;
public static String initServer = null; public static String initServer = null;
public static ServerSelector getInstance() { public static ServerSelector getInstance() {
@@ -39,10 +39,10 @@ public class ServerSelector extends JFrame {
} }
public ServerSelector() { public ServerSelector() {
Queue<ServerWidget> widgets = getServers(); Queue<ServerWidget> widgets = getServers();
if(initServer != null) { if (initServer != null) {
if(runServer(widgets)) { if (runServer(widgets)) {
initServer = null; initServer = null;
return; return;
} }
@@ -53,23 +53,30 @@ public class ServerSelector extends JFrame {
this.setResizable(false); this.setResizable(false);
this.panel = new JPanel(new BorderLayout()); 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); JPanel interior = new JPanel(null);
interior.setPreferredSize(new Dimension(400, widgets.size() * 100));
int i = 0; int i = 0;
int y = 0;
while (widgets != null && !widgets.isEmpty()) { while (widgets != null && !widgets.isEmpty()) {
final ServerWidget w = widgets.poll(); final ServerWidget w = widgets.poll();
w.setSize(400, 100); w.setSize(300, 100);
w.setLocation(0, i * 100); if(i % 2 == 0 && i != 0) {
y += 100;
}
w.setLocation(i % 2 == 0 ? 0 : 300, y);
interior.add(w); interior.add(w);
i++; i++;
} }
y += 100;
interior.setPreferredSize(new Dimension(300, y));
JScrollPane scrlInterior = new JScrollPane(interior); JScrollPane scrlInterior = new JScrollPane(interior);
scrlInterior.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scrlInterior
scrlInterior.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrlInterior
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.panel.add(scrlInterior, BorderLayout.CENTER); this.panel.add(scrlInterior, BorderLayout.CENTER);
this.add(panel); this.add(panel);
@@ -77,19 +84,20 @@ public class ServerSelector extends JFrame {
SwingUtil.finalize(this); SwingUtil.finalize(this);
} }
/** /**
* This method is called when -server argument is given * This method is called when -server argument is given
*
* @param widgets * @param widgets
*/ */
private boolean runServer(Queue<ServerWidget> widgets) { private boolean runServer(Queue<ServerWidget> widgets) {
// TODO: test this method // TODO: test this method
if(widgets == null || widgets.isEmpty()) { if (widgets == null || widgets.isEmpty()) {
return false; return false;
} }
final String serverName = initServer.toLowerCase(); final String serverName = initServer.toLowerCase();
for(ServerWidget widget : widgets) { for (ServerWidget widget : widgets) {
if(widget.desc.serverName.toLowerCase().equals(serverName)) { if (widget.desc.getServerName().toLowerCase().equals(serverName)) {
Environment.load(widget.desc); Environment.load(widget.desc);
return true; return true;
} }
@@ -99,6 +107,7 @@ public class ServerSelector extends JFrame {
/** /**
* Fetches array of server widgets * Fetches array of server widgets
*
* @return widgets array * @return widgets array
*/ */
public Queue<ServerWidget> getServers() { public Queue<ServerWidget> getServers() {
@@ -1,14 +1,15 @@
package org.parabot.core.ui.widgets; package org.parabot.core.ui.widgets;
import java.awt.Color; import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font; import java.awt.Font;
import java.awt.GradientPaint; import java.awt.FontMetrics;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.event.ActionEvent; import java.awt.event.MouseEvent;
import java.awt.event.ActionListener; import java.awt.event.MouseListener;
import javax.swing.JButton; import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import org.parabot.core.desc.ServerDescription; import org.parabot.core.desc.ServerDescription;
@@ -22,49 +23,22 @@ import org.parabot.environment.Environment;
* @author Everel * @author Everel
* *
*/ */
public class ServerWidget extends JPanel { public class ServerWidget extends JPanel implements MouseListener,
MouseMotionListener {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private String name = null; private String name = null;
public ServerDescription desc = null; public ServerDescription desc = null;
private boolean hovered = false;
final ActionListener play = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
load(desc);
}
};
public ServerWidget(final ServerDescription desc) { public ServerWidget(final ServerDescription desc) {
this.desc = desc; this.desc = desc;
setLayout(null); setLayout(null);
this.name = desc.serverName.replaceAll(" ", ""); this.name = desc.getServerName().replaceAll(" ", "");
JLabel l = new JLabel();
l.setFont(new Font("Arial", Font.BOLD, 16)); addMouseListener(this);
l.setForeground(Color.white); addMouseMotionListener(this);
l.setText(desc.serverName); setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
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);
} }
@Override @Override
@@ -80,11 +54,28 @@ public class ServerWidget extends JPanel {
setOpaque(true); setOpaque(true);
int w = getWidth(); int w = getWidth();
int h = getHeight(); int h = getHeight();
Color color1 = new Color(41, 97, 105);
Color color2 = color1.darker(); Color bgColor = Color.LIGHT_GRAY;
GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2); if (hovered) {
g2d.setPaint(gp); bgColor = Color.GRAY;
}
g2d.setColor(bgColor);
g2d.fillRect(0, 0, w, h); 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) { public void load(final ServerDescription desc) {
@@ -98,4 +89,44 @@ public class ServerWidget extends JPanel {
} }
}).start(); }).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) {
}
} }