From: Eneko Pinzolas Murua Date: Fri, 10 Apr 2015 10:37:22 +0000 (+0200) Subject: Minor error correction and some bugs, alongside with modify offers quality of life... X-Git-Url: https://xp-dev.com/git/RRRRHHHH_Code/commitdiff_plain/04582007ad2797602f03edf7442e11464828d00b?hp=e54e0de3e1c331622b5198e110f750e615ac4d5b Minor error correction and some bugs, alongside with modify offers quality of life improvements. --- diff --git a/ruralHouses/src/businessLogic/BookingManager.java b/ruralHouses/src/businessLogic/BookingManager.java index d4069fc..24dd41c 100644 --- a/ruralHouses/src/businessLogic/BookingManager.java +++ b/ruralHouses/src/businessLogic/BookingManager.java @@ -1,6 +1,6 @@ package businessLogic; -import java.sql.Date; +import java.util.Date; import com.db4o.ObjectContainer; import com.db4o.ObjectSet; diff --git a/ruralHouses/src/dataAccess/DB4oManager.java b/ruralHouses/src/dataAccess/DB4oManager.java index 8ec88c7..8078e78 100644 --- a/ruralHouses/src/dataAccess/DB4oManager.java +++ b/ruralHouses/src/dataAccess/DB4oManager.java @@ -5,7 +5,7 @@ import java.io.File; //import java.util.Vector; import java.rmi.RemoteException; -import java.sql.Date; +import java.util.Date; import java.util.HashSet; import java.util.ListIterator; import java.util.Vector; @@ -37,7 +37,7 @@ public class DB4oManager { private static DB4oManager theDB4oManager = null; private static DB4oManagerAux theDB4oManagerAux; - + static ConfigXML c; private DB4oManager() throws Exception { @@ -112,12 +112,14 @@ public class DB4oManager { try { Owner jon = new Owner("Jon"); Owner alfredo = new Owner("Alfredo"); - jon.addRuralHouse("Ezkioko", "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3); - jon.addRuralHouse("Eskiatze", "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4); + jon.addRuralHouse("Ezkioko", "Ezkioko etxea", "Beatriz", 3, 3, 3, 3, + 3); + jon.addRuralHouse("Eskiatze", "Eskiatzeko etxea", "Guazate", 4, 4, 4, + 4, 4); jon.setBankAccount("1349 5677 21 2133567777"); - alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Pitillas", 5, 5, 5, 5, - 5); - alfredo.addRuralHouse("Murgoitz", "", "Murgia", 6, 6, 6, 6, 6); + alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Vegas", 5, + 5, 5, 5, 5); + alfredo.addRuralHouse("Murgoitz", "", "Cedro", 6, 6, 6, 6, 6); alfredo.setBankAccount("4144 0087 23 9700002133"); Account jonAcc = new Account("userJon", "passJon", jon); Account alfredoAcc = new Account("userAlfredo", "passAlfredo", @@ -155,6 +157,7 @@ public class DB4oManager { } + public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay, float price) throws RemoteException, Exception { Offer o = null; @@ -166,8 +169,8 @@ public class DB4oManager { try { - RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), - null, null, null, null); + RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null, + null, null, null); ObjectSet result = db.queryByExample(proto); RuralHouse rh = (RuralHouse) result.next(); o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay, @@ -195,7 +198,6 @@ public class DB4oManager { try { - db.store(offer); db.commit(); @@ -207,9 +209,10 @@ public class DB4oManager { } return offer; } - + @SuppressWarnings("finally") - public void deleteOffer(RuralHouse rh, Offer offer) throws RemoteException, Exception { + public void deleteOffer(RuralHouse rh, Offer offer) throws RemoteException, + Exception { if (c.isDatabaseLocal() == false) openSDB(); else @@ -228,7 +231,6 @@ public class DB4oManager { db.close(); } } - /** * This method creates a book with a corresponding parameters @@ -253,9 +255,8 @@ public class DB4oManager { if (c.isDatabaseLocal() == false) openSDB(); - RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), - null, ruralHouse.getDescription(), ruralHouse.getDistrict(), - null); + RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null, + ruralHouse.getDescription(), ruralHouse.getDistrict(), null); ObjectSet result = db.queryByExample(proto); RuralHouse rh = (RuralHouse) result.next(); @@ -336,10 +337,9 @@ public class DB4oManager { try { - RuralHouse rhn = (RuralHouse) db - .queryByExample( - new RuralHouse(rh.getHouseName(), null, null, - null, null)).next(); + RuralHouse rhn = (RuralHouse) db.queryByExample( + new RuralHouse(rh.getHouseName(), null, null, null, null)) + .next(); if (rhn.overlapsWith(firstDay, lastDay) != null) throw new OverlappingOfferExists(); else @@ -400,8 +400,8 @@ public class DB4oManager { openDB(); boolean stored = false; - RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, - null, null); + RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null, + null); try { ObjectSet result = db.queryByExample(house); if (result.isEmpty()) { @@ -415,18 +415,17 @@ public class DB4oManager { return stored; } - public void removeHouse(RuralHouse rh, Owner owner) { if (c.isDatabaseLocal() == false) openSDB(); else - openDB(); + openDB(); try { ObjectSet result = db.queryByExample(rh); if (!result.isEmpty()) { RuralHouse found = (RuralHouse) result.get(0); -// db.delete(found.getOwner()); + // db.delete(found.getOwner()); db.store(owner); db.delete(found); db.commit(); @@ -438,10 +437,10 @@ public class DB4oManager { } } - - public Vector getRuralHousesByTown(String town){ - RuralHouse rh = new RuralHouse(null,null,null,town,null); - + + public Vector getRuralHousesByTown(String town) { + RuralHouse rh = new RuralHouse(null, null, null, town, null); + if (c.isDatabaseLocal() == false) openSDB(); else @@ -458,49 +457,50 @@ public class DB4oManager { } } - public RuralHouse getRuralHouseByName(String name){ - RuralHouse rh = new RuralHouse(name,null,null,null,null); - + + public RuralHouse getRuralHouseByName(String name) { + RuralHouse rh = new RuralHouse(name, null, null, null, null); + if (c.isDatabaseLocal() == false) openSDB(); else openDB(); - - try{ + + try { ObjectSet result = db.queryByExample(rh); Vector ruralHouses = new Vector(); - while(result.hasNext()) + while (result.hasNext()) ruralHouses.add(result.next()); db.close(); - return ruralHouses.get(0); - } catch (NullPointerException e){ + if (!ruralHouses.isEmpty()) + return ruralHouses.get(0); + else + return null; + } catch (NullPointerException e) { return null; } - - - + } - - public Vector getRuralHouses(String town,int nBed , int nKit, int nBath, int nPark, int nLiv){ - HouseFeatures fea = new HouseFeatures(nBed,nKit,nBath,nLiv,nPark); - RuralHouse rh = new RuralHouse(null,null,null,town,fea); + + public Vector getRuralHouses(String town, int nBed, int nKit, + int nBath, int nPark, int nLiv) { + HouseFeatures fea = new HouseFeatures(nBed, nKit, nBath, nLiv, nPark); + RuralHouse rh = new RuralHouse(null, null, null, town, fea); if (c.isDatabaseLocal() == false) openSDB(); else openDB(); - - try{ + + try { ObjectSet result = db.queryByExample(rh); Vector ruralHouses = new Vector(); - while(result.hasNext()) + while (result.hasNext()) ruralHouses.add(result.next()); db.close(); return ruralHouses; - } catch (NullPointerException e){ + } catch (NullPointerException e) { return null; } - - - + } } diff --git a/ruralHouses/src/domain/RuralHouse.java b/ruralHouses/src/domain/RuralHouse.java index 2e61137..423d5e7 100644 --- a/ruralHouses/src/domain/RuralHouse.java +++ b/ruralHouses/src/domain/RuralHouse.java @@ -130,7 +130,17 @@ public class RuralHouse implements Serializable { return availableOffers; } - + public Vector getAllOffers() { + Vector availableOffers=new Vector(); + Iterator e=offers.iterator(); + Offer offer; + while (e.hasNext()){ + offer=e.next(); + if ( (offer.getBooking()==null) ) + availableOffers.add(offer); + } + return availableOffers; + } /** * This method obtains the offer that match exactly with a given dates that has not been booked * @@ -149,6 +159,7 @@ public class RuralHouse implements Serializable { return null; } + public Offer overlapsWith( Date firstDay, Date lastDay) { Iterator e=offers.iterator(); diff --git a/ruralHouses/src/gui/BookRuralHouseGUI.java b/ruralHouses/src/gui/BookRuralHouseGUI.java deleted file mode 100644 index 7a262fc..0000000 --- a/ruralHouses/src/gui/BookRuralHouseGUI.java +++ /dev/null @@ -1,267 +0,0 @@ -package gui; - -import businessLogic.BookingManager; -import businessLogic.HouseManager; - -import com.toedter.calendar.*; - -import domain.Booking; -import domain.RuralHouse; -import exceptions.OfferCanNotBeBooked; - -import java.beans.*; -import java.sql.Date; -import java.text.*; -import java.util.*; - -import javax.swing.*; - -import java.awt.*; -import java.awt.event.*; - - - -public class BookRuralHouseGUI extends JFrame { -private static final long serialVersionUID = 1L; -public static final long MILLSECS_PER_DAY= 24 * 60 * 60 * 1000; - - private JLabel jLabel1 = new JLabel(); - private JComboBox jComboBox1; - Vector ruralHouses; - private JLabel jLabel2 = new JLabel(); - private JLabel jLabel3 = new JLabel(); - private JLabel jLabel4 = new JLabel(); - private JTextField jTextField2 = new JTextField(); - private JTextField jTextField3 = new JTextField(); - private JTextField jTextField4 = new JTextField(); - private JButton jButton2 = new JButton(); - private JButton jButton3 = new JButton(); - - // Code for JCalendar - private JCalendar jCalendar1 = new JCalendar(); - private Calendar calendarMio = null; - private JLabel jLabel5 = new JLabel(); - - - -public BookRuralHouseGUI() - { - try - { - jbInit(); - - - } - catch(Exception e) - { - e.printStackTrace(); - } - } - public BookRuralHouseGUI(String houseName,Date firstDay,Date lastDay) - { - try - { - jbInit(); - - - // Put the "houseNumber", "firstDay" and "lastDay" in the graphical components of the user interface - for (int i=0; i rhs = rh.getAllOffers().elements(); + while (rhs.hasMoreElements()) { + Offer of = rhs.nextElement(); + Vector row = new Vector(); + row.add(of.getOfferNumber()); + row.add(of.getFirstDay()); + row.add(of.getLastDay()); + row.add(of.getPrice()); + tableModel.addRow(row); + + } + table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() { + @Override + public Component getTableCellRendererComponent(JTable table, + Object value, boolean isSelected, boolean hasFocus, + int row, int col) { + + super.getTableCellRendererComponent(table, value, isSelected, + hasFocus, row, col); + + Date firstDay = (Date) table.getModel().getValueAt(row, 1); + Date lastDay = (Date) table.getModel().getValueAt(row, 2); + if (FirstDay != null && LastDay != null) { + if (LastDay.before(lastDay) || FirstDay.after(firstDay)) { + setBackground(Color.RED); + setForeground(Color.BLACK); + } else { + setBackground(Color.GREEN); + setForeground(Color.BLACK); + } + } + return this; + } + }); + } + + private void jButton_ActionPerformed(ActionEvent arg0) { + BookingManager bookingM = new BookingManager(); + //RegExp to see if telephone number is correct??TODO + Booking book = null; + try { + book = bookingM.createBooking(rh, rh.offers.get(row).getFirstDay(), rh.offers.get(row).getLastDay(), telIn.getText()); + } catch (OfferCanNotBeBooked e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + if (book != null) { + BookRuralHouseConfirmationWindow confirmWindow = new BookRuralHouseConfirmationWindow( + book); + confirmWindow.setVisible(true); + } + } +} diff --git a/ruralHouses/src/gui/ModifyHouseGUI.java b/ruralHouses/src/gui/ModifyHouseGUI.java index 737267e..497829e 100644 --- a/ruralHouses/src/gui/ModifyHouseGUI.java +++ b/ruralHouses/src/gui/ModifyHouseGUI.java @@ -65,7 +65,7 @@ public class ModifyHouseGUI extends JFrame { contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); - JLabel lblCode = new JLabel("House Code:"); + JLabel lblCode = new JLabel("House Name:"); lblCode.setHorizontalAlignment(SwingConstants.RIGHT); lblDistrict = new JLabel("District:"); diff --git a/ruralHouses/src/gui/ModifyOfferGUI.java b/ruralHouses/src/gui/ModifyOfferGUI.java index 651d85c..853ce07 100644 --- a/ruralHouses/src/gui/ModifyOfferGUI.java +++ b/ruralHouses/src/gui/ModifyOfferGUI.java @@ -56,7 +56,10 @@ public class ModifyOfferGUI extends JFrame { jComboBox1 = new JComboBox(v); - comboBox_o = new JComboBox(); + comboBox_o = new JComboBox(((RuralHouse)jComboBox1.getSelectedItem()).getAllOffers()); + DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale()); + jTextField1.setText(dateformat1.format(((Offer) comboBox_o.getSelectedItem()).getFirstDay())); + jTextField2.setText(dateformat1.format(((Offer) comboBox_o.getSelectedItem()).getLastDay())); jComboBox1.setBounds(new Rectangle(115, 12, 115, 20)); jLabel1.setText("List of houses:"); diff --git a/ruralHouses/src/gui/QueryAvailabilityGUI.java b/ruralHouses/src/gui/QueryAvailabilityGUI.java deleted file mode 100644 index 557cca4..0000000 --- a/ruralHouses/src/gui/QueryAvailabilityGUI.java +++ /dev/null @@ -1,275 +0,0 @@ -package gui; - -import businessLogic.HouseManager; - -import com.toedter.calendar.JCalendar; - -import domain.Offer; -import domain.RuralHouse; - -import javax.swing.*; - -import java.awt.*; -import java.awt.event.*; -import java.beans.*; -import java.sql.Date; -import java.text.DateFormat; -import java.util.*; - -import javax.swing.table.DefaultTableModel; - - - - - -public class QueryAvailabilityGUI extends JFrame { -private static final long serialVersionUID = 1L; - - private JLabel jLabel1 = new JLabel(); - private JLabel jLabel2 = new JLabel(); - private JTextField jTextField2 = new JTextField(); - private JLabel jLabel3 = new JLabel(); - private JTextField jTextField3 = new JTextField(); - private JButton jButton1 = new JButton(); - private JButton jButton2 = new JButton(); - - // Code for JCalendar - private JCalendar jCalendar1 = new JCalendar(); - private Calendar calendarMio = null; - private JLabel jLabel4 = new JLabel(); - private JScrollPane scrollPane = new JScrollPane(); - private JComboBox comboBox ; - private JTable table; - private DefaultTableModel tableModel; - private final JLabel labelNoOffers = new JLabel(""); - private String[] columnNames = new String[] { - "Offer#", "Rural House", "First Day", "Last Day", "Price" - }; - - - - public QueryAvailabilityGUI() - { - - try - { - jbInit(); - } - catch(Exception e) - { - e.printStackTrace(); - } - - } - - private void jbInit() throws Exception - { - HouseManager houseM = new HouseManager(); - - - - Vector rhs=houseM.getAllRuralHouses(); - - comboBox = new JComboBox(rhs); - //comboBox.setModel(new DefaultComboBoxModel(rhs)); - - - this.getContentPane().setLayout(null); - this.setSize(new Dimension(433, 548)); - this.setTitle("Query availability"); - jLabel1.setText("Rural house code:"); - jLabel1.setBounds(new Rectangle(27, 20, 128, 25)); - jLabel2.setText("First day:"); - jLabel2.setBounds(new Rectangle(40, 55, 75, 25)); - jTextField2.setBounds(new Rectangle(190, 210, 155, 25)); - jTextField2.setEditable(false); - jLabel3.setText("Number of nights:"); - jLabel3.setBounds(new Rectangle(40, 250, 115, 25)); - jTextField3.setBounds(new Rectangle(190, 250, 155, 25)); - jTextField3.setText("0"); - jButton1.setText("Accept"); - jButton1.setBounds(new Rectangle(55, 455, 130, 30)); - jButton1.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent e) - { - jButton1_actionPerformed(e); - } - }); - jButton2.setText("Close"); - jButton2.setBounds(new Rectangle(230, 455, 130, 30)); - - - jTextField3.addFocusListener(new FocusListener() - { - public void focusGained(FocusEvent e) - { - } - - public void focusLost(FocusEvent e) - { - jTextField3_focusLost(); - } - }); - jButton2.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent e) - { - jButton2_actionPerformed(e); - } - }); - jLabel4.setBounds(new Rectangle(55, 300, 305, 30)); - jLabel4.setForeground(Color.red); - jCalendar1.setBounds(new Rectangle(190, 60, 225, 150)); - scrollPane.setBounds(new Rectangle(45, 305, 320, 116)); - - this.getContentPane().add(scrollPane, null); - - table = new JTable(); - table.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent e) { - int i=table.getSelectedRow(); - String houseName = (String) tableModel.getValueAt(i,1); - Date firstDate=new Date(((java.util.Date)tableModel.getValueAt(i,2)).getTime()); - Date lastDate=new Date(((java.util.Date)tableModel.getValueAt(i,3)).getTime()); - - BookRuralHouseGUI b=new BookRuralHouseGUI(houseName,firstDate,lastDate); - b.setVisible(true);} - }); - - scrollPane.setViewportView(table); - tableModel = new DefaultTableModel( - null, - columnNames); - - table.setModel(tableModel); - this.getContentPane().add(jCalendar1, null); - this.getContentPane().add(jLabel4, null); - this.getContentPane().add(jButton2, null); - this.getContentPane().add(jButton1, null); - this.getContentPane().add(jTextField3, null); - this.getContentPane().add(jLabel3, null); - this.getContentPane().add(jTextField2, null); - this.getContentPane().add(jLabel2, null); - this.getContentPane().add(jLabel1, null); - comboBox.setBounds(new Rectangle(245, 22, 115, 20)); - comboBox.setBounds(189, 22, 115, 20); - - getContentPane().add(comboBox); - labelNoOffers.setBounds(73, 432, 265, 14); - - getContentPane().add(labelNoOffers); - - // Codigo para el JCalendar - this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() - { - public void propertyChange(PropertyChangeEvent propertychangeevent) - { - if (propertychangeevent.getPropertyName().equals("locale")) - { - jCalendar1.setLocale((Locale) propertychangeevent.getNewValue()); - DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale()); - jTextField2.setText(dateformat.format(calendarMio.getTime())); - } - else if (propertychangeevent.getPropertyName().equals("calendar")) - { - calendarMio = (Calendar) propertychangeevent.getNewValue(); - DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale()); - jTextField2.setText(dateformat1.format(calendarMio.getTime())); - jCalendar1.setCalendar(calendarMio); - } - } - }); - - - } - - private void jButton2_actionPerformed(ActionEvent e) - { - this.setVisible(false); - } - - - private void jTextField3_focusLost() - { - try - { - new Integer (jTextField3.getText()); - jLabel4.setText(""); - } - catch (NumberFormatException ex) - { - jLabel4.setText("Error: Introduce a number"); - } - } - - private void jButton1_actionPerformed(ActionEvent e) - { - // House object - RuralHouse rh=(RuralHouse)comboBox.getSelectedItem(); - // First day - Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime()); - //Remove the hour:minute:second:ms from the date - firstDay=Date.valueOf(firstDay.toString()); - final long diams=1000*60*60*24; - long nights= diams * Integer.parseInt(jTextField3.getText()); - // Last day - Date lastDay= new Date((long)(firstDay.getTime()+nights)); - - try { - - - Vector v=rh.getOffers(firstDay, lastDay); - - Enumeration en=v.elements(); - Offer of; - tableModel.setDataVector(null, columnNames); - if (!en.hasMoreElements()) - labelNoOffers.setText("There are no offers at these dates"); - else { - labelNoOffers.setText("Select an offer if you want to book"); - - while (en.hasMoreElements()) { - of=en.nextElement(); - System.out.println("Offer retrieved: "+of.toString()); - Vector row = new Vector(); - row.add(of.getOfferNumber()); - row.add(of.getRuralHouse().getHouseName()); - - // Dates are stored in db4o as java.util.Date objects instead of java.sql.Date objects - // They have to be converted into java.sql.Date objects before - Date firstDaySqlDate = new Date(of.getFirstDay().getTime()); - Date lastDaySqlDate = new Date(of.getLastDay().getTime()); - row.add(firstDaySqlDate); - row.add(lastDaySqlDate); - row.add(of.getPrice()); - - - tableModel.addRow(row); - } - } - - - } catch (Exception e1) { - - labelNoOffers.setText(e1.getMessage()); - }} - private static void addPopup(Component component, final JPopupMenu popup) { - component.addMouseListener(new MouseAdapter() { - public void mousePressed(MouseEvent e) { - if (e.isPopupTrigger()) { - showMenu(e); - } - } - public void mouseReleased(MouseEvent e) { - if (e.isPopupTrigger()) { - showMenu(e); - } - } - private void showMenu(MouseEvent e) { - popup.show(e.getComponent(), e.getX(), e.getY()); - } - }); - } -} \ No newline at end of file diff --git a/ruralHouses/src/gui/QueryAvailabilityGUI2.java b/ruralHouses/src/gui/QueryAvailabilityGUI2.java index a12ad16..cdb3282 100644 --- a/ruralHouses/src/gui/QueryAvailabilityGUI2.java +++ b/ruralHouses/src/gui/QueryAvailabilityGUI2.java @@ -7,7 +7,6 @@ import java.util.*; import javax.swing.*; - import java.awt.*; import java.awt.event.*; @@ -44,7 +43,7 @@ public class QueryAvailabilityGUI2 extends JFrame { private final JLabel lblNewLabel = new JLabel(""); private final JRadioButton ruralHouseName = new JRadioButton( "Use RuralHouse name"); - private final JRadioButton district = new JRadioButton("Use district name"); + private final JRadioButton district = new JRadioButton("Use District Name"); private final ButtonGroup buttonGroup = new ButtonGroup(); private final JMenuBar menuBar = new JMenuBar(); private final JMenu mnMoreOptions = new JMenu("Search Options"); @@ -61,6 +60,7 @@ public class QueryAvailabilityGUI2 extends JFrame { private HouseManager houseMan = new HouseManager(); private final JLabel lblNumberOfLivings = new JLabel("Number of Livings:"); private final JTextField nLivings = new JTextField(); + private final JLabel feedback = new JLabel(""); public QueryAvailabilityGUI2() { nLivings.setText("0"); @@ -83,6 +83,7 @@ public class QueryAvailabilityGUI2 extends JFrame { jComboBox1 = new JComboBox(new DefaultComboBoxModel( Districs.longNames())); + jComboBox1.setEnabled(false); jComboBox1.setBounds(new Rectangle(115, 30, 115, 20)); jLabel2.setText("First day :"); jLabel2.setBounds(new Rectangle(20, 134, 85, 25)); @@ -94,9 +95,8 @@ public class QueryAvailabilityGUI2 extends JFrame { jTextField2.setEditable(false); jLabel4.setText("Rural House name:"); jLabel4.setBounds(new Rectangle(275, 30, 128, 20)); - jTextField3.setEnabled(false); jTextField3.setBounds(new Rectangle(413, 30, 115, 20)); - jButton1.setText("Accept"); + jButton1.setText("Search"); jButton1.setBounds(new Rectangle(60, 377, 165, 30)); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -191,7 +191,7 @@ public class QueryAvailabilityGUI2 extends JFrame { popupMenu.add(lblNumberOfLivings); popupMenu.add(nLivings); - + this.getContentPane().add(jCalendar2, null); this.getContentPane().add(jCalendar1, null); this.getContentPane().add(jLabel5, null); @@ -207,13 +207,12 @@ public class QueryAvailabilityGUI2 extends JFrame { lblNewLabel.setBounds(115, 301, 298, 38); getContentPane().add(lblNewLabel); - - district.setSelected(true); ; district.setBounds(20, 69, 128, 20); getContentPane().add(district); buttonGroup.add(ruralHouseName); buttonGroup.add(district); + ruralHouseName.setSelected(true); ruralHouseName.setBounds(276, 60, 177, 38); district.addItemListener(new ItemListener() { @Override @@ -221,8 +220,11 @@ public class QueryAvailabilityGUI2 extends JFrame { int state = e.getStateChange(); if (state == ItemEvent.SELECTED) { jComboBox1.setEnabled(true); + jCalendar1.setEnabled(false); + jCalendar2.setEnabled(false); } else if (state == ItemEvent.DESELECTED) { jComboBox1.setEnabled(false); + } } }); @@ -233,6 +235,8 @@ public class QueryAvailabilityGUI2 extends JFrame { int state = e.getStateChange(); if (state == ItemEvent.SELECTED) { jTextField3.setEnabled(true); + jCalendar1.setEnabled(true); + jCalendar2.setEnabled(true); } else if (state == ItemEvent.DESELECTED) { jTextField3.setEnabled(false); } @@ -243,6 +247,11 @@ public class QueryAvailabilityGUI2 extends JFrame { JLabel lblNewLabel_2 = new JLabel("District name:"); lblNewLabel_2.setBounds(20, 30, 85, 17); getContentPane().add(lblNewLabel_2); + feedback.setHorizontalAlignment(SwingConstants.CENTER); + feedback.setForeground(Color.RED); + feedback.setBounds(134, 413, 269, 17); + + getContentPane().add(feedback); setJMenuBar(menuBar); @@ -267,14 +276,31 @@ public class QueryAvailabilityGUI2 extends JFrame { listOfHousesGUI list = new listOfHousesGUI(houses); list.setVisible(true); } else if (this.ruralHouseName.isSelected()) { - // show features of specific house - // TODO + RuralHouse rh = houseMan.getHouseByName(jTextField3.getText()); + // The next instruction creates a java.sql.Date object from the date selected in the JCalendar object + Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime()); + // The next instruction removes the hour, minute, second and ms from the date + // This has to be made because the date will be stored in db4o as a java.util.Date object + // that would store those data, and that would give problems when comparing dates later + firstDay=Date.valueOf(firstDay.toString()); + + + Date lastDay=new Date(jCalendar2.getCalendar().getTime().getTime()); + //Remove the hour:minute:second:ms from the date + lastDay=Date.valueOf(lastDay.toString()); + if (rh != null) { + HouseFeaturesGUI hou = new HouseFeaturesGUI(rh,firstDay,lastDay); + hou.setVisible(true); + } else { + feedback.setText("Not matching houses"); + } } } private void jButton2_actionPerformed(ActionEvent e) { this.setVisible(false); } + private static void addPopup(Component component, final JPopupMenu popup) { component.addMouseListener(new MouseAdapter() { diff --git a/ruralHouses/src/gui/StartWindow.java b/ruralHouses/src/gui/StartWindow.java index 8d2b019..e115587 100644 --- a/ruralHouses/src/gui/StartWindow.java +++ b/ruralHouses/src/gui/StartWindow.java @@ -4,7 +4,6 @@ package gui; * @author willCodeForFood */ import exceptions.DB4oManagerCreationException; -import gui.BookRuralHouseGUI; import javax.swing.*; @@ -28,7 +27,6 @@ public class StartWindow extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; - private JButton boton1 = null; private JButton boton2 = null; private JButton boton3 = null; private static configuration.ConfigXML c; @@ -127,39 +125,15 @@ public class StartWindow extends JFrame { */ private JPanel getJContentPane() { if (jContentPane == null) { - GridLayout gridLayout = new GridLayout(); - gridLayout.setRows(4); - gridLayout.setColumns(1); jContentPane = new JPanel(); - jContentPane.setLayout(gridLayout); + jContentPane.setLayout(null); jContentPane.add(getLblNewLabel()); - jContentPane.add(getBoton2(), null); - jContentPane.add(getBoton3(), null); - jContentPane.add(getBoton1(), null); + jContentPane.add(getBoton2()); + jContentPane.add(getBoton3()); } return jContentPane; } - /** - * This method initializes boton1 - * - * @return javax.swing.JButton - */ - private JButton getBoton1() { - if (boton1 == null) { - boton1 = new JButton(); - boton1.setText("Book rural house"); - boton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent e) { - // C?digo cedido por la univerdad - JFrame a = new BookRuralHouseGUI(); - a.setVisible(true); - } - }); - } - return boton1; - } - /** * This method initializes boton2 * @@ -168,6 +142,7 @@ public class StartWindow extends JFrame { private JButton getBoton2() { if (boton2 == null) { boton2 = new JButton(); + boton2.setBounds(0, 74, 479, 93); boton2.setText("Login"); boton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { @@ -188,6 +163,7 @@ public class StartWindow extends JFrame { private JButton getBoton3() { if (boton3 == null) { boton3 = new JButton(); + boton3.setBounds(0, 165, 479, 87); boton3.setText("Query availability"); boton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { @@ -206,6 +182,7 @@ public class StartWindow extends JFrame { private JLabel getLblNewLabel() { if (lblNewLabel == null) { lblNewLabel = new JLabel("Select option:"); + lblNewLabel.setBounds(0, 0, 479, 63); lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 13)); lblNewLabel.setForeground(Color.BLACK); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); diff --git a/ruralHouses/src/gui/TableRowRenderingTip.java b/ruralHouses/src/gui/TableRowRenderingTip.java new file mode 100644 index 0000000..860db19 --- /dev/null +++ b/ruralHouses/src/gui/TableRowRenderingTip.java @@ -0,0 +1,135 @@ +package gui; + +import java.awt.*; +import javax.swing.*; +import javax.swing.table.*; +import javax.swing.border.*; + +public class TableRowRenderingTip extends JPanel +{ + public TableRowRenderingTip() + { + Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"}; + Object[][] data = + { + {"Buy", "IBM", new Integer(1000), new Double(80.5), Boolean.TRUE}, + {"Sell", "Dell", new Integer(2000), new Double(6.25), Boolean.FALSE}, + {"Short Sell", "Apple", new Integer(3000), new Double(7.35), Boolean.TRUE}, + {"Buy", "MicroSoft", new Integer(4000), new Double(27.50), Boolean.FALSE}, + {"Short Sell", "Cisco", new Integer(5000), new Double(20), Boolean.TRUE} + }; + + DefaultTableModel model = new DefaultTableModel(data, columnNames) + { + public Class getColumnClass(int column) + { + return getValueAt(0, column).getClass(); + } + }; + + JTabbedPane tabbedPane = new JTabbedPane(); + tabbedPane.addTab("Alternating", createAlternating(model)); + tabbedPane.addTab("Border", createBorder(model)); + tabbedPane.addTab("Data", createData(model)); + add( tabbedPane ); + } + + private JComponent createAlternating(DefaultTableModel model) + { + JTable table = new JTable( model ) + { + public Component prepareRenderer(TableCellRenderer renderer, int row, int column) + { + Component c = super.prepareRenderer(renderer, row, column); + + // Alternate row color + + if (!isRowSelected(row)) + c.setBackground(row % 2 == 0 ? getBackground() : Color.LIGHT_GRAY); + + return c; + } + }; + + table.setPreferredScrollableViewportSize(table.getPreferredSize()); + table.changeSelection(0, 0, false, false); + return new JScrollPane( table ); + } + + private JComponent createBorder(DefaultTableModel model) + { + + JTable table = new JTable( model ) + { + private Border outside = new MatteBorder(1, 0, 1, 0, Color.RED); + private Border inside = new EmptyBorder(0, 1, 0, 1); + private Border highlight = new CompoundBorder(outside, inside); + + public Component prepareRenderer(TableCellRenderer renderer, int row, int column) + { + Component c = super.prepareRenderer(renderer, row, column); + JComponent jc = (JComponent)c; + + // Add a border to the selected row + + if (isRowSelected(row)) + jc.setBorder( highlight ); + + return c; + } + }; + + table.setPreferredScrollableViewportSize(table.getPreferredSize()); + table.changeSelection(0, 0, false, false); + return new JScrollPane( table ); + } + + private JComponent createData(DefaultTableModel model) + { + JTable table = new JTable( model ) + { + public Component prepareRenderer(TableCellRenderer renderer, int row, int column) + { + Component c = super.prepareRenderer(renderer, row, column); + + // Color row based on a cell value + + if (!isRowSelected(row)) + { + c.setBackground(getBackground()); + int modelRow = convertRowIndexToModel(row); + String type = (String)getModel().getValueAt(modelRow, 0); + if ("Buy".equals(type)) c.setBackground(Color.GREEN); + if ("Sell".equals(type)) c.setBackground(Color.YELLOW); + } + + return c; + } + }; + + table.setPreferredScrollableViewportSize(table.getPreferredSize()); + table.changeSelection(0, 0, false, false); + table.setAutoCreateRowSorter(true); + return new JScrollPane( table ); + } + + public static void main(String[] args) + { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + createAndShowGUI(); + } + }); + } + + public static void createAndShowGUI() + { + JFrame.setDefaultLookAndFeelDecorated(true); + JFrame frame = new JFrame("Table Row Rendering"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.add( new TableRowRenderingTip() ); + frame.pack(); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + } +} diff --git a/ruralHouses/src/gui/listOfHousesGUI.java b/ruralHouses/src/gui/listOfHousesGUI.java index 0478466..b8db0df 100644 --- a/ruralHouses/src/gui/listOfHousesGUI.java +++ b/ruralHouses/src/gui/listOfHousesGUI.java @@ -21,6 +21,8 @@ import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import java.awt.Component; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.sql.Date; import java.util.Enumeration; import java.util.LinkedList; @@ -31,60 +33,66 @@ import javax.swing.Box; public class listOfHousesGUI extends JFrame { private JPanel contentPane; - private JTextField textField; private JTable table; private DefaultTableModel tableModel; - - /** + private Vector houses; + /** * Create the frame. */ - public listOfHousesGUI(Vector houses) { + public listOfHousesGUI(Vector rhs) { try { - init(houses); + this.houses=rhs; + init(); } catch (Exception e) { e.printStackTrace(); } } - private void init(Vector houses) throws Exception { + private void init() throws Exception { setBounds(100, 100, 600, 450); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); - JLabel lblNewLabel = new JLabel("List of available houses in:"); + JLabel lblNewLabel = new JLabel(); lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27)); - lblNewLabel.setBounds(23, 41, 325, 33); + lblNewLabel.setBounds(23, 41, 536, 33); contentPane.add(lblNewLabel); - - textField = new JTextField(); - textField.setEnabled(false); - textField.setEditable(false); - textField.setFont(new Font("Tahoma", Font.PLAIN, 27)); - textField.setBounds(373, 41, 152, 33); - contentPane.add(textField); - textField.setColumns(10); if (houses.isEmpty()) - textField.setText("No houses Matching"); + lblNewLabel.setText("There are not houses matching your search"); else - textField.setText(houses.get(0).getDistrict()); + lblNewLabel.setText("List of houses that match your search:"); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(new Rectangle(45, 305, 320, 116)); scrollPane.setBounds(23, 113, 536, 271); contentPane.add(scrollPane); - table = new JTable(); + table = new JTable() { + private static final long serialVersionUID = 1L; + + public boolean isCellEditable(int row, int column) { + return false; + }; + }; scrollPane.setViewportView(table); tableModel = new DefaultTableModel(null, new String[] { "House Name", "Bedrooms", "Kitchens", "Baths", "Parkings", "Livings" }); + //Maybe there is a better way to avoid interaction. - table.setEnabled(false); + //table.setEnabled(false); table.setModel(tableModel); Enumeration en = houses.elements(); RuralHouse rh; - + table.addMouseListener( new MouseAdapter(){ + @Override + public void mouseClicked(MouseEvent arg0) { + int row = table.getSelectedRow(); + HouseFeaturesGUI feat = new HouseFeaturesGUI(houses.get(row),null,null); + feat.setVisible(true); + } + }); while (en.hasMoreElements()) { rh = en.nextElement(); Vector row = new Vector(); @@ -94,7 +102,6 @@ public class listOfHousesGUI extends JFrame { row.add(rh.getFeatures().getnBaths()); row.add(rh.getFeatures().getnParkings()); row.add(rh.getFeatures().getnLivings()); - System.out.println(rh.getFeatures().getnLivings()); tableModel.addRow(row); }