package jadx.gui.ui.codearea;

import java.awt.*;
import java.awt.event.ActionEvent;

import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import org.fife.ui.rsyntaxtextarea.Token;

import jadx.api.JavaNode;
import jadx.gui.treemodel.JNode;
import jadx.gui.ui.ContentPanel;
import jadx.gui.ui.MainWindow;
import jadx.gui.ui.UsageDialog;
import jadx.gui.utils.NLS;

public final class FindUsageAction extends AbstractAction implements PopupMenuListener {
	private static final long serialVersionUID = 4692546569977976384L;

	private final transient ContentPanel contentPanel;
	private final transient CodeArea codeArea;

	private transient JavaNode node;

	public FindUsageAction(ContentPanel contentPanel, CodeArea codeArea) {
		super(NLS.str("popup.find_usage"));
		this.contentPanel = contentPanel;
		this.codeArea = codeArea;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (node == null) {
			return;
		}
		MainWindow mainWindow = contentPanel.getTabbedPane().getMainWindow();
		JNode jNode = mainWindow.getCacheObject().getNodeCache().makeFrom(node);
		UsageDialog usageDialog = new UsageDialog(mainWindow, jNode);
		usageDialog.setVisible(true);
	}

	@Override
	public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
		node = null;
		Point pos = codeArea.getMousePosition();
		if (pos != null) {
			Token token = codeArea.viewToToken(pos);
			if (token != null) {
				node = codeArea.getJavaNodeAtOffset(token.getOffset());
			}
		}
		setEnabled(node != null);
	}

	@Override
	public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
		// do nothing
	}

	@Override
	public void popupMenuCanceled(PopupMenuEvent e) {
		// do nothing
	}
}
