package org.cytoscape.internal.actions;

import java.awt.event.ActionEvent;

import javax.swing.Action;
import javax.swing.event.MenuEvent;

import org.cytoscape.application.swing.AbstractCyAction;
import org.cytoscape.application.swing.CytoPanel;
import org.cytoscape.application.swing.CytoPanelName;
import org.cytoscape.application.swing.CytoPanelState;
import org.cytoscape.internal.view.CytoPanelNameInternal;
import org.cytoscape.internal.view.CytoscapeDesktop;

/*
 * #%L
 * Cytoscape Swing Application Impl (swing-application-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2017 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 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 General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

@SuppressWarnings("serial")
public class CytoPanelAction extends AbstractCyAction {
	
	protected static String SHOW = "Show ";
	protected static String HIDE = "Hide ";

	private CytoPanelNameInternal position;
	private CytoscapeDesktop desktop;

	public CytoPanelAction(CytoPanelNameInternal position, boolean show, CytoscapeDesktop desktop, float menuGravity) {
		super(show ? HIDE + position.getTitle() : SHOW + position.getTitle());

		this.position = position;
		this.desktop = desktop;
		
		setPreferredMenu("View");
		setMenuGravity(menuGravity);
	}

	/**
	 * Toggles the cytopanel state.  
	 */
	@Override
	public void actionPerformed(ActionEvent ev) {
		CytoPanelState curState = desktop.getCytoPanel(position).getState();

		if (position == CytoPanelNameInternal.SOUTH_WEST && curState == CytoPanelState.HIDE)			// #4100
		{
			CytoPanel west = desktop.getCytoPanel(CytoPanelName.WEST);
			CytoPanelState westState = west.getState();
			if (	westState == CytoPanelState.HIDE)
				west.setState(CytoPanelState.DOCK);
		}
		if (curState == CytoPanelState.HIDE)
			desktop.getCytoPanel(position).setState(CytoPanelState.DOCK);
		else
			desktop.getCytoPanel(position).setState(CytoPanelState.HIDE);
	} 

	/**
	 * This dynamically sets the title of the menu based on the state of the CytoPanel.
	 */
	@Override
	public void menuSelected(MenuEvent me) {
		CytoPanelState curState = desktop.getCytoPanel(position).getState();
		
		if (curState == CytoPanelState.HIDE)
			putValue(Action.NAME, SHOW + position.getTitle());
		else
			putValue(Action.NAME, HIDE + position.getTitle());
	}
}

@@ -2056,16 +2056,75 @@
 ate();%0A%0A
+%09%09// #4100 if west is hidden show southwest must show west%0A
 %09%09if (po
@@ -2203,25 +2203,16 @@
 .HIDE)%09%09
-%09// #4100
 %0A%09%09%7B%0A%09%09%09
@@ -2324,17 +2324,16 @@
 %0A%09%09%09if (
-%09
 westStat
@@ -2805,16 +2805,340 @@
 e();%0A%09%09%0A
+%09%09// #4100 if west is hidden, southwest must be hidden%0A%09%09if (position == CytoPanelNameInternal.SOUTH_WEST && curState == CytoPanelState.DOCK)%09%09%0A%09%09%7B%0A%09%09%09CytoPanelState westState = desktop.getCytoPanel(CytoPanelNameInternal.WEST).getState();%0A%09%09%09if (westState == CytoPanelState.HIDE)%0A%09%09%09%09curState = CytoPanelState.HIDE;%0A%09%09%7D%0A%09%09%09%0A
 %09%09if (cu

handle special case of south_west panels dependency on west visiblity