net.gvcc.jgoffice.dbnavigatorbar.DbNavigatorBar.java Source code

Java tutorial

Introduction

Here is the source code for net.gvcc.jgoffice.dbnavigatorbar.DbNavigatorBar.java

Source

/*
 * Copyright (C) 2013 SGV
 *
 * 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, or
 * (at your option) any later version.
 *
 * 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/>.
 */

package net.gvcc.jgoffice.dbnavigatorbar;

import net.gvcc.jgoffice.dbnavigatorbar.interfaces.IDBNavigatorForm;
import net.gvcc.jgoffice.dbnavigatorbar.interfaces.INavigationBar;
import net.gvcc.jgoffice.dbnavigatorbar.interfaces.INavigatorAction;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.event.ShortcutAction.ModifierKey;
import com.vaadin.server.ThemeResource;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Field;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import java.util.Collection;
import org.vaadin.dialogs.ConfirmDialog;

/**
 * Creates a Navigationbar. <br />
 *
 * @param none
 */
public class DbNavigatorBar extends CustomComponent implements INavigationBar {

    private static final long serialVersionUID = 1L;
    private HorizontalLayout mainLayout;

    private NavigatorButton btnFirst = new NavigatorButton(NavigatorButtons.FIRST,
            new ThemeResource("img/db_first.png"));
    private NavigatorButton btnPrevious = new NavigatorButton(NavigatorButtons.PREVIOUS,
            new ThemeResource("img/db_previous.png"));
    private NavigatorButton btnNext = new NavigatorButton(NavigatorButtons.NEXT,
            new ThemeResource("img/db_next.png"));
    private NavigatorButton btnLast = new NavigatorButton(NavigatorButtons.LAST,
            new ThemeResource("img/db_last.png"));
    private NavigatorButton btnInsert = new NavigatorButton(NavigatorButtons.INSERT,
            new ThemeResource("img/db_insert.png"));
    private NavigatorButton btnDelete = new NavigatorButton(NavigatorButtons.DELETE,
            new ThemeResource("img/db_delete.png"));
    private NavigatorButton btnPost = new NavigatorButton(NavigatorButtons.POST,
            new ThemeResource("img/db_post.png"));
    private NavigatorButton btnEdit = new NavigatorButton(NavigatorButtons.EDIT,
            new ThemeResource("img/db_edit.png"));
    private NavigatorButton btnRefresh = new NavigatorButton(NavigatorButtons.REFRESH,
            new ThemeResource("img/db_last.png"));
    private NavigatorButton btnCancel = new NavigatorButton(NavigatorButtons.CANCEL,
            new ThemeResource("img/db_cancel.png"));

    private JPAContainer<?> container;
    private FieldGroup fieldGroup;

    private Object selectedItemId = null;

    private DBOperations currentDbOperation = DBOperations.NONE;
    private DBOperations previousOperations = null;

    public DbNavigatorBar() {

        buildMainLayout();

        mainLayout.addComponent(btnFirst);
        mainLayout.addComponent(btnPrevious);
        mainLayout.addComponent(btnNext);
        mainLayout.addComponent(btnLast);
        mainLayout.addComponent(btnInsert);
        mainLayout.addComponent(btnDelete);
        mainLayout.addComponent(btnEdit);
        mainLayout.addComponent(btnPost);
        mainLayout.addComponent(btnCancel);
        mainLayout.addComponent(btnRefresh);

        btnFirst.setButtonAction(new FirstAction());
        btnPrevious.setButtonAction(new PreviousAction());
        btnNext.setButtonAction(new NextAction());
        btnLast.setButtonAction(new LastAction());

        btnInsert.setButtonAction(new InsertAction());
        btnDelete.setButtonAction(new DeleteAction());

        btnEdit.setButtonAction(new EditAction());

        btnRefresh.setButtonAction(new RefreshAction());
        btnCancel.setButtonAction(new CancelAction());

        btnPost.setButtonAction(new PostAction());

        btnFirst.setClickShortcut(KeyCode.ARROW_LEFT, ModifierKey.CTRL);
        btnPrevious.setClickShortcut(KeyCode.ARROW_UP, null);
        btnNext.setClickShortcut(KeyCode.ARROW_DOWN, null);
        btnLast.setClickShortcut(KeyCode.ARROW_RIGHT, ModifierKey.CTRL);
        btnDelete.setClickShortcut(KeyCode.DELETE, null);
        btnPost.setClickShortcut(KeyCode.F9, null);

        mainLayout.setMargin(new MarginInfo(false, false, true, false));

        setCompositionRoot(mainLayout);
    }

