Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Hufnagl committed May 5, 2012
0 parents commit 603a573
Show file tree
Hide file tree
Showing 18 changed files with 804 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Businesslogic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.Vector;

import models.Contact;


public class Businesslogic {
static private Businesslogic myinstance = null;
private Businesslogic(){
}
static public Businesslogic getInstance()
{
if ( myinstance == null )
myinstance = new Businesslogic();
return myinstance;
}

public Vector<Contact> getContact() {
Vector<Contact> mydata = Database.getInstance().getContact();
if(mydata == null)
{
System.out.println("Database error");
return null;//Database error
}
return mydata;//auf return wert ändern
}

public boolean deleteContact(Contact argContact){
return Database.getInstance().deleteContact(argContact);
}
public boolean addContact(Contact argContact){
return Database.getInstance().addContact(argContact);
}

public boolean updateContact(Contact argContact){
return Database.getInstance().updateContact(argContact);
}
}
132 changes: 132 additions & 0 deletions ContactBrowser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@


import java.awt.BorderLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;

import models.BackModel;
import models.Contact;
import models.SimpleModel;

public class ContactBrowser extends JPanel implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
Vector<String> col = null;
LayoutManager mgr = null;
JTable mytable = null;
JButton buttonSearch= null, buttonEdit= null, buttonDelete=null, buttonAdd=null;
JFrame frame = null;
SimpleModel model = null;
JTextField inName = null;
//DefaultTableModel model= null;
// SimpleModel aaa = null;

ContactBrowser( )
{


buttonSearch= new JButton("Search");
buttonDelete= new JButton("Delete");
buttonEdit= new JButton("Edit");
buttonAdd= new JButton("Add");
frame = new JFrame("Kontakte");
col= new Vector<String>();
col.add("id");
col.add("name");
//mytable.add
model = new SimpleModel();
System.out.println("fuuuu");
//model = new DefaultTableModel(col, Businesslogic.getInstance().getContact());
mytable = new JTable(model);
setLayout(new BorderLayout());
//plugging components together
frame.getContentPane().add(this);
add(buttonSearch);
add(buttonEdit);
add(buttonDelete);
add(buttonAdd);
add(mytable);
//positioning
setLayout(mgr );
frame.setSize(800,600);
mytable.setLocation(10, 10);
mytable.setSize(500, 500);
mytable.setVisible(true);
//mytable.setS electionMode(ListSelectionModel.SINGLE_SELECTION);

buttonSearch.setLocation(520, 10);
buttonSearch.setSize(270, 100);
buttonDelete.setLocation(520, 120);
buttonDelete.setSize(270, 100);
buttonEdit.setLocation(520, 230);
buttonEdit.setSize(270, 100);
buttonAdd.setLocation(520, 340);
buttonAdd.setSize(270, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//frame.pack();
listen();
}

private void setData(){
model.clearText();
model.addText(Businesslogic.getInstance().getContact());
}

private void listen(){
buttonSearch.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setData();
}

});

buttonDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Contact toDelete;
toDelete = model.getRow(mytable.getSelectedRow());
Businesslogic.getInstance().deleteContact(toDelete);
setData();
}
});


buttonEdit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ContactBrowserDialog(model.getRow(mytable.getSelectedRow()));
setData();
}

});

buttonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ContactBrowserDialog(new Contact());
}

});

}

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}
}
65 changes: 65 additions & 0 deletions ContactBrowserDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JPanel;

import models.Contact;

class ContactBrowserDialog extends JDialog
{
/**
*
*/
private static final long serialVersionUID = 1L;
DialogComponents components = null;
Contact contact = null;
public ContactBrowserDialog(Contact argContact)
{
contact=argContact;
JPanel panel = new JPanel(new GridLayout(0,2));
setContentPane(panel);
components = new DialogComponents();
components.bindFrom(contact);
//adding fields to panel
panel.add(components.getLabelName());
panel.add(components.getName());
panel.add(components.getLabelNName());
panel.add(components.getNName());
panel.add(components.getLabelPhone());
panel.add(components.getPhone());
panel.add(components.getLabelDate());
panel.add(components.getDate());
panel.add(components.getButtonOk());
panel.add(components.getLabelError());

// Use invokeLater AFTER the dialog is shown

setModal(true);
listen();
pack();
setVisible( true );
}
private void endDialog()
{
this.dispose();
}
private void listen(){
components.getButtonOk().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(components.bindTo(contact))
{
if(contact.getId()==null)
Businesslogic.getInstance().addContact(contact);
else
Businesslogic.getInstance().updateContact(contact);
endDialog();
}

}

});

}
}
20 changes: 20 additions & 0 deletions Database.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import interfaces.DataAccessable;


