Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ua.com.ecotep.debtprevention; import java.io.IOException; import java.net.URL; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.ResourceBundle; import java.util.logging.Level; import java.util.logging.Logger; import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; import javafx.concurrent.Task; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.geometry.Point2D; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextField; import javafx.scene.control.TextFormatter; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.AnchorPane; import javafx.stage.Popup; import javafx.stage.Window; import javafx.util.converter.LocalDateStringConverter; import org.apache.commons.beanutils.converters.DateConverter; import ua.com.ecotep.debtprevention.dao.AccessDAO; import ua.com.ecotep.debtprevention.dao.DAOFactory; import ua.com.ecotep.debtprevention.dao.DebPrevDAO; import ua.com.ecotep.debtprevention.model.ClientBean; import ua.com.ecotep.debtprevention.model.DebPrevDetailBean; import ua.com.ecotep.debtprevention.model.DebPrevMainBean; import ua.com.ecotep.debtprevention.utils.CallbackInterface; import ua.com.ecotep.debtprevention.utils.CellDateBox; import ua.com.ecotep.debtprevention.utils.CellMoneyBox; import ua.com.ecotep.debtprevention.utils.DateConverters; import ua.com.ecotep.debtprevention.utils.SelectorInterface; import ua.com.rocketlv.customcontrol.TextMaxLengthFieldInstaller; import ua.com.rocketlv.messages.AlertDialog; /** * FXML Controller class * * @author rocketlv */ public class VnaklController implements Initializable, SelectorInterface<Object> { @FXML TextField dateField; @FXML TextField dateMaxField; @FXML TextField salerField; @FXML TextField clientField; @FXML TextField sumField; @FXML CheckBox groupBox; @FXML CheckBox rateBox; @FXML TableView<DebPrevDetailBean> invTable; @FXML TableView<DebPrevDetailBean> vnaklTable; @FXML Button saveButon; @FXML Button addDocButton; @FXML Button delDocButton; private DebPrevMainBean oldBean; private ClientBean selCl = null; private boolean semaphore = false; private boolean modeControl = false; private LocalDate currentDate; private CallbackInterface parentInterface; public void setParentInterface(CallbackInterface parentInterface) { this.parentInterface = parentInterface; this.initData(); } private void initData() { try { semaphore = true; if (parentInterface.getBean() != null) { oldBean = (DebPrevMainBean) parentInterface.getBean(); selCl = new ClientBean(oldBean.getIdCl(), oldBean.getClientDpm(), oldBean.getGroupDpm(), null, oldBean.getIdSalerDpm(), oldBean.getSalerDpm()); dateFieldFormatter.setValue(oldBean.getDateDpm()); dateMaxFieldFormatter.setValue(oldBean.getControlDpm()); salerField.setText(oldBean.getSalerDpm()); clientField.setText(oldBean.getClientDpm()); sumField.setText(String.format("%.2f ", oldBean.getSumDpm())); groupBox.setSelected(oldBean.getGroupDpm() > 0); rateBox.setSelected(oldBean.isRatecontractDpm()); fillAddTable(oldBean); semaphore = false; } else { sumField.setText("0.00 "); } } catch (Exception ex) { Logger.getLogger(VnaklController.class.getName()).log(Level.SEVERE, null, ex); AlertDialog.showSimpleMessage( " ` !", AlertDialog.ICON_ERROR, parentInterface.getCurrentWindow()); } } Popup selectorPopup; @FXML private void handleDocSelectorAction(ActionEvent event) { if (selCl == null) { AlertDialog.showSimpleMessage("? .", AlertDialog.ICON_INFO, parentInterface.getCurrentWindow()); return; } Task<Object> task = DPSession.getInstance().getTask(); if ((task != null) && (task.isRunning())) { AlertDialog.showSimpleMessage( "? ?, -? .", AlertDialog.ICON_FORBIDDEN, parentInterface.getCurrentWindow()); return; } Scene sceneParent = parentInterface.getCurrentWindow().getScene(); final Point2D windowCoord = new Point2D(sceneParent.getWindow().getX(), sceneParent.getWindow().getY()); final Point2D sceneCoord = new Point2D(sceneParent.getX(), sceneParent.getY()); final Point2D nodeCoord = addDocButton.localToScene(0.0, 0.0); final double coordX = Math.round(windowCoord.getX() + sceneCoord.getX() + nodeCoord.getX()); final double coordY = Math .round(windowCoord.getY() + sceneCoord.getY() + nodeCoord.getY() + addDocButton.getHeight() + 6); try { selectorPopup = new Popup(); selectorPopup.setAutoHide(true); selectorPopup.setAutoFix(true); selectorPopup.setHideOnEscape(true); FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("/fxml/DocSelectScene.fxml")); AnchorPane root = (AnchorPane) loader.load(); DocSelectController controller = loader.getController(); controller.setParentInterface(this); Scene scene = new Scene(root); scene.setFill(null); selectorPopup.getContent().add(root); selectorPopup.setX(coordX); selectorPopup.setY(coordY); selectorPopup.show(parentInterface.getCurrentWindow()); } catch (IOException ex) { Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex); AlertDialog.showSimpleMessage( "? ? !", AlertDialog.ICON_ERROR, parentInterface.getCurrentWindow()); } } @FXML private void handleDeleteDocAction(ActionEvent event) { if (vnaklTable.getSelectionModel().getSelectedIndex() > -1) { AlertDialog ad = new AlertDialog() { @Override public void actionPerformed(int selectedButton) throws Exception { if (selectedButton == AlertDialog.YES_BUTTON) { deleteDoc(); } } @Override public void onHide() { } }; ad.showQuestionDialog(parentInterface.getCurrentWindow(), " ?"); } } @FXML private void handleAutoSelectDocAction(ActionEvent event) { if (invTable.getItems().size() > 0) { AlertDialog ad = new AlertDialog() { @Override public void actionPerformed(int selectedButton) throws Exception { if (selectedButton == AlertDialog.YES_BUTTON) { fillAutoVnaklTable(); } } @Override public void onHide() { } }; ad.showQuestionDialog(parentInterface.getCurrentWindow(), " ?"); } } private void fillAutoVnaklTable() { try { DPSession sess = DPSession.getInstance(); DAOFactory dao = DAOFactory.getDAOFactory(DAOFactory.INTERBASE); DebPrevDAO dpDao = dao.getDebPrevDAO(); ObservableList<DebPrevDetailBean> dpdLst = dpDao.getAvailableVnaklList(oldBean); for (DebPrevDetailBean dpdBean : dpdLst) { boolean found = false; for (DebPrevDetailBean bean : vnaklTable.getItems()) { if (bean.getIdVn() == dpdBean.getIdVn()) { found = true; break; } } if (!found) { vnaklTable.getItems().add(dpdBean); } } if (vnaklTable.getItems().size() > 0) { vnaklTable.getSelectionModel().clearAndSelect(0); } } catch (Exception ex) { Logger.getLogger(VnaklController.class.getName()).log(Level.SEVERE, null, ex); AlertDialog.showSimpleMessage( "? . `? ! ", AlertDialog.ICON_ERROR, parentInterface.getCurrentWindow()); } finally { } } ObservableList<DebPrevDetailBean> delLst = FXCollections.observableArrayList(); private void deleteDoc() throws CloneNotSupportedException { DebPrevDetailBean dpdBean = vnaklTable.getSelectionModel().getSelectedItem(); if (dpdBean.getState() == DebPrevDetailBean.EXISTED_RECORD || dpdBean.getState() == DebPrevDetailBean.CHANGED_RECORD) { DebPrevDetailBean nBean = dpdBean.clone(); nBean.setState(DebPrevDetailBean.DELETED_RECORD); delLst.add(nBean); } vnaklTable.getItems().remove(dpdBean); } private void fillAddTable(DebPrevMainBean oldBean) { if (oldBean != null) { invTable.getItems().clear(); vnaklTable.getItems().clear(); double docsSum = 0.0; for (DebPrevDetailBean bean : oldBean.getDebPrevDetailInvLst()) { invTable.getItems().add(bean); docsSum += bean.getSum(); } if (oldBean.getDebPrevDetailInvLst().size() > 0) { invTable.getSelectionModel().clearAndSelect(0); } for (DebPrevDetailBean bean : oldBean.getDebPrevDetailVnaklLst()) { vnaklTable.getItems().add(bean); } if (oldBean.getDebPrevDetailVnaklLst().size() > 0) { vnaklTable.getSelectionModel().clearAndSelect(0); } this.sumField.setText(String.format("%.2f ", docsSum)); } else { invTable.getItems().clear(); vnaklTable.getItems().clear(); } } /** * Initializes the controller class. */ private TextFormatter dateFieldFormatter, dateMaxFieldFormatter; @Override public void initialize(URL url, ResourceBundle rb) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); dateFieldFormatter = new TextFormatter(new LocalDateStringConverter(dateTimeFormatter, null)); dateField.setTextFormatter(dateFieldFormatter); dateMaxFieldFormatter = new TextFormatter(new LocalDateStringConverter(dateTimeFormatter, null)); dateMaxField.setTextFormatter(dateMaxFieldFormatter); TableColumn numCol = new TableColumn("."); numCol.setPrefWidth(100); numCol.setCellValueFactory(new PropertyValueFactory<DebPrevDetailBean, String>("num")); invTable.getColumns().add(numCol); TableColumn dateCol = new TableColumn(""); dateCol.setPrefWidth(90); dateCol.setCellValueFactory(new PropertyValueFactory<DebPrevDetailBean, String>("date")); dateCol.setCellFactory(CellDateBox.forTableColumn(dateCol, CellDateBox.DATE_NORMAL_VIEW)); invTable.getColumns().add(dateCol); TableColumn sumCol = new TableColumn(""); sumCol.setPrefWidth(90); sumCol.setCellValueFactory(new PropertyValueFactory<DebPrevDetailBean, String>("sum")); sumCol.setCellFactory(CellMoneyBox.forTableColumn(sumCol, " ", 2)); invTable.getColumns().add(sumCol); invTable.autosize(); invTable.getItems().addListener(new ListChangeListener<DebPrevDetailBean>() { @Override public void onChanged(ListChangeListener.Change<? extends DebPrevDetailBean> c) { double sumDpm = 0; for (DebPrevDetailBean bean : c.getList()) { sumDpm += bean.getSum(); } sumField.setText(String.format("%.2f ", sumDpm)); if (sumDpm > 0) { saveButon.setDisable(false); } else { saveButon.setDisable(true); } } }); TableColumn numVCol = new TableColumn("."); numVCol.setPrefWidth(100); numVCol.setCellValueFactory(new PropertyValueFactory<DebPrevDetailBean, String>("num")); vnaklTable.getColumns().add(numVCol); TableColumn dateVCol = new TableColumn(""); dateVCol.setPrefWidth(90); dateVCol.setCellValueFactory(new PropertyValueFactory<DebPrevDetailBean, String>("date")); dateVCol.setCellFactory(CellDateBox.forTableColumn(dateVCol, CellDateBox.DATE_NORMAL_VIEW)); vnaklTable.getColumns().add(dateVCol); TableColumn sumVCol = new TableColumn(""); sumVCol.setPrefWidth(90); sumVCol.setCellValueFactory(new PropertyValueFactory<DebPrevDetailBean, String>("sum")); sumVCol.setCellFactory(CellMoneyBox.forTableColumn(sumVCol, " ", 2)); vnaklTable.getColumns().add(sumVCol); vnaklTable.autosize(); TextMaxLengthFieldInstaller mlControl = new TextMaxLengthFieldInstaller(); mlControl.install(clientField, 150); try { dateFieldFormatter.setValue(currentDate); dateMaxFieldFormatter.setValue(currentDate); } catch (Exception ex) { Logger.getLogger(FilterController.class.getName()).log(Level.SEVERE, null, ex); AlertDialog.showSimpleMessage(" ? !", AlertDialog.ICON_ERROR, parentInterface.getCurrentWindow()); } } @FXML public void handleCloseAction(ActionEvent event) { parentInterface.closeTab(); } @FXML private void handleSaveAction(ActionEvent event) { try { DPSession sess = DPSession.getInstance(); DAOFactory dao = DAOFactory.getDAOFactory(DAOFactory.INTERBASE); DebPrevDAO dpDao = dao.getDebPrevDAO(); DebPrevMainBean dpmBean = oldBean.clone(); dpmBean.getDebPrevDetailVnaklLst().clear(); dpmBean.getDebPrevDetailInvLst().clear(); for (DebPrevDetailBean bean : invTable.getItems()) { DebPrevDetailBean nBean = (DebPrevDetailBean) bean.clone(); dpmBean.getDebPrevDetailInvLst().add(nBean); } for (DebPrevDetailBean bean : vnaklTable.getItems()) { DebPrevDetailBean nBean = (DebPrevDetailBean) bean.clone(); dpmBean.getDebPrevDetailVnaklLst().add(nBean); } dpmBean.getDebPrevDetailVnaklLst().addAll(delLst); dpDao.updateDebPrevMain(dpmBean); parentInterface.setBean(oldBean, dpmBean, true); parentInterface.closeTab(); } catch (Exception ex) { Logger.getLogger(VnaklController.class.getName()).log(Level.SEVERE, null, ex); AlertDialog.showSimpleMessage( "? ? . ? ? ! ", AlertDialog.ICON_ERROR, parentInterface.getCurrentWindow()); } finally { } } @Override public Object getSelBean() { return selCl; } @Override public void setSelBean(Object newValue) { if (newValue != null) { if (newValue instanceof DebPrevDetailBean) { DebPrevDetailBean selBean = (DebPrevDetailBean) newValue; boolean canAdd = true; for (DebPrevDetailBean dtBean : vnaklTable.getItems()) { if (dtBean.getIdVn() == selBean.getIdVn()) { canAdd = false; break; } } if (canAdd) { vnaklTable.getItems().add(selBean); if (vnaklTable.getItems().size() == 1) { vnaklTable.getSelectionModel().select(0); } else { vnaklTable.getSelectionModel().select(selBean); vnaklTable.scrollTo(vnaklTable.getSelectionModel().getSelectedIndex()); } } else { AlertDialog.showSimpleMessage(" ? ?.", AlertDialog.ICON_INFO, parentInterface.getCurrentWindow()); } } } } @Override public Window getCurrentSelectorWindow() { return selectorPopup; } @Override public int getSelectorDataType() { return AccessDAO.CONSIGNMENT; } }