1 /*-
2  * Copyright (c) 2001, 2020 Oracle and/or its affiliates.  All rights reserved.
3  *
4  * See the file LICENSE for license information.
5  *
6  * $Id$
7  */
8 package db_gui.datapage;
9 
10 import db_gui.datapage.BDBDataInitializable;
11 import com.sleepycat.db.DatabaseEntry;
12 
13 import java.net.URL;
14 import java.util.ResourceBundle;
15 
16 import javafx.fxml.FXML;
17 import javafx.scene.control.TextField;
18 
19 /**
20  * Implements the recno Put and Browse data panels of the Data Access Page.
21  */
22 public class RecnoController extends BDBDataInitializable {
23     @FXML
24     private TextField PutKeyTextField;
25     @FXML
26     private TextField GetKeyTextField;
27     final static private String FXMLResource =
28             "/db_gui/datapage/FXMLRecno.fxml";
29 
30     /**
31      * Initializes the controller class.
32      */
33     @Override
initialize(URL url, ResourceBundle rb)34     public void initialize(URL url, ResourceBundle rb) {
35         super.initialize(url, rb);
36     }
37 
38     /**
39      * Clears the fields when the Clear button is pressed.
40      */
41     @Override
clearAllFields()42     public void clearAllFields() {
43         PutKeyTextField.clear();
44         GetKeyTextField.clear();
45         super.clearAllFields();
46     }
47 
48     /**
49      * Converts the data entered into a TextField into a key DatabaseEntry for a
50      * recno database, which is always an integer.
51      *
52      * @param textField - The TextField to be read.
53      * @return - The DatabaseEntry containing the recno key created from the
54      * TextField.
55      */
getDatabaseEntry(TextField textField)56     private DatabaseEntry getDatabaseEntry(TextField textField) {
57         String text = textField.getText();
58         if (text == null || text.length() == 0) {
59             bdbState.printFeedback("Please enter a value for the key.");
60             return null;
61         }
62         DatabaseEntry key = new DatabaseEntry();
63         int recno;
64         try {
65             recno = Integer.parseInt(text);
66         } catch (NumberFormatException ex) {
67             bdbState.printFeedback(
68                     "Invalid value entered for the key: " + text);
69             return null;
70         }
71         key.setRecordNumber(recno);
72         return key;
73     }
74 
75     /**
76      * Displays a DatabaseEntry for a key of a recno database in a TextField.
77      * The keys for recno databases are always integers.
78      *
79      * @param key - The DatabaseEntry that contains the key read from the recno
80      * database.
81      * @param textField - The TextField in which to display the recno key.
82      */
setTextField(DatabaseEntry key, TextField textField)83     private void setTextField(DatabaseEntry key, TextField textField) {
84         String keyString;
85         long recno;
86         if (key == null) {
87             textField.clear();
88             return;
89         }
90         recno = key.getRecordNumber();
91         keyString = "" + recno;
92         textField.setText(keyString);
93     }
94 
95     /**
96      * This function converts the text entered into the
97      * PutKeyTextField into a DatabaseEntry.
98      *
99      * @return The Database entry containing the recno key entered into the
100      * PutKeyTextField.
101      */
102     @Override
getPutKeyDatabaseEntry()103     public DatabaseEntry getPutKeyDatabaseEntry() {
104         return getDatabaseEntry(PutKeyTextField);
105     }
106 
107     /**
108      * Displays the data in the given DatabaseEntry in the PutKeyTextField.
109      *
110      * @param key - The DatabaseEntry containing the recno key to be displayed
111      * in PutKeyTextField.
112      */
113     @Override
setPutKeyDatabaseEntry(DatabaseEntry key)114     public void setPutKeyDatabaseEntry(DatabaseEntry key) {
115         setTextField(key, PutKeyTextField);
116     }
117 
118     /**
119      * This function converts the text entered into the
120      * GetKeyTextField into a DatabaseEntry.
121      *
122      * @return The Database entry containing the recno key entered into the
123      * GetKeyTextField.
124      */
125     @Override
getGetKeyDatabaseEntry()126     public DatabaseEntry getGetKeyDatabaseEntry() {
127         return getDatabaseEntry(GetKeyTextField);
128     }
129 
130     /**
131      * Displays the data in the given DatabaseEntry in the GetKeyTextField.
132      *
133      * @param key - The DatabaseEntry containing the recno key to be displayed
134      * in GetKeyTextField.
135      */
136     @Override
setGetKeyDatabaseEntry(DatabaseEntry key)137     public void setGetKeyDatabaseEntry(DatabaseEntry key) {
138         setTextField(key, GetKeyTextField);
139     }
140 
141     /**
142      * Checks if a value has been entered into GetKeyTextField.
143      *
144      * @return - Whether the GetKeyTextField has text in it or not.
145      */
146     @Override
getGetKeySet()147     public boolean getGetKeySet() {
148         String keyString = GetKeyTextField.getText();
149         return !(keyString == null || keyString.length() == 0);
150     }
151 
152     /**
153      * Returns whether to append or put a new record into the database.
154      *
155      * @return False, since recno records are put, not appended, into the
156      * database.
157      */
158     @Override
getAppendRecords()159     public boolean getAppendRecords() {
160         return false;
161     }
162 }
163