| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 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 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public final class GraphicsUtils { |
| 59 | |
|
| 60 | 0 | private GraphicsUtils() { |
| 61 | |
|
| 62 | 0 | } |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public static Window basicGUIMain(final Component c, |
| 70 | |
final boolean dialog) { |
| 71 | 0 | return basicGUIMain(c, dialog, null); |
| 72 | |
} |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 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 | |
|
| 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 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 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 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
public static void drawPolygons(final Graphics g, |
| 155 | |
final Collection<?> v) { |
| 156 | 0 | drawPolygons(g, v.iterator()); |
| 157 | 0 | } |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 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 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
public static void fillPolygons(final Graphics g, |
| 182 | |
final Collection<?> v) { |
| 183 | 0 | fillPolygons(g, v.iterator()); |
| 184 | 0 | } |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 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; |
| 213 | |
} |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
public static ImageIcon getIcon(final String path) { |
| 221 | 0 | return new ImageIcon(Thread.currentThread().getContextClassLoader().getResource(path)); |
| 222 | |
} |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 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 | |
|
| 233 | |
|
| 234 | |
public static void error(final String message) { |
| 235 | 0 | JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.WARNING_MESSAGE); |
| 236 | 0 | } |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 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 | |
} |