Coverage Report - net.mtu.eggplant.util.gui.GraphicsUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
GraphicsUtils
0%
0/73
0%
0/20
1.714
 
 1  
 /*
 2  
  * Copyright (c) 2000
 3  
  *      Jon Schewe.  All rights reserved
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  * 1. Redistributions of source code must retain the above copyright
 9  
  *    notice, this list of conditions and the following disclaimer.
 10  
  * 2. Redistributions in binary form must reproduce the above copyright
 11  
  *    notice, this list of conditions and the following disclaimer in the
 12  
  *    documentation and/or other materials provided with the distribution.
 13  
  *
 14  
  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 15  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 17  
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 18  
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 19  
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 20  
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 21  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 22  
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 23  
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 24  
  * SUCH DAMAGE.
 25  
  *
 26  
  * I'd appreciate comments/suggestions on the code jpschewe@mtu.net
 27  
  */
 28  
 package net.mtu.eggplant.util.gui;
 29  
 
 30  
 import java.awt.BorderLayout;
 31  
 import java.awt.Component;
 32  
 import java.awt.Container;
 33  
 import java.awt.Dimension;
 34  
 import java.awt.FontMetrics;
 35  
 import java.awt.Graphics;
 36  
 import java.awt.Point;
 37  
 import java.awt.Polygon;
 38  
 import java.awt.Window;
 39  
 import java.awt.geom.Dimension2D;
 40  
 import java.awt.geom.Point2D;
 41  
 import java.awt.geom.Rectangle2D;
 42  
 import java.util.Collection;
 43  
 import java.util.Iterator;
 44  
 
 45  
 import javax.swing.ImageIcon;
 46  
 import javax.swing.JComboBox;
 47  
 import javax.swing.JDialog;
 48  
 import javax.swing.JFrame;
 49  
 import javax.swing.JOptionPane;
 50  
 import javax.swing.JTable;
 51  
 
 52  
 /**
 53  
  * graphics functions that should exist in java, but don't
 54  
  * 
 55  
  * @author Jon Schewe
 56  
  * @version $Revision$
 57  
  */
 58  
 public final class GraphicsUtils {
 59  
 
 60  0
   private GraphicsUtils() {
 61  
     // no instances
 62  0
   }
 63  
 
 64  
   /**
 65  
    * Defaults title to null.
 66  
    * 
 67  
    * @see #basicGUIMain(Component, boolean, String)
 68  
    */
 69  
   public static Window basicGUIMain(final Component c,
 70  
                                     final boolean dialog) {
 71  0
     return basicGUIMain(c, dialog, null);
 72  
   }
 73  
 
 74  
   /**
 75  
    * a basic main for testing a graphical class. Takes the component and puts in
 76  
    * in a JFrame or JDialog and shows the window.
 77  
    * 
 78  
    * @param c the component to display
 79  
    * @param dialog if <code>true</code> put in a JDialog, otherwise put in a
 80  
    *          JFrame
 81  
    * @param title the title for the window
 82  
    **/
 83  
   public static Window basicGUIMain(final Component c,
 84  
                                     final boolean dialog,
 85  
                                     final String title) {
 86  
     final Container container;
 87  
     final Window window;
 88  0
     if (dialog) {
 89  0
       window = new JDialog((Window) null, title);
 90  0
       container = ((JDialog) window).getContentPane();
 91  
     } else {
 92  0
       window = new JFrame(title);
 93  0
       container = ((JFrame) window).getContentPane();
 94  
     }
 95  
 
 96  0
     window.setSize(c.getPreferredSize());
 97  0
     window.addWindowListener(new BasicWindowMonitor());
 98  0
     container.setLayout(new BorderLayout());
 99  0
     container.add(c, BorderLayout.CENTER);
 100  0
     centerWindow(window);
 101  0
     window.pack();
 102  0
     centerWindow(window);
 103  0
     window.setVisible(true);
 104  
 
 105  0
     return window;
 106  
   }
 107  
 
 108  
   /**
 109  
    * Centers the window on the screen.
 110  
    */
 111  
   public static void centerWindow(final Window window) {
 112  0
     final Rectangle2D screenSize = window.getGraphicsConfiguration().getBounds();
 113  0
     final Point2D screenCenter = new Point2D.Double(screenSize.getWidth() / 2, screenSize.getHeight() / 2);
 114  0
     final Dimension2D windowSize = window.getSize();
 115  0
     final Point location = new Point();
 116  0
     location.setLocation(new Point2D.Double(screenCenter.getX()
 117  0
         - windowSize.getWidth() / 2, screenCenter.getY()
 118  0
         - windowSize.getHeight() / 2));
 119  0
     window.setLocation(location);
 120  0
   }
 121  
 
 122  
   /**
 123  
    * draw the points on the Graphics Context. Uses Graphics.drawPolyLine.
 124  
    * 
 125  
    * @param g the graphics context
 126  
    * @param points a collection of points, anything other than a point in this
 127  
    *          vector is simply skipped
 128  
    **/
 129  
   public static void drawPolyLine(final Graphics g,
 130  
                                   final Collection<?> points) {
 131  0
     final int[] xpoints = new int[points.size()];
 132  0
     final int[] ypoints = new int[points.size()];
 133  0
     int npoints = 0;
 134  0
     final Iterator<?> iter = points.iterator();
 135  0
     while (iter.hasNext()) {
 136  0
       Object obj = iter.next();
 137  0
       if (obj instanceof Point) {
 138  0
         final Point p = (Point) obj;
 139  0
         xpoints[npoints] = p.x;
 140  0
         ypoints[npoints] = p.y;
 141  0
         npoints++;
 142  
       }
 143  0
     }
 144  
 
 145  0
     g.drawPolyline(xpoints, ypoints, npoints);
 146  0
   }
 147  
 
 148  
   /**
 149  
    * draw a bunch of polygons
 150  
    * 
 151  
    * @param g the graphics context
 152  
    * @param v a Container of polygons, other classes are ignored
 153  
    **/
 154  
   public static void drawPolygons(final Graphics g,
 155  
                                   final Collection<?> v) {
 156  0
     drawPolygons(g, v.iterator());
 157  0
   }
 158  
 
 159  
   /**
 160  
    * draw a bunch of polygons
 161  
    * 
 162  
    * @param g the graphics context
 163  
    * @param iter an Enumeration of polygons, other classes are ignored
 164  
    **/
 165  
   public static void drawPolygons(final Graphics g,
 166  
                                   final Iterator<?> iter) {
 167  0
     while (iter.hasNext()) {
 168  0
       final Object obj = iter.next();
 169  0
       if (obj instanceof Polygon) {
 170  0
         g.drawPolygon((Polygon) obj);
 171  
       }
 172  0
     }
 173  0
   }
 174  
 
 175  
   /**
 176  
    * fill a bunch of polygons
 177  
    * 
 178  
    * @param g the graphics context
 179  
    * @param v a Container of polygons, other classes are ignored
 180  
    **/
 181  
   public static void fillPolygons(final Graphics g,
 182  
                                   final Collection<?> v) {
 183  0
     fillPolygons(g, v.iterator());
 184  0
   }
 185  
 
 186  
   /**
 187  
    * fill a bunch of polygons
 188  
    * 
 189  
    * @param g the graphics context
 190  
    * @param iter an Enumeration of polygons, other classes are ignored
 191  
    **/
 192  
   public static void fillPolygons(final Graphics g,
 193  
                                   final Iterator<?> iter) {
 194  0
     while (iter.hasNext()) {
 195  0
       final Object obj = iter.next();
 196  0
       if (obj instanceof Polygon) {
 197  0
         g.fillPolygon((Polygon) obj);
 198  
       }
 199  0
     }
 200  0
   }
 201  
 
 202  
   public static int getMaxWidth(final JComboBox<String> combo,
 203  
                                 final FontMetrics fm) {
 204  0
     int maxLen = 0;
 205  0
     for (int i = 0; i < combo.getItemCount(); i++) {
 206  0
       String str = combo.getItemAt(i);
 207  0
       int wi = fm.stringWidth(str);
 208  0
       if (wi > maxLen) {
 209  0
         maxLen = wi;
 210  
       }
 211  
     }
 212  0
     return maxLen + 20; // leave room for the scroll bar
 213  
   }
 214  
 
 215  
   /**
 216  
    * Create an icon from the resource at path.
 217  
    * 
 218  
    * @pre (path != null)
 219  
    **/
 220  
   public static ImageIcon getIcon(final String path) {
 221  0
     return new ImageIcon(Thread.currentThread().getContextClassLoader().getResource(path));
 222  
   }
 223  
 
 224  
   /**
 225  
    * Popup a warning dialog displaying <tt>message</tt>.
 226  
    **/
 227  
   public static void notImplemented(final String message) {
 228  0
     JOptionPane.showMessageDialog(null, message, "Not Implemented", JOptionPane.WARNING_MESSAGE);
 229  0
   }
 230  
 
 231  
   /**
 232  
    * Popup an error dialog with <tt>message</tt> in it.
 233  
    **/
 234  
   public static void error(final String message) {
 235  0
     JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.WARNING_MESSAGE);
 236  0
   }
 237  
 
 238  
   /**
 239  
    * Set the preferred viewport size on a table based upon the number of rows
 240  
    * that should be visible and the current heights of the rows. Based upon code
 241  
    * from http://www.javalobby.org/java/forums/t19559.html
 242  
    * 
 243  
    * @param table the table
 244  
    * @param rows number of rows to have visible without scrolling
 245  
    */
 246  
   public static void setVisibleRowCount(final JTable table,
 247  
                                         final int rows) {
 248  0
     int height = 0;
 249  0
     for (int row = 0; row < rows; row++) {
 250  0
       height += table.getRowHeight(row);
 251  
     }
 252  
 
 253  0
     table.setPreferredScrollableViewportSize(new Dimension(table.getPreferredScrollableViewportSize().width, height));
 254  0
   }
 255  
 
 256  
 }