Complete world record in footer and settings

This commit is contained in:
olivier 2018-02-18 14:23:06 +01:00
parent 4c87a025d6
commit 0fa050d80e
4 changed files with 173 additions and 35 deletions

View file

@ -1,34 +1,42 @@
package org.fenix.WorldRecord;
import javax.swing.*;
import org.fenix.llanfair.dialog.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.ArrayList;
public class RecordDialog extends JDialog
public class RecordDialog extends LlanfairDialog
{
private JLabel searchLabel = new JLabel("Search game:");
private JTextField searchField = new JTextField();
private JButton searchButton = new JButton("Search");
private JTextField searchField = new JTextField();
private JLabel gamesLabel = new JLabel("Games:");
private DefaultComboBoxModel<Game> gameListModel = new DefaultComboBoxModel<>();
private JComboBox<Game> games = new JComboBox<>(gameListModel);
private JLabel gamesLabel = new JLabel("Games:");
private JLabel categoriesLabel = new JLabel("Categories:");
private DefaultComboBoxModel<Category> categoryListModel = new DefaultComboBoxModel<>();
private JComboBox<Category> categories = new JComboBox<>(categoryListModel);
private JLabel categoriesLabel = new JLabel("Categories:");
private JLabel worldRecord = new JLabel();
private JButton ok = new JButton("Ok");
private JButton close = new JButton("Close");
private JButton ok = new JButton("Ok");
private JLabel worldRecord = new JLabel();
private String category_id = "";
private ActionListener categoryListener;
private ActionListener gameListener;
public RecordDialog()
private Category category;
private EditRun editRun;
public RecordDialog(EditRun editRun)
{
this.editRun = editRun;
JPanel searchPanel = new JPanel(new FlowLayout());
{
searchField.setPreferredSize(new Dimension(200,30));
@ -50,13 +58,6 @@ public class RecordDialog extends JDialog
gamesPanel.add(games);
games.setEnabled(false);
games.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
getCategories((Game) games.getSelectedItem());
}
});
}
JPanel categoriesPanel = new JPanel(new GridLayout(1,2));
{
@ -64,13 +65,6 @@ public class RecordDialog extends JDialog
categoriesPanel.add(categories);
categories.setEnabled(false);
categories.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
getWorldRecord((Category) categories.getSelectedItem());
}
});
}
JPanel buttonPanel = new JPanel(new FlowLayout());
{
@ -106,20 +100,31 @@ public class RecordDialog extends JDialog
pack();
}
/**
* Search the game title on speedrun.com
* @param name The name of the game
*/
private void searchGame(String name)
{
this.resetFields();
ArrayList<Game> games = new ArrayList<>();
try {
games = WorldRecordParser.searchGames(name);
} catch (IOException e)
{
System.out.println("fout");
showError();
}
this.setGames(games);
this.addGameListener();
}
/**
* Get the categories for a game on speedrun.com
* @param game A game object received from the game search
*/
private void getCategories(Game game)
{
ArrayList<Category> categories = new ArrayList<>();
@ -128,12 +133,17 @@ public class RecordDialog extends JDialog
categories = WorldRecordParser.getCategories(game);
} catch (IOException e)
{
System.out.println("fout");
showError();
}
this.setCategories(categories);
this.addCategoryListener();
}
/**
* Get the world record time and owner from speedrun.com
* @param category A category object received from the category search
*/
private void getWorldRecord(Category category)
{
String worldRecord = "";
@ -142,16 +152,20 @@ public class RecordDialog extends JDialog
worldRecord = WorldRecordParser.getRecord(category);
} catch (IOException e)
{
System.out.println("fout");
showError();
}
this.worldRecord.setText(worldRecord);
this.category_id = category.getId();
this.category = category;
}
/**
* Append the games in the array to the games combobox
* @param games A ArrayList of game objects
*/
private void setGames(ArrayList<Game> games)
{
gameListModel.removeAllElements();
this.resetFields();
for(Game game: games)
{
@ -161,6 +175,10 @@ public class RecordDialog extends JDialog
this.games.setEnabled(true);
}
/**
* Append the categories in the array to the categories checkbox
* @param categories A ArrayList of Category objects
*/
private void setCategories(ArrayList<Category> categories)
{
categoryListModel.removeAllElements();
@ -173,14 +191,95 @@ public class RecordDialog extends JDialog
this.categories.setEnabled(true);
}
/**
* Close the dialog without saving
*/
private void close()
{
this.setVisible(false);
}
/**
* Close the dialog and send a signal to the parent window
*/
private void actionOk()
{
this.category_id = ((Category) categories.getSelectedItem()).getId();
this.category = ((Category) categories.getSelectedItem());
this.setVisible(false);
this.editRun.recordSet();
}
/**
* Add the changeListener to the categories combobox
*/
private void addCategoryListener()
{
categoryListener = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
getWorldRecord((Category) categories.getSelectedItem());
}
};
categories.addActionListener(categoryListener);
}
/**
* Add the changeListener to the games combobox
*/
private void addGameListener()
{
gameListener = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
getCategories((Game) games.getSelectedItem());
}
};
games.addActionListener(gameListener);
}
/**
* Reset the comboboxes
*/
private void resetFields()
{
try {
categories.removeActionListener(categoryListener);
games.removeActionListener(gameListener);
} catch (Exception e){}
categories.setEnabled(false);
games.setEnabled(false);
gameListModel.removeAllElements();
categoryListModel.removeAllElements();
}
public static void showError()
{
JOptionPane.showMessageDialog(null, "Could not connect to speedrun.com",
"Error", JOptionPane.ERROR_MESSAGE);
}
/**
* Get the selected category object
* @return Category
*/
public Category getCategory()
{
return category;
}
/**
* Get the selected record string
* @return String
*/
public String getRecordString()
{
return worldRecord.getText();
}
}

