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 com.sleepycat.db.DatabaseEntry;
11 
12 import java.net.URL;
13 import java.util.ResourceBundle;
14 
15 import javafx.event.ActionEvent;
16 import javafx.fxml.FXML;
17 import javafx.scene.control.ComboBox;
18 import javafx.scene.control.TextField;
19 
20 /**
21  * Implements the btree and hash Put and Browse data panels of the Data Access
22  * Page.
23  */
24 public class BtreeHashController extends BDBDataInitializable {
25     @FXML
26     private TextField PutKeyTextField;
27     @FXML
28     private ComboBox<String> PutKeyComboBox;
29     @FXML
30     private TextField GetKeyTextField;
31     @FXML
32     private ComboBox<String> GetKeyComboBox;
33     private DatabaseEntry currentGetKey;
34     final static private String FXMLResource =
35             "/db_gui/datapage/FXMLBtreeHash.fxml";
36 
37     /**
38      * Initializes the controller class.
39      */
40     @Override
initialize(URL url, ResourceBundle rb)41     public void initialize(URL url, ResourceBundle rb) {
42         GetKeyComboBox.getItems().addAll(dataFormats);
43         GetKeyComboBox.getSelectionModel().selectFirst();
44         PutKeyComboBox.getItems().addAll(dataFormats);
45         PutKeyComboBox.getSelectionModel().selectFirst();
46         super.initialize(url, rb);
47     }
48 
49     /**
50      * Clears the fields when the Clear button is pressed.
51      */
52     @Override
clearAllFields()53     public void clearAllFields() {
54         PutKeyTextField.clear();
55         GetKeyComboBox.getSelectionModel().selectFirst();
56         GetKeyTextField.clear();
57         PutKeyComboBox.getSelectionModel().selectFirst();
58         currentGetKey = null;
59         super.clearAllFields();
60     }
61 
62     /**
63      * Change how the binary data is displayed in the GetKeyTextField when a
64      * new type is selected in the GetKeyComboBox.
65      *
66      * @param event - Unused.
67      */
68     @FXML
handleGetKeyComboBox(ActionEvent event)69     protected void handleGetKeyComboBox(ActionEvent event) {
70         changeDataDisplay(currentGetKey, GetKeyTextField, GetKeyComboBox);
71     }
72 
73     /**
74      * Converts the data entered into a TextField into a key DatabaseEntry for a
75      * btree or hash database.  The type of the data is stored in the ComboBox.
76      *
77      * @param textField - The TextField containing the data.
78      * @param comboBox - The ComboBox which contains how the data should be
79      * interpreted.
80      * @return - The DatabaseEntry containing the data.
81      */
getDatabaseEntry( TextField textField, ComboBox<String> comboBox)82     private DatabaseEntry getDatabaseEntry(
83             TextField textField, ComboBox<String> comboBox) {
84         String keyString;
85         keyString = textField.getText();
86         if (keyString == null || keyString.length() == 0) {
87             bdbState.printFeedback("Error, please enter a Key value.");
88             return null;
89         }
90         return getDatabaseEntry(keyString,
91                 comboBox.getSelectionModel().getSelectedItem());
92     }
93 
94     /**
95      * Displays a DatabaseEntry for a key of a btree or hash database in a
96      * TextField.  The type of the key is set in the ComboBox.
97      *
98      * @param key - The DatabaseEntry containing the key to be displayed.
99      * @param textField - The TextField in which to display the key.
100      * @param comboBox - The ComboBox containing how the key is to be
101      * interpreted.
102      */
setTextField(DatabaseEntry key, TextField textField, ComboBox<String> comboBox)103     private void setTextField(DatabaseEntry key,
104             TextField textField, ComboBox<String> comboBox) {
105         String keyString;
106         if (key == null) {
107             textField.clear();
108             return;
109         }
110         keyString = getDatabaseEntryString(key,
111                 comboBox.getSelectionModel().getSelectedItem());
112         textField.setText(keyString);
113     }
114 
115     /**
116      * This function converts the text entered into the PutKeyTextField
117      * into a DatabaseEntry using the type selected in the PutKeyComboBox.
118      *
119      * @return - The DatabaseEntry for PutKeyTextField.
120      */
121     @Override
getPutKeyDatabaseEntry()122     public DatabaseEntry getPutKeyDatabaseEntry() {
123         return getDatabaseEntry(PutKeyTextField, PutKeyComboBox);
124     }
125 
126     /**
127      * Displays the data in the given DatabaseEntry in the PutKeyTextField,
128      * using the type selected in the PutKeyComboBox.
129      *
130      * @param key - The DatabaseEntry containing the key to be displayed.
131      */
132     @Override
setPutKeyDatabaseEntry(DatabaseEntry key)133     public void setPutKeyDatabaseEntry(DatabaseEntry key) {
134         setTextField(key, PutKeyTextField, PutKeyComboBox);
135     }
136 
137     /**
138      * This function converts the text entered into the GetKeyTextField
139      * into a DatabaseEntry using the type selected in the GetKeyComboBox.
140      *
141      * @return - The DatabaseEntry for GetKeyTextField.
142      */
143     @Override
getGetKeyDatabaseEntry()144     public DatabaseEntry getGetKeyDatabaseEntry() {
145         return getDatabaseEntry(GetKeyTextField, GetKeyComboBox);
146     }
147 
148     /**
149      * Displays the data in the given DatabaseEntry in the GetKeyTextField,
150      * using the type selected in the GetKeyComboBox.
151      *
152      * @param key - The DatabaseEntry containing the key to be displayed.
153      */
154     @Override
setGetKeyDatabaseEntry(DatabaseEntry key)155     public void setGetKeyDatabaseEntry(DatabaseEntry key) {
156         currentGetKey = key;
157         setTextField(key, GetKeyTextField, GetKeyComboBox);
158     }
159 
160     /**
161      * Checks if a value has been entered into GetKeyTextField.
162      *
163      * @return - True if the GetKeyTextField contains text, false otherwise.
164      */
165     @Override
getGetKeySet()166     public boolean getGetKeySet() {
167         String keyString = GetKeyTextField.getText();
168         return !(keyString == null || keyString.length() == 0);
169     }
170 
171     /**
172      * Whether new records should be appended or put into the database.
173      *
174      * @return - True, since btree and hash records are put, not appended, into
175      * the database.
176      */
177     @Override
getAppendRecords()178     public boolean getAppendRecords() {
179         return false;
180     }
181 }
182