KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > gui > GuiUtils


1 package org.sapia.regis.gui;
2
3 import java.awt.Dimension JavaDoc;
4 import java.awt.Toolkit JavaDoc;
5 import java.io.FileNotFoundException JavaDoc;
6 import java.io.InputStream JavaDoc;
7
8 import javax.swing.JComponent JavaDoc;
9 import javax.swing.JDialog JavaDoc;
10 import javax.swing.JFrame JavaDoc;
11
12 import org.sapia.gumby.RenderContext;
13
14 public class GuiUtils {
15
16   public static Dimension JavaDoc computeScreenCenter(JComponent JavaDoc comp){
17     Toolkit JavaDoc toolkit = Toolkit.getDefaultToolkit();
18     Dimension JavaDoc screenSize = toolkit.getScreenSize();
19     int x = (screenSize.width - comp.getWidth()) / 2;
20     int y = (screenSize.height - comp.getHeight()) / 2;
21     return new Dimension JavaDoc(x, y);
22   }
23   
24   public static Dimension JavaDoc computeScreenCenter(JDialog JavaDoc dia){
25     Toolkit JavaDoc toolkit = Toolkit.getDefaultToolkit();
26     Dimension JavaDoc screenSize = toolkit.getScreenSize();
27     int x = (screenSize.width - dia.getWidth()) / 2;
28     int y = (screenSize.height - dia.getHeight()) / 2;
29     return new Dimension JavaDoc(x, y);
30   }
31   
32   public static Dimension JavaDoc computeScreenCenter(JFrame JavaDoc frame){
33     Toolkit JavaDoc toolkit = Toolkit.getDefaultToolkit();
34     Dimension JavaDoc screenSize = toolkit.getScreenSize();
35     int x = (screenSize.width - frame.getWidth()) / 2;
36     int y = (screenSize.height - frame.getHeight()) / 2;
37     return new Dimension JavaDoc(x, y);
38   }
39   
40   public static void centerToScreen(JFrame JavaDoc frame){
41     Dimension JavaDoc dim = computeScreenCenter(frame);
42     frame.setLocation((int)dim.getWidth(), (int)dim.getHeight());
43   }
44   
45   public static void centerToScreen(JComponent JavaDoc comp){
46     Dimension JavaDoc dim = computeScreenCenter(comp);
47     comp.setLocation((int)dim.getWidth(), (int)dim.getHeight());
48   }
49   
50   public static void centerToScreen(JDialog JavaDoc dia){
51     Dimension JavaDoc dim = computeScreenCenter(dia);
52     dia.setLocation((int)dim.getWidth(), (int)dim.getHeight());
53   }
54   
55   public static Object JavaDoc render(RenderContext ctx, String JavaDoc resource) throws Exception JavaDoc{
56     InputStream JavaDoc is = Thread.currentThread()
57     .getContextClassLoader().getResourceAsStream("org/sapia/regis/gui/screens/" + resource + ".xml");
58     if(is == null){
59       throw new FileNotFoundException JavaDoc(resource);
60     }
61     try{
62       return ctx.render(is);
63     }finally{
64       is.close();
65     }
66   }
67 }
68
69
70
Popular Tags