Gui5250Frame.java
上传用户:xiekaiwei
上传日期:2015-07-04
资源大小:620k
文件大小:16k
源码类别:

Telnet客户端

开发平台:

Java

  1. /**
  2.  * Title: tn5250J
  3.  * Copyright:   Copyright (c) 2001-2003
  4.  * Company:
  5.  * @author  Kenneth J. Pouncey
  6.  * @version 0.4
  7.  *
  8.  * Description:
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2, or (at your option)
  13.  * any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this software; see the file COPYING.  If not, write to
  22.  * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  23.  * Boston, MA 02111-1307 USA
  24.  *
  25.  */
  26. package org.tn5250j;
  27. import java.awt.*;
  28. import java.awt.event.*;
  29. import javax.swing.*;
  30. import java.util.*;
  31. import javax.swing.event.ChangeListener;
  32. import javax.swing.event.ChangeEvent;
  33. import org.tn5250j.tools.logging.*;
  34. import org.tn5250j.event.SessionJumpListener;
  35. import org.tn5250j.event.SessionJumpEvent;
  36. import org.tn5250j.event.SessionListener;
  37. import org.tn5250j.event.SessionChangeEvent;
  38. import org.tn5250j.event.TabClosedListener;
  39. import org.tn5250j.interfaces.GUIViewInterface;
  40. import org.tn5250j.interfaces.ConfigureFactory;
  41. import org.tn5250j.gui.TN5250jTabbedPane;
  42. public class Gui5250Frame extends GUIViewInterface implements
  43.                                                     ChangeListener,
  44.                                                     TN5250jConstants,
  45.                                                     SessionListener,
  46.                                                     TabClosedListener,
  47.                                                     SessionJumpListener {
  48.    BorderLayout borderLayout1 = new BorderLayout();
  49. //   JTabbedPane sessionPane = new JTabbedPane();
  50.    TN5250jTabbedPane sessionPane = new TN5250jTabbedPane();
  51.    private int selectedIndex = 0;
  52.    private boolean embedded = false;
  53.    private boolean hideTabBar = false;
  54.    public static int count = 0;
  55.    private TN5250jLogger log = TN5250jLogFactory.getLogger (this.getClass());
  56.    //Construct the frame
  57.    public Gui5250Frame(My5250 m) {
  58.       super(m);
  59.       enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  60.       try  {
  61.          jbInit();
  62.       }
  63.       catch(Exception e) {
  64.          e.printStackTrace();
  65.       }
  66.    }
  67.    //Component initialization
  68.    private void jbInit() throws Exception  {
  69.       this.getContentPane().setLayout(borderLayout1);
  70.       // update the frame sequences
  71.       frameSeq = sequence++;
  72.       
  73.       
  74. //      try
  75. //      {
  76. //        ClassLoader loader = Gui5250Frame.class.getClassLoader();
  77. //        if (loader == null)
  78. //          loader = ClassLoader.getSystemClassLoader();
  79. //
  80. //        Class       tabClass;
  81. //        Constructor keyProcessorConstructor;
  82. //
  83. //        try {
  84. //           String className = "org.tn5250j.gui.TN5250jTabbedPane";
  85. //           if (!OperatingSystem.hasJava14())
  86. //              className += "13";
  87. //
  88. //           tabClass = loader.loadClass(className);
  89. //           tabConstructor = tabkeyProcessorClass.getConstructor(null);
  90. //           Object obj = keyProcessorConstructor.newInstance(null);
  91. //      }
  92. //      catch (Throwable t) {
  93. //      }
  94.       
  95.       sessionPane.setBorder(BorderFactory.createEtchedBorder());
  96.       sessionPane.setBounds(new Rectangle(78, 57, 5, 5));
  97.       sessionPane.setOpaque(true);
  98.       sessionPane.setRequestFocusEnabled(false);
  99.       sessionPane.setDoubleBuffered(false);
  100.       sessionPane.addChangeListener(this);
  101.       sessionPane.addtabCloseListener(this);
  102.       Properties props = ConfigureFactory.getInstance().
  103.                            getProperties(ConfigureFactory.SESSIONS);
  104.       if (props.getProperty("emul.hideTabBar","no").equals("yes"))
  105.          hideTabBar = true;
  106.       if (!hideTabBar) {
  107.          this.getContentPane().add(sessionPane, BorderLayout.CENTER);
  108.       }
  109.       if (count == 0) setSessionTitle();
  110.       if (packFrame)
  111.          pack();
  112.       else
  113.          validate();
  114.    }
  115.    public void centerFrame() {
  116.       if (packFrame)
  117.          pack();
  118.       else
  119.          validate();
  120.       //Center the window
  121.       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  122.       Dimension frameSize = getSize();
  123.       if (frameSize.height > screenSize.height)
  124.          frameSize.height = screenSize.height;
  125.       if (frameSize.width > screenSize.width)
  126.          frameSize.width = screenSize.width;
  127.       setLocation((screenSize.width - frameSize.width) / 2,
  128.                      (screenSize.height - frameSize.height) / 2);
  129.    }
  130.    public int getFrameSequence() {
  131.       return frameSeq;
  132.    }
  133.    //Overridden so we can exit on System Close
  134.    protected void processWindowEvent(WindowEvent e) {
  135.       super.processWindowEvent(e);
  136.       if(e.getID() == WindowEvent.WINDOW_CLOSING) {
  137.          me.closingDown(this);
  138.       }
  139.    }
  140.    public void update(Graphics g) {
  141.       paint(g);
  142.    }
  143.    public void onSessionJump(SessionJumpEvent jumpEvent) {
  144.       switch (jumpEvent.getJumpDirection()) {
  145.          case JUMP_PREVIOUS:
  146.             prevSession();
  147.             break;
  148.          case JUMP_NEXT:
  149.             nextSession();
  150.             break;
  151.       }
  152.    }
  153.    private void nextSession() {
  154.       final int index = sessionPane.getSelectedIndex();
  155.       sessionPane.setForegroundAt(index,Color.black);
  156. //      sessionPane.setIconAt(index,unfocused);
  157.       sessionPane.setIconAt(unfocused,index);
  158.       SwingUtilities.invokeLater(new Runnable() {
  159.          public void run() {
  160.             int index1 = index;
  161.             if (index1 < sessionPane.getTabCount() - 1) {
  162.                sessionPane.setSelectedIndex(++index1);
  163.                sessionPane.setForegroundAt(index1,Color.blue);
  164. //               sessionPane.setIconAt(index1,focused);
  165.                sessionPane.setIconAt(focused,index1);
  166.             }
  167.             else {
  168.                sessionPane.setSelectedIndex(0);
  169.                sessionPane.setForegroundAt(0,Color.blue);
  170. //               sessionPane.setIconAt(0,focused);
  171.                sessionPane.setIconAt(focused,0);
  172.             }
  173.             ((SessionGUI)sessionPane.getComponent(sessionPane.getSelectedIndex())).grabFocus();
  174.             setSessionTitle();
  175.        }
  176.       });
  177.    }
  178.    private void prevSession() {
  179.       final int index = sessionPane.getSelectedIndex();
  180.       sessionPane.setForegroundAt(index,Color.black);
  181. //      sessionPane.setIconAt(index,unfocused);
  182.       sessionPane.setIconAt(unfocused,index);
  183.       SwingUtilities.invokeLater(new Runnable() {
  184.          public void run() {
  185.             int index1 = index;
  186.             if (index1 == 0) {
  187.                sessionPane.setSelectedIndex(sessionPane.getTabCount() - 1);
  188.                sessionPane.setForegroundAt(sessionPane.getSelectedIndex(),Color.blue);
  189. //               sessionPane.setIconAt(sessionPane.getSelectedIndex(),focused);
  190.                sessionPane.setIconAt(focused,sessionPane.getSelectedIndex());
  191.             }
  192.             else {
  193.                sessionPane.setSelectedIndex(--index1);
  194.                sessionPane.setForegroundAt(index1,Color.blue);
  195. //               sessionPane.setIconAt(index1,focused);
  196.                sessionPane.setIconAt(focused,index1);
  197.             }
  198.             ((SessionGUI)sessionPane.getComponent(sessionPane.getSelectedIndex())).grabFocus();
  199.             setSessionTitle();
  200.        }
  201.       });
  202.    }
  203.    public void stateChanged(ChangeEvent e) {
  204. //      JTabbedPane p = (JTabbedPane)e.getSource();
  205.       TN5250jTabbedPane p = (TN5250jTabbedPane)e.getSource();
  206.       p.setForegroundAt(selectedIndex,Color.black);
  207. //      p.setIconAt(selectedIndex,unfocused);
  208.       p.setIconAt(unfocused,selectedIndex);
  209.       SessionGUI sg = (SessionGUI)p.getComponentAt(selectedIndex);
  210.       sg.setVisible(false);
  211.       sg = (SessionGUI)p.getSelectedComponent();
  212.       if (sg == null)
  213.          return;
  214.       sg.setVisible(true);
  215.       sg.grabFocus();
  216.       selectedIndex = p.getSelectedIndex();
  217.       p.setForegroundAt(selectedIndex,Color.blue);
  218. //      p.setIconAt(selectedIndex,focused);
  219.       p.setIconAt(focused,selectedIndex);
  220.       setSessionTitle();
  221.    }
  222.    private void setSessionTitle() {
  223.       SessionGUI ses = getSessionAt(selectedIndex);
  224.       if (ses != null && ses.getAllocDeviceName() != null && ses.isConnected()) {
  225.          if (sequence - 1 > 0)
  226.             setTitle(ses.getAllocDeviceName() + " - tn5250j <" + sequence + "> - " + tn5250jRelease + tn5250jVersion + tn5250jSubVer);
  227.          else
  228.             setTitle(ses.getAllocDeviceName() + " - tn5250j - " + tn5250jRelease + tn5250jVersion + tn5250jSubVer);
  229.       }
  230.       else {
  231.          if (sequence - 1 > 0)
  232.             setTitle("tn5250j <" + sequence + "> - " + tn5250jRelease + tn5250jVersion + tn5250jSubVer);
  233.          else
  234.             setTitle("tn5250j - " + tn5250jRelease + tn5250jVersion + tn5250jSubVer);
  235.       }
  236. count +=1;
  237.    }
  238.    public void addSessionView(String tabText,SessionGUI sessionView) {
  239.       final SessionGUI session = sessionView;
  240.       if (hideTabBar && sessionPane.getTabCount() == 0 && !embedded) {
  241.          this.getContentPane().add(session, BorderLayout.CENTER);
  242.          session.addSessionListener(this);
  243.          session.resizeMe();
  244.          repaint();
  245.          if (packFrame)
  246.             pack();
  247.          else
  248.             validate();
  249.          embedded = true;
  250.          session.grabFocus();
  251.          setSessionTitle();
  252.       }
  253.       else {
  254.          if (hideTabBar && sessionPane.getTabCount() == 0 ) {
  255.             SessionGUI ses = null;
  256.             for (int x=0; x < this.getContentPane().getComponentCount(); x++) {
  257.                if (this.getContentPane().getComponent(x) instanceof SessionGUI) {
  258.                   ses = (SessionGUI)(this.getContentPane().getComponent(x));
  259.                   this.getContentPane().remove(x);
  260.                   break;
  261.                }
  262.             }
  263.             //ses = (Session)(this.getContentPane().getComponent(0));
  264. //            sessionPane.addTab(tabText,focused,ses);
  265.             sessionPane.addTab(tabText,ses,focused);
  266.             final SessionGUI finalSession = ses;
  267.             SwingUtilities.invokeLater(new Runnable() {
  268.                public void run() {
  269.                   finalSession.resizeMe();
  270.                   finalSession.repaint();
  271.                }
  272.             });
  273.             if (ses.getAllocDeviceName() != null)
  274.                sessionPane.setTitleAt(0,ses.getAllocDeviceName());
  275.             else
  276.                sessionPane.setTitleAt(0,ses.getSessionName());
  277.             ses.addSessionListener(this);
  278.             ses.addSessionJumpListener(this);
  279.             this.getContentPane().add(sessionPane, BorderLayout.CENTER);
  280.             SwingUtilities.invokeLater(new Runnable() {
  281.                public void run() {
  282.                   repaint();
  283.                   finalSession.grabFocus();
  284.                }
  285.             });
  286.          }
  287. //         sessionPane.addTab(tabText,focused,session);
  288.          sessionPane.addTab(tabText,session,focused);
  289.          sessionPane.setForegroundAt(sessionPane.getSelectedIndex(),Color.black);
  290. //         sessionPane.setIconAt(sessionPane.getSelectedIndex(),unfocused);
  291.          sessionPane.setIconAt(unfocused,sessionPane.getSelectedIndex());
  292.          sessionPane.setSelectedIndex(sessionPane.getTabCount()-1);
  293.          sessionPane.setForegroundAt(sessionPane.getSelectedIndex(),Color.blue);
  294. //         sessionPane.setIconAt(sessionPane.getSelectedIndex(),focused);
  295.          sessionPane.setIconAt(focused,sessionPane.getSelectedIndex());
  296.          session.addSessionListener(this);
  297.          session.addSessionJumpListener(this);
  298.          SwingUtilities.invokeLater(new Runnable() {
  299.             public void run() {
  300.                session.resizeMe();
  301.                session.repaint();
  302.                session.grabFocus();
  303.             }
  304.          });
  305.       }
  306.    }
  307.    public void tabClosed(int tabToBeClosed){
  308.       me.closeSession(this.getSessionAt(tabToBeClosed));
  309.    }
  310.    public void removeSessionView(SessionGUI targetSession) {
  311.       if (hideTabBar && sessionPane.getTabCount() == 0) {
  312.          for (int x=0; x < getContentPane().getComponentCount(); x++) {
  313.             if (getContentPane().getComponent(x) instanceof SessionGUI) {
  314.                getContentPane().remove(x);
  315.             }
  316.          }
  317.       }
  318.       else {
  319.          int index = sessionPane.indexOfComponent(targetSession);
  320.          log.info("session found and closing down " + index);
  321.          targetSession.removeSessionListener(this);
  322.          targetSession.removeSessionJumpListener(this);
  323.          int tabs = sessionPane.getTabCount();
  324.          sessionPane.remove(index);
  325.          tabs--;
  326.          if (index < tabs) {
  327.             sessionPane.setSelectedIndex(index);
  328.             sessionPane.setForegroundAt(index,Color.blue);
  329. //            sessionPane.setIconAt(index,focused);
  330.             sessionPane.setIconAt(focused,index);
  331.             ((SessionGUI)sessionPane.getComponentAt(index)).requestFocus();
  332.          }
  333.          else {
  334.             if (tabs > 0) {
  335.                sessionPane.setSelectedIndex(0);
  336.                sessionPane.setForegroundAt(0,Color.blue);
  337. //               sessionPane.setIconAt(0,focused);
  338.                sessionPane.setIconAt(focused,0);
  339.                ((SessionGUI)sessionPane.getComponentAt(0)).requestFocus();
  340.             }
  341.          }
  342.       }
  343.    }
  344.    public int getSessionViewCount() {
  345.       if (hideTabBar && sessionPane.getTabCount() == 0) {
  346.          for (int x=0; x < this.getContentPane().getComponentCount(); x++) {
  347.             if (this.getContentPane().getComponent(x) instanceof SessionGUI) {
  348.                return 1;
  349.             }
  350.          }
  351.          return 0;
  352.       }
  353.       else
  354.          return sessionPane.getTabCount();
  355.    }
  356.    public SessionGUI getSessionAt( int index) {
  357.       if (hideTabBar && sessionPane.getTabCount() == 0) {
  358.          for (int x=0; x < this.getContentPane().getComponentCount(); x++) {
  359.             if (this.getContentPane().getComponent(x) instanceof SessionGUI) {
  360.                return (SessionGUI)getContentPane().getComponent(x);
  361.             }
  362.          }
  363.          return null;
  364.       }
  365.       else {
  366.          if (sessionPane.getTabCount() <= 0)
  367.             return null;
  368.          return (SessionGUI)sessionPane.getComponentAt(index);
  369.       }
  370.    }
  371.    public void onSessionChanged(SessionChangeEvent changeEvent) {
  372.       Session5250 ses5250 = (Session5250)changeEvent.getSource();
  373.       SessionGUI ses = ses5250.getGUI();
  374.       switch (changeEvent.getState()) {
  375.          case STATE_CONNECTED:
  376.             final String d = ses.getAllocDeviceName();
  377.             if (d != null) {
  378.                System.out.println(changeEvent.getState() + " " + d);
  379.                final int index = sessionPane.indexOfComponent(ses);
  380.                if (index >= 0) {
  381.                   Runnable tc = new Runnable () {
  382.                      public void run() {
  383.                         sessionPane.setTitleAt(index,d);
  384.                      }
  385.                   };
  386.                   SwingUtilities.invokeLater(tc);
  387.                }
  388.                setSessionTitle();
  389.             }
  390.             break;
  391.       }
  392.    }
  393.    public boolean containsSession(SessionGUI session) {
  394.       if (hideTabBar && sessionPane.getTabCount() == 0) {
  395.          for (int x=0; x < this.getContentPane().getComponentCount(); x++) {
  396.             if (this.getContentPane().getComponent(x) instanceof SessionGUI) {
  397.                return ((SessionGUI)getContentPane().getComponent(x)).equals(session);
  398.             }
  399.          }
  400.          return false;
  401.       }
  402.       else
  403.          return (sessionPane.indexOfComponent(session) >= 0);
  404.    }
  405. }