Not sure if this was a tough question or simply no one on the user forum has
had such a need but I finally figured out a way to get my particular problem
solved and figured I would share. There likely may be a better way but I
tied the selection to an Override of the DataSource of the DataGrid:

...

    /*
     * ---------------------------------------------------------------------
     * Create data source for the history grid 
     * ---------------------------------------------------------------------
     */
    checkItemHistoryDataSource = new MatrixCheckItemHistoryDataSource(
        NUMBER_OF_DAYS) {

      @Override
      public IModel<MatrixCheckItemModel> model(Object object) {
        MatrixCheckItemModel m = (MatrixCheckItemModel) object;
        IModel im = new MatrixDetachableCheckItemModel(m);
        /*
         * If we have selected a new item that doesn't match the history
item
         * profile.
         */
        if ((historyItemSelected.getId() == null)
            || !historyItemSelected.getMatrixCheckProfile().getId().equals(
                listItemSelected.getMatrixCheckProfile().getId())) {
          /*
           * Reset the currently selected history item if it has the same
date
           * as our new list item. This way it selects the current history
           * instance by default, which is what we want.
           */
          if (m.getMatrixShiftReport().getDay().equals(
              listItemSelected.getMatrixShiftReport().getDay())) {
            // historyItemSelected = m;
            itemSelectedHistoryGrid.selectItem(im, true);
            /*
             * Reset our selected item panel.
             */
            selectedItemPanel.setSelectedItem(historyItemSelected);
          }
        }
        return im;
      }
    };

    /*
     * ---------------------------------------------------------------------
     * Create and add the selected item history grid 
     * ---------------------------------------------------------------------
     */
    itemSelectedHistoryGrid = new MatrixCheckItemHistoryGrid(
        "selectedItemHistoryGrid", checkItemHistoryDataSource, colorblind) {

      private static final long serialVersionUID = 1L;

      @Override
      public void resetSelectedItems() {
        super.resetSelectedItems();

        /*
         * When selection changes the item panel needs to be refreshed.
         */
        AjaxRequestTarget target = AjaxRequestTarget.get();

        /*
         * Refresh ajax target
         */
        if (target != null) {
          /*
           * Set the selected item on the panel and refresh it
           */
          selectedItemPanel.setSelectedItem(historyItemSelected);
          target.addComponent(selectedItemPanel);
        }
      }

      @Override
      public void selectItem(IModel item, boolean isSelect) {
        super.selectItem(item, isSelect);

        /*
         * There will only be one item in the collection as this grid
         * has setAllowSelectMultiple() set to false.
         */
        Collection<IModel> selected = getSelectedItems();
        for (IModel model : selected) {
          historyItemSelected = (MatrixCheckItemModel) model.getObject();
        }

        /*
         * When selection changes the item panel needs to be refreshed.
         */
        AjaxRequestTarget target = AjaxRequestTarget.get();

        /*
         * Refresh ajax target
         */
        if (target != null) {
          /*
           * Set the selected item on the panel and refresh it
           */
          selectedItemPanel.setSelectedItem(historyItemSelected);
          target.addComponent(selectedItemPanel);
        }
      }
    };

...

    /*
     * -------------------------------------------------------------------
     * Create a grid of check items that are for the current shift and day
     * -------------------------------------------------------------------
     */
    currentGrid = new MatrixCheckItemGrid("current",
        new MatrixCheckListDataSource(), colorblind) {

      private static final long serialVersionUID = 1L;

      @Override
      public void selectItem(IModel item, boolean isSelect) {
        super.selectItem(item, isSelect);

        /*
         * There will only be one item in the collection as this grid
         * has setAllowSelectMultiple() set to false.
         */
        Collection<IModel> selected = getSelectedItems();
        for (IModel model : selected) {
          listItemSelected = (MatrixCheckItemModel) model.getObject();
        }

        /*
         * When selection changes the item panel needs to be refreshed.
         */
        AjaxRequestTarget target = AjaxRequestTarget.get();

        /*
         * If we selected something then we can make the panel visible.
         */
        itemSelectedHistoryGrid.setVisible(listItemSelected != null);
        selectedItemPanel.setVisible(listItemSelected != null);

        /*
         * Refresh ajax target
         */
        if (target != null) {
          /*
           * Reset the history data source to the item id and shift report
date
           * for the item selected. In this case the shift report date will
           * always be the same so we may want to optimize that later.
           */
          checkItemHistoryDataSource.setCheckProfileId(listItemSelected
              .getMatrixCheckProfile().getId());
          checkItemHistoryDataSource.setShiftDate(listItemSelected
              .getMatrixShiftReport().getDay());
          /*
           * We know that we want to change the history item if it currently
           * doesn't match the listItemSelected and that the data model will
           * choose the day of the one selected by the listItemSelected by
           * default; however, unfortunately setting it in the
           * checkItemHistoryDataSource doesn't refresh the selectedItem.
           */
          if ((historyItemSelected.getId() == null)
              ||
!historyItemSelected.getMatrixCheckProfile().getId().equals(
                  listItemSelected.getMatrixCheckProfile().getId())) {
            itemSelectedHistoryGrid.resetSelectedItems();
            selectedItemPanel.setSelectedItem(listItemSelected);
            target.addComponent(itemSelectedHistoryGrid);
            target.addComponent(selectedItemPanel);
          }
        }
      }
    };


It took me quite a bit of time to figure this out so maybe it will save
someone else some time or stimulate better ideas on how to solve such a
problem.
-- 
View this message in context: 
http://www.nabble.com/How-do-I-set-a-selected-item-outside-of-an-inmethod-datagrid--tp24565364p24592148.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to