nshandler.java

来自「UCS (Ultra Corba Simulator) is one more 」· Java 代码 · 共 231 行

JAVA
231
字号
/*
 *        JacORB - a free Java ORB
 *
 *   Copyright (C) 1997-2004 Gerald Brose.
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Library General Public
 *   License as published by the Free Software Foundation; either
 *   version 2 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the Free
 *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package com.corba.mnq.ns;

import java.awt.event.*;
import java.awt.*;
import java.io.FileOutputStream;
import java.io.PrintWriter;

import javax.swing.tree.*;
import javax.swing.*;


public class NSHandler
    extends WindowAdapter
    implements ActionListener, MouseListener, KeyListener
{
    JTree tree;
    NSTable nstab;
    Component frame;
    JDialog dlg;
    JTextField editName;
    JPopupMenu popup=new JPopupMenu();

	JMenuItem bindContext = new JMenuItem("BindNewContext");
	JMenuItem exportIor = new JMenuItem("ExportIOR");
	JMenuItem bindObject = new JMenuItem("Bind Object");
	JMenuItem unbind=new JMenuItem("Unbind name");
	JMenuItem connectNsMenu=new JMenuItem("ConnectNamingService");
	JMenuItem refresh=new JMenuItem("Refresh");
	ContextNode currentNode=null;
	
    public void reset(JTree tr,NSTable tab){
		tree = tr;
		nstab=tab;
    }
    
    public NSHandler(Component fr,JTree tr) 
    { 
		frame = fr;
		tree = tr;
	
		bindContext.addActionListener(this);
		bindObject.addActionListener(this);
		unbind.addActionListener(this);
		connectNsMenu.addActionListener(this);
		exportIor.addActionListener(this);
		refresh.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {

		if (e.getActionCommand().equals("Unbind name")) 
		{
		    ((NSTree)tree).unbind();
		}
		else if (e.getActionCommand().equals("Bind Object")) 
		{
		    ObjectDialog dialog = new ObjectDialog((Frame)frame);
		    //dialog.pack(); 
		    //dialog.show();
	
		    if (dialog.isOk)
	            {
	                try
	                {
	                	((NSTree)tree).bindObject( dialog.getName(), dialog.getIOR(), dialog.isRebind());
	                }               
	                catch ( org.omg.CORBA.UserException ue )
	                {
	                    JOptionPane.showMessageDialog( frame, 
	                                                   ue.getClass().getName() + 
	                                                  (ue.getMessage() != null ? (":" + ue.getMessage()):""),
	                                                  "Exception", 
	                                                   JOptionPane.INFORMATION_MESSAGE);
	                }
	            }
		}
		else if (e.getActionCommand().equals("BindNewContext")) 
		{
		    try
		    {
	                String contextName = 
	                    JOptionPane.showInputDialog(frame,
	                                                "Name of the new context", 
	                                                "BindNewContext",
	                                                JOptionPane.QUESTION_MESSAGE);
	
	                // check if user input is okay or if CANCEL was hit
	                if (contextName != null && contextName.length() > 0)
	                	((NSTree)tree).bind(contextName);
		    } 
		    catch ( org.omg.CORBA.UserException ue )
		    {
			JOptionPane.showMessageDialog(frame, 
	                                              ue.getClass().getName() + 
						      (ue.getMessage() != null ? (":" + ue.getMessage()):""),
						      "Exception",
	                                              JOptionPane.INFORMATION_MESSAGE);
		    }
		}else if (e.getActionCommand().equals("ConnectNamingService")) 
		{
			NSUtil.reset();
		}else if (e.getActionCommand().equals("Refresh")) 
		{
			if( tree instanceof NSTree )
				((NSTree)tree).update();
		}else if (e.getActionCommand().equals("ExportIOR")) 
		{
			String ior=nstab.getIOR();
			if( ior==null ){
				JOptionPane.showMessageDialog(frame, 
                        "Cannot export IOR for this object",
                        "Operation tip",
                        JOptionPane.OK_OPTION);
			}
			try{
				JFileChooser dlg=new JFileChooser();
				if( dlg.showSaveDialog(frame)==JFileChooser.APPROVE_OPTION )
				{
	                String fname= dlg.getSelectedFile().getPath();
	                
	                PrintWriter out = new PrintWriter(new FileOutputStream(
	                        fname), true);
	                out.println(ior);
	                out.close();
				}			
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}

    }

    /**
     * @param k java.awt.event.KeyEvent
     */
	
    public void keyPressed(KeyEvent k) {}

    /**
     * @param k java.awt.event.KeyEvent
     */

    public void keyReleased(KeyEvent k) 
    {
    	if( k.getComponent()==tree ){
    		if( k.getKeyCode() == KeyEvent.VK_DELETE )
    			((NSTree)tree).unbind();
    	}else if( k.getComponent()==nstab){
    		if( k.getKeyCode() == KeyEvent.VK_DELETE )
    			nstab.unbind();	
    	}
    }

    public void keyTyped(KeyEvent k) {}
    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}

    /**
     * opens pop-up menu or displays context node content
     */

    public void mouseReleased(MouseEvent e)
    {
    	if( e.getComponent()==tree ){
			// on Solaris, the right mouse button somehow seems not be a popup trigger, so we
		        // accept mouse 3 explicitly
		    TreePath path = tree.getPathForLocation(e.getX(), e.getY());
	
			if (e.isPopupTrigger() || e.getModifiers() == java.awt.event.InputEvent.BUTTON3_MASK )
			{
				popup.removeAll();
				
				if( path!=null ){
				    DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getPathComponent(path.getPathCount()-1);
				    currentNode=(ContextNode)node.getUserObject();
				    popup.add(refresh);
					popup.add(bindContext);
					popup.add(bindObject);
					popup.add(unbind);
			    } else {
			    	popup.add(connectNsMenu);
			    }
				popup.pack();
		    	popup.show(tree, e.getX(), e.getY());
			}
			else
			{
			    if (path != null)
			    {
			    	DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getPathComponent(path.getPathCount()-1);
			    	((ContextNode)node.getUserObject()).display();
			    }
			}
    	}else if( e.getComponent()==nstab ){
			if (e.isPopupTrigger() || e.getModifiers() == java.awt.event.InputEvent.BUTTON3_MASK )
			{
				if( nstab.canExportIor()){
					popup.removeAll();
					popup.add(exportIor);
					popup.pack();
			    	popup.show(nstab, e.getX(), e.getY());
				}
			}
    	}
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?