HJÆLP hvorfor virker min gui ikke
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.tree.*;
import javax.swing.border.*;
/*
* Af Peter Kristensen, Allan Jønsson og Morten Bonnerup
*/
/**
* Denne klasse opretter og tegner den grafiske brugerflade
*
*
*
*/
public class Gui extends JFrame
{
private GridBagLayout layout;
private GridBagLayout vinLayout;
private ArrayList datoList;
private JTextField field;
private JTextField sogeField;
private SpecialVaerktoej valgteVaerktoej;
private Database database;
private JLabel vaerktoejNavn;
private JLabel monthShow;
private Dag nyDag;
private JFrame jframe;
private ReserveringsDato testDato;
private tråd trådobj;
private Thread tråd;
private JPanel oversigtPanel;
private JPanel kalenderPanel;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
private int screenWidth = (int) screen.getWidth();
private int screenHeight = (int) screen.getHeight();
int month = new ReserveringsDato().getMonth();
int date = new ReserveringsDato().getDate();
int year = new ReserveringsDato().getYear();
/**
* Constructor for klassen Gui
*/
public Gui()
{
//Kald til JFrame der så bliver til titlen på framen
super("Værktøjshåndteringssystem - V.H.S");
//Databasen eksekveres
database = new Database();
///Starter tråd
trådobj = new tråd(database);
tråd = new Thread(trådobj);
tråd.start();
//Toolkit hentes, som så eksekveres med stien til et icon, som så placeres i øverste
//venstre hjørne af rammen
Toolkit toolkit = Toolkit.getDefaultToolkit();
setIconImage(toolkit.getImage("ts-cent.gif"));
layout = new GridBagLayout();
vinLayout = new GridBagLayout();
jframe = this;
//Størrelsen af skærmen defineres og vinduet placeres på midten
setSize(500,400);
setLocation(screenWidth/2-250,screenHeight/2-200);
Container vinPanel = getContentPane();
vinPanel.setLayout(vinLayout);
//Fanebladene bliver defineret
JTabbedPane faneblade = new JTabbedPane();
JPanel reserveringFaneBlad = new JPanel();
reserveringFaneBlad.setLayout(layout);
oversigtPanel = new JPanel();
oversigtPanel.setLayout(new BorderLayout());
showSoeg();
JPanel soegPanel = new JPanel();
soegPanel.setLayout(new BorderLayout());
soegPanel.add(bund,BorderLayout.CENTER);
faneblade.add ("Reservering", reserveringFaneBlad);
faneblade.add("Søg",soegPanel);
faneblade.add("Oversigt",oversigtPanel);
kalenderPanel = new JPanel();
kalender();
createTree();
//Indholdet af reserveringsfanebladet defineres og indsættes
GridBagConstraints layoutConstraints = new GridBagConstraints();
layoutConstraints.gridx = 0; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.ipadx = 300;
layoutConstraints.ipady = 300;
layoutConstraints.insets = new Insets(12, 12, 12, 12);
layoutConstraints.anchor = GridBagConstraints.NORTH;
layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 1.0;
vinLayout.setConstraints(faneblade, layoutConstraints);
vinPanel.add(faneblade);
GridBagConstraints con;
Insets ins = new Insets( 5, 5, 5, 5 );
con = createGBC( 0, 0, 3, 1 );
con.fill = GridBagConstraints.HORIZONTAL;
con.insets = ins;
con.ipadx = 20;
con.ipady = 0;
con.weightx = 1;
con.weighty = 0;
sogeField = new JTextField("Skriv værktøjs nr her",20);
add(sogeField, con,reserveringFaneBlad );
con = createGBC( 3, 0, 1, 1 );
con.fill = GridBagConstraints.HORIZONTAL;
con.weightx = 0;
con.weighty = 1;
con.anchor = GridBagConstraints.CENTER;
con.insets = ins;
JButton sogKnap = new JButton("Søg");
sogKnap.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
valgteVaerktoej = database.hentVaerktoej( sogeField.getText());
sogeField.setText("");
kalender();
repaint(); }});
add(sogKnap, con,reserveringFaneBlad );
con = createGBC( 0, 2, 1, 1 );
con.fill = GridBagConstraints.HORIZONTAL;
con.weightx = 0;
con.weighty = 1;
con.anchor = GridBagConstraints.NORTH;
con.insets = ins;
add(new JLabel("Navn : "), con,reserveringFaneBlad );
con = createGBC( 1, 2, 2, 1 );
con.fill = GridBagConstraints.HORIZONTAL;
con.weightx = 0;
con.weighty = 1;
con.anchor = GridBagConstraints.NORTHWEST;
con.insets = ins;
vaerktoejNavn = new JLabel();
add(vaerktoejNavn, con,reserveringFaneBlad );
con = createGBC( 5, 2, 1, 1 );
con.fill = GridBagConstraints.NONE;
con.weightx = 0;
con.weighty = 0;
con.anchor = GridBagConstraints.SOUTH;
con.insets = ins;
JButton knapTilbage = new JButton("Tilbage");
knapTilbage.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
month--;
if ( month < 0) {month = 11;year--;}
kalender();
repaint(); }});
add(knapTilbage, con,reserveringFaneBlad );
con = createGBC( 6, 2, 1, 1 );
con.fill = GridBagConstraints.HORIZONTAL;
con.weightx = 0;
con.weighty = 0;
con.anchor = GridBagConstraints.SOUTH;
con.insets = ins;
monthShow = new JLabel(getMonthName(month));
add(monthShow, con,reserveringFaneBlad );
con = createGBC( 7, 2, 1, 1 );
con.fill = GridBagConstraints.NONE;
con.weightx = 0;
con.weighty = 0;
con.anchor = GridBagConstraints.SOUTH;
con.insets = ins;
JButton knapFrem = new JButton("Frem");
knapFrem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
month++;
if ( month >=12) {month = 0;year++;}
repaint();
kalender();}});
add(knapFrem, con,reserveringFaneBlad );
con = createGBC( 0, 3, 8, 1 );
con.fill = GridBagConstraints.BOTH;
con.weightx = 0;
con.weighty = 5;
con.anchor = GridBagConstraints.SOUTH;
con.insets = ins;
add(kalenderPanel, con,reserveringFaneBlad );
JMenu vaerktoejMenu = new JMenu("Værktøj");
JMenuItem opretVaerktoej = new JMenuItem("Opret Værktøj", new ImageIcon("tools.gif"));
opretVaerktoej.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
new opretVaerktoejVindue();}});
JMenuItem retVaerktoej = new JMenuItem("Rediger Værktøj", new ImageIcon("tools rediger.gif"));
JMenuItem sletVaerktoej = new JMenuItem("Slet Værktøj", new ImageIcon("tools fjern.gif"));
vaerktoejMenu.add(opretVaerktoej);
vaerktoejMenu.add(retVaerktoej);
vaerktoejMenu.add(sletVaerktoej);
sletVaerktoej.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
new sletVaerktoej(); } });
//Menuerne oprettes og indsættes
JMenu personaleMenu = new JMenu("Person");
JMenuItem opretPersonale = new JMenuItem("Opret Person", new ImageIcon("man.gif"));
JMenuItem knytBil = new JMenuItem("Knyt bil til Person", new ImageIcon("knyt.gif"));
JMenuItem retPersonale = new JMenuItem("Rediger Person", new ImageIcon("man rediger.gif"));
JMenuItem sletPersonale = new JMenuItem("Slet Person", new ImageIcon("man fjern.gif"));
knytBil.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
new knytBil();} });
opretPersonale.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
new opretPersonale(); }});
sletPersonale.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
new sletPersonale(); } });
personaleMenu.add(opretPersonale);
personaleMenu.add(knytBil);
personaleMenu.add(retPersonale);
personaleMenu.add(sletPersonale);
JMenu bilMenu = new JMenu("Bil");
JMenuItem opretBil = new JMenuItem("Opret Bil", new ImageIcon("car.gif"));
bilMenu.add(opretBil);
opretBil.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
new opretBil(); } });
JMenuItem retBil = new JMenuItem("Rediger Bil", new ImageIcon("car rediger.gif"));
bilMenu.add(retBil);
JMenuItem sletBil = new JMenuItem("Slet Bil", new ImageIcon("car fjern.gif"));
bilMenu.add(sletBil);
sletBil.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
new sletBil();}});
JMenu filer = new JMenu("Filer");
JMenuItem visLog = new JMenuItem("Vis Log", new ImageIcon("Oversigt.gif"));
visLog.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
JFrame visLogVindue = new JFrame("Vis Log");
visLogVindue.setSize(400,300);
JTextArea visLogLabel = new JTextArea(Log.getStringLog());
visLogLabel.setEditable(false);
visLogLabel.setFont(new Font("fiddo",5,10));
visLogVindue.getContentPane().add(visLogLabel);
visLogVindue.show();}});
filer.add(visLog);
JMenuItem afslut = new JMenuItem("Afslut", new ImageIcon("Exit.gif"));
afslut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
System.exit( 0 ); }});
filer.add(afslut);
JMenuBar menu = new JMenuBar();
menu.add(filer);
menu.add(bilMenu);
menu.add(personaleMenu);
menu.add(vaerktoejMenu);
setJMenuBar(menu);
show();
toFront();
}
private GridBagConstraints createGBC( int x, int y, int width, int height ) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
return gbc;
}
private void add( JComponent component, GridBagConstraints gbc,JPanel panel ) {
layout.setConstraints( component, gbc );
panel.add( component );
}
//Kalenderen oprettes og indsættes
private void kalender() {
kalenderPanel.removeAll();
kalenderPanel.setLayout(new GridLayout(7,7,3,3));
kalenderPanel.setBackground(Color.GRAY);
kalenderPanel.add(new JLabel("Man"));
kalenderPanel.add(new JLabel("Tir"));
kalenderPanel.add(new JLabel("Ons"));
kalenderPanel.add(new JLabel("Tors"));
kalenderPanel.add(new JLabel("Fre"));
kalenderPanel.add(new JLabel("Lør"));
kalenderPanel.add(new JLabel("Søn"));
ReserveringsDato test = new ReserveringsDato(1,month,year);
int dayTest = test.getDay();
if (dayTest == 0 ) { dayTest=7;}
dayTest--;
int y=0;
for(;y<dayTest;y++) {
kalenderPanel.add(new JLabel());
}
datoList = new ArrayList();
testDato = new ReserveringsDato();
testDato.setMonth(month);
testDato.setYear(year);
int x =1;
boolean shouldRun =true;
Date prove = new Date();
while ( shouldRun) {
testDato.setDate(x);
nyDag = new Dag();
nyDag.setDato((ReserveringsDato) testDato.clone());
x++;
if (testDato.getMonth() == month)
{
kalenderPanel.add( nyDag);
if (valgteVaerktoej != null){
if(valgteVaerktoej.containsDato(nyDag.getDato()) ){nyDag.setReserveret();
nyDag.setPerson(valgteVaerktoej.getPersonale(testDato));}
if ((testDato.getDate() >= prove.getDate() && testDato.getMonth() >= prove.getMonth() && testDato.getYear() >= prove.getYear()) ) {
nyDag.addMouseListener(new MouseListener(){private Dag clickDag = nyDag;
public void mouseClicked(MouseEvent e){if (!clickDag.isReserveret()){new makeReservation(clickDag.getDato()); }
else {int valg = JOptionPane.showConfirmDialog(null, "Er du sikker på at du vil slette reserveringen?", "Sletning af reservation", JOptionPane.YES_NO_OPTION);
if(valg == 0){ database.flytTilLager(valgteVaerktoej.getVaerktoejId());
valgteVaerktoej.removeDato(clickDag.getDato());
clickDag.setUnReserveret();
trådobj.opdatere();
jframe.repaint();}}}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}});
}
}
}
else {shouldRun=false;}
}
int z = (x-1)+y;
while (z <=42){
kalenderPanel.add(new JLabel());z++;}
}
public String getMonthName(int num) {
String month = "";
switch(num) {
case 0: month="Januar";
break;
case 1: month="Februar";
break;
case 2: month="Marts";
break;
case 3: month="April";
break;
case 4: month="Maj";
break;
case 5: month="Juni";
break;
case 6: month="Juli";
break;
case 7: month="August";
break;
case 8: month="September";
break;
case 9: month="Oktober";
break;
case 10: month="November";
break;
case 11: month="December";
break;
default: month="unknown";
break;
}
return month;
}
public void repaint() {
String monthName = getMonthName(month);
monthShow.setText(monthName);
if(valgteVaerktoej != null) {
vaerktoejNavn.setText(valgteVaerktoej.getNavn());
}
super.repaint();
}
public void paint(Graphics g) {
createTree();
super.paint(g);
}
//Intern klasse for oprettelse af værktøjs vindue
class opretVaerktoejVindue extends JFrame{
private JPanel opretVaerktoejPanel;
private JLabel vaerktoejsId;
private JLabel vaerktoejsNavn;
private JTextField vaerktoejsIdField;
private JTextField vaerktoejsNavnField;
private JButton confirm;
private JButton cancel;
public opretVaerktoejVindue() {
super("Opret Værktøj");
setResizable(false);
opretVaerktoejPanel = new JPanel();
getContentPane().add(opretVaerktoejPanel);
opretVaerktoejPanel.setLayout(new GridLayout(4,2));
vaerktoejsId = new JLabel("Værktøjs id :");
vaerktoejsNavn = new JLabel("Navn :");
vaerktoejsIdField = new JTextField(8);
vaerktoejsNavnField = new JTextField(8);
confirm = new JButton("Opret");
cancel = new JButton("Annulere");
opretVaerktoejPanel.add(vaerktoejsId);
opretVaerktoejPanel.add(vaerktoejsIdField);
opretVaerktoejPanel.add(vaerktoejsNavn);
opretVaerktoejPanel.add(vaerktoejsNavnField);
opretVaerktoejPanel.add(new JLabel());
opretVaerktoejPanel.add(new JLabel());
opretVaerktoejPanel.add(confirm);
opretVaerktoejPanel.add(cancel);
confirm.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
database.opretVaerktoej(vaerktoejsIdField.getText(),vaerktoejsNavnField.getText());
hide(); }});
//Objekterne bliver pakket i vinduet og vist på midten af skærmen
pack();
setLocation(screenWidth/2-getWidth()/2,screenHeight/2-getHeight()/2);
show();
}
}
//Intern klasse til oprettelse af reservationsvindue
class makeReservation extends JFrame{
private JPanel reserverPanel;
private JLabel vaerktoejNavnLabel;
private JLabel vaerktoejIdLabel;
private JLabel meddelse;
private JTextField initialerField;
private JTextField reserverDatoField;
private JButton confirm;
private JButton cancel;
private JSpinner antalDage;
private ReserveringsDato dato;
//Vinduet oprettes og elementer indsættes
public makeReservation(ReserveringsDato dato2){
super("Lav reservation");
setResizable(false);
setResizable(false);
this.dato = dato2;
reserverPanel = new JPanel();
getContentPane().add(reserverPanel);
reserverPanel.setLayout(new GridLayout(7,2));
reserverPanel.add(new JLabel("Reserver dato:"));
reserverDatoField = new JTextField(8);
reserverPanel.add(reserverDatoField);
reserverDatoField.setEditable(false);
reserverDatoField.setText(dato.getDate() + "/" + (dato.getMonth()+1) + "-" + (dato.getYear()+1900));
reserverPanel.add(new JLabel("Værktøj:"));
vaerktoejNavnLabel = new JLabel(valgteVaerktoej.getNavn());
reserverPanel.add(vaerktoejNavnLabel);
reserverPanel.add(new JLabel("ID:"));
vaerktoejIdLabel = new JLabel(valgteVaerktoej.getVaerktoejId());
reserverPanel.add(vaerktoejIdLabel);
reserverPanel.add(new JLabel("Antal dage frem:"));
antalDage = new JSpinner( new SpinnerNumberModel(0,0,5,1));
reserverPanel.add(antalDage);
reserverPanel.add(new JLabel("Initialer:"));
initialerField = new JTextField(8);
reserverPanel.add(initialerField);
meddelse = new JLabel();
reserverPanel.add(meddelse);
reserverPanel.add(new JLabel());
confirm = new JButton("OK");
reserverPanel.add(confirm);
cancel = new JButton("Annuler");
reserverPanel.add(cancel);
confirm.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {
Personale valgtePersonale = database.hentPersonale(initialerField.getText());
if(valgtePersonale != null && valgtePersonale.getBil() != null){
dato.setPersonale(valgtePersonale);
valgteVaerktoej.reservereDato(dato,((Integer) antalDage.getValue()).intValue());hide();kalender();jframe.repaint();trådobj.opdatere() ;}
else{
meddelse.setText("FEJL 40!");
repaint();}
}});
cancel.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){
hide();} });
pack();
setLocation(screenWidth/2-getWidth()/2,screenHeight/2-getHeight()/2);
show();
}
}
//Intern klasse til oprettelse af personale vindue
class opretPersonale extends JFrame {
private JLabel personaleNavn;
private JLabel personaleID;
private JTextField personaleNavnField;
private JTextField personaleIdField;
private JButton confirm;
private JButton cancel;
private JPanel opretPersonalePanel;
public opretPersonale() {
super("Opret Person");
setResizable(false);
opretPersonalePanel = new JPanel();
getContentPane().add(opretPersonalePanel);
opretPersonalePanel.setLayout(new GridLayout(4,2));
personaleNavn = new JLabel("Navn :");
personaleID = new JLabel("Initialer :");
personaleNavnField = new JTextField(8);
personaleIdField = new JTextField(8);
confirm = new JButton("OK");
cancel = new JButton("Annulere");
opretPersonalePanel.add(personaleNavn);
opretPersonalePanel.add(personaleNavnField);
opretPersonalePanel.add(personaleID);
opretPersonalePanel.add(personaleIdField);
opretPersonalePanel.add(new JLabel());
opretPersonalePanel.add(new JLabel());
opretPersonalePanel.add(confirm);
opretPersonalePanel.add(cancel);
confirm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
database.opretPersonale(personaleIdField.getText(),personaleNavnField.getText());
hide();
}});
cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){
hide();
}});
pack();
setLocation(screenWidth/2-getWidth()/2,screenHeight/2-getHeight()/2);
show();
}
}
//Inter klasse til opretelse af bil vindue
class opretBil extends JFrame
{
private JPanel opretBilPanel;
private JButton confirm;
private JButton cancel;
private JTextField bilNrField;
private JTextField maerkField;
private JTextField modelField;
private JTextField regNrField;
//Vindue for oprettelse af ny bil oprettes og tegnes
public opretBil()
{
super("Opret Bil");
opretBilPanel = new JPanel();
opretBilPanel.setLayout(new GridLayout(6,2));
getContentPane().add(opretBilPanel);
bilNrField = new JTextField(8);
maerkField = new JTextField(8);
modelField = new JTextField(8);
regNrField = new JTextField(8);
confirm = new JButton("OK");
cancel = new JButton("Annuler");
opretBilPanel.add(new JLabel("Bil nr :"));
opretBilPanel.add(bilNrField);
opretBilPanel.add(new JLabel("Mærke :"));
opretBilPanel.add(maerkField);
opretBilPanel.add(new JLabel("Model :"));
opretBilPanel.add(modelField);
opretBilPanel.add(new JLabel("Registrerings nr.:"));
opretBilPanel.add(regNrField);
opretBilPanel.add(new JLabel());
opretBilPanel.add(new JLabel());
opretBilPanel.add(confirm);
opretBilPanel.add(cancel);
confirm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
database.opretBil(bilNrField.getText(),maerkField.getText(),modelField.getText(),regNrField.getText());
hide();
}});
cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
hide();
}});
pack();
setLocation(screenWidth/2-getWidth()/2,screenHeight/2-getHeight()/2);
show();
}
}
//Intern klasse til oprettelse af et bil vindue
class sletBil extends JFrame{
private JPanel sletBilPanel;
private JTextField bilNrField;
private JButton confirm;
private JButton cancel;
public sletBil(){
super("Slet Bil");
sletBilPanel = new JPanel();
sletBilPanel.setLayout(new GridLayout(4,2));
getContentPane().add(sletBilPanel);
bilNrField = new JTextField(8);
confirm = new JButton("Ok");
cancel = new JButton("Annuler");
sletBilPanel.add(new JLabel());
sletBilPanel.add(new JLabel());
sletBilPanel.add(new JLabel("Bil nummer"));
sletBilPanel.add(bilNrField);
sletBilPanel.add(new JLabel());
sletBilPanel.add(new JLabel());
sletBilPanel.add(confirm);
sletBilPanel.add(cancel);
confirm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
int valg = JOptionPane.showConfirmDialog(null, "Er du sikker på at du vil slette denne bil?", "Sletning af bil", JOptionPane.YES_NO_OPTION);
if(valg == 0){
database.fjernBil(bilNrField.getText());
trådobj.opdatere();
hide();
}}});
cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
hide();}});
pack();
setLocation(screenWidth/2-getWidth()/2,screenHeight/2-getHeight()/2);
show();
}
}
class sletPersonale extends JFrame{
private JPanel sletPersonalePanel;
private JTextField personaleIdField;
private JButton confirm;
private JButton cancel;
public sletPersonale(){
super("Slet Person");
sletPersonalePanel = new JPanel();
sletPersonalePanel.setLayout(new GridLayout(4,2));
getContentPane().add(sletPersonalePanel);
personaleIdField = new JTextField(8);
confirm = new JButton("Ok");
cancel = new JButton("Annuler");
sletPersonalePanel.add(new JLabel());
sletPersonalePanel.add(new JLabel());
sletPersonalePanel.add(new JLabel("Personale ID:"));
sletPersonalePanel.add(personaleIdField);
sletPersonalePanel.add(new JLabel());
sletPersonalePanel.add(new JLabel());
sletPersonalePanel.add(confirm);
sletPersonalePanel.add(cancel);
confirm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
int valg = JOptionPane.showConfirmDialog(null, "Er du sikker på at du vil slette denne person?", "Sletning af personale", JOptionPane.YES_NO_OPTION);
if(valg == 0){database.fjernPersonale(personaleIdField.getText());
trådobj.opdatere();
hide();}
}});
cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
hide();
}});
pack();
setLocation(screenWidth/2-getWidth()/2,screenHeight/2-getHeight()/2);
show();
}
}
class sletVaerktoej extends JFrame{
private JPanel sletVaerktoejPanel;
private JTextField vaerktoejIdField;
private JButton confirm;
private JButton cancel;
public sletVaerktoej(){
super("Slet Værktøj");
sletVaerktoejPanel = new JPanel();
sletVaerktoejPanel.setLayout(new GridLayout(4,2));
getContentPane().add(sletVaerktoejPanel);
vaerktoejIdField = new JTextField(8);
confirm = new JButton("Ok");
cancel = new JButton("Annuler");
sletVaerktoejPanel.add(new JLabel());
sletVaerktoejPanel.add(new JLabel());
sletVaerktoejPanel.add(new JLabel("Værktøj ID:"));
sletVaerktoejPanel.add(vaerktoejIdField);
sletVaerktoejPanel.add(new JLabel());
sletVaerktoejPanel.add(new JLabel());
sletVaerktoejPanel.add(confirm);
sletVaerktoejPanel.add(cancel);
confirm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
int valg = JOptionPane.showConfirmDialog(null, "Er du sikker på at du vil slette dette værktøj?", "Sletning af værktøj", JOptionPane.YES_NO_OPTION);
if(valg == 0){database.fjernVaerktoej(vaerktoejIdField.getText());
trådobj.opdatere();
hide();}
}});
cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
hide();
}});
pack();
setLocation(screenWidth/2-getWidth()/2,screenHeight/2-getHeight()/2);
show();
}
}
class knytBil extends JFrame{
private JPanel knytBilPanel;
private JButton confirm;
private JButton cancel;
private JTextField personaleIdField;
private JTextField bilNrField;
public knytBil() {
super("Knyt en Bil til en Person");
knytBilPanel = new JPanel();
getContentPane().add(knytBilPanel);
knytBilPanel.setLayout(new GridLayout(4,2));
personaleIdField = new JTextField(8);
bilNrField = new JTextField(8);
confirm = new JButton("OK");
cancel = new JButton("Annuler");
knytBilPanel.add(new JLabel("Person Initialer :"));
knytBilPanel.add(personaleIdField);
knytBilPanel.add(new JLabel("Bil nr:"));
knytBilPanel.add(bilNrField);
knytBilPanel.add(new JLabel());
knytBilPanel.add(new JLabel());
knytBilPanel.add(confirm);
knytBilPanel.add(cancel);
confirm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){
database.knytBilTilPersonale(personaleIdField.getText(),bilNrField.getText());
hide();
}});
cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){
hide();
}});
pack();
setLocation(screenWidth/2-getWidth()/2,screenHeight/2-getHeight()/2);
show();
}
}
//Der oprettes en DefaultMutableTreeNode som skal indeholde personer, biler og værktøj
public void createTree(){
//De enkelte HashMaps hentes
HashMap personaleMap = database.getPersonaleMap();
HashMap bilMap = database.getBilMap();
HashMap lagerMap = database.getLager().getVaerktoejMap();
// Opret knuder
DefaultMutableTreeNode oversigtTree =new DefaultMutableTreeNode ("V.H.S. Oversigt");
DefaultMutableTreeNode personerTree =new DefaultMutableTreeNode ("Personer");
DefaultMutableTreeNode bilerTree =new DefaultMutableTreeNode ("Biler");
DefaultMutableTreeNode lagerTree =new DefaultMutableTreeNode ("Lager");
// og tilføj dem til oversigten
oversigtTree.add(personerTree);
oversigtTree.add(bilerTree);
oversigtTree.add(lagerTree);
//personaleMap løbes igennem og alle objekter bliver tilføjet som nodes
Set keysPerson = personaleMap.keySet();
Iterator itPerson = keysPerson.iterator();
while(itPerson.hasNext())
{
String key = (String) itPerson.next();
Personale valgtePerson = (Personale) personaleMap.get(key);
String bilnr = "";
Bil personaleBil = valgtePerson.getBil();
if (personaleBil != null) {bilnr = personaleBil.getBilNr();}
personerTree.add(new DefaultMutableTreeNode(valgtePerson.getNavn() + "(" +
valgtePerson.getPersonaleId() + ") - "+ bilnr ));
}
//bilMap løbes igennem og alle objekter bliver tilføjet som nodes
Set keysBil = bilMap.keySet();
Iterator itBil = keysBil.iterator();
while(itBil.hasNext())
{
String key = (String) itBil.next();
Bil valgteBil = (Bil) bilMap.get(key);
DefaultMutableTreeNode valgteBilTree = new DefaultMutableTreeNode(valgteBil.getBilNr() + " - " +
valgteBil.getMaerke());
HashMap bilvaerktoejMap = valgteBil.getVaerktoejMap();
Set keysBilVaerktoej = bilvaerktoejMap.keySet();
Iterator BilVaerktoejIt = keysBilVaerktoej.iterator();
while(BilVaerktoejIt.hasNext()) {
String holderKey = (String) BilVaerktoejIt.next();
Vaerktoej valgteVaerktoej = (Vaerktoej) bilvaerktoejMap.get(holderKey);
String fuks = "";
if(valgteVaerktoej.isSpecial()){fuks="*";}
valgteBilTree.add(new DefaultMutableTreeNode(valgteVaerktoej.getVaerktoejId() + " - " +
valgteVaerktoej.getNavn() +fuks ));
}
bilerTree.add(valgteBilTree);
}
//lagerMap løbes igennem og alle objekter bliver tilføjet som nodes
Set keys = lagerMap.keySet();
Iterator lagerIt = keys.iterator();
while(lagerIt.hasNext()) {
String lagerKey = (String) lagerIt.next();
Vaerktoej lagerVaerktoej = (Vaerktoej) lagerMap.get(lagerKey);
String special = "";
if(lagerVaerktoej.isSpecial()) {special = "*";}
lagerTree.add(new DefaultMutableTreeNode(lagerVaerktoej.getVaerktoejId() + " - " +
lagerVaerktoej.getNavn()+special));
}
// Opret træet
JTree tree = new JTree (oversigtTree);
// Tilføj træet til vinduet
JScrollPane scroll = new JScrollPane(tree);
oversigtPanel.removeAll();
JLabel info = new JLabel("* = special værktøj");
info.setFont(new Font("fiddo",2,10));
oversigtPanel.add(info,BorderLayout.SOUTH);
oversigtPanel.add(scroll,BorderLayout.CENTER);
}
//Forberedelse til oprettelse af indhold i søgefane
private int x;
private String soegStreng;
private String text = "hej jeg bor ude i en gård med en lille hund af ler";
private JPanel bund;
private JPanel venstreSoegePanel;
private JPanel mainSoegePanel;
private JPanel searchToolPanel;
private JPanel topToolPanel;
private JPanel downToolPanel;
private JTextField soegField;
private JButton soegKnap;
private JRadioButton radioBil;
private JRadioButton radioPerson;
private JRadioButton radioVaerktoej;
private TitledBorder title;
private Border etched;
//Et område, her en bestemt String, søges igennem for ligehed med indtastet søgeord
//og resultat tilføjes i panelet
public void search(String soegStreng) {
this.soegStreng = soegStreng;
StringTokenizer st = new StringTokenizer(soegStreng, " .,\t\n", false);
while (st.hasMoreTokens()) {
String test = st.nextToken().toLowerCase();
mainSoegePanel.add( new JLabel(text.indexOf(test) + " = " +test));
}
}
//Indholdet af søge panelet vises
public void showSoeg() {
title = BorderFactory.createTitledBorder("Søgemuligheder");
etched = BorderFactory.createEtchedBorder();
JLabel infoText = new JLabel("Indtast søgeord");
infoText.setFont(new Font("fiddo",3,9));
soegField = new JTextField(6);
soegKnap = new JButton("Søg");
radioBil = new JRadioButton("Biler",new ImageIcon("new icons/car.gif"));
radioPerson = new JRadioButton("Personer",new ImageIcon("new icons/man.gif"));
radioVaerktoej = new JRadioButton("Værktøj",new ImageIcon("new icons/tools.gif"));
ButtonGroup gb = new ButtonGroup();
gb.add(radioBil);
gb.add(radioPerson);
gb.add(radioVaerktoej);
radioBil.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){
radioBil.setIcon(new ImageIcon("new icons/car_select.gif"));
radioPerson.setIcon(new ImageIcon("new icons/man.gif"));
radioVaerktoej.setIcon(new ImageIcon("new icons/tools.gif"));
}});
radioPerson.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){
radioPerson.setIcon(new ImageIcon("new icons/man_select.gif"));
radioVaerktoej.setIcon(new ImageIcon("new icons/tools.gif"));
radioBil.setIcon(new ImageIcon("new icons/car.gif"));
}});
radioVaerktoej.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){
radioVaerktoej.setIcon(new ImageIcon("new icons/tools_select.gif"));
radioBil.setIcon(new ImageIcon("new icons/car.gif"));
radioPerson.setIcon(new ImageIcon("new icons/man.gif"));
}});
soegKnap.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){
search(soegField.getText());
mainSoegePanel.updateUI();
}});
topToolPanel = new JPanel();
downToolPanel = new JPanel();
downToolPanel.setLayout(new GridLayout(3,1));
downToolPanel.add(radioPerson);
downToolPanel.add(radioBil);
downToolPanel.add(radioVaerktoej);
downToolPanel.setBorder(title);
topToolPanel.add(soegField);
topToolPanel.add(soegKnap);
searchToolPanel = new JPanel();
searchToolPanel.setLayout(new BorderLayout());
searchToolPanel.add(topToolPanel,BorderLayout.NORTH);
searchToolPanel.add(downToolPanel,BorderLayout.CENTER);
searchToolPanel.add(infoText,BorderLayout.SOUTH);
bund = new JPanel();
venstreSoegePanel = new JPanel();
venstreSoegePanel.setLayout(new BorderLayout());
venstreSoegePanel.add(searchToolPanel,BorderLayout.NORTH);
mainSoegePanel = new JPanel();
JScrollPane mainSoegePanelScroll = new JScrollPane(mainSoegePanel);
bund.setLayout(new BorderLayout());
bund.add(venstreSoegePanel,BorderLayout.WEST);
bund.add(mainSoegePanelScroll,BorderLayout.CENTER);
mainSoegePanel.setBorder(etched);
}
}