    public DbNavigatorBar(NavigatorButtons... activeButtons) {
        buildMainLayout();
        for (NavigatorButtons navigatorButtons : activeButtons) {
            switch (navigatorButtons) {
            case FIRST:
                mainLayout.addComponent(btnFirst);
                btnFirst.setClickShortcut(KeyCode.ARROW_LEFT, ModifierKey.CTRL);
                break;
            case PREVIOUS:
                mainLayout.addComponent(btnPrevious);
                btnPrevious.setClickShortcut(KeyCode.ARROW_UP, null);
                break;
            case NEXT:
                mainLayout.addComponent(btnNext);
                btnNext.setClickShortcut(KeyCode.ARROW_DOWN, null);
                break;
            case LAST:
                mainLayout.addComponent(btnLast);
                btnLast.setClickShortcut(KeyCode.ARROW_RIGHT, ModifierKey.CTRL);
                break;
            case INSERT:
                mainLayout.addComponent(btnInsert);

                break;
            case DELETE:
                mainLayout.addComponent(btnDelete);
                btnDelete.setClickShortcut(KeyCode.DELETE, null);
                break;
            case POST:
                mainLayout.addComponent(btnPost);
                btnPost.setButtonAction(new PostAction());
                btnPost.setClickShortcut(KeyCode.F9, null);
                break;
            case EDIT:
                mainLayout.addComponent(btnEdit);
                break;
            case CANCEL:
                mainLayout.addComponent(btnCancel);
                break;
            case REFRESH:
                mainLayout.addComponent(btnRefresh);
                break;
            default:
                break;
            }
        }
        mainLayout.setMargin(new MarginInfo(false, false, true, false));
        setCompositionRoot(mainLayout);
    }

    @Override
    public void attach() {

        super.attach();
    }

    /**
     * Abhngig vom
     * <br />
     *
     * @param status Reflects the actual DB Operation
     */
    public void changeButtonStatus(DBOperations status) {
        switch (status) {
        case INSERT:
            btnFirst.setactive(false);
            btnPrevious.setactive(false);
            btnNext.setactive(false);
            btnLast.setactive(false);
            btnDelete.setactive(false);
            btnEdit.setactive(false);
            btnPost.setactive(true);
            btnCancel.setactive(true);
            btnRefresh.setactive(false);
            break;
        case NONE:
            btnFirst.setactive(true);
            btnPrevious.setactive(true);
            btnNext.setactive(true);
            btnLast.setactive(true);
            btnDelete.setactive(true);
            btnEdit.setactive(true);
            btnInsert.setactive(true);
            btnPost.setactive(false);
            btnCancel.setactive(false);
            btnRefresh.setactive(true);
            break;
        case EDIT:
            btnFirst.setactive(false);
            btnPrevious.setactive(false);
            btnNext.setactive(false);
            btnLast.setactive(false);
            btnDelete.setactive(false);
            btnEdit.setactive(true);
            btnInsert.setactive(false);
            btnPost.setactive(true);
            btnCancel.setactive(true);
            btnRefresh.setactive(false);
            break;
        case FIRST:
            btnFirst.setactive(false);
            btnPrevious.setactive(false);
            btnNext.setactive(true);
            btnLast.setactive(true);
            btnDelete.setactive(true);
            btnEdit.setactive(true);
            btnInsert.setactive(true);
            btnPost.setactive(false);
            btnCancel.setactive(false);
            btnRefresh.setactive(true);
            break;
        case LAST:
            btnFirst.setactive(true);
            btnPrevious.setactive(true);
            btnNext.setactive(false);
            btnLast.setactive(false);
            btnDelete.setactive(true);
            btnEdit.setactive(true);
            btnInsert.setactive(true);
            btnPost.setactive(false);
            btnCancel.setactive(false);
            btnRefresh.setactive(true);
            break;
        default:
            btnFirst.setactive(true);
            btnPrevious.setactive(true);
            btnNext.setactive(true);
            btnLast.setactive(true);
            btnDelete.setactive(true);
            btnEdit.setactive(true);
            btnInsert.setactive(true);
            btnPost.setactive(false);
            btnCancel.setactive(false);
            btnRefresh.setactive(true);
            break;
        }
    }

