Source code: dexter/core/Dexter.java
1 /*
2 This file is part of DeXter - Java Internet Communication Solution
3 Copyright (c) 2002 Tobias Riemer
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package dexter.core;
21
22 import dexter.property.*;
23 import javax.swing.*;
24 import java.awt.*;
25 import java.io.*;
26 import java.awt.event.*;
27 import java.util.Vector;
28 import java.util.HashMap;
29 import java.awt.Color;
30
31 public class Dexter implements PropertyChangedListener, PropertyInputListener {
32
33 public static boolean trayIcon = false;
34
35 private ResourceFactory resourceFactory = new ResourceFactory();
36 private dexter.events.EventDispatcher eventDispatcher;
37 protected HashMap services = new HashMap();
38 public static MainFrame mainframe;
39 private String home = "";
40 private String plugindir = null;
41
42 private HashMap pluginConf = new HashMap();
43 private PropertyWizard setupWizard;
44
45 private PropertyFile propertyFile = new PropertyFile();
46 private Property pServices = new Property("Services", Property.VECTOR_VALUE, true);
47 private Property pSkinPath = new Property("Skin Path", Property.OBJECT_VALUE);
48
49 static protected Dexter dexter;
50
51 /**
52 * Constructor
53 */
54
55 public Dexter() {
56 this(null);
57 }
58
59 private String getDefaultDir() {
60 System.out.println("looking for home dir...");
61 String home = System.getProperty( "user.home" ) + File.separator + ".dexter";
62 System.out.println("...HOME=" + home);
63 return home;
64 }
65
66 public Dexter(String home) {
67
68 if (home == null) home = getDefaultDir();
69
70 dexter = this;
71 this.home = home;
72 File hf = new File(home);
73 if (!hf.exists()) hf.mkdir();
74 if (!hf.exists()) {
75 System.err.println("Can't create dexters home dir: " + home);
76 System.exit(-1);
77 }
78
79 Vector serviceStrings = new Vector();
80
81 System.out.println("loading kunstoff...");
82 try {
83 UIManager.setLookAndFeel(new com.incors.plaf.kunststoff.KunststoffLookAndFeel());
84 } catch (Exception ex) {
85 }
86 System.out.println("...done");
87
88 System.out.println("searching for installed plugins...");
89 Vector ch = new Vector();
90
91 Vector cfs = PluginConfigurationFile.getPluginConfigurationFiles("SERVICE");
92 for (int i=0;i<cfs.size();i++) {
93 PluginConfigurationFile cf = (PluginConfigurationFile) cfs.get(i);
94 if (cf.getProperty("CLASS") != null) {
95 ch.add(new PropertyVectorEntry(cf.getProperty("CLASS"),getClass().getResource("/plugins/" + cf.getProperty("CLASS") + "/about.en.html")));
96 pluginConf.put(cf.getProperty("CLASS"),cf);
97 }
98 }
99
100
101 System.out.println("loading properties...");
102 propertyFile.setFileName(getHomeDir() + "main");
103 PropertyGroup pg = new PropertyGroup("Plugins", new ImageIcon(getClass().getResource("/dexter/images/plug3.gif")));
104 propertyFile.addGroup(pg);
105
106 pg.addProperty(pServices);
107
108 pServices.setDisplayType(Property.VECTOR_CHOICE);
109
110 pServices.addPropertyChangedListener(this);
111 pServices.addPropertyInputListener(this);
112
113 pg = new PropertyGroup("Look&Feel", new ImageIcon(getClass().getResource("/dexter/images/plug3.gif")));
114 propertyFile.addGroup(pg);
115
116 pg.addProperty(pSkinPath);
117 pSkinPath.addPropertyChangedListener(this);
118 pSkinPath.setPropertyEditor(new ImageFolderSelectPane());
119 pSkinPath.setObject(new ImageFolderProperty("/dexter/images"));
120 Vector ve = new Vector();
121
122 pServices.setChoiceList(ch);
123 pServices.setVector(ve);
124 if (!propertyFile.loadFile()) {
125 setupWizard = new PropertyWizard("Dexter", null, true, propertyFile);
126 setupWizard.setVisible(true);
127 setupWizard = null;
128 ve = pServices.getVector();
129 }
130
131 System.out.println("...done");
132
133 System.out.println("loading icons....");
134 loadIcons();
135 System.out.println("...done");
136 loadSkinFile();
137
138 System.out.println("creating mainframe...");
139 mainframe = new MainFrame();
140 mainframe.pack();
141 mainframe.setVisible(true);
142 System.out.println("...done");
143 System.out.println("adding plugins...");
144 for(int i=0;i<ve.size();i++) {
145 try {
146 String s = ve.get(i).toString();
147 addService(s);
148 } catch (Exception ex) {
149 ex.printStackTrace();
150 }
151 }
152
153 System.out.println("...done");
154 }
155
156 private void loadSkinFile() {
157 if (!isDefaultIconPath()) {
158 SkinFile sf = new SkinFile(getSkinFileName());
159 sf.updateUI();
160 }
161 }
162
163 private void loadIcons() {
164
165 if (isDefaultIconPath()) {
166 resourceFactory.setIconPath(null);
167 } else {
168 resourceFactory.setIconPath(getIconPath());
169 resourceFactory.registerIcon("dexter","TREE_BARICON", "bar.gif");
170 }
171 resourceFactory.registerIcon("dexter", "PANEL_MINIMIZE", "min.gif");
172 resourceFactory.registerIcon("dexter", "PANEL_MAXIMIZE", "max.gif");
173 resourceFactory.registerIcon("dexter", "PANEL_FULLSCREEN","full.gif");
174 resourceFactory.registerIcon("dexter", "PANEL_TO_TRAY", "tray.gif");
175
176 }
177
178 public void propertyChanged(Property p) {
179
180 if (mainframe == null) return ;
181 if (p == pServices) {
182 Vector oldS = p.getOldVector();
183 Vector newS = p.getVector();
184 Vector newT = new Vector();
185
186 for(int j=0;j<newS.size();j++) {
187 newT.add(newS.get(j));
188
189 }
190
191 for (int i=0;i<oldS.size();i++) {
192 String o = oldS.get(i).toString();
193 boolean found = false;
194 for(int j=0;(j<newT.size()) && (found == false);j++) {
195 String n = newT.get(j).toString();
196 if (o.equals(n)) {
197 found = true;
198 newT.remove(j);
199 }
200 }
201 if (!found)
202 removeService(o);
203 }
204 for(int j=0;j<newT.size();j++) {
205 addService(newT.get(j).toString());
206 }
207 mainframe.reorderPanels();
208 }
209
210 if (p == pSkinPath) {
211 System.out.println("SKIN CHANGE");
212 JOptionPane.showMessageDialog(mainframe,"Skin changes takes effect after restarting DeXter");
213 //loadIcons();
214 //loadSkinFile();
215 //mainframe.updateUI();
216 }
217 }
218
219 public PropertyFile getPropertyFile() {
220 return propertyFile;
221 }
222
223
224 public void addService(String name) {
225 ServiceName sn = new ServiceName(name);
226
227 try {
228 Class c = Class.forName(sn.getFullClassname());
229 Object o = c.newInstance();
230
231 Service s = (Service) o;
232 s.setId(sn.getId().intValue());
233 services.put(sn.getId(),s);
234 s.init();
235 this.mainframe.addService(s);
236 } catch (Exception ex) {
237 ex.printStackTrace();
238 }
239 }
240
241 public HashMap getServices() {
242 return services;
243 }
244
245 public Vector getServiceStrings() {
246 return pServices.getVector();
247 }
248
249 public void removeService(String name) {
250 ServiceName sn = new ServiceName(name);
251 Service s = (Service) services.get(sn.getId());
252 services.remove(sn.getId());
253 this.mainframe.removeService(s);
254 }
255
256 static public Color getMainColor() {
257 return new Color((float)0.20,(float)0.20,(float)0.70);
258 }
259
260 public String getHomeDir() {
261 return home + File.separator;
262 }
263
264 synchronized public PropertyWizard getSetupWizard() {
265 if (setupWizard == null)
266 setupWizard = new PropertyWizard("Dexter",null,true);
267 return setupWizard;
268 }
269
270 public ImageIcon getIcon() {
271 return new javax.swing.ImageIcon(getClass().getResource("/dexter/images/icon.gif"));
272 }
273
274 public String getPluginDir() {
275 if (plugindir == null) {
276 java.net.URL url = getClass().getResource("/plugins/");
277 if (url != null) {
278 plugindir = Dexter.repairFileName(url.getFile());
279 } else plugindir = "";
280 System.out.println("plugindir="+plugindir);
281 }
282 return plugindir;
283 }
284
285 public dexter.events.EventDispatcher getEventDispatcher() {
286 if (eventDispatcher == null) eventDispatcher = new dexter.events.EventDispatcher();
287 return eventDispatcher;
288 }
289
290 public ResourceFactory getResourceFactory() {
291 return resourceFactory;
292 }
293
294 public void propertyInput(Property p) {
295 if (p == pServices) {
296
297 Vector nev = pServices.getNewVector();
298 int maxid = 0;
299 for(int i=0; i<nev.size(); i++) {
300 String s = nev.get(i).toString();
301 int in = s.indexOf(":");
302 if (in != -1) {
303 int id = 0;
304 try {
305 id = Integer.decode(s.substring(in+1)).intValue();
306 } catch (Exception e) {}
307 if (id > maxid) {
308 maxid = id;
309 }
310 }
311 }
312
313 maxid ++;
314 for(int i=0; i<nev.size(); i++) {
315 String s = nev.get(i).toString();
316 int in = s.indexOf(":");
317 if (in == -1) {
318 s = s + ":" + maxid;
319 System.out.println(s);
320 nev.set(i,s);
321 maxid ++;
322
323 ServiceName sn = new ServiceName(s);
324 Service service = (Service) services.get(sn.getId());
325 PluginConfigurationFile pf = (PluginConfigurationFile) pluginConf.get(sn.getClassname());
326
327 String status = pf.getProperty("STATUS");
328 if (status == null) status = "ALPHA";
329 if (status.equals("ALPHA"))
330 // mainframe.showMsgBox("This plugin is an alpa version. It might be very buggy!");
331 JOptionPane.showMessageDialog(null, "This plugin is an alpa version. It might be very buggy!","Warning Message", JOptionPane.WARNING_MESSAGE);
332 if (status.equals("BETA"))
333 //mainframe.showMsgBox("This plugin is an beta version. Some bugs may be hidden!");
334 JOptionPane.showMessageDialog(null, "This plugin is an beta version. Some bugs may be hidden!","Warning Message", JOptionPane.WARNING_MESSAGE);
335
336 }
337 }
338
339 }
340 }
341
342 private String getIconPath() {
343 return ((ImageFolderProperty) pSkinPath.getObject()).getPath();
344 }
345
346 private String getSkinFileName() {
347 return ((ImageFolderProperty) pSkinPath.getObject()).getSkin();
348 }
349
350 private String getDefaultIconPath() {
351 return ((ImageFolderProperty) pSkinPath.getObject()).getStandardPath();
352 }
353
354 private boolean isDefaultIconPath() {
355 return ((ImageFolderProperty) pSkinPath.getObject()).isStandard();
356 }
357
358 public class SwingPopup extends JPopupMenu {
359
360 public SwingPopup() {
361 super("TrayIcon");
362 }
363
364 public void showMenu(Point pt) {
365 JFrame frame = new DummyComponent();
366 Dimension d = this.getSize();
367 show(frame, pt.x, pt.y-d.height-50);
368 updateUI();
369 }
370 }
371
372 class DummyComponent extends JFrame {
373
374 public Point getLocationOnScreen() {
375 return new Point(0,0);
376 }
377
378 }
379
380 public static void main(String[] args) {
381 if (args.length == 0)
382 new Dexter();
383 if (args.length == 2)
384 if (args[0].equals("-home")) new Dexter(args[1]);
385 else printUsage();
386 else printUsage();
387 }
388
389 static public void printUsage() {
390 System.out.println("usage: dexter [-home homedir]");
391 }
392
393 public static Dexter getInstance() {
394 return dexter;
395 }
396
397 public static ResourceFactory resourceFactory() {
398 if (dexter == null) return null;
399 return dexter.getResourceFactory();
400 }
401
402 public static String repairFileName(String fname) {
403 try {
404 fname = java.net.URLDecoder.decode(fname,"UTF-8");
405 } catch ( java.io.UnsupportedEncodingException ex) {}
406 if (fname.charAt(2) == ':') {
407 return fname.substring(1);
408 }
409 return fname;
410 }
411 }