FileDocCategorySizeDatePackage
Client.javaAPI DocphoneME MR2 API (J2ME)9713Wed May 02 17:59:48 BST 2007view

Client

public class Client extends Object

Fields Summary
private MPDataProvider
_data_provider
private JList
_classes_list
private JList
_object_list
private JFrame
_frame
private ViewMemoryPanel
_memory_access_panel
private ViewObjectsPanel
_view_objects_panel
private JButton
_vm_controller
private JButton
_connection_controller
private JButton
_statistics_btn
private boolean
isConnected
private static String
hostName
private static int
port
Constructors Summary
Methods Summary
private voidinitConnection()
Connectivity SECTION

    
    _data_provider.connect(hostName, port);
  
private voidinitUI()
UI SECTION

    _data_provider = MPDataProviderFactory.getMPDataProvider(VMConnectionFactory.getVMConnectionImpl());
    _frame = new JFrame("Java Heap Memory Observe Tool");
    _frame.pack();
    _frame.setLocation(0, 0);
    _frame.setSize(1000, 650);
    _frame.setVisible(true);
    _frame.setResizable(false);
    _frame.getContentPane().setLayout(new GridBagLayout());
    _frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        _data_provider.closeConnections();
        System.exit(0);
      }
    });

    /* memory access panel*/
    _classes_list = new JList();
    _memory_access_panel = new ViewMemoryPanel(_data_provider);
    _memory_access_panel.setSize(1000, 300);
    _memory_access_panel.update();
    
    _frame.getContentPane().add(_memory_access_panel, new GridBagConstraints(0, 0, 3, 1, 1, 1,
                  GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));

    JPanel lists_panel = new JPanel();
    JScrollPane pane = new JScrollPane() {
      public Dimension getPreferredSize() {return new Dimension(350, 250);}
    };
    _classes_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    _classes_list.addListSelectionListener(new ClassesListSelectionListener());
    pane.getViewport().setView(_classes_list);
    lists_panel.add(pane);
    _frame.getContentPane().add(lists_panel, new GridBagConstraints(0, 1, 2, 1, 1, 1, 
                  GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));

    _view_objects_panel = new ViewObjectsPanel(_data_provider);
    _view_objects_panel.initUI(true);
    _frame.getContentPane().add(_view_objects_panel, new GridBagConstraints(2, 1, 1, 1, 1, 1,
                  GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));


    JPanel button_panel = new JPanel();
    _statistics_btn = new JButton("Statistics");
    _statistics_btn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        StatisticsDialog.showDialog(_frame, _data_provider.calculateStatistics());
      }
    });
    button_panel.add(_statistics_btn);
    _vm_controller = new JButton("Resume");
    _vm_controller.addActionListener(new VMActionListener());
    button_panel.add(_vm_controller);
    _connection_controller = new JButton("Connect");
    _connection_controller.addActionListener(new ConnectionActionListener());
    button_panel.add(_connection_controller);

    _frame.getContentPane().add(button_panel, new GridBagConstraints(2, 2, 1, 1, 1, 1,
                  GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));

    setDisconnected();
    _frame.invalidate();
    _frame.validate();
    _frame.repaint();

  
public static voidmain(java.lang.String[] args)



       
    //Parse arguments
    try{
      if(parseArgs(args) < 0){
        System.exit(1);
      }
    }catch(CommandLineException e){
      System.err.println(e);
      System.exit(1);
    }
    (new Client()).initUI();
  
private voidonClassesSelectionChanged()
HANDLERS

    JavaClass item = (JavaClass)_classes_list.getSelectedValue();
    if (item != null) {
      _memory_access_panel.set_selected_class_id(item);
      Object[] objects = _data_provider.getObjectsOfClass(item);
      _view_objects_panel.setObjects(objects);
    } else {
      _view_objects_panel.setObjects(new Object[0]);      
    }
    _frame.invalidate();
    _frame.validate();
    _frame.repaint();
  
private voidonObjectSelectionChanged()

      _frame.invalidate();
      _frame.validate();
      _frame.repaint();
  
private static intparseArgs(java.lang.String[] args)
PARSE ARGS SECTION

        
    //Parse command line parameteres
    boolean portDefined = false;
    boolean hostDefined = false;
    for(int i=0; i<args.length-1 ; i++){
            
      //-port
      if(args[i].equals("-port")) {
        if(portDefined) {
          throw new CommandLineException("Port already defined");
        }
                
        portDefined = true; 
        try{
          port = Integer.valueOf(args[++i]).intValue();
        }catch(NumberFormatException e){
          throw new CommandLineException("Invalid port number");
        }
                
      //-host
      }else if(args[i].equals("-host")){
        if(hostDefined){
          throw new CommandLineException("Host already defined");
        }
        hostDefined = true;
        hostName = args[++i];
                
      //Invalid switch
      }else {
        throw new CommandLineException("Unknown switch " + args[i]);
      }
    }
               
    return 0;
  
private voidsetConnected()

    isConnected = true;
    _vm_controller.setEnabled(true);
    _statistics_btn.setEnabled(true);
    _connection_controller.setText("Disconnect");
  
private voidsetDisconnected()

    isConnected = false;
    _classes_list.setListData(new Object[0]);
    _memory_access_panel.update();
    _vm_controller.setEnabled(false);
    _statistics_btn.setEnabled(false);
    _connection_controller.setText("Connect");
  
private voidupdate()

    try {
      _classes_list.setListData(_data_provider.getClassList());
      _memory_access_panel.update();
    } catch (SocketException e) {
      setDisconnected();
    }
  
private static voidusage()

    System.err.println("Usage:\n" + 
      "java view.Client -host <hostname> -port <portname>");