Revision: 14646 http://gate.svn.sourceforge.net/gate/?rev=14646&view=rev Author: markagreenwood Date: 2011-12-03 15:32:26 +0000 (Sat, 03 Dec 2011) Log Message: ----------- added some new utility classes that are needed by the new creole manager but might be useful elsewhere whithin GATE, the rest of the new code will appear later
Added Paths: ----------- gate/trunk/src/gate/swing/CheckBoxTableCellRenderer.java gate/trunk/src/gate/swing/SpringUtilities.java gate/trunk/src/gate/util/VersionComparator.java Added: gate/trunk/src/gate/swing/CheckBoxTableCellRenderer.java =================================================================== --- gate/trunk/src/gate/swing/CheckBoxTableCellRenderer.java (rev 0) +++ gate/trunk/src/gate/swing/CheckBoxTableCellRenderer.java 2011-12-03 15:32:26 UTC (rev 14646) @@ -0,0 +1,97 @@ +/* + * CheckBoxTableCellRenderer.java + * + * Copyright (c) 2011, The University of Sheffield. See the file COPYRIGHT.txt + * in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt + * + * This file is part of GATE (see http://gate.ac.uk/), and is free software, + * licenced under the GNU Library General Public License, Version 2, June 1991 + * (in the distribution as file licence.html, and also available at + * http://gate.ac.uk/gate/licence.html). + * + * Mark A. Greenwood, 27/11/2011 + */ + +package gate.swing; + +import java.awt.Color; +import java.awt.Component; + +import javax.swing.BorderFactory; +import javax.swing.JCheckBox; +import javax.swing.JTable; +import javax.swing.UIManager; +import javax.swing.border.Border; +import javax.swing.table.TableCellRenderer; + +/** + * A TableCellRenderer for JCheckBox that disables the checkbox when the cell + * isn't editable to make it clear that you can't click on it + * + * @author Mark A. Greenwood + */ +@SuppressWarnings("serial") +public class CheckBoxTableCellRenderer extends JCheckBox implements + TableCellRenderer { + + /** + * An empty border we use when the cell doesn't have focus + */ + private static final Border NO_FOCUS = BorderFactory.createEmptyBorder(1, 1, + 1, 1); + + public CheckBoxTableCellRenderer() { + super(); + + // centre the checkbox within the cell + setHorizontalAlignment(JCheckBox.CENTER); + + // make sure we always paint the cell border + setBorderPainted(true); + + // make sure we always paint the background + setOpaque(true); + } + + public Component getTableCellRendererComponent(JTable table, Object value, + boolean isSelected, boolean hasFocus, int row, int column) { + + // this is needed for Nimbus which has alternative rows in different colours + // hopefully other L&Fs that also do this use the same UIManager key + Color alternate = UIManager.getColor("Table.alternateRowColor"); + + // strangely the background color from Nimbus doesn't render properly unless + // we convert it in this way. I'm guessing the problem is to do with the + // DerivedColor class that nimbus uses + Color normal = new Color(table.getBackground().getRGB()); + + if(isSelected) { + // if the cell is selected then set it's colors from the table + setForeground(table.getSelectionForeground()); + setBackground(table.getSelectionBackground()); + } else { + // if the cell isn't selected set it's colors taking into account the + // possibility of alternating colors that Nimbus throws into the mix + setForeground(table.getForeground()); + setBackground(alternate != null && row % 2 == 0 ? alternate : normal); + } + + // if the cell isn't editable then disable the checkbox to give some visual + // feedback to the user + setEnabled(table.isCellEditable(row, column)); + + // make the checkbox reflect the underlying boolean data + setSelected(value != null && (Boolean)value); + + if(hasFocus) { + // if the cell has focus then draw the border set by the L&F + setBorder(UIManager.getBorder("Table.focusCellHighlightBorder")); + } else { + // if the cell doesn't have the focus then draw the empty border + setBorder(NO_FOCUS); + } + + // now return the checkbox for rendering into the table + return this; + } +} \ No newline at end of file Added: gate/trunk/src/gate/swing/SpringUtilities.java =================================================================== --- gate/trunk/src/gate/swing/SpringUtilities.java (rev 0) +++ gate/trunk/src/gate/swing/SpringUtilities.java 2011-12-03 15:32:26 UTC (rev 14646) @@ -0,0 +1,225 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package gate.swing; + +import javax.swing.*; +import javax.swing.SpringLayout; +import java.awt.*; + +/** + * A 1.4 file that provides utility methods for + * creating form- or grid-style layouts with SpringLayout. + * These utilities are used by several programs, such as + * SpringBox and SpringCompactGrid. + */ +public class SpringUtilities { + /** + * A debugging utility that prints to stdout the component's + * minimum, preferred, and maximum sizes. + */ + public static void printSizes(Component c) { + System.out.println("minimumSize = " + c.getMinimumSize()); + System.out.println("preferredSize = " + c.getPreferredSize()); + System.out.println("maximumSize = " + c.getMaximumSize()); + } + + /** + * Aligns the first <code>rows</code> * <code>cols</code> + * components of <code>parent</code> in + * a grid. Each component is as big as the maximum + * preferred width and height of the components. + * The parent is made just big enough to fit them all. + * + * @param rows number of rows + * @param cols number of columns + * @param initialX x location to start the grid at + * @param initialY y location to start the grid at + * @param xPad x padding between cells + * @param yPad y padding between cells + */ + public static void makeGrid(Container parent, + int rows, int cols, + int initialX, int initialY, + int xPad, int yPad) { + SpringLayout layout; + try { + layout = (SpringLayout)parent.getLayout(); + } catch (ClassCastException exc) { + System.err.println("The first argument to makeGrid must use SpringLayout."); + return; + } + + Spring xPadSpring = Spring.constant(xPad); + Spring yPadSpring = Spring.constant(yPad); + Spring initialXSpring = Spring.constant(initialX); + Spring initialYSpring = Spring.constant(initialY); + int max = rows * cols; + + //Calculate Springs that are the max of the width/height so that all + //cells have the same size. + Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)). + getWidth(); + Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)). + getHeight(); + for (int i = 1; i < max; i++) { + SpringLayout.Constraints cons = layout.getConstraints( + parent.getComponent(i)); + + maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth()); + maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight()); + } + + //Apply the new width/height Spring. This forces all the + //components to have the same size. + for (int i = 0; i < max; i++) { + SpringLayout.Constraints cons = layout.getConstraints( + parent.getComponent(i)); + + cons.setWidth(maxWidthSpring); + cons.setHeight(maxHeightSpring); + } + + //Then adjust the x/y constraints of all the cells so that they + //are aligned in a grid. + SpringLayout.Constraints lastCons = null; + SpringLayout.Constraints lastRowCons = null; + for (int i = 0; i < max; i++) { + SpringLayout.Constraints cons = layout.getConstraints( + parent.getComponent(i)); + if (i % cols == 0) { //start of new row + lastRowCons = lastCons; + cons.setX(initialXSpring); + } else { //x position depends on previous component + cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), + xPadSpring)); + } + + if (i / cols == 0) { //first row + cons.setY(initialYSpring); + } else { //y position depends on previous row + cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), + yPadSpring)); + } + lastCons = cons; + } + + //Set the parent's size. + SpringLayout.Constraints pCons = layout.getConstraints(parent); + pCons.setConstraint(SpringLayout.SOUTH, + Spring.sum( + Spring.constant(yPad), + lastCons.getConstraint(SpringLayout.SOUTH))); + pCons.setConstraint(SpringLayout.EAST, + Spring.sum( + Spring.constant(xPad), + lastCons.getConstraint(SpringLayout.EAST))); + } + + /* Used by makeCompactGrid. */ + private static SpringLayout.Constraints getConstraintsForCell( + int row, int col, + Container parent, + int cols) { + SpringLayout layout = (SpringLayout) parent.getLayout(); + Component c = parent.getComponent(row * cols + col); + return layout.getConstraints(c); + } + + /** + * Aligns the first <code>rows</code> * <code>cols</code> + * components of <code>parent</code> in + * a grid. Each component in a column is as wide as the maximum + * preferred width of the components in that column; + * height is similarly determined for each row. + * The parent is made just big enough to fit them all. + * + * @param rows number of rows + * @param cols number of columns + * @param initialX x location to start the grid at + * @param initialY y location to start the grid at + * @param xPad x padding between cells + * @param yPad y padding between cells + */ + public static void makeCompactGrid(Container parent, + int rows, int cols, + int initialX, int initialY, + int xPad, int yPad) { + SpringLayout layout; + try { + layout = (SpringLayout)parent.getLayout(); + } catch (ClassCastException exc) { + System.err.println("The first argument to makeCompactGrid must use SpringLayout."); + return; + } + + //Align all cells in each column and make them the same width. + Spring x = Spring.constant(initialX); + for (int c = 0; c < cols; c++) { + Spring width = Spring.constant(0); + for (int r = 0; r < rows; r++) { + width = Spring.max(width, + getConstraintsForCell(r, c, parent, cols). + getWidth()); + } + for (int r = 0; r < rows; r++) { + SpringLayout.Constraints constraints = + getConstraintsForCell(r, c, parent, cols); + constraints.setX(x); + constraints.setWidth(width); + } + x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad))); + } + + //Align all cells in each row and make them the same height. + Spring y = Spring.constant(initialY); + for (int r = 0; r < rows; r++) { + Spring height = Spring.constant(0); + for (int c = 0; c < cols; c++) { + height = Spring.max(height, + getConstraintsForCell(r, c, parent, cols). + getHeight()); + } + for (int c = 0; c < cols; c++) { + SpringLayout.Constraints constraints = + getConstraintsForCell(r, c, parent, cols); + constraints.setY(y); + constraints.setHeight(height); + } + y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad))); + } + + //Set the parent's size. + SpringLayout.Constraints pCons = layout.getConstraints(parent); + pCons.setConstraint(SpringLayout.SOUTH, y); + pCons.setConstraint(SpringLayout.EAST, x); + } +} Added: gate/trunk/src/gate/util/VersionComparator.java =================================================================== --- gate/trunk/src/gate/util/VersionComparator.java (rev 0) +++ gate/trunk/src/gate/util/VersionComparator.java 2011-12-03 15:32:26 UTC (rev 14646) @@ -0,0 +1,137 @@ +/* + * VersionComparator.java + * + * Copyright (c) 2011, The University of Sheffield. See the file COPYRIGHT.txt + * in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt + * + * This file is part of GATE (see http://gate.ac.uk/), and is free software, + * licenced under the GNU Library General Public License, Version 2, June 1991 + * (in the distribution as file licence.html, and also available at + * http://gate.ac.uk/gate/licence.html). + * + * Mark A. Greenwood, 26/11/2011 + */ + +package gate.util; + +import gate.Main; + +import java.util.Comparator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * A comparator that can be used for comparing GATE version strings. This + * includes comparing SNAPSHOT versions as well as releases. This is needed to + * be able to determine if a CREOLE plugin is compatible with a specific GATE + * version. + * + * @author Mark A. Greenwood + */ +public class VersionComparator implements Comparator<String> { + + // this pattern matches any dot separated sequence of digits + private static final Pattern VERSION_PATTERN = Pattern + .compile("(\\d+)(?:\\.(\\d+))*"); + + @Override + public int compare(String v1, String v2) { + return compareVersions(v1, v2); + } + + public static int compareVersions(String v1, String v2) { + + // if the two strings are identical then they must represent the same + // version so just return quickly + if(v1.equalsIgnoreCase(v2)) return 0; + + // use the regex to find the digit groupings + Matcher m1 = VERSION_PATTERN.matcher(v1); + Matcher m2 = VERSION_PATTERN.matcher(v2); + + if(!m1.find() | !m2.find()) { + // these don't represent versions so sort as strings + return v1.compareTo(v2); + } + + // get the maximum number of digit groups + int groups = Math.max(m1.groupCount(), m2.groupCount()); + + for(int i = 1; i <= groups; i++) { + // for each digit group get the string and convert it to an int + // if the version string doesn't have this group then set it to 0 + int g1 = (m1.group(i) != null ? Integer.parseInt(m1.group(i)) : 0); + int g2 = (m2.group(i) != null ? Integer.parseInt(m2.group(i)) : 0); + + // now compare the numeric value of the group and return as appropriate + if(g1 < g2) return -1; + if(g1 > g2) return 1; + } + + // we have checked all the numbers and the versions are equal + // so let's check if either is a snapshot (snapshots are considered lower + // than non-snapshot versions with the same number) + boolean v1IsSnapshot = v1.toUpperCase().endsWith("-SNAPSHOT"); + boolean v2IsSnapshot = v2.toUpperCase().endsWith("-SNAPSHOT"); + + if(v1IsSnapshot && !v2IsSnapshot) return -1; + if(!v1IsSnapshot && v2IsSnapshot) return 1; + + // if we get all the way to here then the two strings represent the same + // version + return 0; + } + + /** + * Returns true if the specified version is the same or newer than the version + * of GATE being used. + * + * @param version + * the version number to check + * @return true if the specified version is the same or newer than the version + * of GATE being used, false otherwise + */ + public static boolean isGATENewEnough(String version) { + if(version == null) return true; + + version = version.trim(); + + if(version.equals("") || version.equals("*")) return true; + + return (compareVersions(Main.version, version) >= 0); + } + + /** + * Returns true if the specified version is the same or older than the version + * of GATE being used. + * + * @param version + * the version number to check + * @return true if the specified version is the same or older than the version + * of GATE being used, false otherwise + */ + public static boolean isGATEOldEnough(String version) { + if(version == null) return true; + + version = version.trim(); + + if(version.equals("") || version.equals("*")) return true; + + return (compareVersions(Main.version, version) <= 0); + } + + /** + * Checks to see if the version of GATE falls between the two specified + * versions (this is an inclusive check). + * + * @param min + * the minimum compatible GATE version + * @param max + * the maximum compatible GATE version + * @return if the version of GATE falls between the two specified versions + * (this is an inclusive check), false otherwise + */ + public static boolean isCompatible(String min, String max) { + return isGATENewEnough(min) && isGATEOldEnough(max); + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-novd2d _______________________________________________ GATE-cvs mailing list GATE-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gate-cvs