/* * Copyright (c) 2003,2009 Declarative Engineering LLC. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Declarative Engineering LLC * verson 1 which accompanies this distribution, and is available at * http://declarativeengineering.com/legal/DE_Developer_License_v1.txt */ package com.foundation.view.swt; import org.eclipse.swt.SWT; import org.eclipse.swt.events.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import com.common.debug.*; import com.foundation.view.*; import com.foundation.view.resource.ResourceReference; public class Frame extends Container implements IFrame, ShellListener, Component.IComponentListener { public static final int STYLE_CLOSE = SWT.CLOSE; public static final int STYLE_MIN = SWT.MIN; public static final int STYLE_MAX = SWT.MAX; public static final int STYLE_RESIZE = SWT.RESIZE; public static final int STYLE_TITLE = SWT.TITLE; public static final int STYLE_NO_TRIM = SWT.NO_TRIM; public static final int STYLE_SHELL_TRIM = SWT.SHELL_TRIM; public static final int STYLE_DIALOG_TRIM = SWT.DIALOG_TRIM; public static final int STYLE_ON_TOP = SWT.ON_TOP; public static final int STYLE_TOOL = SWT.TOOL; /** The container's images resource (similar to component's containerImage, but allows multiple images to be specified). */ private SingleResourceAssociation containerImages; /** A holder for the value of the container images. */ private ResourceHolder containerImagesHolder = new ResourceHolder(this); /** The swt images used by the container of this component to represent the component. */ private Image[] swtContainerImages = null; /** The optional method which will be called when the frame is closed. */ private IMethodAssociation closedMethod = null; /** The optional method which will be called when the frame is activated. */ private IMethodAssociation activatedMethod = null; /** The optional method which will be called when the frame is deactivated. */ private IMethodAssociation deactivatedMethod = null; /** The optional menu bar for the frame. */ private Menu menuBar = null; /** * Frame default constructor. *
Warning: This is only for use by the framework. This should never be called to create a useable instance.
*/ public Frame() { }//Frame()// /** * Frame constructor. * This constructor is only used by the Window subclass. * @param name The unique component name. * @param viewContext The context under which this view component operates. For all but the outer most control this can be null. * @param style The style for the control. * @param data The optional data passed to the initializeControl(int, java.lang.Object) method. */ protected Frame(String name, IViewContext viewContext, int style, Object data) { super(null, name, viewContext, style, data); containerImages = new SingleResourceAssociation(this, this, getViewContext(), SingleResourceAssociation.TYPE_IMAGES, false, null); }//Frame()// /** * Frame constructor. * This constructor is only used to create instances of a Frame (Not currently supported by SWT - no floating frames). * @param parent The parent frame. * @param name The unique component name. * @param style The frame style. * @see #STYLE_BORDER * @see #STYLE_H_SCROLL * @see #STYLE_V_SCROLL * @see #STYLE_BORDER * @see #STYLE_LEFT_TO_RIGHT * @see #STYLE_RIGHT_TO_LEFT * @see #STYLE_NO_BACKGROUND * @see #STYLE_NO_FOCUS * @see #STYLE_NO_MERGE_PAINTS * @see #STYLE_NO_REDRAW_RESIZE * @see #STYLE_NO_RADIO_GROUP * @see #STYLE_CLOSE * @see #STYLE_MIN * @see #STYLE_MAX * @see #STYLE_RESIZE * @see #STYLE_TITLE * @see #STYLE_NO_TRIM * @see #STYLE_SHELL_TRIM * @see #STYLE_DIALOG_TRIM * @see #STYLE_ON_TOP * @see #STYLE_TOOL */ public Frame(Container parent, String name, int style) { super(parent, name, style); containerImages = new SingleResourceAssociation(this, this, getViewContext(), SingleResourceAssociation.TYPE_IMAGES, false, null); }//Frame()// /** * Sets the association container used to access the image array. * @param container The images association metadata. */ public void setContainerImagesAssociation(SingleAssociationContainer container) { verifyThread(); this.containerImages.setAssociations(container); }//setContainerImagesAssociation()// /** * Sets the component's default image array resource. * @param images The images to be displayed by this component. */ public void setDefaultContainerImages(ResourceReference images) { verifyThread(); this.containerImages.setDefaultValue(images); }//setDefaultContainerImages()// /** * Sets the container's default images. * @param images The image data array that will be displayed by the container when an image is needed to represent it (example: a tab in a tab panel). */ public void setDefaultContainerImages(JefImage[] images) { verifyThread(); this.containerImages.setDefaultValue(images); }//setDefaultContainerImages()// /** * Refreshes the images for the frame. */ protected void internalViewRefreshContainerImages() { if(containerImages.refresh()) { containerImagesHolder.setValue(containerImages.getValue()); }//if// }//internalViewRefreshContainerImages()// /* (non-Javadoc) * @see com.foundation.view.IView#maximize() */ public void maximize() { getSwtDecorations().setMaximized(true); }//maximize()// /* (non-Javadoc) * @see com.foundation.view.IView#minimize() */ public void minimize() { getSwtDecorations().setMinimized(true); }//minimize()// /* (non-Javadoc) * @see com.foundation.view.IView#show() */ public void show() { if(getSwtDecorations().getMinimized()) { getSwtDecorations().setMinimized(false); }//if// getSwtDecorations().setVisible(true); getSwtDecorations().setFocus(); }//show()// /* (non-Javadoc) * @see com.foundation.view.swt.Component#getDefaultVisibility() */ protected boolean getDefaultVisibility() { return false; }//getDefaultVisibility()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#initializeControl(int) */ protected void initializeControl(int style, Object data) { setSwtWidget(new org.eclipse.swt.widgets.Decorations(((Container) getContainer()).getSwtParent(), style)); getSwtWidget().setData(this); }//initializeControl()// /** * Gets the SWT decorations that represents this frame. * @return The decorations providing visualization for this frame. */ public org.eclipse.swt.widgets.Decorations getSwtDecorations() { return (org.eclipse.swt.widgets.Decorations) getSwtControl(); }//getSwtDecorations()// /** * Sets the method to be called when the frame is closed. * @param closedMethod The method to notify when the close function is performed. */ public void setClosedMethod(IMethodAssociation closedMethod) { this.closedMethod = closedMethod; }//setClosedMethod()// /** * Gets the frame's default button. * @return The default button in this container. */ public Button getDefaultButton() { verifyThread(); return (Button) getSwtDecorations().getDefaultButton().getData(); }//getDefaultButton()// /** * Sets the frame's default button. * @param defaultButton The default button in this container. */ public void setDefaultButton(Button defaultButton) { getSwtDecorations().setDefaultButton(defaultButton != null ? defaultButton.getSwtButton() : null); }//setDefaultButton()// /* (non-Javadoc) * @see com.foundation.view.swt.IDecoration#getMenuBar() */ public Menu getMenuBar() { return menuBar; }//getMenuBar()// /* (non-Javadoc) * @see com.foundation.view.swt.IDecoration#setMenuBar(com.foundation.view.swt.Menu) */ public void setMenuBar(Menu menuBar) { this.menuBar = menuBar; }//setMenuBar()// /** * Centers the frame on the screen. */ public void center() { verifyThread(); Rectangle window = getSwtDecorations().getBounds(); Rectangle screen = getSwtDecorations().getMonitor().getBounds(); int x = (int) ((screen.width / 2) - (window.width / 2)); int y = (int) ((screen.height / 2) - (window.height / 2)); if(x < screen.x) { x = screen.x; }//if// if(y < screen.y) { y = screen.y; }//if// getSwtDecorations().setLocation(x, y); }//center()// /** * Centers the frame on another frame. */ public void center(IView view) { verifyThread(); Rectangle windowRect = getSwtDecorations().getBounds(); Rectangle screen = getSwtDecorations().getDisplay().getBounds(); Rectangle componentRect = ((Container) view).getSwtComposite().getShell().getBounds(); int x = (int) ((componentRect.width / 2) - (windowRect.width / 2) + componentRect.x); int y = (int) ((componentRect.height / 2) - (windowRect.height / 2) + componentRect.y); if((x + windowRect.width) - screen.x > screen.width) { x = (int) (screen.width - windowRect.width + screen.x); }//if// if((y + windowRect.height) - screen.y > screen.height) { y = (int) (screen.height - windowRect.height + screen.y); }//if// if(x < screen.x) { x = screen.x; }//if// if(y < screen.y) { y = screen.y; }//if// getSwtDecorations().setLocation(x, y); }//center()// /** * Sets the method to be called when the frame is activated. * @param activatedMethod The method to notify when the activated function is performed. */ public void setActivatedMethod(IMethodAssociation activatedMethod) { this.activatedMethod = activatedMethod; }//setActivatedMethod()// /** * Sets the method to be called when the frame is deactivated. * @param deactivatedMethod The method to notify when the deactivated function is performed. */ public void setDeactivatedMethod(IMethodAssociation deactivatedMethod) { this.deactivatedMethod = deactivatedMethod; }//setDeactivatedMethod()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewInitializeAll() */ public void internalViewInitializeAll() { try { //Initialize the super class components.// super.internalViewInitializeAll(); //Initialize the menu bar if there is one.// if(getMenuBar() != null) { getMenuBar().internalViewInitializeAll(); }//if// }//try// catch(Throwable e) { Debug.log(e); }//catch// }//internalViewInitializeAll()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewReleaseAll() */ public void internalViewReleaseAll() { if(getSwtControl() instanceof Shell) { ((Shell) getSwtControl()).setLayoutDeferred(false); }//if// if(getMenuBar() != null) { getMenuBar().internalViewReleaseAll(); }//if// super.internalViewReleaseAll(); }//internalViewReleaseAll()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewRefreshAll() */ public void internalViewRefreshAll() { try { super.internalViewRefreshAll(); if(getMenuBar() != null) { getMenuBar().internalViewRefreshAll(); }//if// }//try// catch(Throwable e) { Debug.log(e); }//catch// }//internalViewRefreshAll()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewSynchronizeAll() */ public void internalViewSynchronizeAll() { if(getMenuBar() != null) { getMenuBar().internalViewSynchronizeAll(); }//if// super.internalViewSynchronizeAll(); }//internalViewSynchronizeAll()// /** * Registers the listener(s) necessary to capture shell events. */ protected void registerShellListeners() { /* GCJ Doesn't like this... since I don't use it I am commenting it. getSwtDecorations().addListener(SWT.Close, new org.eclipse.swt.widgets.Listener() { public void handleEvent(Event event) { shellClosed(event); event.doit = false; }//handleEvent()// }); getSwtDecorations().addListener(SWT.Activate, new org.eclipse.swt.widgets.Listener() { public void handleEvent(Event event) { shellActivated(event); }//handleEvent()// }); getSwtDecorations().addListener(SWT.Deactivate, new org.eclipse.swt.widgets.Listener() { public void handleEvent(Event event) { shellDeactivated(event); }//handleEvent()// }); */ /* Not used. getSwtDecorations().addListener(SWT.Iconify, new org.eclipse.swt.widgets.Listener() { public void handleEvent(Event event) { shellIconified(event); }//handleEvent()// }); getSwtDecorations().addListener(SWT.Deiconify, new org.eclipse.swt.widgets.Listener() { public void handleEvent(Event event) { shellDeiconified(event); }//handleEvent()// }); */ }//registerShellListeners()// /* (non-Javadoc) * @see com.foundation.view.swt.Component.IComponentListener#toolTipTextChanged(com.foundation.view.swt.Component, java.lang.String) */ public void toolTipTextChanged(Component component, String newToolTipText) { //Does nothing. The tool tip is already set by the caller.// }//toolTipTextChanged()// /* (non-Javadoc) * @see com.foundation.view.swt.Component.IComponentListener#imageChanged(com.foundation.view.swt.Component, org.eclipse.swt.graphics.Image) */ public void imageChanged(Component component, Image newImage) { getSwtDecorations().setImage(newImage); }//imageChanged()// /* (non-Javadoc) * @see com.foundation.view.swt.Component.IComponentListener#titleChanged(com.foundation.view.swt.Component, java.lang.String) */ public void titleChanged(Component component, String newTitle) { getSwtDecorations().setText(newTitle); }//titleChanged()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewInitialize() */ protected void internalViewInitialize() { try { containerImages.initialize(); //Add this component as a shell listener so we get shell events.// registerShellListeners(); //Register for image and title changes. Note: it is not necessary to initialize the title & image since they will be updated on the container's refresh and an event will fire if they are anything other than null.// registerListener(this); if(getContainerTitle() != null) { getSwtDecorations().setText(getContainerTitle()); }//if// super.internalViewInitialize(); }//try// catch(Throwable e) { Debug.log(e); }//catch// }//internalViewInitialize()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewRelease() */ protected void internalViewRelease() { containerImages.release(); containerImagesHolder.release(); if(swtContainerImages != null) { destroyImages(swtContainerImages); swtContainerImages = null; }//if// //Unregister for image and title changes.// unregisterListener(this); //Release the super class components.// super.internalViewRelease(); }//internalViewRelease()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewRefresh() */ protected void internalViewRefresh() { try { internalViewRefreshContainerImages(); super.internalViewRefresh(); }//try// catch(Throwable e) { Debug.log(e); }//catch// }//internalViewRefresh()// /* (non-Javadoc) * @see com.foundation.view.swt.AbstractComponent#internalViewSynchronize() */ protected void internalViewSynchronize() { super.internalViewSynchronize(); }//internalViewSynchronize()// /* (non-Javadoc) * @see com.foundation.view.swt.Component#internalOnValueChanged(com.foundation.view.SingleResourceAssociation, boolean) */ protected void internalOnValueChanged(SingleResourceAssociation resourceAssociation, int flags) { if(resourceAssociation == containerImages) { internalViewRefreshContainerImages(); }//if// else { super.internalOnValueChanged(resourceAssociation, flags); }//else// }//internalOnValueChanged()// /* (non-Javadoc) * @see com.foundation.view.swt.Component#internalResourceHolderChanged(com.foundation.view.swt.ResourceHolder, java.lang.Object, java.lang.Object) */ protected void internalResourceHolderChanged(ResourceHolder resource, Object oldValue, Object newValue, int flags) { if(resource == containerImagesHolder) { destroyImages(swtContainerImages); swtContainerImages = createImages(containerImagesHolder.getValue() instanceof JefImage ? new JefImage[] {(JefImage) containerImagesHolder.getValue()} : (JefImage[]) containerImagesHolder.getValue()); getSwtDecorations().setImages(swtContainerImages == null ? new Image[0] : swtContainerImages); }//if// else { super.internalResourceHolderChanged(resource, oldValue, newValue, flags); }//else// }//internalOnAssociationChanged()// /* (non-Javadoc) * @see org.eclipse.swt.events.ShellListener#shellClosed(org.eclipse.swt.events.ShellEvent) */ public void shellClosed(ShellEvent event) { if(closedMethod != null) { closedMethod.invoke(null, true); }//if// event.doit = false; }//shellClosed()// /* (non-Javadoc) * @see org.eclipse.swt.events.ShellListener#shellClosed(org.eclipse.swt.events.ShellEvent) */ public void shellClosed(Event event) { if(closedMethod != null) { closedMethod.invoke(null, true); }//if// event.doit = false; }//shellClosed()// /* (non-Javadoc) * @see org.eclipse.swt.events.ShellListener#shellActivated(org.eclipse.swt.events.ShellEvent) */ public void shellActivated(ShellEvent event) { if(activatedMethod != null) { activatedMethod.invoke(null, true); }//if// }//shellActivated()// /* (non-Javadoc) * @see org.eclipse.swt.events.ShellListener#shellActivated(org.eclipse.swt.events.ShellEvent) */ public void shellActivated(Event event) { if(activatedMethod != null) { activatedMethod.invoke(null, true); }//if// }//shellActivated()// /* (non-Javadoc) * @see org.eclipse.swt.events.ShellListener#shellDeactivated(org.eclipse.swt.events.ShellEvent) */ public void shellDeactivated(ShellEvent event) { if(deactivatedMethod != null) { deactivatedMethod.invoke(null, true); }//if// }//shellDeactivated()// /* (non-Javadoc) * @see org.eclipse.swt.events.ShellListener#shellDeactivated(org.eclipse.swt.events.ShellEvent) */ public void shellDeactivated(Event event) { if(deactivatedMethod != null) { deactivatedMethod.invoke(null, true); }//if// }//shellDeactivated()// /* (non-Javadoc) * @see org.eclipse.swt.events.ShellListener#shellDeiconified(org.eclipse.swt.events.ShellEvent) */ public void shellDeiconified(ShellEvent event) { //if(deiconifiedMethod != null) { // deiconifiedMethod.invoke(null, true); //}//if// }//shellDeiconified()// /* (non-Javadoc) * @see org.eclipse.swt.events.ShellListener#shellDeiconified(org.eclipse.swt.events.ShellEvent) */ public void shellDeiconified(Event event) { //if(deiconifiedMethod != null) { // deiconifiedMethod.invoke(null, true); //}//if// }//shellDeiconified()// /* (non-Javadoc) * @see org.eclipse.swt.events.ShellListener#shellIconified(org.eclipse.swt.events.ShellEvent) */ public void shellIconified(ShellEvent event) { //if(iconifiedMethod != null) { // iconifiedMethod.invoke(null, true); //}//if// }//shellIconified()// /* (non-Javadoc) * @see org.eclipse.swt.events.ShellListener#shellIconified(org.eclipse.swt.events.ShellEvent) */ public void shellIconified(Event event) { //if(iconifiedMethod != null) { // iconifiedMethod.invoke(null, true); //}//if// }//shellIconified()// }//Frame//