/****************************************************************************
** Form implementation generated from reading ui file 'editbook.ui'
**
** Created: Wed Aug 8 03:37:45 2001
**      by:  The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
import org.kde.qt.*;
import java.util.HashMap;

public class EditBookForm extends QDialog { 
    QDataBrowser BookDataBrowser;
    QLabel labelPrice;
    QLabel labelTitle;
    QLineEdit QLineEditTitle;
    QLineEdit QLineEditPrice;
    QPushButton PushButtonInsert;
    QPushButton PushButtonUpdate;
    QPushButton PushButtonDelete;
    QPushButton PushButtonClose;
    QPushButton PushButtonFirst;
    QPushButton PushButtonPrev;
    QPushButton PushButtonNext;
    QPushButton PushButtonLast;
    QLabel TextLabel1;
    QComboBox ComboBoxAuthor;

    QVBoxLayout EditBookFormLayout;
    QGridLayout BookDataBrowserLayout;
    QGridLayout Layout2;
    QHBoxLayout Layout6;
    QHBoxLayout Layout3;
    QHBoxLayout Layout6_2;

	HashMap authorMap;
	QSizePolicy policy_1;
	QSqlForm BookDataBrowserForm;

/* 
 *  Constructs a EditBookForm which is a child of 'parent', with the 
 *  name 'name' and widget flags set to 'f' 
 *
 *  The dialog will by default be modeless, unless you set 'modal' to
 *  true to construct a modal dialog.
 */
