summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/demo/sql/EditBookForm.java
blob: 19a3df05109290983cbe5920855b487e6b583406 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/****************************************************************************
** 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.trinitydesktop.qt.*;
import java.util.HashMap;

public class EditBookForm extends TQDialog { 
    TQDataBrowser BookDataBrowser;
    TQLabel labelPrice;
    TQLabel labelTitle;
    TQLineEdit TQLineEditTitle;
    TQLineEdit TQLineEditPrice;
    TQPushButton PushButtonInsert;
    TQPushButton PushButtonUpdate;
    TQPushButton PushButtonDelete;
    TQPushButton PushButtonClose;
    TQPushButton PushButtonFirst;
    TQPushButton PushButtonPrev;
    TQPushButton PushButtonNext;
    TQPushButton PushButtonLast;
    TQLabel TextLabel1;
    TQComboBox ComboBoxAuthor;

    TQVBoxLayout EditBookFormLayout;
    TQGridLayout BookDataBrowserLayout;
    TQGridLayout Layout2;
    TQHBoxLayout Layout6;
    TQHBoxLayout Layout3;
    TQHBoxLayout Layout6_2;

	HashMap authorMap;
	TQSizePolicy policy_1;
	TQSqlForm 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( TQWidget 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 TQVBoxLayout( this );
    EditBookFormLayout.setSpacing( 6 );
    EditBookFormLayout.setMargin( 11 );

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

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

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

    Layout2.addWidget( labelPrice, 1, 0 );

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

    Layout2.addWidget( labelTitle, 0, 0 );

    TQLineEditTitle = new TQLineEdit( BookDataBrowser, "TQLineEditTitle" );

    Layout2.addWidget( TQLineEditTitle, 0, 1 );

    TQLineEditPrice = new TQLineEdit( BookDataBrowser, "TQLineEditPrice" );

    Layout2.addWidget( TQLineEditPrice, 1, 1 );

    BookDataBrowserLayout.addLayout( Layout2, 0, 0 );

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

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

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

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

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

    BookDataBrowserLayout.addLayout( Layout6, 3, 0 );

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

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

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

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

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

    BookDataBrowserLayout.addLayout( Layout3, 2, 0 );

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

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

    ComboBoxAuthor = new TQComboBox( false, BookDataBrowser, "ComboBoxAuthor" );
	policy_1 = new TQSizePolicy( (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 TQSqlForm( this, "BookDataBrowserForm" );
    BookDataBrowserForm.insert( TQLineEditTitle, "title" );
    BookDataBrowserForm.insert( TQLineEditPrice, "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(TQSqlRecord)" ), this, SLOT( "primeUpdateBook(TQSqlRecord)" ) );
    connect( BookDataBrowser, SIGNAL( "beforeUpdate(TQSqlRecord)" ), this, SLOT( "beforeUpdateBook(TQSqlRecord)" ) );
    connect( BookDataBrowser, SIGNAL( "beforeInsert(TQSqlRecord)" ), this, SLOT( "beforeUpdateBook(TQSqlRecord)" ) );
    connect( BookDataBrowser, SIGNAL( "primeInsert(TQSqlRecord)" ), this, SLOT( "primeInsertBook(TQSqlRecord)" ) );
    connect( BookDataBrowser, SIGNAL( "primeInsert(TQSqlRecord)" ), this, SLOT( "primeInsertBook(TQSqlRecord)" ) );

    // tab order
    setTabOrder( TQLineEditTitle, TQLineEditPrice );
    setTabOrder( TQLineEditPrice, 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 ) {
            TQSqlCursor cursor = new TQSqlCursor( "book" );
            BookDataBrowser.setSqlCursor( cursor, true );
            BookDataBrowser.refresh();
            BookDataBrowser.first();
        }
    }
    super.polish();
}

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

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

void primeInsertBook(TQSqlRecord buffer)
{
    TQSqlQuery q = new TQSqlQuery();
    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(TQSqlRecord buffer)
{
    // Who is this book's author?
    TQSqlQuery query = new TQSqlQuery( "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();
    TQSqlQuery query = new TQSqlQuery( "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()
{
}

}