    private void buildMainLayout() {
        mainLayout = new HorizontalLayout();
        mainLayout.setImmediate(false);
        mainLayout.setWidth("-1px");
        mainLayout.setHeight("-1px");
        mainLayout.setMargin(false);
        mainLayout.setSpacing(true);

        setWidth("-1px");
        setHeight("-1px");
    }

    public void setDatasource(JPAContainer<?> container, FieldGroup fields) {
        this.setContainer(container);
        this.setFieldGroup(fields);
        this.setSelectedItemId(container.firstItemId());
    }

    public void setAction(NavigatorButtons button, INavigatorAction action) {
        switch (button) {
        case FIRST:
            btnFirst.setButtonAction(action);
            break;
        case PREVIOUS:
            btnPrevious.setButtonAction(action);
            break;
        case NEXT:
            btnNext.setButtonAction(action);
            break;
        case LAST:
            btnLast.setButtonAction(action);
            break;
        case INSERT:
            btnInsert.setButtonAction(action);
            break;
        case DELETE:
            btnDelete.setButtonAction(action);
            break;
        case EDIT:
            btnEdit.setButtonAction(action);
            break;
        case POST:
            btnPost.setButtonAction(action);
            break;
        case REFRESH:
            btnRefresh.setButtonAction(action);
        case CANCEL:
            btnCancel.setButtonAction(action);
            break;
        default:
            break;
        }
    }

    public JPAContainer<?> getContainer() {
        return container;
    }

    public void setContainer(JPAContainer<?> container) {
        this.container = container;
    }

    public FieldGroup getFieldGroup() {
        return fieldGroup;
    }

    public void setFieldGroup(FieldGroup fieldGroup) {
        this.fieldGroup = fieldGroup;
    }

    public DBOperations getCurrentDbOperation() {
        return currentDbOperation;
    }

    public void setCurrentDbOperation(DBOperations currentDbOperation) {
        this.currentDbOperation = currentDbOperation;
    }

    public void setNavigatorButtonStatus(DBOperations operation) {
        boolean readonly = true;
        switch (operation) {
        case FIRST:
        case PREVIOUS:
        case NEXT:
        case LAST:
        case POST:
        case REFRESH:
        case NONE:
            readonly = true;
            break;
        case EDIT:
        case INSERT:
        case DELETE:
            readonly = false;
            break;
        }

        container.setReadOnly(readonly);
        fieldGroup.setReadOnly(readonly);

        currentDbOperation = operation;

        this.changeButtonStatus(currentDbOperation);
    }

    public Object getSelectedItemId() {
        return selectedItemId;
    }

    public void setSelectedItemId(Object selectedItemId) {
        this.selectedItemId = selectedItemId;
    }

    @Override
    public void addForm(IDBNavigatorForm form) {
        formList.add(form);
    }

    @Override
    public void removeForm(IDBNavigatorForm form) {
        formList.remove(form);
    }

    @Override
    public void notifyForm(NavigatorButtons button) {
        for (IDBNavigatorForm form : formList) {
            form.handleButtonEvents(button);
        }
    }

    /**
     * Navigate to the first record
     */
    private class FirstAction implements INavigatorAction {

        @Override
        public void navigatorActionPerformed() {
            setNavigatorButtonStatus(DBOperations.FIRST);
            selectedItemId = container.firstItemId();
            fieldGroup.setItemDataSource(container.getItem(container.firstItemId()));

            notifyForm(NavigatorButtons.FIRST);
        }

    }

    /*
     * Navigate to the previous record
     */
    private class PreviousAction implements INavigatorAction {

        @Override
        public void navigatorActionPerformed() {
            setNavigatorButtonStatus(DBOperations.NONE);

            selectedItemId = container.prevItemId(selectedItemId);

            if (selectedItemId == null) {
                selectedItemId = container.firstItemId();
                setNavigatorButtonStatus(DBOperations.FIRST);
            }

            fieldGroup.setItemDataSource(container.getItem(selectedItemId));

            notifyForm(NavigatorButtons.PREVIOUS);
        }

    }

    /*
     * Navigate to the next record
     */
    private class NextAction implements INavigatorAction {

        @Override
        public void navigatorActionPerformed() {
            setNavigatorButtonStatus(DBOperations.NONE);

            selectedItemId = container.nextItemId(selectedItemId);

            if (selectedItemId == null) {
                selectedItemId = container.lastItemId();
                setNavigatorButtonStatus(DBOperations.LAST);
            }

            fieldGroup.setItemDataSource(container.getItem(selectedItemId));

            notifyForm(NavigatorButtons.NEXT);

        }

    }

    /*
     * Navigate to the last record
     */
    private class LastAction implements INavigatorAction {

        @Override
        public void navigatorActionPerformed() {
            setNavigatorButtonStatus(DBOperations.LAST);

            selectedItemId = container.lastItemId();
            fieldGroup.setItemDataSource(container.getItem(selectedItemId));

            notifyForm(NavigatorButtons.LAST);
        }

    }

    /*
     * Insert a new record
     * The newly created record will be saved in the PostAction
     */
    private class InsertAction<T> implements INavigatorAction {

        @Override
        public void navigatorActionPerformed() {
            previousOperations = currentDbOperation;

            setNavigatorButtonStatus(DBOperations.INSERT);
            Collection<Field<?>> fields = fieldGroup.getFields();

            for (Field<?> field : fields) {
                if (field instanceof TextField) {
                    ((TextField) field).setValue("");
                } else {
                    field.setValue(null);
                }
            }

            notifyForm(NavigatorButtons.INSERT);

        }
    }

    /*
     * Delete record
     */
    private class DeleteAction implements INavigatorAction {

        @Override
        public void navigatorActionPerformed() {
            if (selectedItemId != null) {
                setNavigatorButtonStatus(DBOperations.DELETE);
                previousOperations = currentDbOperation;

                StringBuilder sb = new StringBuilder();
                sb.append("Record ");
                sb.append(" will be deleted. Continue?");

                ConfirmDialog.show(getUI(), "Confirm...", sb.toString(), "Continue", "Abort",
                        new ConfirmDialog.Listener() {
                            @Override
                            public void onClose(ConfirmDialog arg0) {
                                if (arg0.isConfirmed()) {

                                    container.removeItem(selectedItemId);
                                    fieldGroup.setItemDataSource(
                                            container.getItem(container.nextItemId(selectedItemId)));

                                    notifyForm(NavigatorButtons.DELETE);
                                }

                                setNavigatorButtonStatus(previousOperations);
                                previousOperations = null;

                            }

                        });
            }
        }

    }

    private class EditAction implements INavigatorAction {

        @Override
        public void navigatorActionPerformed() {
            previousOperations = currentDbOperation;
            setNavigatorButtonStatus(DBOperations.EDIT);
            notifyForm(NavigatorButtons.EDIT);
        }

    }

    /*
     * Refresh
     */
    private class RefreshAction implements INavigatorAction {

        @Override
        public void navigatorActionPerformed() {
            setNavigatorButtonStatus(DBOperations.REFRESH);

            container.refresh();
            fieldGroup.setItemDataSource(fieldGroup.getItemDataSource());

            notifyForm(NavigatorButtons.REFRESH);
        }

    }

    /*
     * Cancel
     */
    private class CancelAction implements INavigatorAction {

        @Override
        public void navigatorActionPerformed() {

            ConfirmDialog.show(getUI(), "Confirm...", "All changes are reversed.", "Continue", "Abort",
                    new ConfirmDialog.Listener() {
                        @Override
                        public void onClose(ConfirmDialog arg0) {
                            setNavigatorButtonStatus(previousOperations);
                            previousOperations = null;

                            container.discard();
                            container.refresh();
                            fieldGroup.setItemDataSource(fieldGroup.getItemDataSource());

                            notifyForm(NavigatorButtons.CANCEL);

                            Notification.show("Operation canceled", Notification.Type.TRAY_NOTIFICATION);
                        }

                    });

        }

    }

    private class PostAction implements INavigatorAction {

        @Override
        public void navigatorActionPerformed() {

            try {

                switch (currentDbOperation) {
                case EDIT:
                    try {
                        fieldGroup.commit();
                        Notification.show("Record edited successfully", Notification.Type.TRAY_NOTIFICATION);
                    } catch (FieldGroup.CommitException e) {
                        Notification.show("Couldn't commit values: " + e.getCause().getMessage(),
                                Notification.Type.ERROR_MESSAGE);
                    } finally {
                        setNavigatorButtonStatus(DBOperations.NONE);
                    }
                    break;
                default:
                    break;
                }

            } finally {
                notifyForm(NavigatorButtons.POST);
            }

        }

    }

}