EditBookForm( QWidget parent,  String name, boolean modal, int fl )
{
    super( parent, name, modal, fl );
    if ( name == null )
	setName( "EditBookForm" );
    resize( 524, 371 );
    setCaption( trUtf8( "Edit Books" ) );
    EditBookFormLayout = new QVBoxLayout( this );
    EditBookFormLayout.setSpacing( 6 );
    EditBookFormLayout.setMargin( 11 );

    BookDataBrowser = new QDataBrowser( this, "BookDataBrowser" );
    BookDataBrowserLayout = new QGridLayout( BookDataBrowser );
    BookDataBrowser.setSort( new String[] { "title ASC" } );
    BookDataBrowserLayout.setSpacing( 6 );
    BookDataBrowserLayout.setMargin( 11 );

    Layout2 = new QGridLayout();
    Layout2.setSpacing( 6 );
    Layout2.setMargin( 0 );

    labelPrice = new QLabel( BookDataBrowser, "labelPrice" );
    labelPrice.setText( trUtf8( "Price" ) );

    Layout2.addWidget( labelPrice, 1, 0 );

    labelTitle = new QLabel( BookDataBrowser, "labelTitle" );
    labelTitle.setText( trUtf8( "Title" ) );

    Layout2.addWidget( labelTitle, 0, 0 );

    QLineEditTitle = new QLineEdit( BookDataBrowser, "QLineEditTitle" );

    Layout2.addWidget( QLineEditTitle, 0, 1 );

    QLineEditPrice = new QLineEdit( BookDataBrowser, "QLineEditPrice" );

    Layout2.addWidget( QLineEditPrice, 1, 1 );

    BookDataBrowserLayout.addLayout( Layout2, 0, 0 );

    Layout6 = new QHBoxLayout(); 
    Layout6.setSpacing( 6 );
    Layout6.setMargin( 0 );

    PushButtonInsert = new QPushButton( BookDataBrowser, "PushButtonInsert" );
    PushButtonInsert.setText( trUtf8( "&Insert" ) );
    Layout6.addWidget( PushButtonInsert );

    PushButtonUpdate = new QPushButton( BookDataBrowser, "PushButtonUpdate" );
    PushButtonUpdate.setText( trUtf8( "&Update" ) );
    PushButtonUpdate.setDefault( true );
    Layout6.addWidget( PushButtonUpdate );

    PushButtonDelete = new QPushButton( BookDataBrowser, "PushButtonDelete" );
    PushButtonDelete.setText( trUtf8( "&Delete" ) );
    Layout6.addWidget( PushButtonDelete );

    PushButtonClose = new QPushButton( BookDataBrowser, "PushButtonClose" );
    PushButtonClose.setText( trUtf8( "&Close" ) );
    Layout6.addWidget( PushButtonClose );

    BookDataBrowserLayout.addLayout( Layout6, 3, 0 );

    Layout3 = new QHBoxLayout();
    Layout3.setSpacing( 6 );
    Layout3.setMargin( 0 );

    PushButtonFirst = new QPushButton( BookDataBrowser, "PushButtonFirst" );
    PushButtonFirst.setText( trUtf8( "|< &First" ) );
    Layout3.addWidget( PushButtonFirst );

    PushButtonPrev = new QPushButton( BookDataBrowser, "PushButtonPrev" );
    PushButtonPrev.setText( trUtf8( "<< &Prev" ) );
    Layout3.addWidget( PushButtonPrev );

    PushButtonNext = new QPushButton( BookDataBrowser, "PushButtonNext" );
    PushButtonNext.setText( trUtf8( "&Next >>" ) );
    Layout3.addWidget( PushButtonNext );

    PushButtonLast = new QPushButton( BookDataBrowser, "PushButtonLast" );
    PushButtonLast.setText( trUtf8( "&Last >|" ) );
    Layout3.addWidget( PushButtonLast );

    BookDataBrowserLayout.addLayout( Layout3, 2, 0 );

    Layout6_2 = new QHBoxLayout();
    Layout6_2.setSpacing( 6 );
    Layout6_2.setMargin( 0 );

    TextLabel1 = new QLabel( BookDataBrowser, "TextLabel1" );
    TextLabel1.setText( trUtf8( "Author" ) );
    Layout6_2.addWidget( TextLabel1 );

    ComboBoxAuthor = new QComboBox( false, BookDataBrowser, "ComboBoxAuthor" );
	policy_1 = new QSizePolicy( (int)7, (int)0, ComboBoxAuthor.sizePolicy().hasHeightForWidth() );
    ComboBoxAuthor.setSizePolicy( policy_1  );
    Layout6_2.addWidget( ComboBoxAuthor );

    BookDataBrowserLayout.addLayout( Layout6_2, 1, 0 );
    EditBookFormLayout.addWidget( BookDataBrowser );

    // database support
    BookDataBrowserForm =  new QSqlForm( this, "BookDataBrowserForm" );
    BookDataBrowserForm.insert( QLineEditTitle, "title" );
    BookDataBrowserForm.insert( QLineEditPrice, "price" );
    BookDataBrowser.setForm( BookDataBrowserForm );





    // signals and slots connections
    connect( PushButtonFirst, SIGNAL( "clicked()" ), BookDataBrowser, SLOT( "first()" ) );
    connect( BookDataBrowser, SIGNAL( "firstRecordAvailable( boolean )" ), PushButtonFirst, SLOT( "setEnabled(boolean)" ) );
    connect( PushButtonPrev, SIGNAL( "clicked()" ), BookDataBrowser, SLOT( "prev()" ) );
    connect( BookDataBrowser, SIGNAL( "prevRecordAvailable( boolean )" ), PushButtonPrev, SLOT( "setEnabled(boolean)" ) );
    connect( PushButtonNext, SIGNAL( "clicked()" ), BookDataBrowser, SLOT( "next()" ) );
    connect( BookDataBrowser, SIGNAL( "nextRecordAvailable( boolean )" ), PushButtonNext, SLOT( "setEnabled(boolean)" ) );
    connect( PushButtonLast, SIGNAL( "clicked()" ), BookDataBrowser, SLOT( "last()" ) );
    connect( BookDataBrowser, SIGNAL( "lastRecordAvailable( boolean )" ), PushButtonLast, SLOT( "setEnabled(boolean)" ) );
    connect( PushButtonInsert, SIGNAL( "clicked()" ), BookDataBrowser, SLOT( "insert()" ) );
    connect( PushButtonUpdate, SIGNAL( "clicked()" ), BookDataBrowser, SLOT( "update()" ) );
    connect( PushButtonDelete, SIGNAL( "clicked()" ), BookDataBrowser, SLOT( "del()" ) );
    connect( PushButtonClose, SIGNAL( "clicked()" ), this, SLOT( "accept()" ) );
    connect( BookDataBrowser, SIGNAL( "primeUpdate(QSqlRecord)" ), this, SLOT( "primeUpdateBook(QSqlRecord)" ) );
    connect( BookDataBrowser, SIGNAL( "beforeUpdate(QSqlRecord)" ), this, SLOT( "beforeUpdateBook(QSqlRecord)" ) );
    connect( BookDataBrowser, SIGNAL( "beforeInsert(QSqlRecord)" ), this, SLOT( "beforeUpdateBook(QSqlRecord)" ) );
    connect( BookDataBrowser, SIGNAL( "primeInsert(QSqlRecord)" ), this, SLOT( "primeInsertBook(QSqlRecord)" ) );
    connect( BookDataBrowser, SIGNAL( "primeInsert(QSqlRecord)" ), this, SLOT( "primeInsertBook(QSqlRecord)" ) );

    // tab order
    setTabOrder( QLineEditTitle, QLineEditPrice );
    setTabOrder( QLineEditPrice, ComboBoxAuthor );
    setTabOrder( ComboBoxAuthor, PushButtonFirst );
    setTabOrder( PushButtonFirst, PushButtonPrev );
    setTabOrder( PushButtonPrev, PushButtonNext );
    setTabOrder( PushButtonNext, PushButtonLast );
    setTabOrder( PushButtonLast, PushButtonInsert );
    setTabOrder( PushButtonInsert, PushButtonUpdate );
    setTabOrder( PushButtonUpdate, PushButtonDelete );
    setTabOrder( PushButtonDelete, PushButtonClose );
    init();
}

