KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nqadmin > swingSet > SSDataNavigator


1 /* $Id: SSDataNavigator.java,v 1.35 2005/03/08 16:13:50 prasanth Exp $
2  *
3  * Tab Spacing = 4
4  *
5  * Copyright (c) 2003-2005, The Pangburn Company and Prasanth R. Pasala.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer. Redistributions in binary
13  * form must reproduce the above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or other materials
15  * provided with the distribution. The names of its contributors may not be
16  * used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package com.nqadmin.swingSet;
34
35 import java.awt.*;
36 import java.awt.event.*;
37 import javax.swing.*;
38 import java.sql.*;
39 import java.io.*;
40 import com.nqadmin.swingSet.datasources.SSRowSet;
41 import javax.sql.RowSetListener JavaDoc;
42 import javax.sql.RowSetEvent JavaDoc;
43
44 /**
45  * SSDataNavigator.java
46  *<p>
47  * SwingSet - Open Toolkit For Making Swing Controls Database-Aware
48  *<p><pre>
49  * Component that can be used for data navigation. It provides buttons for
50  * navigation, insertion, and deletion of records in a SSRowSet. The modification
51  * of a SSRowSet can be prevented using the setModificaton() method. Any changes
52  * made to the columns of a record will be updated whenever there is a
53  * navigation.
54  *
55  * For example if you are displaying three columns using the JTextField and the
56  * user changes the text in the text fields then the columns will be updated to
57  * the new values when the user navigates the SSRowSet. If the user wants to revert
58  * the changes he made he can press the Undo button, however this must be done
59  * before any navigation. Once navigation takes place changes can't be reverted
60  * using Undo button (has to be done manually by the user).
61  *</pre><p>
62  * @author $Author: prasanth $
63  * @version $Revision: 1.35 $
64  */

