craftleft.gif (3002 bytes)QuickTable
Home | API | Tutorial | Download | Support | Contact topblack.gif (108 bytes)


QuickTable User Cp  |  Register  |  Members  |  Search  |  Help
    |- QuickTable Discussion Forums > Non-Database mode - Dataobjects/EJB/Hibernate/File/Array/Vector Post New Topic   Post A Reply
Quicktable in non database mode printer friendly version
next newest post
Author Messages
wf@bitplan.com
Unregistered
Edit or delete this message Reply w/Quote
Posted Tuesday, March 14, 2006 @ 15:43:57  

My attempt to use Quicktable in no Database mode today worked great on first attempt.

I'm trying to load in object from Hibernate and then use these with Quicktable. See Code below.
And actually Quicktable starts nicely and displays a table. Quicktable does everything by itsself and decides which columsn to display.
In my case this leads to an unwanted side-effect:
I get lots of redundant columns because my (generated) code contains multiple getters and setters and a tri-state column state attribute per "real" attribute. This way every attribute shows three times in the table as three different columns.

I tried to get access to the way the columns are displayed and was happy to be able to analyze the Source Code for that that I just bought recently. The DBTable.java I have is @version 1.0 05/05/99 - so I'm assuming I have an outdated early version which is deprecated.

I was looking for a way to influence the column layout and was thinking that addColum calls would do the job. The result is that I get Exceptions telling me not to addColumns "java.lang.Exception: addColumn() method cannot be called before calling refresh(...) type methods or createColumnModelFromQuery() method"

Refresh led to the problem in the first place by analyzing my classes with the assumption that all public fields should be displayed which is unfortunately not the case. This assumption does not fit my problem.

Where can I get the most current version which might have better support for non database mode where I can influence the column layout?

Code:

package com.bitplan.test.quicktable;

import com.bitplan.Projektverwaltung.*;
import com.bitplan.Projektverwaltung.impl.*;
import com.bitplan.bobase.UI;

import java.sql.Types;
import java.util.Collection;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.JFrame;

import quick.dbtable.Column;
import quick.dbtable.DBTable;
import quick.dbtable.DBTableCellListener;
import quick.dbtable.DatabaseChangeListener;

