KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > common > DjenericTool


1 package com.genimen.djeneric.tools.common;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.Toolkit JavaDoc;
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.OutputStream JavaDoc;
11 import java.sql.SQLException JavaDoc;
12
13 import javax.swing.JFrame JavaDoc;
14 import javax.swing.JLabel JavaDoc;
15 import javax.swing.JOptionPane JavaDoc;
16 import javax.swing.RepaintManager JavaDoc;
17 import javax.swing.UIManager JavaDoc;
18 import javax.swing.plaf.ColorUIResource JavaDoc;
19
20 import com.genimen.djeneric.language.Messages;
21 import com.genimen.djeneric.repository.DjContext;
22 import com.genimen.djeneric.repository.DjCredentials;
23 import com.genimen.djeneric.repository.DjMessenger;
24 import com.genimen.djeneric.repository.DjRepositoryDescriptor;
25 import com.genimen.djeneric.repository.exceptions.DjenericException;
26 import com.genimen.djeneric.repository.exceptions.LogonException;
27 import com.genimen.djeneric.util.DjLogger;
28 import com.genimen.djeneric.util.DjProperties;
29 import com.genimen.djeneric.util.DjStatusDisplayer;
30 import com.genimen.djeneric.util.DjStringReplacer;
31 import com.incors.plaf.kunststoff.KunststoffLookAndFeel;
32 import com.incors.plaf.kunststoff.KunststoffTheme;
33
34 public class DjenericTool extends JFrame JavaDoc implements DjStatusDisplayer, DjMessenger
35 {
36
37   private static final long serialVersionUID = 1L;
38   private DjProperties _props = new DjProperties();
39   private String JavaDoc _loadedPropertiesFileName = null;
40
41   JLabel JavaDoc _lblStatus = null;
42   Color JavaDoc _normalColor = new Color JavaDoc(102, 102, 153);
43
44   public String JavaDoc getProperty(String JavaDoc key, String JavaDoc dflt)
45   {
46     return _props.getProperty(key, dflt);
47   }
48
49   public void setProperty(String JavaDoc key, String JavaDoc value)
50   {
51     _props.setProperty(key, value);
52   }
53
54   protected void loadProps(String JavaDoc fileName)
55   {
56     _loadedPropertiesFileName = fileName;
57     getPropsFrom(fileName);
58   }
59
60   protected void getPropsFrom(String JavaDoc fileName)
61   {
62     String JavaDoc absFileName = new File JavaDoc(fileName).getAbsoluteFile().toString();
63
64     try
65     {
66       InputStream JavaDoc ios = new FileInputStream JavaDoc(absFileName);
67       _props.load(ios);
68       ios.close();
69
70     }
71     catch (Exception JavaDoc x)
72     {
73       DjLogger.log(Messages.getString("DjenericTool.CouldNotOpenProps", absFileName));
74     }
75   }
76
77   protected void saveProps()
78   {
79     saveProps(_loadedPropertiesFileName);
80   }
81
82   protected void saveProps(String JavaDoc filename)
83   {
84     String JavaDoc absFileName = new File JavaDoc(filename).getAbsoluteFile().toString();
85
86     try
87     {
88       OutputStream JavaDoc os = new FileOutputStream JavaDoc(absFileName);
89
90       updateProperties();
91
92       _props.store(os, Messages.getString("DjenericTool.PropertiesFor", this.getClass().getName()));
93       os.close();
94     }
95     catch (Exception JavaDoc x)
96     {
97       JOptionPane.showMessageDialog(this, Messages.getString("DjenericTool.CouldNotWriteProps", absFileName));
98     }
99   }
100
101   protected void exitProgram()
102   {
103     saveProps();
104
105     System.exit(0);
106   }
107
108   protected void updateProperties()
109   {
110     System.err.println(Messages.getString("DjenericTool.NotUpdated", this.getClass().getName()));
111   }
112
113   public void setStatusLabel(JLabel JavaDoc lbl)
114   {
115     _lblStatus = lbl;
116     _normalColor = _lblStatus.getForeground();
117   }
118
119   public void setStatusMessage(String JavaDoc msg, boolean informative)
120   {
121     if ((msg == null) && !informative) msg = Messages.getString("global.Nullpointer");
122
123     if (_lblStatus == null)
124     {
125       System.err.println(msg);
126       return;
127     }
128
129     if (!informative) _lblStatus.setForeground(Color.red);
130     else _lblStatus.setForeground(_normalColor);
131     if (msg == null || msg.trim().length() == 0) msg = " ";
132
133     if (msg.indexOf("\n") != -1)
134     {
135       DjStringReplacer sr = new DjStringReplacer(msg);
136       msg = sr.replace("\n", " ");
137     }
138
139     _lblStatus.setText(msg);
140   }
141
142   public void setStatusMessageNow(String JavaDoc msg, boolean informative)
143   {
144     setStatusMessage(msg, informative);
145     if (_lblStatus == null) return;
146     RepaintManager JavaDoc rm = RepaintManager.currentManager(_lblStatus);
147     if (rm != null)
148     {
149       rm.addInvalidComponent(_lblStatus);
150       rm.paintDirtyRegions();
151     }
152   }
153
154   public void setStatusMessage(String JavaDoc msg)
155   {
156     setStatusMessage(msg, true);
157   }
158
159   public void setStatusMessage(Throwable JavaDoc e)
160   {
161     String JavaDoc msg = e.getMessage();
162     if (!(e instanceof DjenericException)) msg = e.getClass().getName() + ": " + msg;
163
164     e.printStackTrace();
165     setStatusMessage(msg, false);
166   }
167
168   public void handleException(Exception JavaDoc ex)
169   {
170     String JavaDoc msg = ex.getMessage();
171     if (ex instanceof DjenericException)
172     {
173       DjenericException de = (DjenericException) ex;
174
175       if (de.getRootException() instanceof SQLException JavaDoc)
176       {
177         msg = Messages.getString("DjenericTool.CouldNotConnect", de.getRootException().getMessage());
178       }
179       if (de.getRootException() instanceof ClassNotFoundException JavaDoc)
180       {
181         msg = Messages.getString("DjenericTool.CouldNotLoadClass", de.getRootException().getMessage());
182       }
183     }
184     else
185     {
186       msg = ex.getClass().getName() + ": " + msg;
187     }
188
189     setStatusMessage(msg, false);
190     JOptionPane.showMessageDialog(this, msg, Messages.getString("DjenericTool.CouldNotConnect1"),
191                                   JOptionPane.WARNING_MESSAGE);
192
193     ex.printStackTrace();
194   }
195
196   public DjProperties getProperties()
197   {
198     return _props;
199   }
200
201   public void startApp()
202   {
203     int BORDER_SIZE = 55;
204
205     validate();
206     Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
207     Dimension JavaDoc frameSize = getSize();
208     if (frameSize.height > screenSize.height - BORDER_SIZE)
209     {
210       frameSize.height = screenSize.height - BORDER_SIZE;
211     }
212     if (frameSize.width > screenSize.width - BORDER_SIZE)
213     {
214       frameSize.width = screenSize.width - BORDER_SIZE;
215     }
216     setSize(frameSize);
217     setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
218     checkVmVersion();
219     setVisible(true);
220   }
221
222   protected void checkVmVersion()
223   {
224     String JavaDoc version = System.getProperty("java.version");
225     if (version.startsWith("1.0") || version.startsWith("1.1") || version.startsWith("1.2")
226         || version.startsWith("1.3"))
227     {
228       JOptionPane.showMessageDialog(this, Messages.getString("DjenericTool.WrongVersion", version), "JDK " + version,
229                                     JOptionPane.WARNING_MESSAGE);
230     }
231   }
232
233   public static void setLookAndFeel()
234   {
235     try
236     {
237       KunststoffLookAndFeel klaf = new com.incors.plaf.kunststoff.KunststoffLookAndFeel();
238       KunststoffLookAndFeel.setCurrentTheme(new MyTheme());
239       UIManager.setLookAndFeel(klaf);
240       //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
241
}
242     catch (Exception JavaDoc x)
243     {
244       x.printStackTrace();
245     }
246
247   }
248
249   public DjCredentials getCredentials(DjRepositoryDescriptor repository) throws LogonException
250   {
251     LoginDialog ld = new LoginDialog(this, repository);
252     DjCredentials credentials = new DjCredentials();
253     credentials.setUserid(ld.getUserId());
254     credentials.setPassword(ld.getPassword());
255     return credentials;
256   }
257
258   public DjContext selectContext(DjContext[] ctxts) throws LogonException
259   {
260     if(ctxts.length == 1) return ctxts[0];
261     
262     ContextDialog cd = new ContextDialog(this, ctxts);
263     return cd.getContext();
264   }
265
266   public DjRepositoryDescriptor selectRepository(DjRepositoryDescriptor[] descriptors) throws DjenericException
267   {
268     RepositoryDialog dlg = new RepositoryDialog(this, descriptors);
269     return dlg.getRepository();
270   }
271
272   public void showMessage(String JavaDoc title, String JavaDoc msg)
273   {
274     JOptionPane.showMessageDialog(this, msg, title, JOptionPane.INFORMATION_MESSAGE);
275   }
276
277   public void showWarning(String JavaDoc title, String JavaDoc msg)
278   {
279     JOptionPane.showMessageDialog(this, msg, title, JOptionPane.WARNING_MESSAGE);
280   }
281
282 }
283
284 class MyTheme extends KunststoffTheme
285 {
286
287   private final ColorUIResource JavaDoc primary1 = new ColorUIResource JavaDoc(32, 32, 32);
288
289   private final ColorUIResource JavaDoc primary2 = new ColorUIResource JavaDoc(160, 160, 180);
290
291   private final ColorUIResource JavaDoc primary3 = new ColorUIResource JavaDoc(200, 200, 224);
292
293   private final ColorUIResource JavaDoc secondary1 = new ColorUIResource JavaDoc(130, 130, 130);
294
295   private final ColorUIResource JavaDoc secondary2 = new ColorUIResource JavaDoc(180, 180, 180);
296
297   private final ColorUIResource JavaDoc secondary3 = new ColorUIResource JavaDoc(214, 214, 214);
298
299   public String JavaDoc getName()
300   {
301     return "Default Kunststoff Theme";
302   }
303
304   protected ColorUIResource JavaDoc getPrimary1()
305   {
306     return primary1;
307   }
308
309   protected ColorUIResource JavaDoc getPrimary2()
310   {
311     return primary2;
312   }
313
314   protected ColorUIResource JavaDoc getPrimary3()
315   {
316     return primary3;
317   }
318
319   protected ColorUIResource JavaDoc getSecondary1()
320   {
321     return secondary1;
322   }
323
324   protected ColorUIResource JavaDoc getSecondary2()
325   {
326     return secondary2;
327   }
328
329   protected ColorUIResource JavaDoc getSecondary3()
330   {
331     return secondary3;
332   }
333
334 }
Popular Tags