Commit 43f328be authored by Ondra's avatar Ondra
Browse files

Updating GUI, revorked some classes

parent 826d1f22
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
----------------------------------------------------------------
Mon May 26 22:53:18 CEST 2014:
Booting Derby version The Apache Software Foundation - Apache Derby - 10.10.2.0 - (1582446): instance a816c00e-0146-3a4f-0f22-000000b16af0
on database directory C:\Users\Ondra\Documents\NetBeansProjects\Training diary\database with class loader sun.misc.Launcher$AppClassLoader@df6ccd
Fri May 30 00:40:51 CEST 2014:
Booting Derby version The Apache Software Foundation - Apache Derby - 10.10.2.0 - (1582446): instance a816c00e-0146-4a24-995b-000000b33730
on database directory C:\Users\Ondra\Documents\NetBeansProjects\Training diary\database with class loader sun.misc.Launcher$AppClassLoader@601bb1
Loaded from file:/C:/Users/Ondra/Documents/NetBeansProjects/Training%20diary/libraries/derby.jar
java.vendor=Oracle Corporation
java.runtime.version=1.7.0_40-b43
......
......@@ -86,6 +86,6 @@ public class TypeEntity extends BaseEntity implements Serializable {
*/
@Override
public String toString() {
return "Typ tréninku: " + type + "\n\n";
return "Type: " + type + "\n\n";
}
}
......@@ -3,25 +3,19 @@ package cz.cvut.fel.skoumond.gui;
import cz.cvut.fel.skoumond.utilities.DateReader;
import cz.cvut.fel.skoumond.entities.RunEntity;
import cz.cvut.fel.skoumond.entities.TypeEntity;
import cz.cvut.fel.skoumond.exceptions.NoConnectionException;
import cz.cvut.fel.skoumond.sql.RunRepository;
import cz.cvut.fel.skoumond.sql.TypeRepository;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
......@@ -51,17 +45,16 @@ public class AddActivityDialog extends JDialog {
//private JButton jButton;
private JComboBox<TypeEntity> jComboBox;
private List<TypeEntity> arrayList = TypeRepository.getInstance().all();
private TypeEntity tmp;
private TypeEntity activityType;
private RunEntity entity;
private JPanel jPanel;
private JLabel jLabel;
JFrame jFrame;
static final String gapList[] = {"0", "10", "15", "20"};
final static int maxGap = 50;
private Mainframe mainframe;
public AddActivityDialog(JFrame jFrame) {
super(jFrame, "New Activity", true);
public AddActivityDialog(Mainframe mainframe) {
super(mainframe, "New Activity", true);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
......@@ -70,140 +63,91 @@ public class AddActivityDialog extends JDialog {
pane.add(new JSeparator(), BorderLayout.CENTER);
pane.add(getButtonPanel(), BorderLayout.SOUTH);
this.mainframe = mainframe;
//setSize(100, 200);
pack();
setLocationRelativeTo(null);
setVisible(true);
/*
jComboBox = new JComboBox<>();
for (TypeEntity typeEntity : arrayList) {
jComboBox.addItem(typeEntity);
}
jComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent ie) {
tmp = jComboBox.getItemAt(jComboBox.getSelectedIndex());
}
});
desc = new JLabel("Description: ");
sessionTime = new JLabel("Session length: ");
distInKm = new JLabel("Distance: ");
avrgHeartRate = new JLabel("Average Heart rate: ");
jTextField2 = new JTextField(10);
jTextField3= new JTextField(10);
jTextField4 = new JTextField(10);
jTextField5 = new JTextField(10);
jButton = new JButton("Ok");
this.setLayout(new BorderLayout());
JPanel panel = new JPanel(new FlowLayout());
panel.add(dateTime);
panel.add(dateField);
panel.add(desc);
panel.add(jTextField2);
panel.add(sessionTime);
panel.add(jTextField3);
panel.add(distInKm);
panel.add(jTextField4);
panel.add(avrgHeartRate);
panel.add(jTextField5);
panel.add(jComboBox);
panel.setAlignmentX(CENTER_ALIGNMENT);
panel.setAlignmentY(CENTER_ALIGNMENT);
add(panel, BorderLayout.CENTER);
add(jButton, BorderLayout.SOUTH);
pack();
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
entity = new RunEntity(RunRepository.getInstance().getFreeId(),
getCalendar(),
jTextField2.getText(),
Integer.parseInt(jTextField3.getText()),
Float.parseFloat(jTextField4.getText()),
Integer.parseInt(jTextField5.getText()),
tmp);
RunRepository.getInstance().save(entity);
} catch (Exception ex) {
free();
}
}
});
*/
}
private JPanel getJPanel() {
GridLayout gridLayout = new GridLayout(5,2);
GridLayout gridLayout = new GridLayout(6, 2);
setResizable(false);
jPanel = new JPanel();
jPanel.setLayout(gridLayout);
JLabel cellWidth = new JLabel("********************");
Dimension labelsize = cellWidth.getPreferredSize();
jPanel.setPreferredSize(new Dimension((int)(labelsize.getWidth() * 2.5)+maxGap,
(int)(labelsize.getHeight() * 3.5)+maxGap * 2));
jLabel = new JLabel("Enter date and time: ", JLabel.LEFT);
jPanel.setPreferredSize(new Dimension((int) (labelsize.getWidth() * 2.5) + maxGap,
(int) (labelsize.getHeight() * 3.5) + maxGap * 2));
jLabel = new JLabel(" Enter date and time: ", JLabel.LEFT);
jPanel.add(jLabel);
dateField = new JTextField(10);
jLabel.setLabelFor(dateField);
jPanel.add(dateField);
jLabel = new JLabel("Enter description: ", JLabel.LEFT);
jLabel = new JLabel(" Enter description: ", JLabel.LEFT);
jPanel.add(jLabel);
descField = new JTextField(10);
jLabel.setLabelFor(descField);
jPanel.add(descField);
jLabel = new JLabel("Enter activity lenght: ", JLabel.LEFT);
jLabel = new JLabel(" Enter activity lenght: ", JLabel.LEFT);
jPanel.add(jLabel);
sessionTimeField = new JTextField(10);
jLabel.setLabelFor(sessionTimeField);
jPanel.add(sessionTimeField);
jLabel = new JLabel("Enter running distance (Km) :", JLabel.LEFT);
jLabel = new JLabel(" Enter running distance (Km) :", JLabel.LEFT);
jPanel.add(jLabel);
distInKmField = new JTextField(10);
jLabel.setLabelFor(distInKmField);
jPanel.add(distInKmField);
jLabel = new JLabel("Enter average heart rate: ", JLabel.LEFT);
jLabel = new JLabel(" Enter average heart rate: ", JLabel.LEFT);
jPanel.add(jLabel);
avrgHeartRateField = new JTextField(10);
jLabel.setLabelFor(avrgHeartRateField);
jPanel.add(avrgHeartRateField);
jLabel = new JLabel(" Choose the activity type: ", JLabel.LEFT);
jPanel.add(jLabel);
jPanel.add(getComboBox());
return jPanel;
}
private JPanel getButtonPanel() {
private JComboBox getComboBox() {
jComboBox = new JComboBox<>();
for (TypeEntity typeEntity : arrayList) {
jComboBox.addItem(typeEntity);
}
jComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent ie) {
activityType = jComboBox.getItemAt(jComboBox.getSelectedIndex());
}
});
return jComboBox;
}
private JPanel getButtonPanel() {
JPanel panel = new JPanel();
JButton jButton = new JButton("Ok");
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
//onDialogOk();
onDialogOk();
}
});
panel.add(jButton);
......@@ -217,11 +161,36 @@ public class AddActivityDialog extends JDialog {
panel.add(jButton);
return panel;
}
private void onDialogOk() {
try {
mainframe.createNewRow(
getCalendar(),
descField.getText(),
Integer.parseInt(sessionTimeField.getText()),
Float.parseFloat(distInKmField.getText()),
Integer.parseInt(avrgHeartRateField.getText()),
activityType);
JOptionPane.showMessageDialog(this,
"New activity added.",
"Information Message",
JOptionPane.INFORMATION_MESSAGE);
setVisible(false);
dispose();
} catch (NumberFormatException | NoConnectionException ex) {
JOptionPane.showMessageDialog(this,
"Bad entered information. Try it again.",
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
private void onDialogCancel() {
setVisible(false);
dispose();
}
public RunEntity getEntity() {
......@@ -229,10 +198,6 @@ public class AddActivityDialog extends JDialog {
}
private Calendar getCalendar() {
return DateReader.getInstance().readDateTime(dateField.getText());
}
private void free() {
dispose();
return new DateReader().readDateTime(dateField.getText());
}
}
\ No newline at end of file
......@@ -33,18 +33,20 @@ class LoginDialog extends JDialog {
private Connection connect;
private JPanel jPanel;
private JLabel jLabel;
private JFrame controllingFrame; //needed for dialogs
private JFrame jFrame; //needed for dialogs
public LoginDialog(JFrame jFrame) {
super(jFrame, "Loading Training activities...", true);
this.jFrame = jFrame;
initDialogLayout();
setSize(300, 150);
setLocationRelativeTo(null);
notCancelled = false;
//pack();
pack();
setVisible(true);
}
......@@ -108,7 +110,7 @@ class LoginDialog extends JDialog {
if ((isPasswordCorrect(password)) && (isUsernameCorrect(username))) {
success = true;
} else {
JOptionPane.showMessageDialog(controllingFrame,
JOptionPane.showMessageDialog(jFrame,
"Bad password or username. Try it again.",
"Error Message",
JOptionPane.ERROR_MESSAGE);
......
This diff is collapsed.
......@@ -65,8 +65,23 @@ class ResultSetTableModel extends AbstractTableModel {
return out;
}
public void addRow(RunEntity entity) {
public void addRow(Calendar dateTime, String desc,
int sessionTime, float distInKm,
int avrgHeartRate, TypeEntity type) {
// Create new run object
RunEntity entity = new RunEntity(
RunRepository.getInstance().getFreeId(),
dateTime, desc, sessionTime, distInKm,
avrgHeartRate, type
);
// Update actual tablemodel
arrayList.add(entity);
// Update database
RunRepository.getInstance().save(entity);
// Get database know something happened
fireTableDataChanged();
}
......
......@@ -46,4 +46,6 @@ public interface IRepository<T> {
* @param entity
*/
public void delete(T entity);
public int getFreeId();
}
......@@ -49,8 +49,4 @@ public abstract class Repository<T extends BaseEntity> implements IRepository<T>
throw new NoConnectionException();
}
}
public int getFreeId() {
return 1;
}
}
......@@ -41,6 +41,8 @@ public class RunRepository extends Repository<RunEntity> {
* Příkaz SQL pro smazání daných dat z databáze.
*/
private final String DELETE = "DELETE FROM APP.RUN WHERE R_ID = ?";
private final String GET_ID = "SELECT R_ID FROM APP.RUN ORDER BY R_ID";
private RunRepository() {
entities = new HashMap();
......@@ -202,4 +204,30 @@ public class RunRepository extends Repository<RunEntity> {
throw new NoConnectionException(e.getMessage());
}
}
@Override
public int getFreeId() {
int i;
try (Connection con = getConnection();
PreparedStatement ps = con.prepareStatement(GET_ID)) {
//DriverManager.setLogWriter(new PrintWriter(new BufferedOutputStream(System.out)));
ResultSet rs = ps.executeQuery();
for (i = 1; rs.next(); i++) {
if (rs.getInt(1) != i) {
// return free ID
return i;
}
}
// else return first free ID out of database
return i;
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException(e.getMessage());
}
}
}
......@@ -40,6 +40,8 @@ public class TypeRepository extends Repository<TypeEntity> {
* Příkaz SQL pro smazání daných tréninkových typů z databáze.
*/
private final String DELETE = "DELETE FROM APP.TYPE WHERE T_ID = ?";
private final String GET_ID = "SELECT T_ID FROM APP.TYPE ORDER BY T_ID";
private TypeRepository() {
entities = new HashMap();
......@@ -177,4 +179,30 @@ public class TypeRepository extends Repository<TypeEntity> {
throw new NoConnectionException(e.getMessage());
}
}
@Override
public int getFreeId() {
int i;
try (Connection con = getConnection();
PreparedStatement ps = con.prepareStatement(GET_ID)) {
//DriverManager.setLogWriter(new PrintWriter(new BufferedOutputStream(System.out)));
ResultSet rs = ps.executeQuery();
for (i = 1; rs.next(); i++) {
if (rs.getInt(1) != i) {
// return free ID
return i;
}
}
// else return first free ID out of database
return i;
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException(e.getMessage());
}
}
}
......@@ -4,14 +4,15 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* Třída sloužící pro správné získání formátu data a času
*
* @author Ondra
*/
public class DateReader {
private static DateReader instance = null;
/**
* formatovani dne, mesice, roku, hodiny a minut, staticke aby bylo
* pouzitelne pro vsechny tridy
......@@ -21,21 +22,16 @@ public class DateReader {
* formatovani data bez casu
*/
public static DateFormat formatDataWithountTime = new SimpleDateFormat("d.M.yyyy");
boolean read;
/**
* vytvoreni instance podporuj9c9 Gui
*/
private DateReader() {
instance = new DateReader();
public DateReader() {
read = true;
}
public static DateReader getInstance() {
if (instance == null) {
instance = new DateReader();
}
return instance;
}
/**
* Nacita fotmatovane datum, vraci instanci calendare, rekurzivne vola sebe
* sama pri spatnem zadani
......@@ -51,7 +47,7 @@ public class DateReader {
dateTime.setLenient(false);
dateTime.getTime();
} catch (Exception e) {
//return readCalendar(formatData);
read = false; //throw e;
}
return dateTime;
}
......@@ -61,6 +57,9 @@ public class DateReader {
*/
public Calendar readDateTime(String string) {
//"Zadejte datum a čas ve tvaru [1.1.2013 14:00]
if (!read) {
throw new NumberFormatException();
}
return readCalendar(formatData, string);
}
......@@ -69,6 +68,9 @@ public class DateReader {
*/
public Calendar readDate(String string) {
//System.out.println("Zadejte datum ve tvaru [1.1.2013]:");
if (!read) {
throw new NumberFormatException();
}
return readCalendar(formatDataWithountTime, string);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment