# AcquiredLocksView.java

    /*
    ** Module   : AcquiredLocksView.java
    ** Abstract : AcquiredLocksView is a view to manage acquired locks.
    **
    ** Copyright (c) 2017, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ----------------Description----------------------
    ** 001 SBI 20170601 Created initial version.
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero 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 Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.admin.client.application.home.console.acquiredlocks;

    import java.util.Set;

    import org.gwtbootstrap3.client.ui.Button;
    import org.gwtbootstrap3.client.ui.gwt.DataGrid;

    import com.goldencode.p2j.admin.RecordInfo;
    import com.goldencode.p2j.admin.client.application.home.BaseViewWithUiHandlers;
    import com.goldencode.p2j.admin.client.application.home.console.acquiredlocks.AcquiredLocksPresenter.AcquiredLocksViewStates;
    import com.goldencode.p2j.admin.client.widget.GridHandle;
    import com.goldencode.p2j.admin.client.widget.GridHelper;
    import com.goldencode.p2j.admin.client.widget.GridHelper.ColumnInfo;
    import com.goldencode.p2j.admin.client.widget.dialog.InputDialog;
    import com.google.gwt.event.dom.client.ClickEvent;
    import com.google.gwt.uibinder.client.*;
    import com.google.gwt.user.client.ui.*;
    import com.google.gwt.view.client.MultiSelectionModel;
    import com.google.gwt.view.client.SelectionChangeEvent;
    import com.google.inject.*;

    /**
     * A view to manage acquired locks.
     */
    public class AcquiredLocksView
    extends BaseViewWithUiHandlers<AcquiredLocksUIHandler>
    implements AcquiredLocksPresenter.MyView
    {
       /**
        * GWT UI creator.
        */
       interface Binder
       extends UiBinder<Widget, AcquiredLocksView>
       {}

       /** The acquired locks table view */
       @UiField(provided = true)
       DataGrid<RecordInfo> dataGrid = GridHelper.<RecordInfo>create();

       /** The Lock button */
       @UiField
       Button lockRecords;

       /** The Unlock button */
       @UiField
       Button unlockRecords;

       /** The Add record button */
       @UiField
       Button addRecord;

       /** The Delete selected button */
       @UiField
       Button deleteSelected;

       /** The Delete All button */
       @UiField
       Button deleteAllRecords;

       /** The modal container */
       @UiField
       HasWidgets modalFragment;

       /** The common input dialogs provider */
       private final Provider<InputDialog> inputDialogProvider;

       /** Column names. */
       private final String[] columnNames =
       {
          "Record ID",
          "Database",
          "Table Name"
       };

       /** The acquired locks grid handle */
       private final GridHandle<RecordInfo> gridHandle;

       /** The acquired locks selection model */
       private final MultiSelectionModel<RecordInfo> selectionModel;

       /** The Add Record Lock View */
       private final AddRecordLockView addRecordLockView;

       /**
        * Constructs this view, used by MPV gwtplatform of ArcBees Inc.
        *
        * @param    binder
        *           The injected GWT UI creator
        * @param    inputDialogProvider
        *           The injected InputDialog provider
        * @param    addRecordLockView
        *           The injected Add Record Lock View
        */
       @Inject
       AcquiredLocksView(AcquiredLocksView.Binder binder,
                         Provider<InputDialog> inputDialogProvider,
                         AddRecordLockView addRecordLockView)
       {
          initWidget(binder.createAndBindUi(this));
          setupModalSlot(AcquiredLocksPresenter.MODAL_CONTENT, modalFragment);
          addDialog(addRecordLockView);
          this.addRecordLockView = addRecordLockView;

          this.inputDialogProvider = inputDialogProvider;

          ColumnInfo<RecordInfo> col0 = new ColumnInfo<RecordInfo>(columnNames[0],
                   (ri) -> {return String.valueOf(ri.getRecordID());},
                   (r1, r2) -> { return r1.getRecordID().compareTo(r2.getRecordID());});
          ColumnInfo<RecordInfo> col1 = new ColumnInfo<RecordInfo>(columnNames[1],
                   (ri) -> { return String.valueOf(ri.getDatabaseName());},
                   (r1, r2) -> { return r1.getDatabaseName().compareTo(r2.getDatabaseName());});
          ColumnInfo<RecordInfo> col2 = new ColumnInfo<RecordInfo>(columnNames[2],
                   (ri) -> { return String.valueOf(ri.getTableName());},
                   (r1, r2) -> { return r1.getTableName().compareTo(r2.getTableName());});

          this.gridHandle = GridHelper.initListGrid(dataGrid, true, col0, col1, col2);

          this.selectionModel = (MultiSelectionModel<RecordInfo>) this.gridHandle.getSelectionModel();

          this.selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler()
          {

             @Override
             public void onSelectionChange(SelectionChangeEvent event)
             {
                Set<RecordInfo> selected = AcquiredLocksView.this.selectionModel.getSelectedSet();
                if (selected.isEmpty())
                {
                   getUiHandlers().setCurrentState(AcquiredLocksViewStates.SELECTION_IS_EMPTY);
                }
                else
                {
                   getUiHandlers().setCurrentState(AcquiredLocksViewStates.RECORDS_SELECTED);
                }

             }
          });
       }

       /**
        * Sets the acquired locks table model.
        *
        * @param    rows
        *           The array of records
        */
       @Override
       public void setRecordLocks(RecordInfo[] rows)
       {
          gridHandle.setRows(rows);
       }

       /**
        * The Lock button click handler.
        *
        * @param    e
        *           Click event
        */
       @UiHandler("lockRecords")
       void onLockRecords(ClickEvent e)
       {
          getUiHandlers().lockRecords();
       }

       /**
        * The Unlock button click handler.
        *
        * @param    e
        *           Click event
        */
       @UiHandler("unlockRecords")
       void onUnLockRecords(ClickEvent e)
       {
          getUiHandlers().unlockRecords();
       }

       /**
        * The Delete Selected button click handler.
        *
        * @param    e
        *           Click event
        */
       @UiHandler("deleteSelected")
       void onDeleteSelected(ClickEvent e)
       {
          Set<RecordInfo> selected = this.selectionModel.getSelectedSet();
          getUiHandlers().deleteSelectedRecords(selected);
       }

       /**
        * The Delete All button click handler.
        *
        * @param    e
        *           Click event
        */
       @UiHandler("deleteAllRecords")
       void onDeleteAllRecords(ClickEvent e)
       {
          getUiHandlers().deleteAllRecords();
       }

       /**
        * Provides all components that must be managed by its view state machine.
        *
        * @return   The array of components managed by its view state machine
        */
       @Override
       public HasEnabled[] get()
       {
          return new HasEnabled[] {
                                     lockRecords,
                                     unlockRecords,
                                     addRecord,
                                     deleteSelected,
                                     deleteAllRecords
                                  };
       }

       /**
        * Tests if the tested record set has been selected.
        *
        * @param    records
        *           The set of tested records
        *
        * @return   True if the tested record set has been selected, otherwise false.
        */
       @Override
       public boolean isRecordsSelected(Set<RecordInfo> records)
       {
          Set<RecordInfo> selected = this.selectionModel.getSelectedSet();

          return selected.containsAll(records);
       }

       /**
        * Gets a reference to the Add Record Lock View.
        *
        * @return   The Add Record Lock View
        */
       @Override
       public AddRecordLockView getAddRecordLockView()
       {
          return this.addRecordLockView;
       }
    }