65 public class SSDataNavigator extends JPanel {
66
67     /**
68      * Button to navigate to the first record in the SSRowSet.
69      */

70     protected JButton firstButton = new JButton();
71
72     /**
73      * Button to navigate to the previous record in the SSRowSet.
74      */

75     protected JButton previousButton = new JButton();
76
77     /**
78      * Text field for viewing/changing the current record number.
79      */

80     protected JTextField txtCurrentRow = new JTextField();
81
82     /**
83      * Button to navigate to the next record in the SSRowSet.
84      */

85     protected JButton nextButton = new JButton();
86
87     /**
88      * Button to navigate to the last record in the SSRowSet.
89      */

90     protected JButton lastButton = new JButton();
91
92     /**
93      * Button to commit screen changes to the SSRowSet.
94      */

95     protected JButton commitButton = new JButton(); // Commit button
96

97     /**
98      * Button to revert screen changes based on the SSRowSet.
99      */

100     protected JButton undoButton = new JButton();
101
102     /**
103      * Button to refresh the screen based on any changes to the SSRowSet.
104      */

105     protected JButton refreshButton = new JButton(); // REFRESH BUTTON
106

107     /**
108      * Button to add a record to the SSRowSet.
109      */

110     protected JButton addButton = new JButton();
111
112     /**
113      * Button to delete the current record in the SSRowSet.
114      */

115     protected JButton deleteButton = new JButton();
116
117     /**
118      * Label to display the total number of records in the SSRowSet.
119      */

120     protected JLabel lblRowCount = new JLabel();
121
122     /**
123      * Indicator to allow/disallow changes to the SSRowSet.
124      */

125     protected boolean modification = true;
126
127     /**
128      * Indicator to allow/disallow deletions from the SSRowSet.
129      */

130     protected boolean deletion = true;
131
132     /**
133      * Indicator to allow/disallow insertions to the SSRowSet.
134      */

135     protected boolean insertion = true;
136
137     /**
138      * Indicator to force confirmation of SSRowSet deletions.
139      */

140     protected boolean confirmDeletes = true;
141
142     /**
143      * Indicator to cause the navigator to skip the execute() function call on
144      * the specified SSRowSet. Must be false for MySQL (see FAQ).
145      */

146     protected boolean callExecute = true;
147
148     /**
149      * SSRowSet from which component will get/set values.
150      */

151     protected SSRowSet sSRowSet = null;
152
153     /**
154      * Container (frame or internal frame) which contains the navigator.
155      */

156     protected SSDBNav dBNav = null;
157
158     /**
159      * Number of rows in SSRowSet. Set to zero if next() method returns false.
160      */

161     protected int rowCount = 0;
162
163     /**
164      * Row number for current record in SSRowSet.
165      */

166     protected int currentRow = 0;
167
168     /**
169      * Indicator used to determine if a row is being inserted into the SSRowSet.
170      */

171     protected boolean onInsertRow = false;
172
173     /**
174      * Navigator button dimensions.
175      */

176     protected Dimension buttonSize = new Dimension(40, 20);
177
178     /**
179      * Current record text field dimensions.
180      */

181     protected Dimension txtFieldSize = new Dimension(65, 20);
182
183     /**
184      * Listener on the SSRowSet used by data navigator.
185      */

186     private final SSDBNavRowSetListener sSRowSetListener = new SSDBNavRowSetListener();
187
188     /**
189      * Creates a object of SSDataNavigator.
190      * Note: you have to set the SSRowSet before you can start using it.
191      */

192     public SSDataNavigator() {
193         addToolTips();
194         createPanel();
195         addListeners();
196     }
197
198     /**
199      * Constructs a SSDataNavigator for the given SSRowSet
200      *
201      * @param _sSRowSet The SSRowSet to which the SSDataNavigator has to be bound
202      */

203     public SSDataNavigator(SSRowSet _sSRowSet) {
204         setSSRowSet(_sSRowSet);
205         addToolTips();
206         createPanel();
207         addListeners();
208     }
209
210     /**
211      * Constructs the SSDataNavigator with the given SSRowSet and sets the size of the buttons
212      * on the navigator to the given size
213      *
214      * @param _sSRowSet the SSRowSet to which the navigator is bound to
215      * @param _buttonSize the size to which the button on navigator have to be set
216      */

217     public SSDataNavigator(SSRowSet _sSRowSet, Dimension _buttonSize) {
218         buttonSize = _buttonSize;
219         setSSRowSet(_sSRowSet);
220         addToolTips();
221         createPanel();
222         addListeners();
223     }
224
225     /**
226      * Returns true if the SSRowSet contains one or more rows, else false.
227      *
228      * @return return true if SSRowSet contains data else false.
229      */

230      public boolean containsRows() {
231
232          if ( rowCount == 0 ) {
233             return false;
234          }
235
236          return true;
237      }
238
239     /**
240      * Method to cause the navigator to skip the execute() function call on the
241      * underlying SSRowSet. This is necessary for MySQL (see FAQ).
242      *
243      * @param _callExecute false if using MySQL database - otherwise true
244      */

245     public void setCallExecute(boolean _callExecute) {
246         boolean oldValue = callExecute;
247         callExecute = _callExecute;
248         firePropertyChange("callExecute", oldValue, callExecute);
249     }
250
251     /**
252      * Indicates if the navigator will skip the execute function call on the
253      * underlying SSRowSet (needed for MySQL - see FAQ).
254      *
255      * @return value of execute() indicator
256      */

257     public boolean getCallExecute() {
258         return callExecute;
259     }
260
261     /**
262      * Sets the preferredSize and the MinimumSize of the buttons to the specified size
263      *
264      * @param _buttonSize the required dimension of the buttons
265      */

266     public void setButtonSize(Dimension _buttonSize) {
267         Dimension oldValue = buttonSize;
268         buttonSize = _buttonSize;
269         firePropertyChange("buttonSize", oldValue, buttonSize);
270         setButtonSizes();
271     }
272
273     /**
274      * Returns the size of buttons on the data navigator.
275      *
276      * @return returns a Dimension object representing the size of each button
277      * on the data navigator.
278      */

279     public Dimension getButtonSize() {
280         return buttonSize;
281     }
282
283     /**
284      * Function that passes the implementation of the SSDBNav interface. This
285      * interface can be implemented by the developer to perform custom actions
286      * when the insert button is pressed
287      *
288      * @param _dBNav implementation of the SSDBNav interface
289      */

290     public void setDBNav(SSDBNav _dBNav) {
291         SSDBNav oldValue = dBNav;
292         dBNav = _dBNav;
293         firePropertyChange("dBNav", oldValue, dBNav);
294     }
295
296     /**
297      * Returns any custom implementation of the SSDBNav interface, which is used
298      * when the insert button is pressed to perform custom actions.
299      *
300      * @return any custom implementation of the SSDBNav interface
301      */

302     public SSDBNav getDBNav() {
303         return dBNav;
304     }
305
306     /**
307      * Enables or disables the modification-related buttons on the SSDataNavigator.
308      * If the user can only navigate through the records with out making any changes
309      * set this to false. By default, the modification-related buttons are enabled.
310      *
311      * @param _modification indicates whether or not the modification-related
312      * buttons are enabled.
313      */

314     public void setModification(boolean _modification) {
315         boolean oldValue = modification;
316         modification = _modification;
317         firePropertyChange("modification", oldValue, modification);
318
319         if (!modification) {
320             commitButton.setEnabled(false);
321             undoButton.setEnabled(false);
322             addButton.setEnabled(false);
323             deleteButton.setEnabled(false);
324         } else {
325             commitButton.setEnabled(true);
326             undoButton.setEnabled(true);
327             addButton.setEnabled(true);
328             deleteButton.setEnabled(true);
329         }
330     }
331
332     /**
333      * Returns true if the user can modify the data in the SSRowSet, else false.
334      *
335      * @return returns true if the user modifications are written back to the
336      * database, else false.
337      */

338     public boolean getModification() {
339         return modification;
340     }
341
342     /**
343      * Enables or disables the row deletion button. This method should be used
344      * if row deletions are not allowed. True by default.
345      *
346      * @param _deletion indicates whether or not to allow deletions
347      */

348     public void setDeletion(boolean _deletion) {
349         boolean oldValue = deletion;
350         deletion = _deletion;
351         firePropertyChange("deletion", oldValue, deletion);
352
353         if (!deletion) {
354             deleteButton.setEnabled(false);
355         } else {
356             deleteButton.setEnabled(true);
357         }
358     }
359
360     /**
361      * Returns true if deletions are allowed, else false.
362      *
363      * @return returns true if deletions are allowed, else false.
364      */

365     public boolean getDeletion(){
366         return deletion;
367     }
368
369     /**
370      * Enables or disables the row insertion button. This method should be used
371      * if row insertions are not allowed. True by default.
372      *
373      * @param _insertion indicates whether or not to allow insertions
374      */

375     public void setInsertion(boolean _insertion) {
376         boolean oldValue = insertion;
377         insertion = _insertion;
378         firePropertyChange("insertion", oldValue, insertion);
379
380         if (!insertion) {
381             addButton.setEnabled(false);
382         } else{
383             addButton.setEnabled(true);
384         }
385     }
386
387     /**
388      * Returns true if insertions are allowed, else false.
389      *
390      * @return returns true if insertions are allowed, else false.
391      */

392     public boolean getInsertion() {
393         return insertion;
394     }
395
396     /**
397      * Sets the confirm deletion indicator. If set to true, every time delete
398      * button is pressed, the navigator pops up a confirmation dialog to the
399      * user. Default value is true.
400      *
401      * @param _confirmDeletes indicates whether or not to confirm deletions
402      */

403     public void setConfirmDeletes(boolean _confirmDeletes) {
404         boolean oldValue = confirmDeletes;
405         confirmDeletes = _confirmDeletes;
406         firePropertyChange("confirmDeletes", oldValue, confirmDeletes);
407     }
408
409     /**
410      * Returns true if deletions must be confirmed by user, else false.
411      *
412      * @return returns true if a confirmation dialog is displayed when the user
413      * deletes a record, else false.
414      */

415     public boolean getConfirmDeletes() {
416         return confirmDeletes;
417     }
418
419     /**
420      * This method changes the SSRowSet to which the navigator is bound.
421      * The execute() and next() methods MUST be called on the SSRowSet
422      * before you set the SSRowSet for the SSDataNavigator.
423      *
424      * @param _sSRowSet a SSRowSet object to which the navigator will be bound
425      */

426     public void setSSRowSet(SSRowSet _sSRowSet) {
427         if(sSRowSet != null){
428             sSRowSet.removeRowSetListener(sSRowSetListener);
429         }
430
431         SSRowSet oldValue = sSRowSet;
432         sSRowSet = _sSRowSet;
433         firePropertyChange("sSRowSet", oldValue, sSRowSet);
434
435         // SEE IF THERE ARE ANY ROWS IN THE GIVEN SSROWSET
436
try {
437             if (callExecute) {
438                 sSRowSet.execute();
439             }
440
441             if (!sSRowSet.next()) {
442                 rowCount = 0;
443                 currentRow = 0;
444             } else {
445             // IF THERE ARE ROWS GET THE ROW COUNT
446
sSRowSet.last();
447                 rowCount = sSRowSet.getRow();
448                 sSRowSet.first();
449                 currentRow = sSRowSet.getRow();
450             }
451         // SET THE ROW COUNT AS LABEL
452
lblRowCount.setText("of " + rowCount);
453             txtCurrentRow.setText(String.valueOf(currentRow));
454
455             sSRowSet.addRowSetListener(sSRowSetListener);
456         } catch(SQLException se) {
457             se.printStackTrace();
458         }
459
460         //IF NO ROWS ARE PRESENT DISABLE NAVIGATION
461
//ELSE ENABLE THEN ELSE IS USEFUL WHEN THE SSROWSET IS CHNAGED
462
// IF THE INITIAL SSROWSET HAS ZERO ROWS NEXT IF THE USER SETS A NEW SSROWSET THEN THE
463
// BUTTONS HAVE TO BE ENABLED
464
if (rowCount == 0) {
465             firstButton.setEnabled(false);
466             previousButton.setEnabled(false);
467             nextButton.setEnabled(false);
468             lastButton.setEnabled(false);
469         } else{
470             firstButton.setEnabled(true);
471             previousButton.setEnabled(true);
472             nextButton.setEnabled(true);
473             lastButton.setEnabled(true);
474         }
475
476         try {
477             if (sSRowSet.isLast()) {
478                 nextButton.setEnabled(false);
479                 lastButton.setEnabled(false);
480             }
481             if (sSRowSet.isFirst()) {
482                 firstButton.setEnabled(false);
483                 previousButton.setEnabled(false);
484             }
485
486         } catch(SQLException se) {
487             se.printStackTrace();
488         }
489
490     // ENABLE OTHER BUTTONS IF NEED BE.
491

492         // THIS IS NEEDED TO HANDLE USER LEAVING THE SCREEN IN AN INCONSISTENT
493
// STATE EXAMPLE: USER CLICKS ADD BUTTON, THIS DISABLES ALL THE BUTTONS
494
// EXCEPT COMMIT & UNDO. WITH OUT COMMITING OR UNDOING THE ADD USER
495
// CLOSES THE SCREEN. NOW IF THE SCREEN IS OPENED WITH A NEW SSROWSET.
496
// THE REFRESH, ADD & DELETE WILL BE DISABLED.
497
refreshButton.setEnabled(true);
498         if (insertion) {
499             addButton.setEnabled(true);
500         }
501         if (deletion) {
502             deleteButton.setEnabled(true);
503         }
504     }
505
506     /**
507      * Returns the SSRowSet being used.
508      *
509      * @return returns the SSRowSet being used.
510      */

511     public SSRowSet getSSRowSet() {
512         return sSRowSet;
513     }
514
515     /**
516      * Writes the present row back to the SSRowSet. This is done automatically
517      * when any navigation takes place, but can also be called manually.
518      *
519      * @return returns true if update succeeds else false.
520      */

521     public boolean updatePresentRow() {
522         try {
523             if (!onInsertRow) {
524                 sSRowSet.updateRow();
525             }
526             return true;
527         } catch(Exception JavaDoc e) {
528             return false;
529         }
530     }
531
532     /**
533      * Calls the doClick on First Button.
534      */

535     public void doFirstButtonClick(){
536         firstButton.doClick();
537     }
538
539     /**
540      * Calls the doClick on Previous Button.
541      */

542     public void doPreviousButtonClick(){
543         previousButton.doClick();
544     }
545
546     /**
547      * Calls the doClick on Next Button.
548      */

549     public void doNextButtonClick(){
550         nextButton.doClick();
551     }
552
553     /**
554      * Calls the doClick on Last Button.
555      */

556     public void doLastButtonClick(){
557         lastButton.doClick();
558     }
559
560     /**
561      * Calls the doClick on Refresh Button.
562      */

563     public void doRefreshButtonClick(){
564         refreshButton.doClick();
565     }
566
567     /**
568      * Calls the doClick on Commit Button.
569      */

570     public void doCommitButtonClick(){
571         commitButton.doClick();
572     }
573
574     /**
575      * Calls the doClick on Undo Button.
576      */

577     public void doUndoButtonClick(){
578         undoButton.doClick();
579     }
580
581     /**
582      * Calls the doClick on Add Button.
583      */

584     public void doAddButtonClick(){
585         addButton.doClick();
586     }
587
588     /**
589      * Calls the doClick on Delete Button.
590      */

591     public void doDeleteButtonClick(){
592         deleteButton.doClick();
593     }
594
595     /**
596      * Method to add tooltips and button graphics (or text) to navigator components.
597      */

598     protected void addToolTips() {
599
600         try {
601             ClassLoader JavaDoc cl = this.getClass().getClassLoader();
602             firstButton.setIcon(new ImageIcon(cl.getResource("images/first.gif")));
603             previousButton.setIcon(new ImageIcon(cl.getResource("images/prev.gif")));
604             nextButton.setIcon(new ImageIcon(cl.getResource("images/next.gif")));
605             lastButton.setIcon(new ImageIcon(cl.getResource("images/last.gif")));
606             commitButton.setIcon(new ImageIcon(cl.getResource("images/commit.gif")));
607             undoButton.setIcon(new ImageIcon(cl.getResource("images/undo.gif")));
608             refreshButton.setIcon(new ImageIcon(cl.getResource("images/refresh.gif")));
609             addButton.setIcon(new ImageIcon(cl.getResource("images/add.gif")));
610             deleteButton.setIcon(new ImageIcon(cl.getResource("images/delete.gif")));
611         } catch(Exception JavaDoc e) {
612             firstButton.setText("<<");
613             previousButton.setText("<");
614             nextButton.setText(">");
615             lastButton.setText(">>");
616             commitButton.setText("Commit");
617             undoButton.setText("Undo");
618             refreshButton.setText("Refresh");
619             addButton.setText("Add");
620             deleteButton.setText("Delete");
621             System.out.println("Unable to load images for navigator buttons");
622         }
623
624         firstButton.setToolTipText("First");
625         previousButton.setToolTipText("Previous");
626         nextButton.setToolTipText("Next");
627         lastButton.setToolTipText("Last");
628         commitButton.setToolTipText("Commit");
629         undoButton.setToolTipText("Undo");
630         refreshButton.setToolTipText("Refresh");
631         addButton.setToolTipText("Add Record");
632         deleteButton.setToolTipText("Delete Record");
633
634     } // end protected void addToolTips() {
635

636     /**
637      * Sets the dimensions for the navigator components.
638      */

639     protected void setButtonSizes() {
640
641         // SET THE PREFERRED SIZES
642
firstButton.setPreferredSize(buttonSize);
643             previousButton.setPreferredSize(buttonSize);
644             nextButton.setPreferredSize(buttonSize);
645             lastButton.setPreferredSize(buttonSize);
646             commitButton.setPreferredSize(buttonSize);
647             undoButton.setPreferredSize(buttonSize);
648             refreshButton.setPreferredSize(buttonSize);
649             addButton.setPreferredSize(buttonSize);
650             deleteButton.setPreferredSize(buttonSize);
651             txtCurrentRow.setPreferredSize(txtFieldSize);
652             lblRowCount.setPreferredSize(txtFieldSize);
653             lblRowCount.setHorizontalAlignment(SwingConstants.CENTER);
654
655         // SET MINIMUM BUTTON SIZES
656
firstButton.setMinimumSize(buttonSize);
657             previousButton.setMinimumSize(buttonSize);
658             nextButton.setMinimumSize(buttonSize);
659             lastButton.setMinimumSize(buttonSize);
660             commitButton.setMinimumSize(buttonSize);
661             undoButton.setMinimumSize(buttonSize);
662             refreshButton.setMinimumSize(buttonSize);
663             addButton.setMinimumSize(buttonSize);
664             deleteButton.setMinimumSize(buttonSize);
665             txtCurrentRow.setMinimumSize(txtFieldSize);
666             lblRowCount.setMinimumSize(txtFieldSize);
667     }
668
669     /**
670      * Adds the navigator components to the navigator panel.
671      */

672     protected void createPanel() {
673
674         setButtonSizes();
675         //SET THE BOX LAYOUT
676
setLayout(new BoxLayout(this,BoxLayout.LINE_AXIS) );
677
678         // ADD BUTTONS TO THE PANEL
679
add(firstButton);
680         add(previousButton);
681         add(txtCurrentRow);
682         add(nextButton);
683         add(lastButton);
684         add(commitButton);
685         add(undoButton);
686         add(refreshButton);
687         add(addButton);
688         add(deleteButton);
689         add(lblRowCount);
690         //pack();
691

692     }
693
694     /**
695      * Adds the listeners for the navigator components.
696      */

697     private void addListeners() {
698
699         // WHEN THIS BUTTON IS PRESSED THE RECORD ON WHICH USER WAS WORKING IS SAVED
700
// AND MOVES THE SSROWSET TO THE FIRST ROW
701
// SINCE ROW SET IS IN FIRST ROW DISABLE PREVIOUS BUTTON AND ENABLE NEXT BUTTON
702
firstButton.addActionListener(new ActionListener(){
703                 public void actionPerformed(ActionEvent ae) {
704                     try {
705                         if ( modification ) {
706                             sSRowSet.updateRow();
707                         }
708                         sSRowSet.first();
709
710                         firstButton.setEnabled(false);
711                         previousButton.setEnabled(false);
712                         if (!sSRowSet.isLast()) {
713                             nextButton.setEnabled(true);
714                             lastButton.setEnabled(true);
715                         } else {
716                             nextButton.setEnabled(false);
717                             lastButton.setEnabled(false);
718                         }
719                         if ( dBNav != null ) {
720                             dBNav.performNavigationOps(SSDBNav.NAVIGATION_FIRST);
721                         }
722                     // GET THE ROW NUMBER AND SET IT TO ROW NUMBER TEXT FIELD
723
currentRow = 1;
724                         txtCurrentRow.setText(String.valueOf(currentRow));
725                     } catch(SQLException se) {
726                         se.printStackTrace();
727                         JOptionPane.showMessageDialog(SSDataNavigator.this,"Exception occured while updating row or moving the cursor.\n"+se.getMessage());
728                     }
729                 }
730             });
731
732         // WHEN BUTTON 2 IS PRESSED THE CURRENT RECORD IS SAVED AND SSROWSET IS
733
// MOVED TO PREVIOUS RECORD
734
// CALLING PREVIOUS ON ENPTY SSROWSET IS ILLEGAL SO A CHECK IS MADE FOR THAT
735
// IF NUMBER OF ROWS == 0 THEN SSROWSET IS EMPTY
736
previousButton.addActionListener(new ActionListener() {
737                 public void actionPerformed(ActionEvent ae) {
738                     try {
739                         //if( sSRowSet.rowUpdated() )
740
if ( modification ) {
741                             sSRowSet.updateRow();
742                         }
743                         if ( sSRowSet.getRow() != 0 && !sSRowSet.previous() ) {
744                             sSRowSet.first();
745                         }
746                         // IF IN THE FIRST RECORD DISABLE PREVIOUS BUTTON
747
if (sSRowSet.isFirst() || sSRowSet.getRow() == 0){
748                             firstButton.setEnabled(false);
749                             previousButton.setEnabled(false);
750                         }
751
752                         // IF NEXT BUTTON IS DISABLED ENABLE IT.
753
if ( !sSRowSet.isLast() ) {
754                             nextButton.setEnabled(true);
755                             lastButton.setEnabled(true);
756                         }
757
758                         if ( dBNav != null ) {
759                             dBNav.performNavigationOps(SSDBNav.NAVIGATION_PREVIOUS);
760                         }
761                     // GET THE ROW NUMBER AND SET IT TO ROW NUMBER TEXT FIELD
762
currentRow = sSRowSet.getRow();
763                         txtCurrentRow.setText(String.valueOf(currentRow));
764                     } catch(SQLException se) {
765                         se.printStackTrace();
766                         JOptionPane.showMessageDialog(SSDataNavigator.this,"Exception occured while updating row or moving the cursor.\n"+se.getMessage());
767                     }
768                 }
769             });
770
771         // WHEN BUTTON 3 PRESSED THE CURRENT RECORD IS SAVED AND THE SSROWSET IS
772
// MOVED TO NEXT RECORD. IF THIS IS THE LAST RECORD THEN BUTTON 3 IS DISABLED
773
// ALSO IF THE PREVIOUS BUTTON IS NOT ENABLED THEN IT IS ENABLED
774
nextButton.addActionListener(new ActionListener() {
775                 public void actionPerformed(ActionEvent ae) {
776                     try {
777                         //if( sSRowSet.rowUpdated() )
778
if ( modification ) {
779                             sSRowSet.updateRow();
780                         }
781                         if ( !sSRowSet.next() ) {
782                             nextButton.setEnabled(false);
783                             lastButton.setEnabled(false);
784                             sSRowSet.last();
785                         }
786                         // IF LAST RECORD THEN DISABLE NEXT BUTTON
787
if ( sSRowSet.isLast() ) {
788                             nextButton.setEnabled(false);
789                             lastButton.setEnabled(false);
790                         }
791
792                         // IF THIS IS NOT FIRST ROW ENABLE FIRST AND PREVIOUS BUTTONS
793
if ( !sSRowSet.isFirst() ) {
794                             previousButton.setEnabled(true);
795                             firstButton.setEnabled(true);
796                         }
797
798                         if ( dBNav != null ) {
799                             dBNav.performNavigationOps(SSDBNav.NAVIGATION_NEXT);
800                         }
801                     // GET THE ROW NUMBER AND SET IT TO ROW NUMBER TEXT FIELD
802
currentRow = sSRowSet.getRow();
803                         txtCurrentRow.setText(String.valueOf(currentRow));
804                     } catch(SQLException se) {
805                         se.printStackTrace();
806                         JOptionPane.showMessageDialog(SSDataNavigator.this,"Exception occured while updating row or moving the cursor.\n"+se.getMessage());
807                     }
808                 }
809             });
810
811
812         // BUTTON 4 ( "LAST" BUTTON ) CAUSED THE SSROWSET TO MOVE TO LAST RECORD.
813
// BEFORE MOVING CURRENT RECORD IS SAVED
814
// AFTER MOVING TO LAST RECORD THE NEXT BUTTON IS DIAABLED AND PREVIOUS BUTTON
815
// ENABLED
816
lastButton.addActionListener(new ActionListener() {
817                 public void actionPerformed(ActionEvent ae) {
818                     try {
819                         //if( sSRowSet.rowUpdated() )
820
if ( modification ) {
821                             sSRowSet.updateRow();
822                         }
823                         sSRowSet.last();
824
825                         nextButton.setEnabled(false);
826                         lastButton.setEnabled(false);
827                         if (!sSRowSet.isFirst()) {
828                             firstButton.setEnabled(true);
829                             previousButton.setEnabled(true);
830                         } else {
831                             firstButton.setEnabled(false);
832                             previousButton.setEnabled(false);
833                         }
834                         if ( dBNav != null ) {
835                             dBNav.performNavigationOps(SSDBNav.NAVIGATION_LAST);
836                         }
837                     // GET THE ROW NUMBER AND SET IT TO ROW NUMBER TEXT FIELD
838
currentRow = sSRowSet.getRow();
839                         txtCurrentRow.setText(String.valueOf(currentRow));
840                     } catch(SQLException se) {
841                         se.printStackTrace();
842                         JOptionPane.showMessageDialog(SSDataNavigator.this,"Exception occured while updating row or moving the cursor.\n"+se.getMessage());
843                     }
844                 }
845             });
846
847         // THIS BUTTON INSERTS THE ROW AND MOVES TO THE NEWLY INSERTED ROW.
848
// WHEN INSERT BUTTON IS PRESSED NAVIGATION WILL BE DISABLED SO THOSE HAVE TO BE
849
// ENABLED HERE
850
commitButton.addActionListener(new ActionListener() {
851                 public void actionPerformed(ActionEvent ae) {
852                     try {
853                         if (onInsertRow) {
854                         // IF ON INSERT ROW ADD THE ROW.
855
sSRowSet.insertRow();
856                             if ( dBNav != null ) {
857                                 dBNav.performPostInsertOps();
858                             }
859
860                             sSRowSet.moveToCurrentRow();
861                         // MOVE TO CURRENT ROW MOVES SSROWSET TO RECORD AT WHICH ADD WAS PRESSED.
862
// BUT IT NICE TO BE ON THE ADDED ROW WHICH IS THE LAST ONE IN THE SSROWSET.
863
// ALSO MOVE TO CURRENT ROW MOVES THE SSROWSET POSITION BUT DOES NOT TRIGGER
864
// ANY EVENT FOR THE LISTENERS AS A RESULT VALUES ON THE SCREEN WILL NOT
865
// DISPLAY THE CURRENT RECORD VALUES.
866
sSRowSet.last();
867                         // INCREMENT THE ROW COUNT
868
rowCount++;
869                         // SET THE ROW COUNT AS LABEL
870
lblRowCount.setText("of " + rowCount);
871                         // GET CURRENT ROW NUMBER
872
currentRow = sSRowSet.getRow();
873                         // UPDATE THE TEXT FEILD
874
txtCurrentRow.setText(String.valueOf(currentRow));
875                         } else {
876                         // ELSE UPDATE THE PRESENT ROW VALUES.
877
sSRowSet.updateRow();
878                         }
879
880                         onInsertRow = false;
881
882                         if (!sSRowSet.isFirst()) {
883                             firstButton.setEnabled(true);
884                             previousButton.setEnabled(true);
885                         }
886                         if (!sSRowSet.isLast()) {
887                             nextButton.setEnabled(true);
888                             lastButton.setEnabled(true);
889                         }
890                         refreshButton.setEnabled(true);
891
892                         if (insertion) {
893                             addButton.setEnabled(true);
894                         }
895                         if (deletion) {
896                             deleteButton.setEnabled(true);
897                         }
898                     } catch(SQLException se) {
899                         JOptionPane.showMessageDialog(SSDataNavigator.this,"Exception occured while inserting row.\n"+se.getMessage());
900                         se.printStackTrace();
901                     }
902                 }
903             });
904
905         // THIS BUTTON IS USED TO CANCEL THE CHANGES MADE TO THE RECORD.
906
// IT CAN ALSO BE USED TO CANCEL INSERT ROW.
907
// SO THE BUTTONS DISABLED AT THE INSERT BUTTON EVENT HAVE TO BE ENABLED
908
undoButton.addActionListener(new ActionListener() {
909                 public void actionPerformed(ActionEvent ae) {
910                     try {
911                     // CALL MOVE TO CURRENT ROW IF ON INSERT ROW.
912
if(onInsertRow){
913                             sSRowSet.moveToCurrentRow();
914                         }
915                     // THIS FUNCTION IS NOT NEED IF