public class HibernateTable extends JFrame {
org.hibernate.Session session;
DBTable dBTable1;

public HibernateTable()
{
//set Frame properties
setSize(600,400);
setVisible(true);

//create a new quicktable
dBTable1 = new DBTable();

//add to frame
getContentPane().add(dBTable1);

//to create the navigation bars for the table
dBTable1.createControlPanel();

try
{
// get Hibernate objects
session=com.bitplan.bobase.HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx=session.beginTransaction();
java.util.List customerList=session.createSQLQuery("select * from Kunde").addEntity(KundeImpl.class).list();
System.out.println("found "+customerList.size()+" customers");
if (customerList.size()==0) {
System.out.println("Creating new customer ");
String[] namen={"Fahl","Müller","Meier","Schulze"};
String[] orte ={"Willich","Münster","Frankfurt","Stuttgart"};
for (int i=0;i<namen.length;i++) {
Kunde k=new KundeImpl();
k.name(namen[i]);
k.adresse(orte[i]);
session.save(k);
}
customerList=session.createSQLQuery("select * from Kunde").addEntity(KundeImpl.class).list();
}

//Hibernate Server usually returns a collection of Remote Objects for a findXXX method
//Using a collection of remote objects directly will be very ineffecient
//so implement the find method such that it returns a collection of local objects.
//The performance will improve a lot when you use local objects

Column c = new Column();
c.setReadOnly(true);
c.setType(Types.VARCHAR);
//we want the column header of this column to be "Order No". In this header we want the
//"Order" to be in first line & "No." to be in second line. To get this we have to separate
//the Order & No. by a newline character
c.setHeaderValue("oid");
//we want the display width of the column to be 60 (this is not the maximum data length)
c.setPreferredWidth(15);
dBTable1.addColumn(c);
c = new Column();
c.setReadOnly(true);
c.setType(Types.VARCHAR);
//we want the column header of this column to be "Order No". In this header we want the
//"Order" to be in first line & "No." to be in second line. To get this we have to separate
//the Order & No. by a newline character
c.setHeaderValue("name");
//we want the display width of the column to be 60 (this is not the maximum data length)
c.setPreferredWidth(15);
dBTable1.addColumn(c);
//load the collection of objects into quicktable
dBTable1.refreshDataObject(customerList,null);
}
catch(Exception e)
{
e.printStackTrace();
}

//add the cell listener so that any changes to any changes made by teh customer in the table
//are updated in the actual data object
dBTable1.addTableCellListener(new HibernateCellListener());

//whenever customer add/delete/modify execute teh corresponding create/remove/set methods
dBTable1.addDatabaseChangeListener(new HibernateChangeListener());

//make all the columns to be editable
dBTable1.setEditable(true);

} // TestQuicktable

//Whenever a customer tries to add/modify/delete a record, we have to call the corresponding Hibernate method
class HibernateChangeListener extends DatabaseChangeListener
{
public boolean beforeDelete(int row)
{
//get the customer id corresponding to this row
String customerId =(String) dBTable1.getValueAt(row, 0);

int option = JOptionPane.showConfirmDialog( HibernateTable.this,"Are you sure to delete the Customer:" + customerId ,"",JOptionPane.OK_CANCEL_OPTION);

if( option == JOptionPane.OK_OPTION )
{
try
{
session.load(Kunde.class,customerId);
JOptionPane.showMessageDialog( HibernateTable.this,"Sucessfully deleted","",JOptionPane.OK_OPTION);
}
catch(Exception e)
{
JOptionPane.showMessageDialog( HibernateTable.this,"Unable to delete " + e.getMessage() ,"",JOptionPane.OK_OPTION);
}
}

//since we have handled the delete, we don't want quicktable to do its delete
return false;
}

public boolean beforeInsert(int row)
{
//get the customer id corresponding to this row
Object customerId = dBTable1.getValueAt(row, 0);

try
{
//to increase the performance, try to do the following create, in one method call
Kunde c = new KundeImpl();
c.name( (String)dBTable1.getValueAt(row, 1));
c.adresse((String)dBTable1.getValueAt(row, 2));
}
catch(Exception e)
{
e.printStackTrace();
}

//since we have handled the delete, we don't want quicktable to do its delete
return false;
}

public boolean beforeUpdate(int row)
{
// whenever customer changes the data in a cell,, we should not update the server immediately
//Instead we should combine all the updates for a record & then do the update in one step,
//when the customer moves to other record
Kunde c = (Kunde)dBTable1.getRowObject(row);
updateLocalCopyInServer(c);
return false;
}
}

/**
* update a local Ocpy
* @param c
*/
public void updateLocalCopyInServer(Kunde c)
{
//this should update the local copy in the server
//I leave the implementation to the developers
}

//whenever customer changes a cell data, we have to update the underlying data object
class HibernateCellListener implements DBTableCellListener
{
public Object cellValueChanged(int row, int col, Object oldValue, Object newValue)
{
try {
Kunde c = (Kunde)dBTable1.getRowObject(row);

//based on which column the customer has updated, call the corresponding object method
if( col == 2)
{
c.name((String)dBTable1.getValueAt(row, 1));
}
else if( col == 3)
{
c.adresse((String)dBTable1.getValueAt(row, 2));
}

} catch (Throwable t) {
UI.internalError(t,"cellValueChanged");
}
//always return the newvalue, so that quicktable updates this newvalue in table
return newValue;
}
}


/**
* @param args
*/
public static void main(String[] args) {
{
try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}

//create a new table frame
HibernateTable myframe = new HibernateTable();
}
} // main

} // TestQuicktable


Admin
Board Owner

Gender: Unspecified
Location:
Registered: Jul 2003
Status: Offline
Posts: 9

Click here to see the profile for Admin Send email to Admin Send private message to Admin Find more posts by Admin Edit or delete this message Reply w/Quote
Posted Tuesday, March 14, 2006 @ 18:25:32  

QuickTable already has the feature to show only the necessary columns from the DataObject.

Currently you are using the method

dBTable1.refreshDataObject(customerList,null);

instead use the below method:

if methods are getName(), getId(), then methodName = {"getName","getId"}

dBTable1.refreshDataObject(customerList,methodName);

You don't need to use the addColumn() methods.

Post New Topic   Post A Reply Jump to:
Contact Us | QuickTable - A Java DBGrid | Privacy Policy All times are GMT -5 Hours.
Welcome to QuickTable Forums, Guest!  
Login
Username :
Password :
In order to fully utilize the abilities of this board, you are required to register as a member. Registration is free, and allows you to do lots of things including turning on or off certain features of this board. Register now!
Powered by CuteCast v2.0 BETA 2
Copyright © 2001-2003 ArtsCore Studios