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.java2d.RasterGraphics;
  42: import gnu.x11.Display;
  43: 
  44: import java.awt.Font;
  45: import java.awt.Graphics2D;
  46: import java.awt.GraphicsDevice;
  47: import java.awt.GraphicsEnvironment;
  48: import java.awt.image.BufferedImage;
  49: import java.io.File;
  50: import java.io.FileInputStream;
  51: import java.io.FileNotFoundException;
  52: import java.io.IOException;
  53: import java.util.ArrayList;
  54: import java.util.Locale;
  55: import java.util.Properties;
  56: 
  57: /**
  58:  * Represents the X environment for AWT.
  59:  *
  60:  * @author Roman Kennke (kennke@aicas.com)
  61:  */
  62: public class XGraphicsEnvironment
  63:   extends GraphicsEnvironment
  64: {
  65: 
  66:   /**
  67:    * The default graphics device. This is normally the local main X
  68:    * Display, but can be configured to be any X connection.
  69:    */
  70:   private XGraphicsDevice defaultDevice;
  71: 
  72:   /**
  73:    * All configured devices.
  74:    */
  75:   private XGraphicsDevice[] devices;
  76: 
  77:   /**
  78:    * Creates a new XGraphicsEnvironment. This loads the configuration if
  79:    * there is one present and initializes the XGraphicsDevices in the
  80:    * environment. If there is no configuration, then there is one
  81:    * default device initialized with the local main X device.
  82:    */
  83:   public XGraphicsEnvironment()
  84:   {
  85:     // Initiliaze the devices.
  86:     Properties props = new Properties();
  87:     File config = new File(System.getProperty("user.home"),
  88:                            ".xawt.properties");
  89: 
  90:     try
  91:       {
  92:         FileInputStream configIn = new FileInputStream(config);
  93:         props.load(configIn);
  94:         int dev = 1;
  95:         ArrayList deviceList = new ArrayList();
  96:         while (true)
  97:           {
  98:             String propName = "display." + dev;
  99:             String propValue = props.getProperty(propName);
 100:             if (propValue != null)
 101:               {
 102:                 Display.Name displayName = new Display.Name(propValue);
 103:                 XGraphicsDevice device = new XGraphicsDevice(displayName);
 104:                 if (dev == 1)
 105:                   defaultDevice = device;
 106:                 deviceList.add(device);
 107:                 dev++;
 108:               }
 109:             else
 110:               {
 111:                 if (dev == 1)
 112:                   {
 113:                     defaultDevice = initDefaultDevice();
 114:                     deviceList.add(defaultDevice);
 115:                   }
 116:                 break;
 117:               }
 118:           }
 119:         devices = (XGraphicsDevice[]) deviceList.toArray
 120:                                       (new XGraphicsDevice[deviceList.size()]);
 121:       }
 122:     catch (FileNotFoundException ex)
 123:       {
 124:         defaultDevice = initDefaultDevice();
 125:         devices = new XGraphicsDevice[]{ defaultDevice };
 126:       }
 127:     catch (IOException ex)
 128:       {
 129:         defaultDevice = initDefaultDevice();
 130:         devices = new XGraphicsDevice[]{ defaultDevice };
 131:       }
 132:     
 133:   }
 134: 
 135:   /**
 136:    * Helper method that initializes the default device in the case when there
 137:    * is no configuration for the default.
 138:    */
 139:   private XGraphicsDevice initDefaultDevice()
 140:   {
 141:     String display = System.getenv("DISPLAY");
 142:     if (display == null)
 143:       display = ":0.0";
 144:     Display.Name displayName = new Display.Name(display);
 145:     return new XGraphicsDevice(displayName);
 146:   }
 147: 
 148:   /**
 149:    * Returns all configured screen devices.
 150:    *
 151:    * @return all configured screen devices
 152:    */
 153:   public GraphicsDevice[] getScreenDevices()
 154:   {
 155:     // We return a copy so that nobody can fiddle with our devices.
 156:     XGraphicsDevice[] copy = new XGraphicsDevice[devices.length];
 157:     System.arraycopy(devices, 0, copy, 0, devices.length);
 158:     return copy;
 159:   }
 160: 
 161:   /**
 162:    * Returns the default screen device.
 163:    *
 164:    * @return the default screen device
 165:    */
 166:   public GraphicsDevice getDefaultScreenDevice()
 167:   {
 168:     return defaultDevice;
 169:   }
 170: 
 171:   /**
 172:    * Returns a Graphics instance suitable for drawing on top of the
 173:    * BufferedImage.
 174:    *
 175:    * @param image the buffered image to create a graphics for
 176:    *
 177:    * @return a Graphics2D instance for drawing on the BufferedImage
 178:    */
 179:   public Graphics2D createGraphics(BufferedImage image)
 180:   {
 181:     return new RasterGraphics(image.getRaster(), image.getColorModel());
 182:   }
 183: 
 184:   public Font[] getAllFonts()
 185:   {
 186:     // TODO: Implement this.
 187:     throw new UnsupportedOperationException("Not yet implemented.");
 188:   }
 189: 
 190:   public String[] getAvailableFontFamilyNames()
 191:   {
 192:     // TODO: Implement this.
 193:     throw new UnsupportedOperationException("Not yet implemented.");
 194:   }
 195: 
 196:   public String[] getAvailableFontFamilyNames(Locale l)
 197:   {
 198:     // TODO: Implement this.
 199:     throw new UnsupportedOperationException("Not yet implemented.");
 200:   }
 201: 
 202: }