View file

@ -117,9 +117,9 @@ public class WorldRecordParser
String time = "";
if(hours != 0)
time += hours + ":";
time += String.format("%02d:", hours);
time += minutes + ":" + seconds;
time += String.format("%02d:%02d", minutes, seconds);
if(time_seconds % 1 != 0)
{

View file

@ -1,6 +1,8 @@
package org.fenix.llanfair;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import org.fenix.WorldRecord.Category;
import org.fenix.WorldRecord.WorldRecordParser;
import org.fenix.llanfair.config.Settings;
import org.fenix.utils.TableModelSupport;
import org.fenix.utils.config.Configuration;
@ -104,7 +106,7 @@ public class Run implements TableModel, Serializable {
public static final String COMPLETED_ATTEMPT_COUNTER_PROPERTY = "run.completedAttemptCounter";
public static final String RECORD_CATEGORY_PROPERTY = "run.record.category";
public static final String DELAYED_START_PROPERTY = "run.delayedStart";
@ -185,6 +187,8 @@ public class Run implements TableModel, Serializable {
@XStreamOmitField
private int sessionAttempts;
private Category recordCategory;
// ----------------------------------------------------------- CONSTRUCTORS
/**
@ -598,6 +602,23 @@ public class Run implements TableModel, Serializable {
public int getSessionAttempts() { return sessionAttempts; }
public String getRecordString() {
String recordString;
try {
recordString = WorldRecordParser.getRecord(this.recordCategory);
} catch (Exception e) {
recordString = "Unknown World Record";
}
return recordString;
}
public Category getRecordCategory()
{
return recordCategory;
}
// ---------------------------------------------------------------- SETTERS
public<T> T getSetting( String key ) {
@ -648,6 +669,15 @@ public class Run implements TableModel, Serializable {
pcSupport.firePropertyChange(DELAYED_START_PROPERTY, old, delayedStart);
}
public void setRecordCategory(Category category)
{
Category old = this.recordCategory;
this.recordCategory = category;
pcSupport.firePropertyChange(RECORD_CATEGORY_PROPERTY, old, name);
}
/**
* Inserts the given segment at the end. If it's the first segment being
* added, the run becomes {@link State#READY}, meaning it can be started.
@ -1155,6 +1185,9 @@ public class Run implements TableModel, Serializable {
if ( configuration == null ) {
configuration = new Configuration();
}
if(recordCategory == null) {
recordCategory = new Category("", "");
}
}
/**

View file

@ -29,6 +29,7 @@ class Footer extends JPanel {
private static final int TEXT = 0x04;
private static final int BEST = 0x08;
private static final int VERBOSE = 0x10;
private static final int WORLD_RECORD = 0x20;
private static final int INSET = 3;
@ -90,7 +91,7 @@ class Footer extends JPanel {
preferredSize = null;
resize = false;
worldRecord = new JLabel("World record: 5 by xem92");
worldRecord = new JLabel();
setRun(run);
setOpaque(false);
@ -252,6 +253,8 @@ class Footer extends JPanel {
updateValues(TIME | TEXT);
updateSize();
forceResize();
} else if (Run.RECORD_CATEGORY_PROPERTY.equals(property)) {
updateValues(WORLD_RECORD);
}
}
@ -585,6 +588,9 @@ class Footer extends JPanel {
labelDeltaBest.setText("" + Language.LB_FT_DELTA_BEST);
labelSumOfBest.setText("" + Language.LB_FT_SUM_OF_BEST);
}
if((identifier & WORLD_RECORD) == WORLD_RECORD) {
worldRecord.setText(run.getRecordString());
}
}
private void updateSize() {