package org.csstudio.opibuilder.actions;

import org.csstudio.opibuilder.OPIBuilderPlugin;
import org.csstudio.platform.ui.util.CustomMediaFactory;
import org.eclipse.gef.ui.actions.WorkbenchPartAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.internal.WorkbenchWindow;


/**The action to make CSS full screen.
 * @author Xihui Chen
 *
 */
@SuppressWarnings("restriction")
public class CompactModeAction extends WorkbenchPartAction {
	
	public static final String ID = "org.csstudio.opibuilder.actions.compactMode";
	
	private ActionFactory.IWorkbenchAction toggleToolbarAction;
	private Menu menuBar;
	private boolean inFullScreenMode = false;
	private Shell shell;
	private ImageDescriptor fullScreenImage = 
		CustomMediaFactory.getInstance().getImageDescriptorFromPlugin(
			OPIBuilderPlugin.PLUGIN_ID, "icons/compact_mode.png");
	private ImageDescriptor exitFullScreenImage = 
		CustomMediaFactory.getInstance().getImageDescriptorFromPlugin(
			OPIBuilderPlugin.PLUGIN_ID, "icons/exit_compact_mode.gif");
	private IWorkbenchWindow window;
	private boolean toolbarWasInvisible;
	/**
	 * Constructor.
	 * @param part The workbench part associated with this PrintAction
	 */
	public CompactModeAction(IWorkbenchPart part) {
		super(part);
		window = part.getSite().getWorkbenchWindow();
		 toggleToolbarAction = ActionFactory.TOGGLE_COOLBAR.create(window); 
		 shell = part.getSite().getWorkbenchWindow().getShell();
		 menuBar = shell.getMenuBar();
		 setImageDescriptor(fullScreenImage);
		 
	}
	
	/**
	 * @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnabled()
	 */
	protected boolean calculateEnabled() {
		return true;
	}
	
	/**
	 * @see org.eclipse.gef.ui.actions.EditorPartAction#init()
	 */
	protected void init() {
		super.init();
		setText("Compact Mode");
		setId(ID);
		}
	
	/**
	 * @see org.eclipse.jface.action.Action#run()
	 */
	public void run() {
		if(inFullScreenMode){
			if(!toolbarWasInvisible)
				toggleToolbarAction.run();
			shell.setMenuBar(menuBar);		
			inFullScreenMode = false;
			setText("Compact Mode");
			setImageDescriptor(fullScreenImage);
		}else {
			if(window instanceof WorkbenchWindow && !((WorkbenchWindow) window).getCoolBarVisible()){
				toolbarWasInvisible = true;
			}else{
				toolbarWasInvisible = false;
				toggleToolbarAction.run();
			}
			shell.setMenuBar(null);		
			inFullScreenMode = true;
			setText("Exit Compact Mode");
			setImageDescriptor(exitFullScreenImage);
		}
	}

}

@@ -659,16 +659,148 @@
 ion %7B%0A%09%0A
+%09private static final String COMPACT_MODE = %22Compact Mode%22;%0A%0A%09private static final String EXIT_COMPACT_MODE = %22Exit Compact Mode%22;%0A%0A
 %09public 
@@ -870,16 +870,30 @@
 ctMode%22;
+ //$NON-NLS-1$
 %0A%09%0A%09priv
@@ -1823,17 +1823,248 @@
 ar();%0A%09%09
- 
+if(window instanceof WorkbenchWindow && !((WorkbenchWindow) window).getCoolBarVisible())%7B%0A%09%09%09inFullScreenMode = true;%0A%09%09%09setImageDescriptor(exitFullScreenImage);%0A%09%09%09setText(EXIT_COMPACT_MODE);%0A%0A%09%09%7Delse%7B%0A%09%09%09setText(COMPACT_MODE);%0A%09%09%09
 setImage
@@ -2092,16 +2092,20 @@
 Image);%0A
+%09%09%7D%0A
 %09%09 %0A%09%7D%0A%09
@@ -2365,35 +2365,8 @@
 ();%0A
-%09%09setText(%22Compact Mode%22);%0A
 %09%09se
@@ -2613,30 +2613,28 @@
 setText(
-%22Compact Mode%22
+COMPACT_MODE
 );%0A%09%09%09se
@@ -2950,35 +2950,33 @@
 setText(
-%22Exit Compact Mode%22
+EXIT_COMPACT_MODE
 );%0A%09%09%09se
@@ -3022,10 +3022,14 @@
 %09%09%7D%0A%09%7D%0A%0A
-
 %7D%0A
+%0A%0A%0A%0A

fix a bug on Compact Mode which will cause the toolbar lost.