/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */
package datasourceAPI;

import java.awt.event.ActionEvent;
import java.util.Collection;
import java.util.Iterator;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JPopupMenu;

import ilog.cpl.interactor.IlpAbstractPopupMenuFactory;
import ilog.cpl.model.IlpObject;
import ilog.cpl.util.selection.IlpObjectSelectionModel;
import ilog.tgo.model.IltAlarm;
import ilog.tgo.model.IltObject;
import ilog.tgo.model.IltObjectState;
import ilog.tgo.model.IltTrap;
import shared.ResourceUtils;

/**
 * This class models a pop-up menu factory that creates a pop-up menu to
 * acknowledge alarms in objects.
 *
 * <p>
 * The alarm is acknowledged on the current selected objects.
 */
public class AlarmPopupMenuFactory extends IlpAbstractPopupMenuFactory {

  /**
   * Action which is used in the contextual pop-up. See below for usage.
   */
  Action acknowledgeAlarmsAction = new AbstractAction(ResourceUtils.getString("action.acknowledgeAlarms")) {
    Override
    public void actionPerformed(ActionEvent e) {
      Collection<?> selectedObjects = (Collection<?>) getValue("SelectedObjects");
      // When invoked with non-empty selection, acknowledge the alarms of
      // of the selected objects
      if (selectedObjects != null) {
        Iterator<?> i = selectedObjects.iterator();
        while (i.hasNext()) {
          IlpObject bo = (IlpObject) i.next();
          if (bo instanceof IltObject) {
            IltObject iltObj = (IltObject) bo;
            IltObjectState objState = iltObj.getObjectState();
            if (objState != null) {
              IltAlarm.State alarms = iltObj.getAlarmState();
              if (alarms != null)
                alarms.acknowledgeAllAlarms();
              else {
                IltTrap.State traps = iltObj.getTrapState();
                if (traps != null)
                  traps.acknowledgeAllAlarms();
              }
            }
          }
        }
        putValue("SelectedObjects", null);
      }
    }
  };

  /**
   * Create a contextual pop-up menu. If one or more objects are selected, allow
   * their alarms to be acknowledged If nothing is selected, disable the menu
   * item
   */
  private JPopupMenu createContextualPopup(IlpObjectSelectionModel ilpSelectionModel) {
    // Create an empty pop-up menu
    JPopupMenu menu = new JPopupMenu();
    // Create an action to put in the menu
    // Access the selected objects from the selection model
    Collection<?> selectedObjects = ilpSelectionModel.getSelectedObjects();
    // Pass the selected objects to the action
    acknowledgeAlarmsAction.putValue("SelectedObjects", selectedObjects);
    // Action should not be enabled if there are no selected objects
    acknowledgeAlarmsAction.setEnabled((selectedObjects != null) && !selectedObjects.isEmpty());
    // Add the action to the menu
    menu.add(acknowledgeAlarmsAction);
    return menu;
  }

  /**
   * Overrides the abstract method definition to create a pop-up menu that
   * depends on the objects that are currently selected.
   */
  Override
  public JPopupMenu createPopupMenu(IlpObjectSelectionModel ilpSelectionModel) {
    return createContextualPopup(ilpSelectionModel);
  }

}