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