/*
Copyright 2011-2016 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.google.security.zynamics.binnavi.ZyGraph.Menus.Actions;

import com.google.common.base.Preconditions;
import com.google.security.zynamics.binnavi.yfileswrap.Gui.GraphWindows.Searchers.Text.Gui.CGraphSearchField;
import com.google.security.zynamics.binnavi.yfileswrap.zygraph.NaviNode;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

/**
 * Action class that can be used to search for selected node content.
 */
public final class CSearchForSelection extends AbstractAction {
  /**
   * Used for serialization.
   */
  private static final long serialVersionUID = 1024247400294590764L;

  /**
   * Search field that is used for the search.
   */
  private final CGraphSearchField m_searchField;

  /**
   * Node that provides the selected content.
   */
  private final NaviNode m_node;

  /**
   * Creates a new action object.
   *
   * @param searchField Search field that is used for the search.
   * @param node Node that provides the selected content.
   */
  public CSearchForSelection(final CGraphSearchField searchField, final NaviNode node) {
    super("Search for selection");

    Preconditions.checkNotNull(searchField, "IE00942: Search field argument can not be null");

    Preconditions.checkNotNull(node, "IE00943: Node argument can not be null");

    m_searchField = searchField;
    m_node = node;
  }

  @Override
  public void actionPerformed(final ActionEvent event) {
    final String searchString = m_node.getRealizer().getNodeContent().getSelectedText();

    m_searchField.searchFor(searchString);
  }
}
