Source for gnu.java.awt.peer.x.XGraphicsEnvironment

   1: /* XGraphicsEnvironment.java -- Represents the X environment
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.awt.peer.x;
  40: 
  41: import gnu.java.awt.font.OpenTypeFontPeer;
  42: import gnu.java.awt.java2d.RasterGraphics;
  43: import gnu.x11.Display;
  44: 
  45: import java.awt.Font;
  46: import java.awt.Graphics2D;
  47: import java.awt.GraphicsDevice;
  48: import java.awt.GraphicsEnvironment;
  49: import java.awt.image.BufferedImage;
  50: import java.io.File;
  51: import java.io.FileInputStream;
  52: import java.io.FileNotFoundException;
  53: import java.io.IOException;
  54: import java.util.ArrayList;
  55: import java.util.Locale;
  56: import java.util.Properties;
  57: 
  58: /**
  59:  * Represents the X environment for AWT.
  60:  *
  61:  * @author Roman Kennke (kennke@aicas.com)
  62:  */
  63: public class XGraphicsEnvironment
  64:   extends GraphicsEnvironment
  65: {
  66: 
  67:   /**
  68:    * The default graphics device. This is normally the local main X
  69:    * Display, but can be configured to be any X connection.
  70:    */
  71:   private XGraphicsDevice defaultDevice;
  72: 
  73:   /**
  74:    * All configured devices.
  75:    */
  76:   private XGraphicsDevice[] devices;
  77: 
  78:   /**
  79:    * Creates a new XGraphicsEnvironment. This loads the configuration if
  80:    * there is one present and initializes the XGraphicsDevices in the
  81:    * environment. If there is no configuration, then there is one
  82:    * default device initialized with the local main X device.
  83:    */
  84:   public XGraphicsEnvironment()
  85:   {
  86:     // Initiliaze the devices.
  87:     Properties props = new Properties();
  88:     File config = new File(System.getProperty("user.home"),
  89:                            ".xawt.properties");
  90: 
  91:     try
  92:       {
  93:         FileInputStream configIn = new FileInputStream(config);
  94:         props.load(configIn);
  95:         int dev = 1;
  96:         ArrayList deviceList = new ArrayList();
  97:         while (true)
  98:           {
  99:             String propName = "display." + dev;
 100:             String propValue = props.getProperty(propName);
 101:             if (propValue != null)
 102:               {
 103:                 Display.Name displayName = new Display.Name(propValue);
 104:                 XGraphicsDevice device = new XGraphicsDevice(displayName);
 105:                 if (dev == 1)
 106:                   defaultDevice = device;
 107:                 deviceList.add(device);
 108:                 dev++;
 109:               }
 110:             else
 111:               {
 112:                 if (dev == 1)
 113:                   {
 114:                     defaultDevice = initDefaultDevice();
 115:                     deviceList.add(defaultDevice);
 116:                   }
 117:                 break;
 118:               }
 119:           }
 120:         devices = (XGraphicsDevice[]) deviceList.toArray
 121:                                       (new XGraphicsDevice[deviceList.size()]);
 122:       }
 123:     catch (FileNotFoundException ex)
 124:       {
 125:         defaultDevice = initDefaultDevice();
 126:         devices = new XGraphicsDevice[]{ defaultDevice };
 127:       }
 128:     catch (IOException ex)
 129:       {
 130:         defaultDevice = initDefaultDevice();
 131:         devices = new XGraphicsDevice[]{ defaultDevice };
 132:       }
 133: 
 134:   }
 135: 
 136:   /**
 137:    * Helper method that initializes the default device in the case when there
 138:    * is no configuration for the default.
 139:    */
 140:   private XGraphicsDevice initDefaultDevice()
 141:   {
 142:     String display = System.getenv("DISPLAY");
 143:     if (display == null)
 144:       display = ":0.0";
 145:     Display.Name displayName = new Display.Name(display);
 146:     return new XGraphicsDevice(displayName);
 147:   }
 148: 
 149:   /**
 150:    * Returns all configured screen devices.
 151:    *
 152:    * @return all configured screen devices
 153:    */
 154:   public GraphicsDevice[] getScreenDevices()
 155:   {
 156:     // We return a copy so that nobody can fiddle with our devices.
 157:     XGraphicsDevice[] copy = new XGraphicsDevice[devices.length];
 158:     System.arraycopy(devices, 0, copy, 0, devices.length);
 159:     return copy;
 160:   }
 161: 
 162:   /**
 163:    * Returns the default screen device.
 164:    *
 165:    * @return the default screen device
 166:    */
 167:   public GraphicsDevice getDefaultScreenDevice()
 168:   {
 169:     return defaultDevice;
 170:   }
 171: 
 172:   /**
 173:    * Returns a Graphics instance suitable for drawing on top of the
 174:    * BufferedImage.
 175:    *
 176:    * @param image the buffered image to create a graphics for
 177:    *
 178:    * @return a Graphics2D instance for drawing on the BufferedImage
 179:    */
 180:   public Graphics2D createGraphics(BufferedImage image)
 181:   {
 182:     return new RasterGraphics(image.getRaster(), image.getColorModel());
 183:   }
 184: 
 185:   public Font[] getAllFonts()
 186:   {
 187:     // TODO: Implement this.
 188:     throw new UnsupportedOperationException("Not yet implemented.");
 189:   }
 190: 
 191:   public String[] getAvailableFontFamilyNames()
 192:   {
 193:     return getAvailableFontFamilyNames(Locale.getDefault());
 194:   }
 195: 
 196:   public String[] getAvailableFontFamilyNames(Locale l)
 197:   {
 198:     // TODO: This doesn't work when we are using X fonts.
 199:     // Fix this.
 200:     return OpenTypeFontPeer.getAvailableFontFamilyNames(l);
 201:   }
 202: 
 203: }