Package DisplayProject.actions

Source Code of DisplayProject.actions.UserWindow

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "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.


*/
package DisplayProject.actions;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ComponentListener;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;

import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.help.HelpSetException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;

import DisplayProject.Constants;
import DisplayProject.IconizeHideAdapter;
import DisplayProject.MaximizeHideAdapter;
import DisplayProject.UIutils;
import DisplayProject.WindowManager;
import Framework.CloneHelper;
import Framework.ErrorMgr;
import Framework.File;
import Framework.StringUtils;
import Framework.TextData;
import Framework.UsageException;

/**
* this class provides support for various UserWindow functions not
* directly available on a JFrame
*
*/
public class UserWindow extends PendingAction {
  static Logger _log = Logger.getLogger(UserWindow.class);
    public UserWindow(Component pComponent) {
        super(pComponent);
    }

    public void performAction() {

    }

    public static void open(JFrame win) {
        open(win, false);
    }

    public static void open(final JFrame win, boolean applicationModal) {
        // Assign the function keys, just like Forte did
        UIutils.setAsFunctionKey(win, Constants.FN_F1, true);
        UIutils.setAsFunctionKey(win, Constants.FN_F2, true);
        UIutils.setAsFunctionKey(win, Constants.FN_F3, true);
        UIutils.setAsFunctionKey(win, Constants.FN_F4, true);
        UIutils.setAsFunctionKey(win, Constants.FN_F5, true);
        UIutils.setAsFunctionKey(win, Constants.FN_F6, true);
        UIutils.setAsFunctionKey(win, Constants.FN_F7, true);
        UIutils.setAsFunctionKey(win, Constants.FN_F8, true);
        UIutils.setAsFunctionKey(win, Constants.FN_F9, true);
        UIutils.setAsFunctionKey(win, Constants.FN_F10, true);
        UIutils.setAsFunctionKey(win, Constants.FN_F11, true);
        UIutils.setAsFunctionKey(win, Constants.FN_F12, true);

        UIutils.processGUIActions();
        // TF:14/05/2009:ASG-2:Forced this to execute on the EDT, but not as a pending action
        UIutils.invokeOnGuiThread(new Runnable() {
          public void run() {
            win.pack();
            //PM:20/2/08 added a call to setInitialPositionPolicy to cause the window to locate correctly
            setInitialPositionPolicy(win);
          }
        });
        WindowManager.registerWindow(win, applicationModal);
       
        // Get the window display state to actually do the set visible (if necessary).
        // We do this so it will also set the focus if required.  CraigM: 26/02/2008.
//        int displayState = WindowDisplayState.get(win);
//        if (displayState != Constants.DS_HIDDEN) {
//            win.setVisible(true);
//        }
        WindowDisplayState.set(win, WindowDisplayState.get(win));
    }

    public static void close(final JFrame win) {
        // Close the window on the EDT, otherwise there are timing issues.
      // TF:14/07/2009:It is possible that the code does:
      //    UserWindow.open(myWin);
      //    UserWindow.close(myWin);
      // The open is done as a PendingAction, but the close is done directly on the EDT (as there is no
      // juncture where we can call processGuiActions). So in this case we're trying to close the window
      // before we open it. Hence, we must call processGuiActions first/
      UIutils.processGUIActions();
        UIutils.invokeOnGuiThread(new Runnable() {

            public void run() {
                win.setVisible(false);
                WindowManager.deregisterWindow(win);
                win.dispose();
            }
        });

        // TF:29/9/07:Closing a window can introduce a race condition when returning from a window. Imagine
        // a field on an array which launches a new child window when clicked. The field is editable, but in
        // view only more. The click puts the field in edit mode and launches the new window. User picks data
        // from child window, window closes, data is returned to the calling window. THis calling window
        // maps the data onto the array which backs the array field, specifically onto the cell which was
        // clicked.
        //
        // Now, that cell also gets a focus loss event which takes it out of edit mode, and commits it's current
        // data, which is blank. (Surprisingly, this focus loss occurs only on re-entry to the window because
        // it was only the cell editor which received the focus loss event previously). So you have the cell
        // trying to commit blank data to the model on the EDT, which will be reflected onto the underlying
        // value, and the value changing as a result of the window returning which will map it's data onto the
        // underlying table via the EDT.
        //
        // To fix this, we will yield control for a bit, then wait until the event queue is empty so we know
        // all focus and window closing events have been processed.
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {}
        UIutils.waitForEDTToBeIdle();
    }
    /**
     * clones the window form only.
     * @param win
     * @return
     */
    public static Object cloneWindowOnly(JFrame win){
        UIutils.processGUIActions();
// Functionality now exists in clone helper.  CraigM. 24/12/2007.
//        JPanel form = UIutils.getForm(win);
//        Object clone = CloneHelper.clone(form, true);
//        return clone;
        return CloneHelper.clone(win, true);
    }

