Source code: org/schooltool/client/gui/controller/ApplicationController.java
1 /*
2 * ApplicationController.java
3 *
4 * Created on April 10, 2001, 3:25 PM
5 */
6
7 package org.schooltool.client.gui.controller;
8
9 import org.schooltool.client.gui.model.ApplicationModel;
10 import org.schooltool.client.gui.view.ApplicationView;
11 import org.schooltool.client.gui.model.FrameModel;
12 import org.schooltool.client.gui.view.FrameView;
13 import java.util.Hashtable;
14 import org.schooltool.client.gui.events.*;
15 import org.schooltool.client.gui.utilities.props;
16 import java.net.URL;
17
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import javax.swing.JOptionPane;
23 import org.schooltool.ejb.session.client.access.*;
24
25 import javax.naming.*;
26 import javax.jnlp.*;
27
28
29 /**
30 *
31 * @author root
32 * @version
33 */
34
35
36 public class ApplicationController{
37 private FrameController childController;
38 private ApplicationModel model;
39 private ApplicationView view;
40 //An application can have many ChildControllers...Future implementation
41 //but can do the piping now already
42 //Do this by holding a list to all ChildControllers the last controller set
43 //becomes the active controller
44 private Hashtable listOfChildControllers;
45 private String applicationPropertyFile;
46 private props ApplicationProperties;
47 public static boolean startViaWebstart = false;
48
49
50 /** Holds value of property hostIp. */
51 private String hostIp;
52
53 public ApplicationController() {
54 listOfChildControllers = new Hashtable();
55 model = null;
56 view = null;
57 applicationPropertyFile = null;
58 getProperties();
59 }
60
61 public ApplicationController(String propertyFile) {
62 listOfChildControllers = new Hashtable();
63 model = null;
64 view = null;
65 applicationPropertyFile = propertyFile;
66 getProperties();
67 }
68
69 private void getProperties(){
70 if (!startViaWebstart){
71 String currentDirectory = System.getProperty("user.dir");
72 File directory = new File(currentDirectory);
73 props applicationProperties = new props(directory.getPath() + File.separatorChar + "resource" + File.separatorChar + "application.properties");
74 if (applicationProperties.getErrorMessage() != null){
75 JOptionPane.showMessageDialog(null, "!!! Error Message while trying to load properties application.properties");
76 System.exit(0);
77 }
78 String hostIp = applicationProperties.getProperty("host_ip");
79 this.setHostIp(hostIp);
80 }else{
81 javax.jnlp.BasicService bs = null;
82 try{
83 bs = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
84 } catch(UnavailableServiceException ue) {
85 // JOptionPane.showMessageDialog(null, ue.getMessage());
86 this.setHostIp("localhost");
87 return;
88 }
89 URL codebase = bs.getCodeBase();
90 this.setHostIp(codebase.getHost());
91 }
92 }
93
94 public void setApplicationPropertiesFile(String propertyFile){
95 applicationPropertyFile = propertyFile;
96 }
97
98 public String getApplicationPropertiesFile(){
99 return applicationPropertyFile;
100 }
101
102 public void setView(ApplicationView appView){
103 view = appView;
104 view.setController(this);
105 }
106 public ApplicationView getView(){
107 return view;
108 }
109
110 public void setModel(ApplicationModel appModel){
111 model = appModel;
112 }
113
114 public ApplicationModel getModel(){
115 return model;
116 }
117
118 public void setChildController(FrameController pChildController){
119 if (childController != null){
120 //First remove any child controller with the same name
121 listOfChildControllers.remove(childController.getClass().getName());
122 }
123 childController = pChildController;
124 //Add to list
125 listOfChildControllers.put(childController.getClass().getName(), childController);
126 }
127
128 public void init(){
129 createChildTriad();
130 childController.init();
131 view.init();
132 model.setView(view);
133 model.init();
134
135 }
136
137
138 //Application Speaks to Frame so set up Frame trio
139 public void createChildTriad(){
140 if (view == null)
141 view = new ApplicationView("Default Application View");
142 FrameView frameView = new FrameView(view.getRenderer());
143 FrameModel frameModel = new FrameModel();
144 frameModel.setHostIp(this.getHostIp());
145
146 FrameController frameController = new FrameController();
147 frameController.setModel(frameModel);
148 frameController.setView(frameView);
149 frameController.setParentController(this);
150 frameModel.setController(frameController);
151 setChildController(frameController);
152 }
153
154 public void handleEvents(EventInterface event){
155 }
156
157 public static void main(String args[]){
158 if (args.length > 0){
159 if (args[0] != null && args[0].equals("MANUAL_INSTALL")){
160 startViaWebstart = false;
161 }else{
162 startViaWebstart = true;
163 }
164 }else{
165 startViaWebstart = true;
166 }
167
168 ApplicationView view = new ApplicationView("SchoolTool version 1.0");
169 ApplicationModel model = new ApplicationModel();
170 ApplicationController ctlr = new ApplicationController();
171 ctlr.setView(view);
172 ctlr.setModel(model);
173 ctlr.init();
174 }
175
176 /** Getter for property hostIp.
177 * @return Value of property hostIp.
178 */
179 public String getHostIp() {
180 return hostIp;
181 }
182
183 /** Setter for property hostIp.
184 * @param hostIp New value of property hostIp.
185 */
186 public void setHostIp(String hostIp) {
187 this.hostIp = hostIp;
188 }
189
190 }