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

Quick Search    Search Deep

Source code: Freenet/client/GUI.java


1   package Freenet.client;
2   import Freenet.*;
3   import Freenet.message.*;
4   import java.util.*;
5   import java.io.*;
6   import javax.swing.*;
7   import java.awt.*;
8   import javax.swing.event.*;
9   import java.awt.event.*;
10  
11  public class GUI
12  {
13      public static ImageIcon icon = new ImageIcon("pixmaps/fn16x16.gif");
14  
15      protected static String version = "0.1";
16      protected static JTextArea log;
17      protected static JFrame main;
18      protected static JDesktopPane desktop;
19      protected static Listener listener;
20      protected static int listenPort;
21      protected static Hashtable messageCBs = new Hashtable();
22  
23      public static void main(String[] args)
24      {
25    main = new JFrame("Freenet Client "+version);
26    main.setIconImage(icon.getImage());
27    Container c = main.getContentPane();
28    
29    initMenuBar();
30    desktop = new JDesktopPane();
31    c.add(desktop);
32    initEventLog();
33    class WinHandler extends WindowAdapter
34    {
35        public void windowClosing(WindowEvent evt)
36        {
37      System.exit(0);
38        }
39    }
40    main.addWindowListener(new WinHandler());
41    main.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
42    main.setSize(600, 500);
43    main.show();
44    startListener();
45      }
46      
47      public static void startListener()
48      {
49    Random r = new Random();
50    int giveup = 10;
51    while (true)
52        {
53      listenPort = Math.abs((r.nextInt() % 10000))+10000;
54      try
55          {
56        listener = ListenerFactory.listen(new ListeningAddress("tcp/"+listenPort));
57        break;
58          }
59      catch (Exception e) 
60          {
61        giveup--;
62        if (giveup == 0)
63            throw new RuntimeException("Failed to bind to any of 10 ports, giving up");
64        log.append("Failed to bind to random port "+listenPort+", trying another\n");
65          }
66        }
67    log.append("Listener started on port "+listenPort+"\n");
68      }
69  
70  
71      public static void acceptConnections()
72      {
73    Connection conn;
74  
75    while(true)
76        {
77      try
78          {
79        conn = listener.accept();
80        new HandleConn(conn);
81          }
82      catch (IOException e)
83          {
84        throw new RuntimeException("Problem accepting next connection: "+e);
85          }
86        }
87      }
88  
89      public static void initMenuBar()
90      {
91    JMenuBar menu = new JMenuBar();
92    JMenu messageMenu = new JMenu("Messages");
93    messageMenu.add(new EditMessageAction(new RequestMessageEditor()));
94    messageMenu.add(new EditMessageAction(new InsertMessageEditor()));
95    menu.add(messageMenu);
96    JMenu settingsMenu = new JMenu("Settings");
97    settingsMenu.add(new JMenuItem("Default Server"));
98    menu.add(settingsMenu);
99    JMenu helpMenu = new JMenu("Help");
100   menu.add(helpMenu);
101   main.setJMenuBar(menu);
102     }
103     
104     public static void initEventLog()
105     {
106   JInternalFrame eventLog = new JInternalFrame("Event Log",
107                  true,
108                  false,
109                  true,
110                  true);
111   eventLog.setFrameIcon(icon);
112   JPanel panel = new JPanel(new GridLayout(1,1), true);
113   log = new JTextArea("Started Freenet Client V"+version+"\n");
114   log.setEditable(false);
115   panel.add(log);
116   JScrollPane scrollPane = new JScrollPane(panel,
117              JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
118              JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
119   eventLog.getContentPane().add(scrollPane);
120   eventLog.setLocation(5,300);
121   eventLog.setSize(200,100);
122   desktop.add(eventLog);
123     }
124     
125     class NewRequestAction extends AbstractAction
126     {
127   public void actionPerformed(ActionEvent aw)
128   {
129       
130   }
131     }
132 }
133 
134 interface MessageCB
135 {
136     public abstract void received(Message m);
137 }
138 
139 class HandleConn extends Thread
140 {
141     Connection conn;
142     JTextArea log;
143     Hashtable messageCBs;
144 
145     public HandleConn(Connection conn)
146     {
147   this.conn = conn;
148   start();
149     }
150     
151     public void run()
152     {
153   RawMessage m = null;
154   try
155       {
156     m = new RawMessage(conn.in);
157     Message msg = MessageFactory.toMessage(m);
158     msg.initSources(conn.getMyAddress(),conn.getPeerAddress(msg.source.listenPart()));
159     GUI.log.append("Received "+m.messageType+" message with ID "+msg.id+"\n");
160     try
161         {
162       ((MessageCB) GUI.messageCBs.get(new Long(msg.id))).received(msg);
163         }
164     catch (Exception e)
165         {
166       GUI.log.append("Failed to handle message correctly\n");
167         }
168       }
169   catch (Exception e)
170       {
171     GUI.log.append("Problem processing incoming connection\n");
172       }
173     }
174 }
175 
176 class EditMessageAction extends AbstractAction
177 {
178     MessageEditor me;
179     public EditMessageAction(MessageEditor me)
180     {
181   super(me.getName());
182   this.me = me;
183 
184     }
185 
186     public void actionPerformed(ActionEvent e)
187     {
188   me.display();
189     }
190 }
191 
192 class TabbedPaneListener implements ChangeListener 
193 {
194     int i = -1;
195     public void stateChanged(ChangeEvent ce)
196     {
197   JTabbedPane tp = (JTabbedPane) ce.getSource();
198   if (i == -1 || i != tp.getSelectedIndex())
199       {
200     tp.setEnabledAt(tp.getSelectedIndex(), false);
201     if (i != -1)
202         tp.setEnabledAt(i, true);
203       }
204   i = tp.getSelectedIndex();
205     }
206 }
207 
208 abstract class MessageEditor
209 {
210     public abstract String getName();
211     public abstract Message getMessage();
212 
213     public void display()
214     {
215   JInternalFrame iFr = new JInternalFrame(getName(),
216             true,
217             true,
218             true,
219             true);
220   iFr.setFrameIcon(GUI.icon);
221   Container c = iFr.getContentPane();
222   JTabbedPane tp = new JTabbedPane();
223   JPanel editPanel = new JPanel();
224   editPanel.setLayout(new BoxLayout(editPanel, BoxLayout.Y_AXIS));
225   JPanel advancedPanel = new JPanel();
226         advancedPanel.setLayout(new BoxLayout(advancedPanel, BoxLayout.Y_AXIS));
227   Box servIPB = Box.createHorizontalBox();
228   servIPB.add(Box.createHorizontalGlue());
229         JTextField servIPtf = new JTextField();
230   servIPB.add(new JLabel("Server Name"));
231   servIPB.add(servIPtf);
232   Box servPB = Box.createHorizontalBox();
233   servPB.add(Box.createHorizontalGlue());
234         JTextField servPtf = new JTextField();
235   servPB.add(new JLabel("Server Port"));
236   servPB.add(servPtf);
237   advancedPanel.add(Box.createVerticalGlue());
238   advancedPanel.add(servIPB);
239   advancedPanel.add(Box.createVerticalGlue());
240   advancedPanel.add(servPB);
241   advancedPanel.add(Box.createVerticalGlue());
242   tp.addTab("Edit", editPanel);
243   tp.addTab("Advanced", advancedPanel);
244   tp.addChangeListener(new TabbedPaneListener());
245   c.add(tp);
246 
247   Box sendBox = Box.createHorizontalBox();
248   sendBox.add(Box.createHorizontalGlue());
249   sendBox.add(new JButton("Send Message"));
250   sendBox.add(Box.createHorizontalGlue());
251   c.add(sendBox, BorderLayout.SOUTH);
252   iFr.setLocation(10,10);
253   iFr.setSize(300,150);
254   GUI.desktop.add(iFr);
255   try
256       {
257     iFr.setSelected(true);
258       }
259   catch (Exception e) {}
260     }
261 }
262 
263 class RequestMessageEditor extends MessageEditor
264 {
265     protected DataRequest msg;
266 
267     public String getName()
268     {
269   return "Request Data";
270     }
271 
272     public Message getMessage()
273     {
274   return msg;
275     }
276 }
277 
278 class InsertMessageEditor extends MessageEditor
279 {
280     protected DataInsert msg;
281 
282     public String getName()
283     {
284   return "Insert Data";
285     }
286 
287     public Message getMessage()
288     {
289   return msg;
290     }
291 
292 }