    /**
     * Set the position of the passed window according to the passed policy. The passed policy should
     * be one of:<p>
     * <li>PP_SYSTEMDEFAULT</li>
     * <li>PP_PRIMARYCENTERED</li>
     * <li>PP_PRIMARYRELATIVE</li>
     * <li>PP_SCREENCENTERED</li>
     * <li>PP_SCREENRELATIVE</li>
     */
    public static void setInitialPositionPolicy(Window win, int policy) {
        switch (policy){
            case Constants.PP_PRIMARYCENTERED:
              // TF:27/06/2008:Added code in to cater for the primary window
              Point p;
              Dimension d;
              Window primaryWin = getPrimaryWindow(win);
              if (primaryWin == null) {
                p = WindowManager.getPrimaryLocation();
                  d = WindowManager.getPrimarySize();
              }
              else {
                p = primaryWin.getLocation();
                d = primaryWin.getSize();
              }
                if ((p != null) && (d != null)) {
                    Dimension window = win.getSize();
                    int xCoord = (d.width/2) - (window.width/2) + p.x;
                    int yCoord = (d.height/2) - (window.height/2) + p.y;
                    win.setLocation( xCoord, yCoord );
                }
                break;
               
            case Constants.PP_PRIMARYRELATIVE:
              Point primaryLocation;
                Window topMostWindow = getPrimaryWindow(win);
                if (topMostWindow == null) {
                  topMostWindow = WindowManager.getWindowToActivate();
                  if (topMostWindow == win) {
                      topMostWindow = WindowManager.getPreviousWindow(win);
                  }
                  primaryLocation = WindowManager.getPrimaryLocation();
                }
                else {
                  primaryLocation = topMostWindow.getLocation();
                }
                win.setLocationRelativeTo(topMostWindow);
                if (primaryLocation != null) {
                    int xCoord = primaryLocation.x + initialX(win);
                    int yCoord = primaryLocation.y + initialY(win);
                    win.setLocation( xCoord, yCoord );
                }
                break;
            case Constants.PP_SCREENCENTERED:
                Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
                int xCoord = (screen.width/2) - (win.getWidth()/2);
                int yCoord = (screen.height/2) - (win.getHeight()/2);
                win.setLocation( xCoord, yCoord );
                break;
            case Constants.PP_SCREENRELATIVE:
                win.setLocation( initialX(win), initialY(win) );
                break;
            case Constants.PP_SYSTEMDEFAULT:
                win.setLocationByPlatform(true);
                break;
        }
    }

    /**
     * Get the primary window specified to display the passed window on. This method will return null if
     * the passed window is null, the passed window does not specify the PrimaryWindow property, or the
     * PrimaryWindow property exists, but is null.
     * @param win
     * @return
     */
    private static Window getPrimaryWindow(Window win) {
        Window result = null;
        try {
            PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(win.getClass(), "primaryWindow");
            result = ((Window)pd.getReadMethod().invoke(win, (Object[])null));
        } catch (Exception e) {
          // Do nothing, result is already null
        }
        return result;
    }

