 /**
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * Copyright © 2009 Mindoo GmbH
 *
 * @author Tammo Riedinger
 *
 */
package com.mindoo.ec09.example2;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;

import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import org.eclipse.albireo.core.SwingControl;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.mindoo.ec09.framework.actions.ActionManager;
import com.mindoo.ec09.legacy.AbstractLegacyApplication;

/**
 * This sample demonstrates a simple life-cycle of a Swing application being integrated as a single view
 * into the Lotus Notes client.
 *  
 * @author Tammo Riedinger
 */
public class SampleView extends ViewPart {
	/**
	 * Member to hold a reference to the displayed control.
	 */
	private SwingControl control;
	/**
	 * Member to hold a reference to the application being generated.
	 * This reference is used to free any resources used by the application
	 * when the ViewPart is disposed.
	 * In this case, the application-reference can be regarded as a bridge to the running application.
	 */
	private AbstractLegacyApplication app;

	@Override
	public void createPartControl(final Composite parent) {
		// start of life-cycle
		// Initialize the application and reposition its main-component
		// to the SwingControl (e.g. return the main-component in SwingControl#createSwingComponent()).
		control=new SwingControl(parent, SWT.NONE) {
			@Override
			protected JComponent createSwingComponent() {
				try {
					// initialize our legacy application while overriding some of its methods
					app=new AbstractLegacyApplication() {
						@Override
					    protected void installMenuBar(Component window, Container contentPane) {
						    // put the menubar at the top of the window-panel
							JMenuBar mb = createMenuBar(contentPane);
						
							if (window instanceof JPanel) {
								((JPanel)window).add(mb,BorderLayout.NORTH);
							}
							else
								throw new RuntimeException("Unknown type of application window");
						}
						
						@Override
						protected void installContentPane(Component window, Container contentPane) {
							super.installContentPane(window, contentPane);
							
							if (window instanceof JPanel) {
								// wrap the contentpane again to display the toolbar
								
								// this panel will receive the toolbar at BorderLayout.NORTH
								JPanel panel=new JPanel(new BorderLayout()) {
									private static final long serialVersionUID = -177381675357084800L;

									@Override
									public Component add(Component c) {
										if (c instanceof JToolBar) {	// handle toolbar insertion
											super.add(c,BorderLayout.NORTH);
											return c;
										}
										return super.add(c);
									}
								};
								
								panel.add(contentPane,BorderLayout.CENTER);
								
								((JPanel)window).add(panel,BorderLayout.CENTER);
							}
							else
								throw new RuntimeException("Unknown type of application window");
						}

					    @Override
					    protected Component createWindow() {
					    	JPanel panel=new JPanel(new BorderLayout());
							
							return panel;
						}
					    
					    @Override
						protected void show(Component window) {
							// do nothing
						}

						@Override
						protected void hide(Component window) {
							// do nothing
						}
						
						@Override
						protected ActionManager createActionManager(Component window, Container contentPane) {
							return new ActionManager(null,contentPane.getParent()) {
								@Override
								public void registerAction(Action a, String setID, String menuName, String toolbarName) {
									if (EXIT_ACTION==a) // do not append the exit-action
										return;
									super.registerAction(a, setID, menuName, toolbarName);
								}
							};
						};
					};

					app.launch();
				} catch (Throwable t) {
					throw new RuntimeException(t);
				}
				

				// return the content-pane of the applet to be displayed inside the Swing-Control
				return (JComponent)((app!=null) ? app.getWindow() : null);
			}
			
			@Override
		    protected void afterComponentCreatedSWTThread() {
		    	// eventually make sure the visible frame has the focus
		    }

			@Override
			public Composite getLayoutAncestor() {
				return parent;
			}

		};
	}

	@Override
	public void dispose() {
		// shut-down the instance of your legacy-application, 
		// when the last related ViewPart
		// in this case we use one instance per ViewPart
		super.dispose();

		if (app!=null) {
			// shut-down the application
			app.exit();
		}
		// end of life-cycle
	}

	@Override
	public void setFocus() {
		// hand the focus over to the control, whenever the ViewPart receives the focus
		if (control!=null && !control.isDisposed())
			control.setFocus();
	}
}