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

Quick Search    Search Deep

Source code: com/arranger/jarl/shell/JarlShell.java


1   package com.arranger.jarl.shell;
2   
3   import com.arranger.jarl.shell.commands.BaseCommand;
4   import com.arranger.jarl.shell.models.BaseModel;
5   import com.arranger.jarl.shell.views.BaseView;
6   import com.arranger.jarl.shell.views.ViewUtil;
7   import com.arranger.jarl.shell.views.MainJarlShellFrame;
8   import com.arranger.jarl.util.Debug;
9   import com.arranger.jarl.util.IOUtil;
10  import com.arranger.jarl.util.StringTools;
11  
12  import java.util.*;
13  
14  /**
15   * JarlShell
16   */
17  public class JarlShell {
18  
19      //config
20      protected ShellConfig m_shellConfig;
21  
22      //gui
23      protected boolean m_fullGUI = true;
24      protected MainJarlShellFrame m_mainJarlShellFrame;
25  
26      //data
27      protected Map m_commands = new HashMap();
28      protected Map m_models = new HashMap();
29      protected Map m_viewModelMap = new HashMap();
30  
31      //runtime
32      protected boolean m_shouldQuit = false;
33  
34      public JarlShell(boolean fullGUI) throws Exception {
35          m_fullGUI = fullGUI;
36          initialize();
37      }
38  
39      public ShellConfig getShellConfig() {
40          return m_shellConfig;
41      }
42  
43      public boolean isFullGUI() {
44          return m_fullGUI;
45      }
46  
47      public MainJarlShellFrame getMainJarlShellFrame() {
48          return m_mainJarlShellFrame;
49      }
50  
51      public void setShouldQuit(boolean shouldQuit) {
52          m_shouldQuit = shouldQuit;
53      }
54  
55      public Map getCommands() {
56          return m_commands;
57      }
58  
59      public Map getModels() {
60          return m_models;
61      }
62  
63      public BaseModel getModel(String modelClassName) {
64          return (BaseModel) m_models.get(modelClassName);
65      }
66  
67      public void setModel(BaseModel baseModel) {
68          BaseModel currentModel = getModel(baseModel.getClass().getName());
69          if (baseModel.equals(currentModel)) {
70              return;
71          }
72          m_models.put(baseModel.getClass().getName(), baseModel);
73          initializeSubscribingViews(baseModel);
74      }
75  
76      public Map getViewModelMap() {
77          return m_viewModelMap;
78      }
79  
80      public void setViewModelMap(Map viewModelMap) {
81          m_viewModelMap = viewModelMap;
82      }
83  
84      public BaseModel createModel(String className) throws Exception {
85          BaseModel baseModel = (BaseModel) Class.forName(className).newInstance();
86          baseModel.initialize(this);
87          baseModel.load(getShellConfig());
88          return baseModel;
89      }
90  
91      public void out(String text) {
92          System.out.println(text);
93      }
94  
95      public void saveModels() throws Exception {
96          for (Iterator it = m_models.values().iterator(); it.hasNext();) {
97              ((BaseModel) it.next()).save(m_shellConfig);
98          }
99          m_shellConfig.save();
100     }
101 
102     public void run() throws Exception {
103         while (!m_shouldQuit) {
104             //get next command
105             String command = IOUtil.promptForInput("> ");
106             String[] commands = StringTools.getStringArrayFromDelimitedString(command, " ");
107             if (commands.length == 0) {
108                 continue;
109             }
110 
111             if (m_commands.containsKey(commands[0])) {
112                 try {
113                     ((BaseCommand) m_commands.get(commands[0])).invoke(commands);
114                 } catch (Exception e) {
115                     out("Exception occurred: " + e.getMessage());
116                     out(Debug.getStackTrace(e));
117                 }
118             } else {
119                 out("Command not found: " + commands[0]);
120             }
121         }
122 
123         System.exit(0);
124     }
125 
126     protected void initialize() throws Exception {
127         m_shellConfig = new ShellConfig();
128 
129         //commands
130         initializeCommands();
131 
132         //views
133         initializeViews();
134 
135         //models
136         initializeModels();
137 
138         if (m_fullGUI) {
139             m_mainJarlShellFrame = new MainJarlShellFrame(this, "JarlShell");
140         }
141     }
142 
143     protected void initializeCommands() throws Exception {
144         for (int index = 0; index < ICommands.COMMANDS.length; index++) {
145             BaseCommand baseCommand = (BaseCommand) Class.forName(ICommands.COMMANDS[index]).newInstance();
146             baseCommand.initialize(this);
147             m_commands.put(baseCommand.getCommand(), baseCommand);
148         }
149     }
150 
151     protected void initializeModels() throws Exception {
152         for (int index = 0; index < IModels.MODELS.length; index++) {
153             BaseModel baseModel = createModel(IModels.MODELS[index]);
154             setModel(baseModel);
155         }
156     }
157 
158     protected void initializeViews() throws Exception {
159         for (int index = 0; index < IViews.Views.length; index++) {
160             BaseView baseView = (BaseView) Class.forName(IViews.Views[index]).newInstance();
161             baseView.initialize(this);
162             String[] models = baseView.getModelsToSubscribe();
163             for (int i = 0; i < models.length; i++) {
164                 String modelClass = models[i];
165                 List viewList = (List) m_viewModelMap.get(modelClass);
166                 if (viewList == null) {
167                     viewList = new ArrayList();
168                     m_viewModelMap.put(modelClass, viewList);
169                 }
170 
171                 viewList.add(baseView);
172             }
173         }
174     }
175 
176     protected void initializeSubscribingViews(BaseModel baseModel) {
177         List list = (List) m_viewModelMap.get(baseModel.getClass().getName());
178         if (list != null) {
179             for (Iterator it = list.iterator(); it.hasNext();) {
180                 BaseView baseView = (BaseView) it.next();
181                 try {
182                     baseView = (BaseView) Class.forName(baseView.getClass().getName()).newInstance();
183                     baseView.initialize(this);
184                     baseModel.addListener(baseView);
185                 } catch (Exception e) {
186                     out(e.getMessage());
187                 }
188             }
189         }
190     }
191 
192     public static void main(String[] args) throws Exception {
193         ViewUtil.initNativeLookAndFeel();
194         boolean useGUI = true;
195         if (args.length == 1 && "console".equals(args[0])) {
196             useGUI = false;
197         }
198 
199         new JarlShell(useGUI).run();
200     }
201 }