    private static void setInitialPositionPolicy(Window win) {
        int positionPolicy = 0;
        try {
            PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(win.getClass(), "initialPositionPolicy");
            positionPolicy = ((Integer)pd.getReadMethod().invoke(win, (Object[])null));
            setInitialPositionPolicy(win, positionPolicy);
        } catch (BeansException e) {
            UsageException errorVar = new UsageException("Cannot set initial position policy of Window: " + win.getName(), e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (IllegalArgumentException e) {
            UsageException errorVar = new UsageException("Cannot set initial position policy of Window: " + win.getName(), e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (IllegalAccessException e) {
            UsageException errorVar = new UsageException("Cannot set initial position policy of Window: " + win.getName(), e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (InvocationTargetException e) {
            UsageException errorVar = new UsageException("Cannot set initial position policy of Window: " + win.getName(), e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
    }

    private static int initialX(Window win){
        int x = 0;
        try {
            PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(win.getClass(), "initialX");
            x = ((Integer)pd.getReadMethod().invoke(win, (Object[])null));
            x = UIutils.milsToPixels(x);
        } catch (BeansException e) {
            UsageException errorVar = new UsageException("Cannot get initial position of Window: " + win.getName(), e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (IllegalArgumentException e) {
            UsageException errorVar = new UsageException("Cannot get initial position of Window: " + win.getName(), e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (IllegalAccessException e) {
            UsageException errorVar = new UsageException("Cannot get initial position of Window: " + win.getName(), e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (InvocationTargetException e) {
            UsageException errorVar = new UsageException("Cannot get initial position of Window: " + win.getName(), e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
        return x;
    }
    private static int initialY(Window win){
        int y = 0;
        try {
            PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(win.getClass(), "initialY");
            y = ((Integer)pd.getReadMethod().invoke(win, (Object[])null));
            y = UIutils.milsToPixels(y);
        } catch (BeansException e) {
            UsageException errorVar = new UsageException("Cannot get initial position of Window: " + win.getName(), e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (IllegalArgumentException e) {
            UsageException errorVar = new UsageException("Cannot get initial position of Window: " + win.getName(), e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (IllegalAccessException e) {
            UsageException errorVar = new UsageException("Cannot get initial position of Window: " + win.getName(), e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        } catch (InvocationTargetException e) {
            UsageException errorVar = new UsageException("Cannot get initial position of Window: " + win.getName(), e);
            ErrorMgr.addError(errorVar);
            throw errorVar;
        }
        return y;
    }

    /**
     * This method sets up the JavaHelp for the window.
     * It loads the help set, creates the {@link javax.help.HelpSet} and a {@link javax.help.HelpBroker}.
     * It then enables the help key.
     * The {@link javax.help.HelpSet} and {@link javax.help.HelpBroker} are attached as client properties to the root pane.
     * @param frame
     * @param helpSetName
     */
    public static void createHelp(JFrame frame, String helpSetName){
        ClassLoader loader = frame.getClass().getClassLoader();
        URL url = HelpSet.findHelpSet(loader, "file://" + helpSetName);
        createHelp(frame, url);
    }
    /**
     * This method sets up the JavaHelp for the window.
     * It loads the help set, creates the {@link javax.help.HelpSet} and a {@link javax.help.HelpBroker}.
     * It then enables the help key.
     * The {@link javax.help.HelpSet} and {@link javax.help.HelpBroker} are attached as client properties to the root pane.
     * @param frame
     * @param helpSetName
     */
    public static void createHelp(JFrame frame, File file){
        URL url = null;
        try {
            String urlString = file.getPortableName(true,true).toString();
            /*
             * if we have a file name begining with c:, remove it
             * as the Help URL doesn't like it
             */
            if (urlString.charAt(1) == ':')
                urlString = urlString.substring(2);
            url = new URL("file://" + urlString);
        } catch (MalformedURLException e) {
            UsageException err = new UsageException("Cannot create help URL", e);
            ErrorMgr.addError(err);
            throw err;
        }
        createHelp(frame, url);
    }
    /**
     * This method sets up the JavaHelp for the window.
     * It loads the help set, creates the {@link javax.help.HelpSet} and a {@link javax.help.HelpBroker}.
     * It then enables the help key.
     * The {@link javax.help.HelpSet} and {@link javax.help.HelpBroker} are attached as client properties to the root pane.
     * @param frame
     * @param helpURL
     */
    public static void createHelp(JFrame frame, URL helpURL){
        ClassLoader loader = frame.getClass().getClassLoader();
        HelpSet hs;
        HelpBroker hb;
        try {
            hs = new HelpSet(loader, helpURL);
            hb = hs.createHelpBroker();
            hb.enableHelpKey(frame.getRootPane(), "Frame", hs);
            frame.getRootPane().putClientProperty("qq_HelpSet", hs);
            frame.getRootPane().putClientProperty("qq_HelpBroker", hb);
        }
        catch (Exception ex) {
            if (ex instanceof HelpSetException){
                Logger.getLogger("task.part.logmgr").error("Cannot create help", ex);
            } else {
                UsageException newEx = new UsageException("Cannot create help", ex);
                ErrorMgr.addError(newEx);
                throw newEx;
            }
        }
    }
    /**
     * This method removes all components from the menu tree of a JFrame
     * @param frame
     */
    public static void freeMenus(JFrame frame){
      if (frame == null)
        return;
      JMenuBar menuBar = frame.getJMenuBar();
      if (menuBar == null)
        return;
      /*
       * traverse the tree and remove the leaves
       */
      removeLeaf(menuBar);
     
    }
    /**
     * remove the menu leaf
     * @param item
     */
    private static void removeLeaf(Component item){
      if (item instanceof JMenu){
        for (Component leaf : ((JMenu)item).getMenuComponents()){
          removeLeaf(leaf);
        }
      } else if (item instanceof JMenuBar){
        for (Component leaf : ((JMenu)item).getMenuComponents()){
          removeLeaf(leaf);
        }
      }
      item.getParent().remove(item);
    }
    /**
     * The messageDialog method displays a message dialog on a main window, which makes the window application-modal, meaning that it prevents input to all other windows while it is active.
     * A message dialog window contains a message and a button for dismissing the dialog.
     * @param frame The parent JFrame
     * @param messageText The messageText parameter specifies the text to use as the message in the dialog.
     * @param messageType The messageType parameter specifies the type of window system dialog to contain the message. Window systems use their own window styles and icons to distinguish different types of messages, such as warnings and errors. The default is MT_INFO.
     * @param wrapWidth The wrapWidth parameter specifies maximum number of characters per line to use in the dialog�s message text before word wrapping begins. You can also use explicit newline characters to break dialog lines.
     * @deprecated use {@link WindowManager#messageDialog(Component, String, int, int)}
     */
    public static void messageDialog(JFrame frame, String messageText, int messageType, int wrapWidth){
      String caption = "";
      switch (messageType){
      case Constants.MT_INFO:
        caption = "Information";
        break;
      case Constants.MT_WARNING:
        caption = "Warning";
        break;
      case Constants.MT_ERROR:
        caption = "Error";
        break;
      default:
        caption = "Information";
      break;
      }
      JOptionPane.showMessageDialog(frame, StringUtils.wrapText(messageText, "", wrapWidth, 4), caption, JOptionPane.ERROR_MESSAGE );
    }
    public static void messageDialog(JFrame frame, TextData messageText, int messageType, int wrapWidth){
      messageDialog(frame, messageText.toString(), messageType, wrapWidth);
    }
    public static void messageDialog(JFrame frame, TextData messageText, int messageType, double wrapWidth){
      messageDialog(frame, messageText, messageType, (int)wrapWidth);
    }
   
    /**
     * Shows whether a main window has an iconize symbol.
     * @param frame
     * @return
     */
    //PM:5/5/08
    public static boolean isIconizeEnabled(JFrame frame){
    for (ComponentListener cl : frame.getComponentListeners()) {
      if (cl instanceof IconizeHideAdapter) {
        return false;
      }
    }
      return true;
    }
    /**
     * Sets whether a main window has an iconize symbol.
     * @param frame
     * @param value
     */
    //PM:5/5/08
    public static void setIconizeEnabled(JFrame frame, boolean value){
      if (value == false) {
        frame.addComponentListener(new IconizeHideAdapter(frame));
      } else {
        for (ComponentListener cl : frame.getComponentListeners()) {
          if (cl instanceof IconizeHideAdapter) {
            frame.removeComponentListener(cl);
            break;
          }
        }
      }
    }
    /**
     * Shows whether a main window has an alternate symbol.
     * @param frame
     * @return
     */
    //PM:5/5/08
    public static boolean isMaximizeEnabled(JFrame frame){
    for (ComponentListener cl : frame.getComponentListeners()) {
      if (cl instanceof MaximizeHideAdapter) {
        return false;
      }
    }
      return true;
    }
    /**
     * Sets whether a main window has an alternate symbol.
     * @param frame
     * @param value
     */
    //PM:5/5/08
    public static void setMaximizeEnabled(JFrame frame, boolean value){
      if (value == false) {
        frame.addComponentListener(new MaximizeHideAdapter(frame));
      } else {
        for (ComponentListener cl : frame.getComponentListeners()) {
          if (cl instanceof MaximizeHideAdapter) {
            frame.removeComponentListener(cl);
            break;
          }
        }
      }
    }
    /**
     * post a message to the LaunchManager that the applet has started
     * @return
     */
    public static AppletConnectionInfo postAPPLETStarted(){
      AppletConnectionInfo client = null;
      String appletName = System.getProperty("qq_APPLETName");
      String appletPortString = System.getProperty("qq_APPLETPort");
      if (appletPortString == null)
        return client;
        int port = Integer.parseInt(appletPortString);
      ObjectOutputStream out = null;
      if (appletName != null){
        try {
          Socket socket = new Socket("localhost", port);
          out = new ObjectOutputStream(socket.getOutputStream());
          client = new AppletConnectionInfo(socket, null, out);
          out.flush();
          /*
           * send the Started message
           */
          String message = appletName + ":started";
          _log.debug("Sending to launch server: " + message);
          out.writeObject(message);
          out.flush();
        } catch (UnknownHostException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      return client;
    }
    /**
     * posts a message to the LaunchManager that the applet has stopped normal execution
     * @param client
     */
    public static void postAPPLETStopped(AppletConnectionInfo client){
      if (client == null)
        return;
      ObjectOutputStream out = client.out;
      String appletName = System.getProperty("qq_APPLETName");
      if (appletName != null){

        try {
          out.flush();
          /*
           * send the Started message
           */
          String message = appletName + ":stopped";
          _log.debug("Sending to launch server: " + message);
          out.writeObject(message);
          out.flush();
          out.close();
          client.socket.close();
        } catch (IOException e) {
          e.printStackTrace();
          try {
          out.close();
          client.socket.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
      }
    }
   
}
TOP

Related Classes of DisplayProject.actions.UserWindow

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.