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

Quick Search    Search Deep

Source code: com/ubermq/chord/ui/InfrastructureViewer.java


1   package com.ubermq.chord.ui;
2   
3   import com.ubermq.chord.jms.*;
4   import java.awt.*;
5   import java.awt.event.*;
6   import javax.swing.*;
7   
8   /**
9    * A Swing-based chord infrastructure viewer
10   * complete with menu bar and containing a
11   * scrollable chord display panel.
12   */
13  public class InfrastructureViewer
14      extends JFrame
15  {
16      private final InfrastructureModel m;
17  
18      // ACTIONS
19      private Action connectAction, disconnectAction, refreshAction, exitAction, queryAction, storeAction;
20  
21      // IMAGES
22      static final ImageIcon networkIcon =
23          new ImageIcon(Toolkit.getDefaultToolkit().getImage(Class.class.getResource("/images/network.png")));
24      static final ImageIcon stopIcon =
25          new ImageIcon(Toolkit.getDefaultToolkit().getImage(Class.class.getResource("/images/stop.png")));
26      static final ImageIcon trashIcon =
27          new ImageIcon(Toolkit.getDefaultToolkit().getImage(Class.class.getResource("/images/trashcan_empty.png")));
28      static final ImageIcon scriptIcon =
29          new ImageIcon(Toolkit.getDefaultToolkit().getImage(Class.class.getResource("/images/history.png")));
30      static final ImageIcon envelopeIcon =
31          new ImageIcon(Toolkit.getDefaultToolkit().getImage(Class.class.getResource("/images/mail_generic.png")));
32      static final ImageIcon rightArrowIcon =
33          new ImageIcon(Toolkit.getDefaultToolkit().getImage(Class.class.getResource("/images/1rightarrow.png")));
34      static final ImageIcon downArrowIcon =
35          new ImageIcon(Toolkit.getDefaultToolkit().getImage(Class.class.getResource("/images/1downarrow.png")));
36      static final ImageIcon homeIcon =
37          new ImageIcon(Toolkit.getDefaultToolkit().getImage(Class.class.getResource("/images/gohome.png")));
38      static final ImageIcon refreshIcon =
39          new ImageIcon(Toolkit.getDefaultToolkit().getImage(Class.class.getResource("/images/refresh.png")));
40  
41      public InfrastructureViewer(final InfrastructureModel m)
42      {
43          super("chord");
44          this.m = m;
45  
46          // construct the display panel
47          JScrollPane p = new JScrollPane(new ChordDisplayPanel(m));
48          getContentPane().add(p);
49  
50          // menubar
51          connectAction = new ConnectAction();
52          disconnectAction = new DisconnectAction();
53          disconnectAction.setEnabled(false);
54          refreshAction = new RefreshAction();
55          exitAction = new ExitAction();
56          queryAction = new QueryAction();
57          storeAction = new StoreAction();
58          setJMenuBar(setupMenuBar());
59      }
60  
61      private class ConnectAction extends AbstractAction
62      {
63          ConnectAction() {super("Connect...", networkIcon);}
64          public void actionPerformed(ActionEvent e)
65          {
66              String uri =
67                  JOptionPane.showInputDialog(InfrastructureViewer.this,
68                                              "Please enter the URI of a node in the chord infrastructure.",
69                                              "ubermq://localhost:4000");
70              if (uri == null ||
71                  uri.length() == 0)
72                  return;
73  
74              try
75              {
76                  m.connect(JMSChordInfrastructure.getInfrastructure(LocalChordNode.DEFAULT_IDENTIFIER_FACTORY,
77                                                                     java.net.URI.create(uri)));
78  
79                  pack();
80                  repaint();
81  
82                  // enable/disable
83                  setEnabled(false);
84                  disconnectAction.setEnabled(true);
85              }
86              catch (java.rmi.RemoteException x) {
87                  JOptionPane.showMessageDialog(InfrastructureViewer.this, x.getMessage());
88              }
89  
90          }
91      }
92  
93      private class DisconnectAction extends AbstractAction
94      {
95          DisconnectAction() {super("Disconnect", stopIcon);}
96          public void actionPerformed(ActionEvent e)
97          {
98              m.disconnect();
99  
100             setEnabled(false);
101             connectAction.setEnabled(true);
102         }
103 
104     }
105 
106     private class RefreshAction extends AbstractAction
107     {
108         RefreshAction()
109         {
110             super("Refresh", refreshIcon);
111             putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
112         }
113 
114         public void actionPerformed(ActionEvent e)
115         {
116             m.refresh();
117         }
118     }
119 
120     private class QueryAction extends AbstractAction
121     {
122         QueryAction()
123         {
124             super("Query...", envelopeIcon);
125             putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
126         }
127 
128         public void actionPerformed(ActionEvent e)
129         {
130             String query =
131                 JOptionPane.showInputDialog(InfrastructureViewer.this,
132                                             "Please enter the key to query.");
133             if (query != null)
134             {
135                 try
136                 {
137                     Object result = m.getInfrastructure().query(query);
138                     JOptionPane.showMessageDialog(InfrastructureViewer.this,
139                                                   "Result: " + result);
140                 }
141                 catch (java.rmi.RemoteException x) {
142                     JOptionPane.showMessageDialog(InfrastructureViewer.this, x.getMessage());
143                 }
144             }
145         }
146     }
147 
148     private class StoreAction extends AbstractAction
149     {
150         StoreAction()
151         {
152             super("Store...", scriptIcon);
153             putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, KeyEvent.CTRL_MASK));
154         }
155 
156         public void actionPerformed(ActionEvent e)
157         {
158             try
159             {
160                 String key =
161                     JOptionPane.showInputDialog(InfrastructureViewer.this,
162                                                 "Where do you want to store the value?");
163                 if (key != null)
164                 {
165                     String value =
166                         JOptionPane.showInputDialog(InfrastructureViewer.this,
167                                                     "What do you want to store?");
168 
169                     if (value != null)
170                         m.getInfrastructure().store(key, value);
171                 }
172             }
173             catch (java.rmi.RemoteException x) {
174                 JOptionPane.showMessageDialog(InfrastructureViewer.this, x.getMessage());
175             }
176         }
177     }
178 
179     private class ExitAction extends AbstractAction
180     {
181         ExitAction() {super("Exit");}
182         public void actionPerformed(ActionEvent e)
183         {
184             System.exit(0);
185         }
186     }
187 
188     JMenuBar setupMenuBar()
189     {
190         JMenuBar menuBar = new JMenuBar();
191 
192         // Server |
193         JMenu serverMenu = new JMenu("Infrastructure");
194         serverMenu.add(connectAction);
195         serverMenu.add(disconnectAction);
196         serverMenu.addSeparator();
197         serverMenu.add(refreshAction);
198         serverMenu.addSeparator();
199         serverMenu.add(exitAction);
200 
201         JMenu qMenu = new JMenu("Query");
202         qMenu.add(queryAction);
203         qMenu.add(storeAction);
204 
205         // add menus to bar.
206         menuBar.add(serverMenu);
207         menuBar.add(qMenu);
208         return menuBar;
209     }
210 
211     /**
212      * Constructs the viewer application window.
213      */
214     public static void main(String s[])
215     {
216         InfrastructureModel m = new InfrastructureModel();
217 
218         // display
219         InfrastructureViewer frame = new InfrastructureViewer(m);
220 
221         frame.addWindowListener(new WindowAdapter() {
222                     public void windowClosing(WindowEvent e) {System.exit(0);}
223                 });
224 
225         frame.pack();
226         frame.setVisible(true);
227     }
228 
229 }
230 
231