comparison src/org/dancres/blitz/serviceui/DashboardUI.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.serviceui;
2
3 import net.jini.lookup.entry.UIDescriptor;
4 import net.jini.lookup.entry.Name;
5
6 import net.jini.lookup.ui.MainUI;
7 import net.jini.lookup.ui.factory.JFrameFactory;
8 import net.jini.lookup.ui.factory.JComponentFactory;
9 import net.jini.lookup.ui.attribute.UIFactoryTypes;
10
11 import net.jini.core.lookup.ServiceItem;
12
13 import net.jini.core.entry.Entry;
14
15 import net.jini.admin.JoinAdmin;
16 import net.jini.admin.Administrable;
17
18 import org.dancres.blitz.remote.StatsAdmin;
19 import org.dancres.blitz.tools.dash.DashBoardFrame;
20 import org.dancres.blitz.tools.dash.StartDashBoard;
21
22 import javax.swing.JFrame;
23 import javax.swing.JComponent;
24 import javax.swing.JLabel;
25 import javax.swing.JOptionPane;
26 import javax.swing.JEditorPane;
27 import javax.swing.JScrollPane;
28
29 import java.awt.event.WindowListener;
30 import java.awt.event.WindowEvent;
31 import java.awt.event.WindowAdapter;
32
33 import java.util.HashSet;
34 import java.util.Set;
35
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38
39 import java.rmi.MarshalledObject;
40 import java.rmi.RemoteException;
41 import java.io.IOException;
42 import java.net.URL;
43
44 public class DashboardUI{
45 static Logger theLogger =
46 Logger.getLogger("org.dancres.blitz.serviceui.DashboardUI");
47
48 public static UIDescriptor getUIDescriptor()
49 throws IOException {
50
51 UIDescriptor desc = new UIDescriptor();
52 desc.role = MainUI.ROLE;
53 desc.toolkit = JFrameFactory.TOOLKIT;
54 desc.attributes=new HashSet();
55
56 Set types = new HashSet();
57 types.add(JFrameFactory.TYPE_NAME);
58 types.add(JComponentFactory.TYPE_NAME);
59 UIFactoryTypes factoryTypes=new UIFactoryTypes( types );
60
61 desc.attributes.add(factoryTypes);
62
63 desc.factory = new MarshalledObject(new Factory());
64 return desc;
65 }
66 private static class Factory
67 implements JFrameFactory, JComponentFactory{
68
69 static final long serialVersionUID = -1750620586876278578L;
70
71 //cache the current instance as it doesn't make any sense
72 //have multiple dashboards connected to the same space
73 private static DashBoardFrame _frame;
74
75 public JFrame getJFrame(Object roleObject){
76 //if we are already running then return the
77 //cached JFrame and bring it to the front
78 if(_frame!=null){
79 //TO DO:
80 //Add a method to the Dashboard to bring all its child
81 //frames to the front, by invoking toFront() on each child
82 //e.g _frame.showWindows();
83
84 _frame.toFront();
85 return _frame;
86 }
87 try{
88 ServiceItem si=(ServiceItem)roleObject;
89
90 Administrable admin=(Administrable)si.service;
91 StatsAdmin sa=(StatsAdmin)admin.getAdmin();
92 JoinAdmin ja=(JoinAdmin)admin.getAdmin();
93
94 String title=StartDashBoard.VER+" ["+getSpaceName(ja)+"]";
95 boolean closeOnExit=false; //don't call System.exit()
96 _frame = new DashBoardFrame(title,sa,closeOnExit);
97
98 _frame.addWindowListener(new WindowAdapter(){
99 public void windowClosing(WindowEvent evt){
100 //set _frame to null so next time
101 //we create a new window
102 _frame=null;
103 }
104 });
105 return _frame;
106 }catch(RemoteException ex){
107 ex.printStackTrace();
108 JFrame f= new JFrame("Blitz Dashboard Exception");
109
110 f.getContentPane().add( new JLabel(""+ex));
111 f.setSize(200,200);
112 f.setVisible(true);
113 return f;
114 }
115 }
116 //JComponentFactory
117 //return an html view
118 public JComponent getJComponent(Object roleObject){
119 try{
120
121 URL url=Factory.class.getResource("blitz.htm");
122 JEditorPane htmlView=new JEditorPane(url);
123 htmlView.setEditable(false);
124 //set the AccessibleContext Name for this view
125 //so the Inca X browser will display it
126 JScrollPane sp=new JScrollPane(htmlView);
127 sp.getAccessibleContext().setAccessibleName("Blitz dashboard");
128 return sp;
129 }catch(Exception ex){
130 theLogger.log(Level.SEVERE, "Got exception", ex);
131 return new JLabel(ex.toString());
132 }
133 }
134 //get the space name from lookup attributes
135 private String getSpaceName(JoinAdmin ja){
136 try{
137
138 Entry [] atts=ja.getLookupAttributes();
139 for(int i=0;i<atts.length;i++){
140 if(atts[i] instanceof Name){
141 return ((Name)atts[i]).name;
142 }
143 }
144 }catch(Exception ex){
145 theLogger.log(Level.SEVERE, "Dashboard failed to get spacename",
146 ex);
147 }
148 return "";
149 }
150 }
151 }