package gui; import java.beans.Beans; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import java.security.NoSuchAlgorithmException; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.persistence.EntityManager; import javax.persistence.Persistence; import javax.persistence.Query; import util.MD5; import entity.AccessLevel; import entity.Configuration; import entity.User; import javafx.application.Application; import javafx.beans.property.StringProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.HPos; import javafx.geometry.Orientation; import javafx.geometry.Pos; import javafx.geometry.VPos; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.scene.control.PasswordBox; import javafx.scene.control.ProgressBar; import javafx.scene.control.ProgressIndicator; import javafx.scene.control.Separator; import javafx.scene.control.SplitPane; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; import javafx.scene.control.TextArea; import javafx.scene.control.TextBox; import javafx.scene.control.ToolBar; import javafx.scene.control.Tooltip; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.stage.Modality; import javafx.stage.Stage; public class UserEdit { private boolean returnStatus; private final boolean RET_CANCEL = false; private final boolean RET_OK = true; private BorderPane windowBorderPane; private BorderPane applicationBorderPane; private Button okButton; private Button cancelButton; private Button emailButton; private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( this); private final PropertyChangeListener propertyChangeListener; private CheckBox userActiveCheckBox; private ChoiceBox accessLevelChoiceBox; private Configuration configuration; private EntityManager trainingManager; private GridPane mainDataGridPane; private GridPane contactDataGridPane; private GridPane accessDataGridPane; private GridPane statusPanelGridPane; private HBox logoHBox; private HBox progressHBox; /* * private KeyAdapter textFieldKeyListener; private KeyAdapter * passwordFieldKeyListener; private PropertyChangeListener pcl; */ private Label nameLabel; private Label nifLabel; private Label addressLabel; private Label zipCodeLabel; private Label cityLabel; private Label provinceLabel; private Label countryLabel; private Label phoneLabel; private Label cellPhoneLabel; private Label emailLabel; private Label userNameLabel; private Label passwordLabel; private Label accessLevelLabel; private Label statusMessageLabel; private List accessLevelList; private List userList; private PasswordBox passwordPasswordBox; private ProgressBar progressBar; private ProgressIndicator progressIndicator; private Query accessLevelQuery; private Query userQuery; private Stage stage; private Stage parentStage; private StringProperty nameProperty; private StringProperty addressProperty; private Tab contactDataTab; private Tab accessDataTab; private Tab commentsTab; private TabPane dataTabPane; private TextArea commentsTextArea; private TextBox nameTextBox; private TextBox nifTextBox; private TextBox addressTextBox; private TextBox zipCodeTextBox; private TextBox cityTextBox; private TextBox provinceTextBox; private TextBox countryTextBox; private TextBox phoneTextBox; private TextBox cellPhoneTextBox; private TextBox emailTextBox; private TextBox userNameTextBox; private ToolBar toolBar; private User user; public UserEdit(Stage parentStage) { this.parentStage = parentStage; setReturnStatus(RET_CANCEL); user = new User(); propertyChangeListener = new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { System.out.println("Name = " + evt.getPropertyName()); System.out.println("Old Value = " + evt.getOldValue()); System.out.println("New Value = " + evt.getNewValue()); } }; propertyChangeSupport.addPropertyChangeListener(propertyChangeListener); initComponents(); } public void addPropertyChangeListener(PropertyChangeListener listener) { propertyChangeSupport.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { propertyChangeSupport.removePropertyChangeListener(listener); } private void initComponents() { configuration = new Configuration(); trainingManager = Beans.isDesignTime() ? null : Persistence .createEntityManagerFactory("TrainingManager") .createEntityManager(); accessLevelQuery = Beans.isDesignTime() ? null : trainingManager .createQuery("SELECT a FROM AccessLevel a"); userQuery = Beans.isDesignTime() ? null : trainingManager .createQuery("SELECT u FROM User u ORDER BY u.name ASC"); userQuery.setFirstResult(0); userQuery.setMaxResults(2); accessLevelList = Beans.isDesignTime() ? java.util.Collections .emptyList() : FXCollections.observableList(accessLevelQuery .getResultList()); userList = Beans.isDesignTime() ? java.util.Collections.emptyList() : FXCollections.observableList(userQuery.getResultList()); windowBorderPane = new BorderPane(); applicationBorderPane = new BorderPane(); okButton = new Button("Guardar y Cerrar", new ImageView(new Image( getClass().getResourceAsStream("resources/icons/32/ok.png")))); okButton.setTooltip(new Tooltip("Añadir Nuevo Usuario")); okButton.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { saveAndClose(); } }); cancelButton = new Button("Cancelar y Cerrar", new ImageView(new Image( getClass().getResourceAsStream("resources/icons/32/tool.png")))); cancelButton.setTooltip(new Tooltip("Modificar Usuario Seleccionado")); cancelButton.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { discardAndClose(); } }); emailButton = new Button("Enviar E-mail", new ImageView(new Image(getClass().getResourceAsStream( "resources/icons/32/email.png")))); emailButton.setTooltip(new Tooltip("Eliminar Usuario Seleccionado")); userActiveCheckBox = new CheckBox("Activo"); accessLevelChoiceBox = new ChoiceBox(); for (AccessLevel al : accessLevelList) { accessLevelChoiceBox.getItems().add(al.getName()); } ChangeListener nameChangeListener = new ChangeListener() { public void changed(ObservableValue arg0, Object oldValue, Object newValue) { System.out.println("from adapter: " + oldValue + newValue + arg0.getClass()); /* * User oldUser = new User(); user.setName((String) newValue); * propertyChangeSupport.firePropertyChange("user", oldUser, * user); */ } }; ChangeListener addressChangeListener = new ChangeListener() { public void changed(ObservableValue arg0, Object oldValue, Object newValue) { System.out.println("from adapter: " + oldValue + newValue + arg0.getClass()); } }; mainDataGridPane = new GridPane(); mainDataGridPane.setHgap(10); mainDataGridPane.setVgap(10); mainDataGridPane.setAlignment(Pos.CENTER); contactDataGridPane = new GridPane(); contactDataGridPane.setHgap(10); contactDataGridPane.setVgap(10); contactDataGridPane.setAlignment(Pos.CENTER); accessDataGridPane = new GridPane(); accessDataGridPane.setHgap(10); accessDataGridPane.setVgap(10); accessDataGridPane.setAlignment(Pos.CENTER); statusPanelGridPane = new GridPane(); statusPanelGridPane.setHgap(10); statusPanelGridPane.setVgap(10); statusPanelGridPane.setAlignment(Pos.CENTER_RIGHT); logoHBox = new HBox(); logoHBox.setSpacing(5); logoHBox.setAlignment(Pos.CENTER_LEFT); logoHBox.getChildren().add( new ImageView(new Image(UserList.class .getResourceAsStream("resources/images/Logo.jpg")))); progressHBox = new HBox(); progressHBox.setSpacing(5); progressHBox.setAlignment(Pos.CENTER_RIGHT); nameLabel = new Label("Nombre:"); nifLabel = new Label("N.I.F:"); addressLabel = new Label("Dirección:"); zipCodeLabel = new Label("Código Postal:"); cityLabel = new Label("Localidad:"); provinceLabel = new Label("Provincia:"); countryLabel = new Label("País:"); phoneLabel = new Label("Teléfono:"); cellPhoneLabel = new Label("Teléfono Móvil:"); emailLabel = new Label("E-mail:"); userNameLabel = new Label("Usuario:"); passwordLabel = new Label("Contraseña:"); accessLevelLabel = new Label("Nivel de Acceso:"); statusMessageLabel = new Label(""); passwordPasswordBox = new PasswordBox(); passwordPasswordBox.setColumns(10); passwordPasswordBox.setPromptText("Máximo 10 caracteres"); progressBar = new ProgressBar(1); progressIndicator = new ProgressIndicator(1); SplitPane mainPanelSplitPane = new SplitPane(); mainPanelSplitPane.setOrientation(Orientation.VERTICAL); commentsTextArea = new TextArea(); contactDataTab = new Tab("Datos de Contacto"); contactDataTab.setContent(contactDataGridPane); accessDataTab = new Tab("Datos de Acceso"); accessDataTab.setContent(accessDataGridPane); commentsTab = new Tab("Observaciones"); commentsTab.setContent(commentsTextArea); dataTabPane = new TabPane(); dataTabPane.getTabs() .addAll(contactDataTab, accessDataTab, commentsTab); nameTextBox = new TextBox(); nameTextBox.setColumns(5); nameTextBox.setPromptText("Máximo 64 caracteres"); nameTextBox.setOnKeyReleased(new EventHandler() { @Override public void handle(KeyEvent e) { if (nameTextBox.getText() != null && !nameTextBox.getText().isEmpty()) { System.out.println("Me Pulsan " + nameTextBox.getText().length()); if (nameTextBox.getText().contains("'")) { nameTextBox.setText(nameTextBox.getText().replace("'", "")); } if (nameTextBox.getText().length() > nameTextBox .getColumns()) { System.out.println("Me Paso"); /* * Toolkit.getDefaultToolkit().beep(); * JOptionPane.showMessageDialog( UserEdit.this, * "El campo no puede contener más de " + * textField.getColumns() + " caracteres.", * "Formato de Campo no Válido", * JOptionPane.WARNING_MESSAGE); */ nameTextBox.setText(nameTextBox.getText().substring(0, nameTextBox.getColumns())); } } } }); nifTextBox = new TextBox(); nifTextBox.setColumns(10); nifTextBox.setPromptText("Máximo 10 caracteres"); addressTextBox = new TextBox(); addressTextBox.setColumns(64); addressTextBox.setPromptText("Máximo 64 caracteres"); zipCodeTextBox = new TextBox(); zipCodeTextBox.setColumns(5); zipCodeTextBox.setPromptText("Máximo 5 caracteres"); cityTextBox = new TextBox(); cityTextBox.setColumns(64); cityTextBox.setPromptText("Máximo 64 caracteres"); provinceTextBox = new TextBox(); provinceTextBox.setColumns(64); provinceTextBox.setPromptText("Máximo 64 caracteres"); countryTextBox = new TextBox(); countryTextBox.setColumns(64); countryTextBox.setPromptText("Máximo 64 caracteres"); phoneTextBox = new TextBox(); phoneTextBox.setColumns(15); phoneTextBox.setPromptText("Máximo 15 caracteres"); cellPhoneTextBox = new TextBox(); cellPhoneTextBox.setColumns(15); cellPhoneTextBox.setPromptText("Máximo 15 caracteres"); emailTextBox = new TextBox(); emailTextBox.setColumns(32); emailTextBox.setPromptText("Máximo 32 caracteres"); userNameTextBox = new TextBox(); userNameTextBox.setColumns(10); userNameTextBox.setPromptText("Máximo 10 caracteres"); /* * pcl = new PropertyChangeListener() { * * public void propertyChange(PropertyChangeEvent e) { * System.out.println("Ha cambiado la muy hija de puta"); } }; */ /* * textFieldKeyListener = new KeyAdapter() { * * public void keyReleased(KeyEvent e) { TextBox textField = (TextBox) * e.getSource(); if (!textField.getText().isEmpty()) { if * (textField.getText().contains("'")) { * textField.setText(textField.getText().replace("'", "")); } if * (textField.getText().length() > textField.getColumns()) { * Toolkit.getDefaultToolkit().beep(); * System.out.println("Eres una perra"); * JOptionPane.showMessageDialog(UserEdit.this, * "El campo no puede contener más de " + textField.getColumns() + * " caracteres.", "Formato de Campo no Válido", * JOptionPane.WARNING_MESSAGE); * textField.setText(textField.getText().substring(0, * textField.getColumns())); } } } }; * * passwordFieldKeyListener = new KeyAdapter() { * * public void keyReleased(KeyEvent e) { PasswordBox passwordField = * (PasswordBox) e.getSource(); if (!passwordField.getText().isEmpty()) * { String pass = new String(passwordField.getText()); if * (pass.contains("'")) { passwordField.setText(pass.replace("'", "")); * } if (passwordField.getText().length() > passwordField.getColumns()) * { pass = new String(passwordField.getText()); * Toolkit.getDefaultToolkit().beep(); * JOptionPane.showMessageDialog(UserEdit.this, * "El campo no puede contener más de " + passwordField.getColumns() + * " caracteres.", "Formato de Campo no Válido", * JOptionPane.WARNING_MESSAGE); passwordField.setText(pass.substring(0, * passwordField.getColumns())); } } } }; * * nifTextBox.addKeyListener(textFieldKeyListener); * nameTextBox.addKeyListener(textFieldKeyListener); * addressTextBox.addKeyListener(textFieldKeyListener); * zipCodeTextBox.addKeyListener(textFieldKeyListener); * cityTextBox.addKeyListener(textFieldKeyListener); * provinceTextBox.addKeyListener(textFieldKeyListener); * countryTextBox.addKeyListener(textFieldKeyListener); * phoneTextBox.addKeyListener(textFieldKeyListener); * cellPhoneTextBox.addKeyListener(textFieldKeyListener); * emailTextBox.addKeyListener(textFieldKeyListener); * userNameTextBox.addKeyListener(textFieldKeyListener); * passwordPasswordBox.addKeyListener(passwordFieldKeyListener); */ nameProperty = new StringProperty(""); nameProperty.addListener(nameChangeListener); addressProperty = new StringProperty(""); addressProperty.addListener(addressChangeListener); nameTextBox.rawTextProperty().bindBidirectional(nameProperty); addressTextBox.rawTextProperty().bindBidirectional(addressProperty); toolBar = new ToolBar(); toolBar.getStyleClass().add("map-toolbar"); toolBar.getItems().addAll(okButton, cancelButton, new Separator(), emailButton); GridPane.setValignment(nameLabel, VPos.CENTER); GridPane.setHalignment(nameLabel, HPos.RIGHT); GridPane.setValignment(nameTextBox, VPos.CENTER); GridPane.setHalignment(nameTextBox, HPos.LEFT); GridPane.setValignment(nifLabel, VPos.CENTER); GridPane.setHalignment(nifLabel, HPos.RIGHT); GridPane.setValignment(nifTextBox, VPos.CENTER); GridPane.setHalignment(nifTextBox, HPos.LEFT); GridPane.setValignment(addressLabel, VPos.CENTER); GridPane.setHalignment(addressLabel, HPos.RIGHT); GridPane.setValignment(addressTextBox, VPos.CENTER); GridPane.setHalignment(addressTextBox, HPos.LEFT); GridPane.setValignment(zipCodeLabel, VPos.CENTER); GridPane.setHalignment(zipCodeLabel, HPos.RIGHT); GridPane.setValignment(zipCodeTextBox, VPos.CENTER); GridPane.setHalignment(zipCodeTextBox, HPos.LEFT); GridPane.setValignment(cityLabel, VPos.CENTER); GridPane.setHalignment(cityLabel, HPos.RIGHT); GridPane.setValignment(cityTextBox, VPos.CENTER); GridPane.setHalignment(cityTextBox, HPos.LEFT); GridPane.setValignment(provinceLabel, VPos.CENTER); GridPane.setHalignment(provinceLabel, HPos.RIGHT); GridPane.setValignment(provinceTextBox, VPos.CENTER); GridPane.setHalignment(provinceTextBox, HPos.LEFT); GridPane.setValignment(countryLabel, VPos.CENTER); GridPane.setHalignment(countryLabel, HPos.RIGHT); GridPane.setValignment(countryTextBox, VPos.CENTER); GridPane.setHalignment(countryTextBox, HPos.LEFT); GridPane.setValignment(phoneLabel, VPos.CENTER); GridPane.setHalignment(phoneLabel, HPos.RIGHT); GridPane.setValignment(phoneTextBox, VPos.CENTER); GridPane.setHalignment(phoneTextBox, HPos.LEFT); GridPane.setValignment(cellPhoneLabel, VPos.CENTER); GridPane.setHalignment(cellPhoneLabel, HPos.RIGHT); GridPane.setValignment(cellPhoneTextBox, VPos.CENTER); GridPane.setHalignment(cellPhoneTextBox, HPos.LEFT); GridPane.setValignment(emailLabel, VPos.CENTER); GridPane.setHalignment(emailLabel, HPos.RIGHT); GridPane.setValignment(emailTextBox, VPos.CENTER); GridPane.setHalignment(emailTextBox, HPos.LEFT); GridPane.setValignment(userNameLabel, VPos.CENTER); GridPane.setHalignment(userNameLabel, HPos.RIGHT); GridPane.setValignment(userNameTextBox, VPos.CENTER); GridPane.setHalignment(userNameTextBox, HPos.LEFT); GridPane.setValignment(passwordLabel, VPos.CENTER); GridPane.setHalignment(passwordLabel, HPos.RIGHT); GridPane.setValignment(passwordPasswordBox, VPos.CENTER); GridPane.setHalignment(passwordPasswordBox, HPos.LEFT); GridPane.setValignment(accessLevelLabel, VPos.CENTER); GridPane.setHalignment(accessLevelLabel, HPos.RIGHT); GridPane.setValignment(accessLevelChoiceBox, VPos.CENTER); GridPane.setHalignment(accessLevelChoiceBox, HPos.LEFT); GridPane.setValignment(userActiveCheckBox, VPos.CENTER); GridPane.setHalignment(userActiveCheckBox, HPos.RIGHT); mainDataGridPane.add(nameLabel, 0, 0); mainDataGridPane.add(nameTextBox, 1, 0); mainDataGridPane.add(nifLabel, 2, 0); mainDataGridPane.add(nifTextBox, 3, 0); mainDataGridPane.add(addressLabel, 0, 1); mainDataGridPane.add(addressTextBox, 1, 1); mainDataGridPane.add(zipCodeLabel, 2, 1); mainDataGridPane.add(zipCodeTextBox, 3, 1); mainDataGridPane.add(cityLabel, 0, 2); mainDataGridPane.add(cityTextBox, 1, 2); mainDataGridPane.add(provinceLabel, 0, 3); mainDataGridPane.add(provinceTextBox, 1, 3); mainDataGridPane.add(countryLabel, 0, 4); mainDataGridPane.add(countryTextBox, 1, 4); contactDataGridPane.add(phoneLabel, 0, 0); contactDataGridPane.add(phoneTextBox, 1, 0); contactDataGridPane.add(cellPhoneLabel, 0, 1); contactDataGridPane.add(cellPhoneTextBox, 1, 1); contactDataGridPane.add(emailLabel, 0, 2); contactDataGridPane.add(emailTextBox, 1, 2); accessDataGridPane.add(userNameLabel, 0, 0); accessDataGridPane.add(userNameTextBox, 1, 0); accessDataGridPane.add(passwordLabel, 0, 1); accessDataGridPane.add(passwordPasswordBox, 1, 1); accessDataGridPane.add(accessLevelLabel, 0, 2); accessDataGridPane.add(accessLevelChoiceBox, 1, 2); accessDataGridPane.add(userActiveCheckBox, 0, 3); GridPane.setValignment(statusMessageLabel, VPos.CENTER); GridPane.setHalignment(statusMessageLabel, HPos.LEFT); GridPane.setValignment(progressHBox, VPos.CENTER); GridPane.setHalignment(progressHBox, HPos.RIGHT); statusPanelGridPane.add(statusMessageLabel, 0, 0); statusPanelGridPane.add(progressHBox, 1, 0); progressHBox.getChildren().addAll(progressBar, progressIndicator); mainPanelSplitPane.getItems().addAll(mainDataGridPane, dataTabPane); applicationBorderPane.setTop(toolBar); applicationBorderPane.setCenter(mainPanelSplitPane); applicationBorderPane.setBottom(statusPanelGridPane); windowBorderPane.setTop(logoHBox); windowBorderPane.setCenter(applicationBorderPane); } public boolean getReturnStatus() { return returnStatus; } private void setReturnStatus(boolean returnStatus) { this.returnStatus = returnStatus; } private void doClose(boolean returnStatus) { this.returnStatus = returnStatus; stage.setVisible(false); stage.close(); } public void discardAndClose() { doClose(RET_CANCEL); } public void saveAndClose() { doClose(RET_OK); } public User getUser() { user.setName(nameTextBox.getText()); user.setNif(nifTextBox.getText()); user.setAddress(addressTextBox.getText()); user.setZipCode(zipCodeTextBox.getText()); user.setCity(cityTextBox.getText()); user.setProvince(provinceTextBox.getText()); user.setCountry(countryTextBox.getText()); user.setPhone(phoneTextBox.getText()); user.setCellPhone(cellPhoneTextBox.getText()); user.setEmail(emailTextBox.getText()); user.setUserName(userNameTextBox.getText()); user.setActive(userActiveCheckBox.isSelected()); user.setComments(commentsTextArea.getText()); if (!passwordPasswordBox.getText().isEmpty() && passwordPasswordBox.getText().length() < 32) { try { MD5 md5 = MD5.getInstance(); this.user.setPassword(md5.hashData(new String( passwordPasswordBox.getText()).getBytes())); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex); } } return user; } public void setUser(User user) { this.user.setId(user.getId()); this.user.setName(user.getName()); this.user.setNif(user.getNif()); this.user.setAddress(user.getAddress()); this.user.setZipCode(user.getZipCode()); this.user.setCity(user.getCity()); this.user.setProvince(user.getProvince()); this.user.setCountry(user.getCountry()); this.user.setPhone(user.getPhone()); this.user.setCellPhone(user.getCellPhone()); this.user.setEmail(user.getEmail()); this.user.setUserName(user.getUserName()); this.user.setPassword(user.getPassword()); this.user.setAccessLevel(user.getAccessLevel()); this.user.setActive(user.isActive()); this.user.setComments(user.getComments()); nameTextBox.setText(user.getName()); nifTextBox.setText(user.getNif()); addressTextBox.setText(user.getAddress()); zipCodeTextBox.setText(user.getZipCode()); cityTextBox.setText(user.getCity()); provinceTextBox.setText(user.getProvince()); countryTextBox.setText(user.getCountry()); phoneTextBox.setText(user.getPhone()); cellPhoneTextBox.setText(user.getCellPhone()); emailTextBox.setText(user.getEmail()); userNameTextBox.setText(user.getUserName()); passwordPasswordBox.setText(user.getPassword()); accessLevelChoiceBox.getSelectionModel().select( user.getAccessLevel().getName()); userActiveCheckBox.setSelected(user.isActive()); commentsTextArea.setText(user.getComments()); /* * photoPanel = new PhotoPanel("Documentos/Usuarios/" + user.getId()); * photoPanel.setName("Fotografía"); documentPanel = new * DocumentList("Documentos/Usuarios/" + user.getId(), ""); * documentPanel.setName("Documentos"); mainDataPanel.add(photoPanel, * BorderLayout.LINE_END); dataTabbedPanel.add(documentPanel); */ returnStatus = RET_CANCEL; } public void start(Stage stage) { this.stage = stage; Group rootGroup = new Group(); Scene scene = new Scene(rootGroup, 800, 800); scene.setRoot(windowBorderPane); stage.initModality(Modality.APPLICATION_MODAL); stage.initOwner(parentStage); stage.setTitle("Edición de Usuarios"); stage.setResizable(true); stage.setScene(scene); stage.centerOnScreen(); stage.setVisible(true); } public static void main(String[] args) { Application.launch(args); } }