/*
 *  Widget polish.  Reimplemented to handle
 *  default data browser initialization
 */
public void polish()
{
    if ( BookDataBrowser != null ) {
        if ( BookDataBrowser.sqlCursor() == null ) {
            QSqlCursor cursor = new QSqlCursor( "book" );
            BookDataBrowser.setSqlCursor( cursor, true );
            BookDataBrowser.refresh();
            BookDataBrowser.first();
        }
    }
    super.polish();
}

void beforeUpdateBook(QSqlRecord buffer)
{
    int[] id = { 0 };
    mapAuthor( ComboBoxAuthor.currentText(), id, false );
    buffer.setValue( "authorid", new QVariant(id[0]) );
}

void mapAuthor(String name, int[] id, boolean populate)
{
//    if ( populate )
//	authorMap[ name ] = id;
//    else
//	id[0] = authorMap[ name ];
}

void primeInsertBook(QSqlRecord buffer)
{
    QSqlQuery q = new QSqlQuery();
    q.exec( "UPDATE sequence SET sequence = sequence + 1 WHERE tablename='book';" );
    q.exec( "SELECT sequence FROM sequence WHERE tablename='book';" );
    if ( q.next() ) {
	buffer.setValue( "id", q.value( 0 ) );
    }
}

void primeUpdateBook(QSqlRecord buffer)
{
    // Who is this book's author?
    QSqlQuery query = new QSqlQuery( "SELECT surname FROM author WHERE id=" +
	buffer.value( "authorid" ).toString() + ";" );
    String author = "";
    if ( query.next() )
	author = query.value( 0 ).toString();
    // Set the ComboBox to the right author
    for ( int i = 0; i < ComboBoxAuthor.count(); i++ ) {
	if ( ComboBoxAuthor.text( i ) == author ) {
	    ComboBoxAuthor.setCurrentItem( i ) ;
	    break;
	}
    }
}

void init()
{
	authorMap = new HashMap();
    QSqlQuery query = new QSqlQuery( "SELECT surname, id FROM author ORDER BY surname;" );
    while ( query.next() ) {
	ComboBoxAuthor.insertItem( query.value( 0 ).toString() );
	int[] id = new int[1];
	id[0] = query.value( 1 ).toInt();
	mapAuthor( query.value( 0 ).toString(), id, true );
    }
}

public void destroy()
{
}

}
