KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > GUI


1 import javax.swing.*;
2 import java.awt.*;
3 import java.io.*;
4 import java.util.*;
5 import org.jivesoftware.smack.*;
6 import javax.swing.plaf.*;
7 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
8 import org.jivesoftware.smack.packet.Packet;
9 import java.util.Date JavaDoc;
10 import java.util.Calendar JavaDoc;
11 import java.text.DateFormat JavaDoc;
12 import java.text.SimpleDateFormat JavaDoc;
13 import whisper.Message;
14
15 /** Staic methods that help in building the GUI.*/
16 public final class GUI{
17     
18     /** The wait mouse cursor.*/
19     public static final Cursor WAIT=Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
20     /** The normal mouse cursor.*/
21     public static final Cursor NORMAL=Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
22         
23     private static final DateFormat JavaDoc LOCAL_FORMAT=DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
24     private static final DateFormat JavaDoc LOCAL_TIME_FORMAT=DateFormat.getTimeInstance(DateFormat.SHORT);
25     private static final SimpleDateFormat JavaDoc WHISPER_FORMAT=new SimpleDateFormat JavaDoc("yyyy-MM-dd hh:mm:ss");
26     
27     private GUI(){
28         // do nothing - prevents initialisation
29
}
30     
31     /** Returns the current date and time as a localised string.*/
32     public static String JavaDoc getDate(){
33         return LOCAL_FORMAT.format(new Date JavaDoc());
34     }
35     
36     /** Returns the time as a localised string.*/
37     public static String JavaDoc getTime(){
38         return LOCAL_TIME_FORMAT.format(new Date JavaDoc());
39     }
40     
41     /** Localises the timestamp in a whisper message.*/
42     public static String JavaDoc getDate(whisper.Message m){
43         Calendar JavaDoc cal = Calendar.getInstance();
44         // Convert the UTC time to local time.
45
try{
46             cal.setTime(new Date JavaDoc(WHISPER_FORMAT.parse(m.getTime()).getTime()+cal.getTimeZone().getOffset(cal.getTimeInMillis())));
47             return LOCAL_FORMAT.format(cal.getTime());
48         }
49         catch(Exception JavaDoc e){
50             return "UTC:"+m.getTime();
51         }
52     }
53     
54     /** Localises the timestamp in a whisper message and returns the time only.*/
55     public static String JavaDoc getTime(whisper.Message m){
56         Calendar JavaDoc cal = Calendar.getInstance();
57         // Convert the UTC time to local time.
58
try{
59             cal.setTime(new Date JavaDoc(WHISPER_FORMAT.parse(m.getTime()).getTime()+cal.getTimeZone().getOffset(cal.getTimeInMillis())));
60             return LOCAL_TIME_FORMAT.format(cal.getTime());
61         }
62         catch(Exception JavaDoc e){
63             return "UTC:"+m.getTime();
64         }
65     }
66     
67     /** Returns the date and time a packet was sent.
68     * If the packet does not contain a jabber:x:delay extension,
69     * the current time and date is returned.
70     * @return The time and date as a localised String.*/

71     public static String JavaDoc getDate(Packet p){
72         if(p==null){
73             return getDate();
74         }
75         DelayExtension de=(DelayExtension)p.getExtension("x","jabber:x:delay");
76         if(de==null){
77             return getDate();
78         }
79         else{
80             return de.getLocalStamp();
81         }
82     }
83     
84     /** Returns the time a packet was sent.
85     * If the packet does not contain a jabber:x:delay extension,
86     * the current time is returned.
87     * @return The time as a localised String.*/

88     public static String JavaDoc getTime(Packet p){
89         if(p==null){
90             return getTime();
91         }
92         DelayExtension de=(DelayExtension)p.getExtension("x","jabber:x:delay");
93         if(de==null){
94             return getTime();
95         }
96         else{
97             return de.getLocalTime();
98         }
99     }
100     
101     /** Sets a label mnemonic, sets the label for a component and adds a tooltip.
102     * mnemonic and tooltip are internationalised useing the lang resource.
103     * tooltip my be null, in which case no tooltip is set.*/

104     public static final void connect(JLabel label, JComponent component,String JavaDoc mnemonic, String JavaDoc tooltip){
105         label.setLabelFor(component);
106         if(mnemonic!=null){
107             label.setDisplayedMnemonic(Lang.s2k(mnemonic));
108         }
109         if(tooltip!=null){
110             component.setToolTipText(Lang.gs(tooltip));
111         }
112     }
113     
114     public static final Box box(JLabel l,JComponent c){
115         final Box r=Box.createHorizontalBox();
116         r.add(l);
117         r.add(r.createGlue());
118         r.add(c);
119         r.add(r.createGlue());
120         return r;
121     }
122     
123     /** Returns a panel with the compoenets added, left alighned.*/
124     public static final JPanel panel(JComponent first,JComponent second, int gap){
125         JPanel panel=new JPanel(new FlowLayout(FlowLayout.LEFT,gap,gap));
126         panel.add(first);
127         panel.add(second);
128         return panel;
129     }
130     
131     /** Displays an error message, but does not stop application.
132     * The parameters are automatically translated.
133     * title may be null in which case, "error" is used as title.
134     * frame maybe null.*/

135     public static void showError(Component frame,String JavaDoc title, String JavaDoc msg){
136         if(title==null){
137             JOptionPane.showMessageDialog(frame,Lang.gs(msg),Lang.gs("error"), JOptionPane.ERROR_MESSAGE);
138         }
139         else{
140             JOptionPane.showMessageDialog(frame,Lang.gs(msg),Lang.gs(title), JOptionPane.ERROR_MESSAGE);
141         }
142     }
143     
144     /** Displays a warning message, but does not stop application.
145     * The parameters are automatically translated.
146     * title may be null in which case, "error" is used as title.
147     * frame maybe null.*/

148     public static void showWarning(Component frame,String JavaDoc title, String JavaDoc msg){
149         if(title==null){
150             JOptionPane.showMessageDialog(frame,Lang.gs(msg),Lang.gs("error"), JOptionPane.WARNING_MESSAGE);
151         }
152         else{
153             JOptionPane.showMessageDialog(frame,Lang.gs(msg),Lang.gs(title), JOptionPane.WARNING_MESSAGE);
154         }
155     }
156     
157     /** Displays a warning message, but does not stop application.
158     * The parameters are automatically translated.
159     * title may be null in which case, "error" is used as title.
160     * frame maybe null.*/

161     public static void showWarning(Component frame,String JavaDoc title, String JavaDoc msg, String JavaDoc additional){
162         if(title==null){
163             JOptionPane.showMessageDialog(frame,Lang.gs(msg)+"\r\n"+additional,Lang.gs("error"), JOptionPane.WARNING_MESSAGE);
164         }
165         else{
166             JOptionPane.showMessageDialog(frame,Lang.gs(msg)+"\r\n"+additional,Lang.gs(title), JOptionPane.WARNING_MESSAGE);
167         }
168     }
169     
170     /** Displays an error message, but does not stop application.
171     * The parameters msg and title are automatically translated.
172     * additional is *not* translated.
173     * title may be null in which case, "error" is used as title.
174     * frame maybe null.*/

175     public static void showError(Component frame,String JavaDoc title, String JavaDoc msg, String JavaDoc additional){
176         if(msg==null){msg="error";}
177         if(additional==null){additional="";}
178         if(title==null){
179             JOptionPane.showMessageDialog(frame,Lang.gs(msg)+"\r\n"+additional,Lang.gs("error"), JOptionPane.ERROR_MESSAGE);
180         }
181         else{
182             JOptionPane.showMessageDialog(frame,Lang.gs(msg)+"\r\n"+additional,Lang.gs(title), JOptionPane.ERROR_MESSAGE);
183         }
184     }
185     
186     /** Displays an XMPPException error message, but does not stop application.
187     * The parameters msg and title are automatically translated.
188     * The XMPPException is *not* translated.
189     * title may be null in which case, "error" is used as title. Additional may be null.
190     * frame maybe null.*/

191     public static void showError(Component frame,String JavaDoc title, String JavaDoc msg,String JavaDoc additional, XMPPException xe){
192         String JavaDoc detail=xe.getMessage();
193         if(additional==null){additional="";}
194         if(xe.getXMPPError()!=null){
195             if(xe.getXMPPError().getMessage()!=null){
196                 detail=xe.getXMPPError().getMessage();
197             }
198         }
199         showError(frame,title,msg,additional+"\r\n"+detail);
200     }
201     
202     /** Displays an info message.
203     * frame maybe null.*/

204     public static void showInfo(Component frame,String JavaDoc title, String JavaDoc msg){
205         JOptionPane.showMessageDialog(frame,Lang.gs(msg),Lang.gs(title), JOptionPane.INFORMATION_MESSAGE);
206     }
207     
208     public static void showInfo(Component frame,String JavaDoc title, String JavaDoc msg, String JavaDoc additional){
209         JOptionPane.showMessageDialog(frame,Lang.gs(msg)+"\r\n"+additional,Lang.gs(title), JOptionPane.INFORMATION_MESSAGE);
210     }
211     
212     /** Shows the about box.*/
213     public static void showAbout(Component parent){
214         WhisperIM.About.show(parent);
215     }
216     
217     /** Displays an error message and stops the application.
218     * msg and/or t may be null.
219     * @param msg A message to display
220     * @param t The error that caused the critical stop.*/

221     public static void errorStop(String JavaDoc msg, Throwable JavaDoc t){
222         if(msg==null){
223             msg=new String JavaDoc("Critical Error");
224         }
225         if(t!=null){
226             msg=msg+"\r\n"+t.toString();
227         }
228         try{ // Try to log the error detail to error.txt
229
System.err.println(new java.util.Date JavaDoc().toString()); // print date and time
230
System.err.println(msg); //print error message
231
if(t!=null){
232                 t.printStackTrace(System.err); // print stack trace
233
}
234             System.err.println(); // print blank line
235
System.err.close(); // close
236
}
237         catch (Exception JavaDoc e){
238             // we dont anything if there's an error logging the errror as we can't do anything about it
239
}
240         JOptionPane.showMessageDialog(null,msg,Lang.gs("error"), JOptionPane.ERROR_MESSAGE); //display dialog
241
System.exit(-1);
242     }
243     
244     /** Loads the user preferences.
245     * If there is an error, we report and use default preferences.*/

246     public static void loadPrefs(){
247         try{
248             WhisperIM.UserPref.load(new FileInputStream(new File(WhisperIM.userDir,WhisperIM.USER_PREF_FILE_NAME))); //load the preferences file
249
}
250         catch(Exception JavaDoc e){
251             WhisperIM.UserPref=new DefaultUserPref();
252             JOptionPane.showMessageDialog(WhisperIM.Login_Window.getFrame(),Lang.gs("cnlup")+e.toString()+")",Lang.gs("error"),
253                 JOptionPane.ERROR_MESSAGE);
254         }
255         // See if user has specified a language to use
256
if(!WhisperIM.UserPref.getProperty("lang").equals("auto")){//not using auto detect
257
loadLang();
258         }
259         WhisperIM.userPrefLoaded=true;
260     }
261     
262     /** Loads a language bundle based on user preferences.*/
263     public static void loadLang(){
264         String JavaDoc parts[]=WhisperIM.UserPref.getProperty("lang").split("_");
265         if(parts.length==0 || parts.length>3){
266             //shouldn't happen
267
System.err.println("Bad language preference:"+WhisperIM.UserPref.getProperty("lang"));
268             WhisperIM.lang=ListResourceBundle.getBundle("Lang"); //reset to default
269
return;
270         }
271         Locale l=null;
272         if(parts.length==1){
273             l=new Locale(parts[0]);
274         }
275         if(parts.length==2){
276             l=new Locale(parts[0],parts[1]);
277         }
278         if(parts.length==3){
279             l=new Locale(parts[0],parts[1],parts[2]);
280         }
281         WhisperIM.lang=ListResourceBundle.getBundle("Lang",l);
282     }
283         
284     
285     public static void setScrollBars(JScrollPane sp){
286         sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
287         if(WhisperIM.isMac){
288             sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
289         }
290         else{
291             sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
292         }
293     }
294     
295     public static String JavaDoc[] translate(String JavaDoc[] a){
296         String JavaDoc result[]=new String JavaDoc[a.length];
297         String JavaDoc txt;
298         for(int i=0;i<a.length;i++){
299             try{
300                 txt=(Lang.gs("form_"+a[i].trim().toLowerCase()));
301             }
302             catch(MissingResourceException e){
303                 txt=a[i];
304             }
305             result[i]=txt;
306         }
307         return result;
308     }
309         
310     public static String JavaDoc getHeader(){
311         return "<html><body bgcolor=\""+WhisperIM.UserPref.getProperty("background_colour")+"\"><font face=\""+WhisperIM.UserPref.getProperty("text_font")+"\" color=\""+WhisperIM.UserPref.getProperty("text_colour")+"\" size=\""+WhisperIM.UserPref.getProperty("text_size")+"\">";
312     }
313     
314     public static final String JavaDoc FOOTER="</font></body></html>";
315 }
Popular Tags