Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/strangeberry/rendezvous/tools/Browser.java


1   // Copyright (C) 2002  Strangeberry Inc.
2   // @(#)Browser.java, 1.13, 11/29/2002
3   //
4   // This library is free software; you can redistribute it and/or
5   // modify it under the terms of the GNU Lesser General Public
6   // License as published by the Free Software Foundation; either
7   // version 2.1 of the License, or (at your option) any later version.
8   // 
9   // This library is distributed in the hope that it will be useful,
10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  // Lesser General Public License for more details.
13  // 
14  // You should have received a copy of the GNU Lesser General Public
15  // License along with this library; if not, write to the Free Software
16  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  
18  package com.strangeberry.rendezvous.tools;
19  
20  import java.io.*;
21  import java.net.*;
22  import java.util.*;
23  import java.awt.*;
24  import java.awt.event.*;
25  import javax.swing.*;
26  import javax.swing.table.*;
27  import javax.swing.border.*;
28  import javax.swing.event.*;
29  
30  import com.strangeberry.rendezvous.*;
31  
32  /**
33   * User Interface for browsing Rendezvous services.
34   *
35   * @author  Arthur van Hoff
36   * @version   1.13, 11/29/2002
37   */
38  public class Browser extends JFrame implements ServiceListener, ListSelectionListener
39  {
40      Rendezvous rendezvous;
41      Vector headers;
42      DefaultListModel services;
43      String type;
44      JList typeList;
45      JList serviceList;
46      JTextArea info;
47      
48      Browser(Rendezvous rendezvous)
49      {
50    this(rendezvous, new String[] {
51        "_http._tcp.local.",
52        "_ftp._tcp.local.",
53        "_tftp._tcp.local.",
54        "_ssh._tcp.local.",
55        "_smb._tcp.local.",
56        "_printer._tcp.local.",
57        "_airport._tcp.local.",
58        "_afpovertcp._tcp.local.",
59        "_ichat._tcp.local.",
60        "_eppc._tcp.local."
61    });
62      }
63      Browser(Rendezvous rendezvous, String types[])
64      { 
65          super("JRendezvous Browser");
66    this.rendezvous = rendezvous;
67  
68    Color bg = new Color(230, 230, 230);
69          EmptyBorder border = new EmptyBorder(5, 5, 5, 5);
70    Container content = getContentPane();
71          content.setLayout(new GridLayout(1, 3));
72    Arrays.sort(types);
73    type = types[0];
74    
75    typeList = new JList(types);
76          typeList.setBorder(border);
77    typeList.setBackground(bg);
78    typeList.setSelectedIndex(0);
79    typeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
80    typeList.addListSelectionListener(this);
81  
82    JPanel typePanel = new JPanel();
83    typePanel.setLayout(new BorderLayout());
84    typePanel.add("North", new JLabel("Types"));
85    typePanel.add("Center", new JScrollPane(typeList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
86    content.add(typePanel);
87  
88    services = new DefaultListModel();
89    serviceList = new JList(services);
90    serviceList.setBorder(border);
91    serviceList.setBackground(bg);
92    serviceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
93    serviceList.addListSelectionListener(this);
94  
95    JPanel servicePanel = new JPanel();
96    servicePanel.setLayout(new BorderLayout());
97    servicePanel.add("North", new JLabel("Services"));
98    servicePanel.add("Center", new JScrollPane(serviceList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
99    content.add(servicePanel);
100 
101   info = new JTextArea();
102   info.setBorder(border);
103   info.setBackground(bg);
104   info.setEditable(false);
105   info.setLineWrap(true);
106 
107   JPanel infoPanel = new JPanel();
108   infoPanel.setLayout(new BorderLayout());
109   infoPanel.add("North", new JLabel("Details"));
110   infoPanel.add("Center", new JScrollPane(info, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
111   content.add(infoPanel);
112   
113   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
114         setLocation(100, 100);
115         setSize(600, 400);
116         show();
117 
118   rendezvous.addServiceListener(type, this);
119     }
120 
121     /**
122      * Add a service.
123      */
124     public void addService(Rendezvous rendezvous, String type, String name)
125     {
126   if (name.endsWith("." + type)) {
127       name = name.substring(0, name.length() - (type.length() + 1));
128   }
129   //System.out.println("ADD: " + name);
130   services.addElement(name);
131     }
132 
133     /**
134      * Remove a service.
135      */
136     public void removeService(Rendezvous rendezvous, String type, String name)
137     {
138   if (name.endsWith("." + type)) {
139       name = name.substring(0, name.length() - (type.length() + 1));
140   }
141   //System.out.println("REMOVE: " + name);
142   services.removeElement(name);
143     }
144 
145     /**
146      * List selection changed.
147      */
148     public void valueChanged(ListSelectionEvent e)
149     {
150   if (!e.getValueIsAdjusting()) {
151       if (e.getSource() == typeList) {
152     type = (String)typeList.getSelectedValue();
153     rendezvous.removeServiceListener(this);
154     services.setSize(0);
155     info.setText("");
156     rendezvous.addServiceListener(type, this);
157       } else if (e.getSource() == serviceList) {
158     String name = (String)serviceList.getSelectedValue();
159     if (name == null) {
160         info.setText("");
161     } else {
162         if (!name.endsWith(".")) {
163       name = name + "." + type;
164         }
165         ServiceInfo service = rendezvous.getServiceInfo(type, name);
166         if (service == null) {
167       info.setText("service not found");
168         } else {
169       StringBuffer buf = new StringBuffer();
170       buf.append(name);
171       buf.append('\n');
172       buf.append(service.getAddress());
173       buf.append(':');
174       buf.append(service.getPort());
175       buf.append('\n');
176       String txt = service.getTextString();
177       if (txt != null) {
178           buf.append('\n');
179           buf.append(txt);
180           buf.append('\n');
181       }
182         
183       info.setText(buf.toString());
184         }
185     }
186       }
187   }
188     }
189 
190     /**
191      * Table data.
192      */
193     class ServiceTableModel extends AbstractTableModel
194     {
195   public String getColumnName(int column)
196   {
197       switch (column) {
198         case 0: return "service";
199         case 1: return "address";
200         case 2: return "port";
201         case 3: return "text";
202       }
203       return null;
204   }
205   public int getColumnCount()
206   {
207       return 1;
208   }
209         public int getRowCount()
210         {
211       return services.size();
212         }
213         public Object getValueAt(int row, int col)
214         {
215       return services.elementAt(row);
216         }
217     }
218 
219     public String toString()
220     {
221   return "RVBROWSER";
222     }
223 
224     /**
225      * Main program.
226      */
227     public static void main(String argv[]) throws IOException
228     {
229   new Browser(new Rendezvous());
230     }
231 }