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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
package fxmltableview;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.util.Callback;
public class FXMLTableViewController implements Initializable {
@FXML private TableView<Person> tableView;
@FXML private TableColumn<Person, String> firstNameColumn;
@FXML private TableColumn<Person, String> lastNameColumn;
@FXML private TableColumn<Person, Integer> testColumn;
@FXML private TextField txtNombre;
@FXML private TextField txtPaterno;
@FXML private TextField txtMaterno;
@FXML private Label lblInfo;
//+++
@FXML private TextField txtURL;
//+++
Connection con;
private DBClass objDbClass;
private ObservableList<Person> data;
// private LinkedHashMap <String,String> map ;
private String strLike;
private int nbErreurs;
private int numRegistros;
private DBCantos objDbCantos;
@Override
public void initialize(URL location, ResourceBundle resources) {
lblInfo.setVisible(false);
tableView.setVisible(false);
}
@FXML
private void buscarCantos() throws IOException {
final URL fxmlURL = getClass().getResource("fxml_tableview.fxml");
final FXMLLoader fxmlLoader = new FXMLLoader(fxmlURL);
final Node control = (Node) fxmlLoader.load();
final Map<String,String> mapx = control.lookupAll(".text-field")
.stream()
.map((Node node)->(TextField)node)
.collect(Collectors.toMap(TextField::getId,TextField::getText));
mapx.forEach((id,text)->{
System.out.printf("Cle = %s Valeur = %s \n",id,text);
});
SQLBuilder g = new SQLBuilder("");
objDbCantos = new DBCantos();
try {
con = objDbCantos.getConnection();
} catch (ClassNotFoundException ex) {
Logger.getLogger(FXMLTableViewController.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(FXMLTableViewController.class.getName()).log(Level.SEVERE, null, ex);
}
buildCantos(g);
}
@FXML
private void lanzarBusqueda() throws IOException {
nbErreurs=0;
numRegistros=0;
lblInfo.setVisible(true);
LinkedHashMap <String,String> map = new LinkedHashMap <>();
map.put("nombre", txtNombre.getText().toUpperCase());
map.put("apellido", txtPaterno.getText().toUpperCase());
map.put("amaterno", txtMaterno.getText().toUpperCase());
SQLBuilder gen = new SQLBuilder("");
map.entrySet().stream().forEach((Map.Entry<String, String> entry) -> {
//a. Verifica que campos no tienen datos
if("".equals(entry.getValue()) || (entry.getValue().trim().length() == 0)){
nbErreurs++;
}
//b. Construye la cadena SQL
strLike=entry.getValue();
strLike = "%"+ strLike+"%";
gen.and(!"".equals(entry.getValue()),"UPPER ("+ entry.getKey()+") "+" like ?", strLike);
// gen.and(!"".equals(c.getTxtValeur()), "upper ("+ c.getTxtNom()+") "+" like ?", strLike);
});
if(nbErreurs==3){
lblInfo.setText
("¡ERROR! Todos los campos de búsqueda están en blanco. Por favor introduzca datos... ");
Image image = new Image(getClass().getResourceAsStream("/fxmltableview/iconos/error_16x16.gif"));
lblInfo.setGraphic(new ImageView(image));
lblInfo.setTextFill(Color.web("#FF3333"));
tableView.setVisible(false);
}
else{
objDbClass = new DBClass();
try{
con = objDbClass.getConnection();
buildData(gen);
tableView.setRowFactory(
(TableView<Person> tableView1) -> {
final TableRow<Person> row = new TableRow<>();
final ContextMenu rowMenu = new ContextMenu();
MenuItem editItem = new MenuItem("Edit");
editItem.setId("rpt01");
editItem.setOnAction((ActionEvent event) -> {
System.out.println("%%%%%"+editItem.getId());
});
MenuItem removeItem = new MenuItem("Delete");
removeItem.setId("rpt02");
removeItem.setOnAction((ActionEvent event) -> {
System.out.println("+++++"+row.getItem().getTest());
});
rowMenu.getItems().addAll(editItem, removeItem);
row.contextMenuProperty().bind(
Bindings.when(Bindings.isNotNull(row.itemProperty()))
.then(rowMenu)
.otherwise((ContextMenu)null));
tableView1.setOnMouseClicked((MouseEvent e) -> {
rowMenu.show(tableView1, e.getScreenX(), e.getScreenY());
});
return row;
});
}
catch(ClassNotFoundException | SQLException ce){
}
}
}
private void buildData(SQLBuilder gen){
data = FXCollections.observableArrayList();
PreparedStatement pstmt;
try {
pstmt = gen.createStatement(con, "SELECT idtest, nombre, apellido FROM tbltest ");
String strWhere = pstmt.toString();
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
Person cm = new Person();
cm.setTest(rs.getInt("idtest"));
cm.setFirstName(rs.getString("nombre"));
cm.setLastName(rs.getString("apellido"));
data.add(cm);
++numRegistros;
}
if (numRegistros==0) {
/* lblInfo.setText
("No se encontraron personas. Por favor verifique sus criterios de busqueda...");
Image image = new Image(getClass().getResourceAsStream("/fxmltableview/iconos/advertencia_16x16.png"));
lblInfo.setGraphic(new ImageView(image));
lblInfo.setTextFill(Color.web("#C68E17"));*/
mostrarTabla("No se encontraron personas. Por favor verifique sus criterios de busqueda...",
"/fxmltableview/iconos/nada_16x16.png","#C68E17", false);
// tableView.setVisible(false);
}
if (numRegistros==1) {
// lblInfo.setText("SE ENCONTRÓ " + numRegistros + " PERSONA");
/* Image image = new Image(getClass().getResourceAsStream("error_24x24.png"));
lblInfo.setGraphic(new ImageView(image));
lblInfo.setTextFill(Color.web("#C68E17"));*/
// tableView.setVisible(true);
mostrarTabla("SE ENCONTRÓ " + numRegistros + " PERSONA."+
"Haga clic sobre la persona para abrir el menú de opciones.",
"/fxmltableview/iconos/ok_16x16.png","#C68E17", true);
}
if (numRegistros>2) {
// lblInfo.setText("SE ENCONTRARON " + numRegistros + " PERSONAS");
// tableView.setVisible(true);
mostrarTabla("SE ENCONTRARON " + numRegistros + " PERSONAS. "+
"Haga clic sobre la persona para abrir el menú de opciones.",
"/fxmltableview/iconos/ok_16x16.png","#C68E17", true);
}
}
catch(SQLException e){
System.out.println("Error on Building Data"+e.getErrorCode()+ ": "+e.getMessage());
}
}
private void mostrarTabla(String strInfo, String strImage, String strColor, boolean tblVisible){
Image image = new Image(getClass().getResourceAsStream(strImage));
lblInfo.setGraphic(new ImageView(image));
lblInfo.setTextFill(Color.web(strColor));
// tableView.setVisible(false);
lblInfo.setText(strInfo);
lblInfo.setVisible(true);
if (tblVisible==true){
assert tableView != null : "fx:id=\"tableView\" was not injected: check your FXML file...";
firstNameColumn.setCellValueFactory(new PropertyValueFactory<>("test"));
lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
testColumn.setCellValueFactory(new PropertyValueFactory<>("lastName"));
tableView.setItems(data);
tableView.getItems();
}
tableView.setVisible(tblVisible);
nbErreurs=0;
}
//----
private void buildCantos(SQLBuilder gen){
data = FXCollections.observableArrayList();
String strPreURL = "https://docs.google.com/uc?export=download&id=";
PreparedStatement pstmt;
try {
pstmt = gen.createStatement(con, "SELECT * FROM tblcantos ");
String strWhere = pstmt.toString().toUpperCase();
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
Person cm = new Person();
cm.setTest(rs.getInt("id_canto"));
cm.setFirstName(rs.getString("titulo"));
cm.setLastName(rs.getString("url_comentario"));
txtURL.setText(strPreURL+rs.getString("url_comentario"));
data.add(cm);
++numRegistros;
}
if (numRegistros==0) {
/* lblInfo.setText
("No se encontraron personas. Por favor verifique sus criterios de busqueda...");
Image image = new Image(getClass().getResourceAsStream("/fxmltableview/iconos/advertencia_16x16.png"));
lblInfo.setGraphic(new ImageView(image));
lblInfo.setTextFill(Color.web("#C68E17"));*/
mostrarCanto("No se encontraron personas. Por favor verifique sus criterios de busqueda...",
"/fxmltableview/iconos/nada_16x16.png","#C68E17", false);
// tableView.setVisible(false);
}
if (numRegistros==1) {
// lblInfo.setText("SE ENCONTRÓ " + numRegistros + " PERSONA");
/* Image image = new Image(getClass().getResourceAsStream("error_24x24.png"));
lblInfo.setGraphic(new ImageView(image));
lblInfo.setTextFill(Color.web("#C68E17"));*/
// tableView.setVisible(true);
mostrarCanto("SE ENCONTRÓ " + numRegistros + " PERSONA."+
"Haga clic sobre la persona para abrir el menú de opciones.",
"/fxmltableview/iconos/ok_16x16.png","#C68E17", true);
}
if (numRegistros>2) {
// lblInfo.setText("SE ENCONTRARON " + numRegistros + " PERSONAS");
// tableView.setVisible(true);
mostrarCanto("SE ENCONTRARON " + numRegistros + " PERSONAS. "+
"Haga clic sobre la persona para abrir el menú de opciones.",
"/fxmltableview/iconos/ok_16x16.png","#C68E17", true);
}
}
catch(SQLException e){
System.out.println("Error on Building Data"+e.getErrorCode()+ ": "+e.getMessage());
}
}
private void mostrarCanto(String strInfo, String strImage, String strColor, boolean tblVisible){
Image image = new Image(getClass().getResourceAsStream(strImage));
lblInfo.setGraphic(new ImageView(image));
lblInfo.setTextFill(Color.web(strColor));
// tableView.setVisible(false);
lblInfo.setText(strInfo);
lblInfo.setVisible(true);
if (tblVisible==true){
assert tableView != null : "fx:id=\"tableView\" was not injected: check your FXML file...";
firstNameColumn.setCellValueFactory(new PropertyValueFactory<>("test"));
lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
testColumn.setCellValueFactory(new PropertyValueFactory<>("lastName"));
tableView.setItems(data);
tableView.getItems();
// tableView.setEditable(tblVisible);
}
tableView.setVisible(tblVisible);
nbErreurs=0;
}
@FXML
private void abrirFrmBuscarPersona(){}
} |
Partager