DictionaryClient.java



/** RemoteDictionaryのクライアントのクラス */
import java.util.*;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class DictionaryClient extends JPanel {

 /** 分散オブジェクトRemoteDictionary */
    public RemoteDictionary dictionary;

 /** キーの一覧を表示する JListのオブジェクト */
    public JList list;

 /** データを表示するJTextAreaのオブジェクト */
    public JTextArea area;

 /** コンストラクタ */
    public DictionaryClient( ORB orb ) throws Exception {
        super();
        try {
            org.omg.CORBA.Object service
               = orb.resolve_initial_references( "NameService" );
            NamingContext ncnt
               = NamingContextHelper.narrow( service );
            NameComponent ncmp = new NameComponent( "RemoteDictionary", "" );
            NameComponent path[] = { ncmp };
            dictionary = RemoteDictionaryHelper.narrow( ncnt.resolve( path ) );

            String[] data = dictionary.keys();
            list = new JList( data );
            list.setVisibleRowCount( 8 );
            JScrollPane spane = new JScrollPane( list );
            add( spane );
            area = new JTextArea( 16, 32 );
            area.setEditable( false );
            add( area );
            SelectionAdapter adapter = new SelectionAdapter();
            list.addListSelectionListener( adapter );
        }
        catch( Exception e ) {
            throw e;
        }
    }

  /** 最初に呼び出されるルーチン */
    public static void main( String argv[] ) {
        try {
            Properties props = new Properties();
            props.put( "org.omg.CORBA.ORBInitialHost", argv[0] );
            props.put( "org.omg.CORBA.ORBInitialPort", "10005" );
            ORB orb = ORB.init( argv, props );
            DictionaryClient client = new DictionaryClient( orb );
            JFrame frame = new JFrame( "Dictionary Client" );
            frame.getContentPane().setLayout( new BorderLayout() );
            frame.getContentPane().add( client, "Center" );
            frame.pack();
            frame.show();
        }
        catch( Exception e ) {
            System.err.println( "Error:" + e.toString() );
            System.exit(-1);
        }
    }

 /** イベント処理のアダプタのクラス */
    class SelectionAdapter implements ListSelectionListener {

        public void valueChanged( ListSelectionEvent evet ) {
            try {
                String key = (String)list.getSelectedValue();
                String value = (String)dictionary.answer( key );
                area.setText( value.toString() );
            }
            catch( Exception e ) {
                area.setText( "Error:" + e.getMessage() );
            }
        }
    }
}