/* * UFO Saved Game Editor * Copyright (C) 2010 Christopher Davoren * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package net.rubikscomplex.ufosge.util; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.Insets; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.text.NumberFormat; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFormattedTextField; import javax.swing.JLabel; import javax.swing.JTextField; import net.rubikscomplex.ufosge.gui.NumericInputVerifier; /** * Utility class - a collection of useful methods that are used by different * classes in a range of contexts. * * @author Chris Davoren */ public class Util { /** * Default constructor. It is protected because all methods of this * class are static. */ protected Util() { } /** * Returns the integer value of a JTextField. If the value does not parse * to an integer, returns 0. * * @param field the field from which to parse the integer. * @return the integer value of the field text. If the value doesn't parse, * returns 0. */ public static int parseTextFieldInteger(JTextField field) { String fText = field.getText(); if (fText.length() == 0) { return 0; } else { try { return Integer.parseInt(fText); } catch (NumberFormatException e) { return 0; } } } /** * Forces the display of a component tooltop by sending it a CTRL-F1 key event. * * @param component the component whose tooltip will be displayed. */ public static void displayTooltip(JComponent component) { component.dispatchEvent(new KeyEvent(component, KeyEvent.KEY_PRESSED, 0, KeyEvent.CTRL_MASK, KeyEvent.VK_F1, KeyEvent.CHAR_UNDEFINED)); } /** * A convenience method for creating a JFormattedTextField that accepts only * numerical integer input between the given limits. * * @param minValue the maximum allowable value in the field. * @param maxValue the minimum allowable value in the field. * @return the new JFormattedTextField. */ public static JFormattedTextField createValidatedNumericField(long minValue, long maxValue) { NumberFormat formatter = NumberFormat.getInstance(); formatter.setMaximumFractionDigits(0); formatter.setParseIntegerOnly(true); JFormattedTextField field = new JFormattedTextField(formatter); field = new JFormattedTextField(formatter); field.setInputVerifier(new NumericInputVerifier(minValue, maxValue)); field.setToolTipText("Value must be between " + formatter.format(minValue) + " and " + formatter.format(Integer.MAX_VALUE)); return field; } /** * Utility method to add an input control and an optional label to a component with a GridBagLayout. * The method assumes a two-column layout in which the label is in the left column and the input * control in the right. * * @param parent the component with a GridBagLayout that is acting as an input form. * @param input the input control to add. * @param label an optional label. If this value is null, the input control will span both columns. * @param row the row to add the component to. * @param column the column to use as the left hand side (usually label) column. */ public static void addFormRow(JComponent parent, JComponent input, String label, int row, int column) { addFormRow(input, parent, label, row, column, false); } /** * Utility method to add an input control and an optional label to a component with a GridBagLayout. * The method assumes a two-column layout in which the label is in the left column and the input * control in the right. * * @param parent the component with a GridBagLayout that is acting as an input form. * @param input the input control to add. * @param label an optional label. If this value is null, the input control will span both columns. * @param row the row to add the component to. * @param column the column to use as the left hand side (usually label) column. * @param lastRow indicates whether this is the last row in the form. If it is, the weighting of * the row is adjusted so that it takes up all remaining vertical space. */ public static void addFormRow(JComponent parent, JComponent input, String label, int row, int column, boolean lastRow) { GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.NORTHWEST; gbc.gridx = column; gbc.gridy = row; gbc.weightx = 0.0; gbc.weighty = lastRow ? 1.0 : 0.0; if (label == null) { gbc.gridwidth = 2; gbc.weightx = 1.0; parent.add(input, gbc); } else { gbc.insets = new Insets(3, 3, 3, 3); JLabel formLabel = new JLabel(label + ":"); formLabel.setLabelFor(input); // System.out.println("gridx: " + gbc.gridx + " | gridy: " + gbc.gridy + " | weightx: " + gbc.weightx + " | weighty: " + gbc.weighty); parent.add(formLabel, gbc); gbc.gridx++; gbc.weightx = 1.0; // System.out.println("gridx: " + gbc.gridx + " | gridy: " + gbc.gridy + " | weightx: " + gbc.weightx + " | weighty: " + gbc.weighty); parent.add(input, gbc); } } /** * Convenience method that sets both the preferred and maximum size of a JButton. * * @param button the button whose dimensions will be set. * @param width the desired width of the button. * @param height the desired height of the button. */ public static void setButtonSize(JButton button, int width, int height) { Dimension bDim = new Dimension(width, height); button.setPreferredSize(bDim); button.setMaximumSize(bDim); } /** * Convenience method for 'clearing' a BufferedImage. Sets all pixels to * black and completely transparent. * * @param image the image to clear. */ public static void clearImage(BufferedImage image) { int width = image.getWidth(); int height = image.getHeight(); int numpix = width * height; int[] argbClear = new int[width * height]; for(int i = 0; i < width * height; i++) { argbClear[i] = 0x00000000; } image.setRGB(0, 0, width, height, argbClear, 0, width); } }