package tyo_8;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import BreezySwing.GBDialog;
import BreezySwing.GBFrame;
public class EditPrivateCourse_GUI extends GBDialog
{
static final long serialVersionUID = 1;
/**
* variable to move information from GUI and database.
*/
private PrivateCourse privateCourse;
private Omaedu_database omaedu;
private Classroom classroom;
private Educator educator;
private ArrayList<Classroom> roomlist;
private ArrayList<Educator> edulist;
private Iterator<Classroom> roomiter;
private Iterator<Educator> eduiter;
private int identification=0;
private ArrayList<PrivateCourse> temp = new ArrayList<PrivateCourse>();
private Iterator<PrivateCourse> iter;
/**
* Button to add privateCourse-object information.
*/
private JButton edit =
addButton("Edit", 15, 6, 1, 1);
private JButton choosecourse =
addButton("Open course", 2, 4, 1, 1);
/**
* labels
*/
private JLabel headLabel =
addLabel("Course information", 1,3,1,1);
private JLabel setcourse =
addLabel("Choose course by id:", 2,2,1,1);
private JLabel privateCourseIdLabel =
addLabel("Course id", 3,2,1,1);
private JLabel privateCourseTitleLabel =
addLabel("Coursetitle", 4,2,1,1);
private JLabel privateCourseTypeLabel =
addLabel("Coursetype", 5,2,1,1);
private JLabel privateCourseSubjectLabel =
addLabel("Coursesubject", 6,2,1,1);
private JLabel privateCourseContent =
addLabel("Coursecontent", 7,2,1,1);
private JLabel privateCoursePriceLabel =
addLabel("Courseprice", 8,2,1,1);
private JLabel privateCourseMaxParticipantsLabel =
addLabel("Maxparticipants", 9,2,1,1);
private JLabel privateCourseMinParticipantsLabel =
addLabel("Minparticipants", 10,2,1,1);
private JLabel privateCourseDateLabel =
addLabel("Coursedate", 11,2,1,1);
private JLabel privateCourseClassroomLabel =
addLabel("Classroom", 12,2,1,1);
private JLabel privateCoursePersonnelLabel =
addLabel("Personnel", 13,2,1,1);
/**
* textfield
*/
private JTextField privateCourseIdField = addTextField("",3,3,2,1);
private JTextField privateCourseTitleField = addTextField("",4,3,2,1);
private JTextField privateCourseTypeField = addTextField("",5,3,2,1);
private JTextField privateCourseSubjectField = addTextField("",6,3,2,1);
private JTextField privateCourseContentField = addTextField("",7,3,2,1);
private JTextField privateCoursePriceField = addTextField("",8,3,2,1);
private JTextField privateCourseMaxParticipantsField = addTextField("",9,3,2,1);
private JTextField privateCourseMinParticipantsField = addTextField("",10,3,2,1);
private JTextField privateCourseDateField = addTextField("",11,3,2,1);
private JTextField privateCourseClassroomField = addTextField("",12,3,2,1);
private JTextField privateCoursePersonnelField = addTextField("",13,3,2,1);
/**
* Empty labels for
*/
private JLabel emptyLabel1 = addLabel(" ", 1,1,1,1);
private JLabel emptyLabel2 = addLabel(" ", 5,1,1,1);
private JLabel emptyLabel3 = addLabel(" ", 7,1,1,1);
private JLabel emptyLabel4 = addLabel(" ", 1,5,1,1);
static boolean debugger = false;
/**
* Creating combobox for classroom.
*/
private JComboBox classRoom = addComboBox(0,0,1,1);
/**
* Creating combobox for personnel.
*
*/
private JComboBox personnel = addComboBox(12,4,1,1);
private JComboBox id = addComboBox(13,4,1,1);
/**
* Creating Dialog-window
*
* @param w to which frame it refers
* @param c is the course information that is read
*/
public EditPrivateCourse_GUI(GBFrame w, Omaedu_database omaEdu, PrivateCourse p)
{
super(w);
this.omaedu = omaEdu;
temp = omaedu.getPrivateCourses();
this.privateCourse = p;
this.roomlist = omaedu.getRooms();
this.edulist = omaedu.getEducators();
privateCourseIdField.setEditable(false);
privateCourseTitleField.setEditable(true);
privateCourseTypeField.setEditable(true);
privateCourseSubjectField.setEditable(true);
privateCourseContentField.setEditable(true);
privateCoursePriceField.setEditable(true);
privateCourseMaxParticipantsField.setEditable(true);
privateCourseMinParticipantsField.setEditable(true);
privateCourseDateField.setEditable(true);
privateCourseClassroomField.setEditable(false);
privateCoursePersonnelField.setEditable(false);
if (!temp.isEmpty() && temp != null && temp.get(0) != null )
{
PrivateCourse help = temp.get(0);
identification = help.getId();
displayData(help);
}
if (!roomlist.isEmpty() && roomlist != null)
{
roomiter = roomlist.iterator();
while (roomiter.hasNext())
{
Classroom help = roomiter.next();
classRoom.addItem(help.getId()+" "+help.getLocation());
}
}
else { classRoom.addItem("No rooms"); }
if (!edulist.isEmpty() && edulist != null)
{
eduiter = edulist.iterator();
while (eduiter.hasNext())
{
Educator help = eduiter.next();
personnel.addItem(help.getEmpnum()+" "+help.getEmpname());
}
}
if (!temp.isEmpty() && temp != null)
{
iter = temp.iterator();
while (iter.hasNext())
{
PrivateCourse help = iter.next();
id.addItem(help.getId()+" ");
}
}
}
private void displayData(PrivateCourse p){
privateCourseIdField.setText(p.getId() + "");
privateCourseTitleField.setText(p.getPrivateCourseTitle());
privateCourseTypeField.setText(p.getPrivateCourseType());
privateCourseSubjectField.setText(p.getPrivateCourseSubject());
privateCourseContentField.setText(p.getPrivateCourseContent());
privateCoursePriceField.setText(p.getPrivateCoursePrice() + "");
privateCourseMaxParticipantsField.setText(p.getPrivateCourseMaxParticipants() +"");
privateCourseMinParticipantsField.setText(p.getPrivateCourseMinPraticipants() + "");
privateCourseDateField.setText(p.getPrivateCourseDate());
String classroom = Integer.toString(p.getPrivateCourseClassroom().getId()) + p.getPrivateCourseClassroom().getLocation();
privateCourseClassroomField.setText(classroom);
String educator = Integer.toString(p.getPrivateCoursePersonnel().getEmpnum());
privateCoursePersonnelField.setText(educator);
}
public void buttonClicked(JButton button)
{
if (button == choosecourse)
{
PrivateCourse param = new PrivateCourse(0,"empty","empty","empty","empty",0.0, 0, 0,"empty",null,null);
String help = (String)id.getSelectedItem();
int i = help.indexOf(" ");
String ref = help.substring(0, i);
identification = Integer.parseInt(ref);
iter = temp.iterator();
while (iter.hasNext())
{
PrivateCourse course = iter.next();
if (course.getId() == identification)
{
param = course;
}
}
displayData(param);
}
else if (button == edit)
{
try
{
String privateCourseTitle = privateCourseTitleField.getText();
String privateCourseType = privateCourseTypeField.getText();
String privateCourseSubject = privateCourseSubjectField.getText();
String privateCourseContent = privateCourseContentField.getText();
double privateCoursePrice = Double.parseDouble(privateCoursePriceField.getText());
int privateCourseMaxParticipants = Integer.parseInt(privateCourseMaxParticipantsField.getText());
int privateCourseMinParticipants = Integer.parseInt(privateCourseMinParticipantsField.getText());
String privateCourseDate = privateCourseDateField.getText();
classroom = new Classroom(0,"empty",0,"empty");
if (!roomlist.isEmpty() && roomlist != null)
{
String help = (String)classRoom.getSelectedItem();
int i = help.indexOf(" ");
String roomref = help.substring(0, i);
int roomid = Integer.parseInt(roomref);
roomiter = roomlist.iterator();
while (roomiter.hasNext())
{
Classroom temp = roomiter.next();
if (temp.getId() == roomid)
{
classroom = temp;
}
}
}
educator = new Educator(0,"empty","empty","empty","empty","empty");
String help2 = (String)personnel.getSelectedItem();
int j = help2.indexOf(" ");
String eduref = help2.substring(0, j);
int eduid = Integer.parseInt(eduref);
eduiter = edulist.iterator();
while (eduiter.hasNext())
{
Educator temp = eduiter.next();
if (temp.getEmpnum() == eduid)
{
educator = temp;
}
}
System.out.println("The id is: " + identification);
privateCourse.setId(identification);
System.out.println("id toimii");
privateCourse.setPrivateCourseTitle(privateCourseTitle);
System.out.println("titteli toimii");
privateCourse.setPrivateCourseType(privateCourseType);
System.out.println("type toimii");
privateCourse.setPrivateCourseSubject(privateCourseSubject);
System.out.println("sub toimii");
privateCourse.setPrivateCourseContent(privateCourseContent);
System.out.println("content toimii");
privateCourse.setPrivateCoursePrice(privateCoursePrice);
System.out.println("price toimii");
privateCourse.setPrivateCoursemaxParticipants(privateCourseMaxParticipants);
System.out.println("max toimii");
privateCourse.setPrivateCourseMinParticipants(privateCourseMinParticipants);
System.out.println("min toimii");
privateCourse.setPrivateCourseDate(privateCourseDate);
System.out.println("date toimii");
privateCourse.setPrivateCourseClassroom(classroom);
System.out.println("class toimii");
privateCourse.setPrivateCoursePersonnel(educator);
System.out.println("pers toimii");
omaedu.editPrivateCourse(privateCourse);
dispose();
} catch (Exception e)
{ System.out.println(e);
}
}
}
}