KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > gui > GlobalContext


1 package org.sapia.regis.gui;
2
3 import javax.swing.JFrame JavaDoc;
4 import javax.swing.JOptionPane JavaDoc;
5
6 import org.sapia.gumby.RenderContext;
7 import org.sapia.gumby.event.EventManager;
8 import org.sapia.regis.Registry;
9 import org.sapia.regis.gui.event.TaskEvent;
10 import org.sapia.regis.gui.task.Task;
11 import org.sapia.regis.remote.Authenticating;
12
13 public class GlobalContext implements GuiConsts{
14   
15   static GlobalContext instance = new GlobalContext();
16
17   String JavaDoc username, password;
18   Registry registry;
19   RenderContext renderContext;
20   ModelManager modelManager = new ModelManager();
21   
22   public static GlobalContext getInstance(){
23     return instance;
24   }
25   
26   public void login(String JavaDoc username, String JavaDoc password, String JavaDoc host, int port, String JavaDoc jndiName) throws Exception JavaDoc{
27     this.username = username;
28     this.password = password;
29     if((host == null || host.trim().length() == 0) &&
30        (jndiName == null || jndiName.trim().length() == 0)){
31       registry = RegistryFactory.newDebugInstance();
32     }
33     else{
34       registry = RegistryFactory.newRemoteInstance(host, port, jndiName);
35     }
36     if(registry instanceof Authenticating){
37       try{
38         if(!((Authenticating)registry).authenticate(username, password)){
39           GlobalContext.getInstance().error("Invalid username or password");
40           registry.close();
41           System.exit(0);
42         }
43       }catch(SecurityException JavaDoc e){
44         GlobalContext.getInstance().error(e);
45         registry.close();
46         System.exit(0);
47       }
48     }
49     
50     GlobalContext.getInstance().getModelManager().getNodeModel().setNode(registry.getRoot());
51   }
52
53   public Registry getRegistry() {
54     if(registry == null) throw new IllegalStateException JavaDoc("Registry not set");
55     return registry;
56   }
57
58   public RenderContext getRenderContext() {
59     if(renderContext == null) throw new IllegalStateException JavaDoc("RenderContext not set");
60     return renderContext;
61   }
62
63   public String JavaDoc getPassword() {
64     return password;
65   }
66
67   public String JavaDoc getUsername() {
68     return username;
69   }
70
71   public ModelManager getModelManager() {
72     return modelManager;
73   }
74   
75   public void error(Throwable JavaDoc e){
76     JOptionPane.showMessageDialog(
77         getFrame(),
78         e.getMessage(),
79         "Error",
80         JOptionPane.ERROR_MESSAGE
81         );
82     e.printStackTrace();
83   }
84   
85   public void error(String JavaDoc error){
86     JOptionPane.showMessageDialog(
87         null,
88         error,
89         "Error",
90         JOptionPane.ERROR_MESSAGE
91         );
92   }
93   
94   public void success(String JavaDoc msg){
95     JOptionPane.showMessageDialog(
96         null,
97         msg,
98         "Success",
99         JOptionPane.INFORMATION_MESSAGE
100         );
101   }
102   
103   public void dispatchEvent(Object JavaDoc event){
104     EventManager.getInstance().dispatchEvent(event);
105   }
106   
107   public void dispatchTask(Task task){
108     EventManager.getInstance().dispatchEvent(new TaskEvent(task));
109   }
110   
111   public Object JavaDoc getWidget(String JavaDoc name){
112     Object JavaDoc w = renderContext.getEnv().get(name, WIDGETS_SCOPE);
113     if(w == null){
114       throw new IllegalStateException JavaDoc("No UI component found for " + name);
115     }
116     return w;
117   }
118   
119   public JFrame JavaDoc getFrame(){
120     return (JFrame JavaDoc)getWidget(FRAME_KEY);
121   }
122
123 }
124
Popular Tags