public class Database {
static private Database myinstance = null;
static DataAccessable mycon = null;
private Database(){
mycon = new MysqlDatabase();
}
static public DataAccessable getInstance()
{
if ( myinstance == null )
myinstance = new Database();
return mycon;
}




}
125 changes: 125 additions & 0 deletions DialogComponents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import java.awt.Color;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import models.Contact;


public class DialogComponents {
private JTextField name = null, date=null, nName=null, phone=null;
private JButton buttonOk= null;
private JLabel labelName = null, labelDate= null, labelNName=null, labelPhone=null;
private JLabel errorLabel = null;
DialogComponents(){

name = new JTextField("",20);
labelName = new JLabel("Name");
nName = new JTextField("",20);
labelNName = new JLabel("Nachname");
phone = new JTextField("",20);
labelPhone = new JLabel("Telephonnummmer");
date = new JTextField("",10);
labelDate = new JLabel("Datum");
buttonOk =new JButton("Ok");
errorLabel= new JLabel("",10);
}
public JButton getButtonOk(){return buttonOk;}
public JTextField getName(){return name;}
public JTextField getDate(){return date;}
public JTextField getNName(){return nName;}
public JTextField getPhone(){return phone;}
public JLabel getLabelName(){return labelName;}
public JLabel getLabelNName(){return labelNName;}
public JLabel getLabelPhone(){return labelPhone;}
public JLabel getLabelDate(){return labelDate;}
public JLabel getLabelError(){return errorLabel;}
@SuppressWarnings("deprecation")
public boolean bindTo(Contact argContact){
InputChecker myInputChecker = new InputChecker();
boolean errors = true; //if any field is invalid it will bee set to false
Color errorcolor = new Color(255,0,0);

//first name check + bind
if(myInputChecker.checkName(name.getText()))//checking first name
{//valid
argContact.setName(name.getText());
}
else
{//not valid
errors = false;
name.setToolTipText("Der Vorname ist ungültig!");
name.setBackground(errorcolor);
}

//last name check + bind
if(myInputChecker.checkName(nName.getText()))//checking last name
{//valid
argContact.setNName(nName.getText());
}
else
{//not valid
errors = false;
nName.setToolTipText("Der Nachname ist ungültig!");
nName.setBackground(errorcolor);
}

//phone check + bind
if(myInputChecker.checkPhone(phone.getText()))
{//valid
argContact.setPhone(phone.getText());
}
else
{//not valid
errors = false;
phone.setToolTipText("Die Telephonnummer ist ungültig");
phone.setBackground(errorcolor);
}

//phone number check + binding
if(myInputChecker.checkPhone(phone.getText()))//checking if phone is valid
{
argContact.setPhone(phone.getText());
}
else
{//not valid
errors = false;
phone.setToolTipText("Die Telephonnummer ist ungültig!");
phone.setBackground(errorcolor);
}

//date check + binding
if(myInputChecker.checkDate(date.getText()))//checking if date is valid
{//valid
String mydate = date.getText();
String[] mySplitDate = mydate.split("\\.");
System.out.println(mySplitDate[1]);
argContact.setDate(new Date(1988,02,22));


}
else
{//not valid
errors = false;
date.setToolTipText("Der Name ist ungültig!");
date.setBackground(errorcolor);
}
return errors;

}
public boolean bindFrom(Contact argContact){
boolean error = true;
SimpleDateFormat ft = new SimpleDateFormat ("dd. MM. yyyy");
if(argContact.getName()!=null)
name.setText(argContact.getName());
if(argContact.getNName()!=null)
nName.setText(argContact.getNName());
if(argContact.getPhone()!=null)
phone.setText(argContact.getPhone());
if(argContact.getDate()!=null)
date.setText(ft.format(argContact.getDate()));
return error;
}
}
Loading

0 comments on commit 603a573

Please sign in to comment.