net.meddeb.md.admin.client.MenuPQParams.java Source code

Java tutorial

Introduction

Here is the source code for net.meddeb.md.admin.client.MenuPQParams.java

Source

package net.meddeb.md.admin.client;
/*--------------------------------------------------------------------
mdDiradmin, Merged data directory administration tool
Copyright (C) 2015, Abdelhamid MEDDEB (abdelhamid@meddeb.net)  
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------*/

import net.meddeb.md.admin.shared.LDAPServersDto;
import net.meddeb.md.common.shared.PQParamsDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MenuPQParams {
    private MainMsg mainMsg = GWT.create(MainMsg.class);
    PQParamsDto pqParams = new PQParamsDto();
    MenuLauncher menuLauncher = null;
    //Params
    private final Grid grid = new Grid(5, 5);
    private final Label lblParamsTitle = new Label(mainMsg.pqParamsTitle());
    private final Label lblUppers = new Label(mainMsg.lblUppers());
    private final Label lblLowers = new Label(mainMsg.lblLowers());
    private final Label lblDigits = new Label(mainMsg.lblDigits());
    private final Label lblSpecials = new Label(mainMsg.lblSpecials());
    private final Label lblForbiddens = new Label(mainMsg.lblForbiddens());
    private final TextBox edtUppers = new TextBox();
    private final TextBox edtLowers = new TextBox();
    private final TextBox edtDigits = new TextBox();
    private final TextBox edtSpecials = new TextBox();
    private final TextBox edtForbiddens = new TextBox();
    private final Button btnSave = new Button(mainMsg.save());
    private final Button btnCancel = new Button(mainMsg.cancel());
    private final HorizontalPanel btnPanel = new HorizontalPanel();
    private DecoratorPanel paramsPanel = new DecoratorPanel();
    private final VerticalPanel mainPanel = new VerticalPanel();
    //LDAP servers
    private final Label lblServersTitle = new Label(mainMsg.serverListTitle());
    private final Grid serversGrid = new Grid(6, 6);
    private final Button btnSynctest = new Button(mainMsg.syncTest());
    private final Button btnSyncsrv = new Button(mainMsg.syncSrv());

    private boolean isParamsModified() {
        int edtValue = Integer.parseInt(edtDigits.getText());
        boolean rslt = (pqParams.getDigit() != edtValue);
        if (rslt)
            return rslt;
        edtValue = Integer.parseInt(edtLowers.getText());
        rslt = (pqParams.getLower() != edtValue);
        if (rslt)
            return rslt;
        edtValue = Integer.parseInt(edtUppers.getText());
        rslt = (pqParams.getUpper() != edtValue);
        if (rslt)
            return rslt;
        edtValue = Integer.parseInt(edtSpecials.getText());
        rslt = (pqParams.getSpecial() != edtValue);
        if (rslt)
            return rslt;
        rslt = (!pqParams.getForbidden().equalsIgnoreCase(edtForbiddens.getText()));
        return rslt;
    }

    private void setControlStatus() {
        boolean edtModified = isParamsModified();
        if (edtModified)
            menuLauncher.showHelp(mainMsg.editPqSettings());
        btnSave.setEnabled(edtModified);
        btnCancel.setEnabled(edtModified);
    }

    private void edtToParamsDto() {
        pqParams.setDigit(edtDigits.getText());
        pqParams.setUpper(edtUppers.getText());
        pqParams.setLower(edtLowers.getText());
        pqParams.setSpecial(edtSpecials.getText());
        pqParams.setForbidden(edtForbiddens.getText());
    }

    private void paramsDtoToEdt() {
        edtDigits.setText(pqParams.getDigitStr());
        edtUppers.setText(pqParams.getUpperStr());
        edtLowers.setText(pqParams.getLowerStr());
        edtSpecials.setText(pqParams.getSpecialStr());
        edtForbiddens.setText(pqParams.getForbidden());
    }

    private void setParams(PQParamsDto p) {
        pqParams.setDigit(p.getDigit());
        pqParams.setUpper(p.getUpper());
        pqParams.setLower(p.getLower());
        pqParams.setSpecial(p.getSpecial());
        pqParams.setForbidden(p.getForbidden());
    }

    private PQParamsDto getParamsFromEdt() {
        PQParamsDto rslt = new PQParamsDto();
        rslt.setDigit(edtDigits.getText());
        rslt.setUpper(edtUppers.getText());
        rslt.setLower(edtLowers.getText());
        rslt.setSpecial(edtSpecials.getText());
        rslt.setForbidden(edtForbiddens.getText());
        return rslt;
    }

    private void showLdapServerStatus(LDAPServersDto serverList) {
        DateTimeFormat df = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_TIME_MEDIUM);
        String strTime = "";
        String strCheck = "";
        for (int i = 0; i < serverList.size(); i++) {
            serversGrid.setText(i + 1, 0, serverList.getFullname(i));
            if (serverList.isSync(i)) {
                strCheck = "";
                serversGrid.getCellFormatter().setStylePrimaryName(i + 1, 1, "cellOk");
            } else {
                strCheck = "";
                serversGrid.getCellFormatter().setStylePrimaryName(i + 1, 1, "cellKo");
            }
            serversGrid.setText(i + 1, 1, strCheck);
            strTime = df.format(serverList.getSynctime(i));
            serversGrid.setText(i + 1, 2, strTime);
            if (serverList.isTest(i)) {
                strCheck = "";
                serversGrid.getCellFormatter().setStylePrimaryName(i + 1, 3, "cellOk");
            } else {
                strCheck = "";
                serversGrid.getCellFormatter().setStylePrimaryName(i + 1, 3, "cellKo");
            }
            serversGrid.setText(i + 1, 3, strCheck);
            strTime = df.format(serverList.getTesttime(i));
            serversGrid.setText(i + 1, 4, strTime);
            strTime = df.format(serverList.getLastsuccess(i));
            serversGrid.setText(i + 1, 5, strTime);
        }
    }

    private ClickHandler btnSaveParamsClickHandler = new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            setEditStatus(false);
            menuLauncher.showHelp(mainMsg.saving());
            PQParamsDto pqpDto = getParamsFromEdt();
            PasswordPolicySettings.Access.getInstance().setPQParameters(pqpDto, setPQParamsCallBack);
        }
    };

    private ClickHandler btnCancelParamsClickHandler = new ClickHandler() {
        @Override
        public void onClick(ClickEvent arg0) {
            paramsDtoToEdt();
            setControlStatus();
            menuLauncher.showHelp(mainMsg.editPqSettings());
        }
    };

    private ClickHandler btnSynctestClickHandler = new ClickHandler() {
        @Override
        public void onClick(ClickEvent arg0) {
            setBtnsyncStatus(false);
            menuLauncher.showHelp(mainMsg.testing());
            PasswordPolicySettings.Access.getInstance().testPQParametersSync(testLdapServerSyncCallBack);
        }
    };

    private ClickHandler btnSyncsrvClickHandler = new ClickHandler() {
        @Override
        public void onClick(ClickEvent arg0) {
            setBtnsyncStatus(false);
            menuLauncher.showHelp(mainMsg.synchronizing());
            PasswordPolicySettings.Access.getInstance().setPQParameters(null, syncLdapServersCallBack);
        }
    };

    private AsyncCallback<PQParamsDto> getPQParamsCallBack = new AsyncCallback<PQParamsDto>() {
        @Override
        public void onFailure(Throwable e) {
            menuLauncher.showWarning(mainMsg.serverError());
        }

        @Override
        public void onSuccess(PQParamsDto pqParams) {
            setParams(pqParams);
            paramsDtoToEdt();
            setControlStatus();
            menuLauncher.showHelp(mainMsg.editPqSettings());
        }
    };

    private AsyncCallback<Boolean> dataAccessibleCallback = new AsyncCallback<Boolean>() {
        @Override
        public void onFailure(Throwable arg0) {
            setEditStatus(false);
            menuLauncher.showWarning(mainMsg.serverError());
        }

        @Override
        public void onSuccess(Boolean dataAccessible) {
            if (dataAccessible) {
                setEditStatus(true);
                PasswordPolicySettings.Access.getInstance().getPQParameters(getPQParamsCallBack);
            } else {
                setEditStatus(false);
                menuLauncher.showHelp(mainMsg.cannotAccessData());
            }
        }
    };

    private AsyncCallback<LDAPServersDto> setPQParamsCallBack = new AsyncCallback<LDAPServersDto>() {
        @Override
        public void onFailure(Throwable e) {
            menuLauncher.showWarning(mainMsg.serverError());
            setEditStatus(true);
        }

        @Override
        public void onSuccess(LDAPServersDto serverList) {
            setEditStatus(true);
            if (serverList != null) {
                edtToParamsDto();
                menuLauncher.showHelp(mainMsg.editPqSettingsSaved());
                if (serverList.size() == 0) {
                } else {
                    showLdapServerStatus(serverList);
                }
            } else
                menuLauncher.showWarning(mainMsg.serverError());
        }
    };

    private AsyncCallback<LDAPServersDto> testLdapServerSyncCallBack = new AsyncCallback<LDAPServersDto>() {
        @Override
        public void onFailure(Throwable e) {
            setBtnsyncStatus(true);
            menuLauncher.showHelp(mainMsg.serverError());
            menuLauncher.showWarning(mainMsg.serverError());
        }

        @Override
        public void onSuccess(LDAPServersDto serverList) {
            setBtnsyncStatus(true);
            menuLauncher.showHelp(mainMsg.testDone());
            if (serverList != null) {
                showLdapServerStatus(serverList);
            } else
                menuLauncher.showWarning(mainMsg.serverError());
        }

    };

    private AsyncCallback<LDAPServersDto> syncLdapServersCallBack = new AsyncCallback<LDAPServersDto>() {
        @Override
        public void onFailure(Throwable e) {
            setBtnsyncStatus(true);
            menuLauncher.showHelp(mainMsg.serverError());
            menuLauncher.showWarning(mainMsg.serverError());
        }

        @Override
        public void onSuccess(LDAPServersDto serverList) {
            setBtnsyncStatus(true);
            if (serverList != null) {
                menuLauncher.showHelp(mainMsg.syncDone());
                showLdapServerStatus(serverList);
            } else
                menuLauncher.showWarning(mainMsg.serverError());
        }
    };

    private AsyncCallback<LDAPServersDto> getLDAPServers = new AsyncCallback<LDAPServersDto>() {
        @Override
        public void onFailure(Throwable e) {
        }

        @Override
        public void onSuccess(LDAPServersDto serverList) {
            if (serverList.size() > 0) {
                showLdapServerStatus(serverList);
            }
        }
    };

    private KeyPressHandler edtDigitKeyPressHandler = new KeyPressHandler() {
        @Override
        public void onKeyPress(KeyPressEvent event) {
            int edtLength = ((TextBox) event.getSource()).getText().trim().length();
            if ((edtLength >= 2) || (!Character.isDigit(event.getCharCode())))
                ((TextBox) event.getSource()).cancelKey();
        }
    };

    private KeyUpHandler edtKeyUPHandler = new KeyUpHandler() {
        @Override
        public void onKeyUp(KeyUpEvent evnt) {
            setControlStatus();
        }
    };

    private BlurHandler edtFocusListener = new BlurHandler() {
        @Override
        public void onBlur(BlurEvent event) {
            TextBox edtBox = (TextBox) event.getSource();
            if (edtBox.getText().trim().isEmpty())
                edtBox.setText("0");
        }
    };

    private void setEditStatus(boolean active) {
        edtDigits.setEnabled(active);
        edtForbiddens.setEnabled(active);
        edtLowers.setEnabled(active);
        edtSpecials.setEnabled(active);
        edtUppers.setEnabled(active);
        if (!active) {
            btnCancel.setEnabled(active);
            btnSave.setEnabled(active);
        }
        setBtnsyncStatus(active);
    }

    private void setBtnsyncStatus(boolean active) {
        btnSyncsrv.setEnabled(active);
        btnSynctest.setEnabled(active);
    }

    private void initParamsGrid() {
        grid.setWidth("100%");
        grid.setCellSpacing(5);
        grid.getColumnFormatter().setWidth(0, "35%");
        grid.getColumnFormatter().setWidth(1, "5%");
        grid.getColumnFormatter().setWidth(2, "20%");
        grid.getColumnFormatter().setWidth(3, "35%");
        grid.getColumnFormatter().setWidth(4, "5%");
        grid.getCellFormatter().getElement(3, 1).setAttribute("colSpan", "4");
        grid.getCellFormatter().getElement(4, 1).setAttribute("colSpan", "4");
        grid.getCellFormatter().setAlignment(4, 0, HasHorizontalAlignment.ALIGN_LEFT,
                HasVerticalAlignment.ALIGN_MIDDLE);

        grid.setWidget(1, 0, lblUppers);
        grid.setWidget(2, 0, lblLowers);
        grid.setWidget(3, 0, lblForbiddens);
        grid.setWidget(1, 1, edtUppers);
        grid.setWidget(2, 1, edtLowers);

        grid.setWidget(1, 3, lblDigits);
        grid.setWidget(2, 3, lblSpecials);
        grid.setWidget(3, 1, edtForbiddens);
        grid.setWidget(1, 4, edtDigits);
        grid.setWidget(2, 4, edtSpecials);

        grid.setWidget(4, 0, btnPanel);
    }

    private void initParamsEdt() {
        lblParamsTitle.setStyleName("sectiontitle");
        edtUppers.setWidth("100%");
        edtLowers.setWidth("100%");
        edtDigits.setWidth("100%");
        edtSpecials.setWidth("100%");
        edtForbiddens.setWidth("100%");

        edtUppers.addKeyPressHandler(edtDigitKeyPressHandler);
        edtUppers.addKeyUpHandler(edtKeyUPHandler);
        edtUppers.addBlurHandler(edtFocusListener);
        edtLowers.addKeyPressHandler(edtDigitKeyPressHandler);
        edtLowers.addKeyUpHandler(edtKeyUPHandler);
        edtLowers.addBlurHandler(edtFocusListener);
        edtDigits.addKeyPressHandler(edtDigitKeyPressHandler);
        edtDigits.addKeyUpHandler(edtKeyUPHandler);
        edtDigits.addBlurHandler(edtFocusListener);
        edtSpecials.addKeyPressHandler(edtDigitKeyPressHandler);
        edtSpecials.addKeyUpHandler(edtKeyUPHandler);
        edtSpecials.addBlurHandler(edtFocusListener);
        edtForbiddens.addKeyUpHandler(edtKeyUPHandler);
    }

    private void initParamsButtons() {
        btnSave.addClickHandler(btnSaveParamsClickHandler);
        btnCancel.addClickHandler(btnCancelParamsClickHandler);
        btnPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
        btnPanel.setSpacing(4);
        btnPanel.add(btnCancel);
        btnPanel.add(btnSave);
    }

    private void initParams() {
        paramsPanel.addStyleName("decPanels");
        paramsPanel.setWidth("720px");
        paramsPanel.add(grid);
        mainPanel.setSpacing(5);
        mainPanel.setWidth("100%");
        mainPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
        mainPanel.add(lblParamsTitle);
        mainPanel.add(paramsPanel);
    }

    private void initServerList() {
        lblServersTitle.setStyleName("sectiontitle");
        serversGrid.setWidth("100%");
        serversGrid.setCellSpacing(1);
        serversGrid.getColumnFormatter().setWidth(0, "30%");
        serversGrid.getColumnFormatter().setWidth(1, "8%");
        serversGrid.getColumnFormatter().setStyleName(1, "colCenter");
        serversGrid.getColumnFormatter().setWidth(2, "18%");
        serversGrid.getColumnFormatter().setWidth(3, "8%");
        serversGrid.getColumnFormatter().setStyleName(3, "colCenter");
        serversGrid.getColumnFormatter().setWidth(4, "18%");
        serversGrid.getColumnFormatter().setWidth(5, "18%");
        serversGrid.getRowFormatter().setStyleName(0, "tabTitle");
        serversGrid.setText(0, 0, mainMsg.serverTitle());
        serversGrid.setText(0, 1, mainMsg.synchronizedTitle());
        serversGrid.setText(0, 2, mainMsg.lastSyncTitle());
        serversGrid.setText(0, 3, mainMsg.testStatusTitle());
        serversGrid.setText(0, 4, mainMsg.lastTestTitle());
        serversGrid.setText(0, 5, mainMsg.lastSuccessTitle());
        btnSynctest.addClickHandler(btnSynctestClickHandler);
        btnSyncsrv.addClickHandler(btnSyncsrvClickHandler);
        btnSynctest.setWidth("100%");
        btnSyncsrv.setWidth("100%");
        serversGrid.setWidget(5, 4, btnSynctest);
        serversGrid.setWidget(5, 2, btnSyncsrv);
        mainPanel.add(lblServersTitle);
        mainPanel.add(serversGrid);
        PasswordPolicySettings.Access.getInstance().getLDAPServers(getLDAPServers);
    }

    public MenuPQParams(MenuLauncher menuLauncher) {
        this.menuLauncher = menuLauncher;
        initParamsGrid();
        initParamsEdt();
        initParamsButtons();
        initParams();
        initServerList();
    }

    public void show() {
        RootPanel.get("main").clear();
        RootPanel.get("main").add(mainPanel);
        setEditStatus(false);
        PasswordPolicySettings.Access.getInstance().isDataAccessible(dataAccessibleCallback);
    }

    public void hide() {
        RootPanel.get("main").clear();
    }

}