[Schmitzm-commits] r1807 - in trunk/schmitzm-core/src/main/java/de/schmitzm: io lang swing temp

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Dec 16 12:21:13 CET 2011


Author: mojays
Date: 2011-12-16 12:21:13 +0100 (Fri, 16 Dec 2011)
New Revision: 1807

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileSourceList.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileSourceMap.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/temp/BaseTypeUtil.java
Log:
SelectableJTable: different background for alternating columns
SwingUtil: method determining default background color from L&F
LangUtil.stringConcatWithSep(.): handle native arrays specified as object list 
BaseTypeUtil: handling of native type arrays

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileSourceList.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileSourceList.java	2011-12-12 21:21:10 UTC (rev 1806)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileSourceList.java	2011-12-16 11:21:13 UTC (rev 1807)
@@ -28,7 +28,7 @@
 import java.util.Vector;
 
 /**
- * A {@link Vector} which is bases on a source file.
+ * A {@link Vector} which is based on a source file.
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
  */
 public class FileSourceList<E> extends Vector<E> implements FileSource {

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileSourceMap.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileSourceMap.java	2011-12-12 21:21:10 UTC (rev 1806)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileSourceMap.java	2011-12-16 11:21:13 UTC (rev 1807)
@@ -29,7 +29,7 @@
 import java.util.Vector;
 
 /**
- * A {@link HashMap} which is bases on a source file.
+ * A {@link HashMap} which is based on a source file.
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
  */
 public class FileSourceMap<K,V> extends HashMap<K,V> implements FileSource {

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2011-12-12 21:21:10 UTC (rev 1806)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2011-12-16 11:21:13 UTC (rev 1807)
@@ -68,6 +68,7 @@
 
 import de.schmitzm.io.IOUtil;
 import de.schmitzm.regex.RegexCache;
+import de.schmitzm.temp.BaseTypeUtil;
 
 /**
  * Diese Klasse stellt Hilfsfunktionen fuer Basisoperationen bereit.
@@ -599,6 +600,14 @@
 	 */
 	public static String stringConcatWithSep(String sep, String maskChar, boolean ignoreNulls, Object... str) {
 		StringBuffer sb = new StringBuffer();
+		
+		// wenn statt einem Object-Array ein nativer Array angegeben wird,
+		// wird dieser nur als ein Parameter behandelt
+		// -> In Objekt-Liste umkopieren
+		if ( str.length == 1 && BaseTypeUtil.isNativeArray(str[0]) ) {
+		  str = BaseTypeUtil.copyToList(str[0], Object.class).toArray();
+		}
+		
 		for (int i = 0; str != null && i < str.length; i++) {
 			if (ignoreNulls && (str[i] == null || str[i].toString().trim().equals("")))
 				continue;

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java	2011-12-12 21:21:10 UTC (rev 1806)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java	2011-12-16 11:21:13 UTC (rev 1807)
@@ -29,6 +29,7 @@
  ******************************************************************************/
 package de.schmitzm.swing;
 
+import java.awt.Color;
 import java.awt.Component;
 import java.util.Vector;
 
@@ -48,9 +49,17 @@
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
  */
 public class SelectableJTable extends JTable {
+  /** A very light gray which can be used as default for alternating
+   *  column background. */
+  public static final Color VERY_LIGHT_GRAY = new Color(238,236,225);
+
   /** Indicates whether column width automatically grows with
    *  data (Default: false). */ 
   protected boolean autoGrowColumns = false;
+  /** Default column background. */
+  protected Color columnBackgroundDefault = SwingUtil.getDefaultBackground(this);
+  /** Column background for alternating columns (index 1, 3, 5, ...). */
+  protected Color columnBackgroundGreyed = null;
 
 
   /**
@@ -176,11 +185,16 @@
 
   /**
    * If {@link #setAutoGrowColums(boolean)} is set the preferred
-   * column width is increased automatically with larger data.
+   * column width is increased automatically with larger data. 
+   * Furthermore the column background is set according to
+   * {@link #getColumnBackground(int)}.
    */
   @Override
   public Component prepareRenderer(final TableCellRenderer renderer, final int row, final int column) {
     final Component prepareRenderer = super.prepareRenderer(renderer, row, column);
+    // set column background for cell
+    prepareRenderer.setBackground( getColumnBackground(column) );
+
     if ( !hasAutoGrowColumns() )
       return prepareRenderer;
     // if current cell is broader, extend the table column  
@@ -192,4 +206,59 @@
         ));
     return prepareRenderer;
   }
+  
+  /**
+   * Returns the background for a specific column. For the even
+   * columns (0, 2, 4, ...) the default column background color
+   * is returned, for the odd columns (1, 3, 5, ...) the alternating
+   * background color. 
+   * @param col column index
+   * @return {@link #getDefaultColumnBackground()} for all columns if
+   *         not alternating background color is set
+   * @see #setDefaultColumnBackground(Color)
+   * @see #setAlternatingColumnBackground(Color)
+   */
+  protected Color getColumnBackground(int col) {
+    // if alternating column colors are disabled always
+    // return the default color
+    if ( getAlternatingColumnBackground() == null )
+      return getDefaultColumnBackground();
+    
+    return col % 2 == 0 ? getDefaultColumnBackground() : getAlternatingColumnBackground(); 
+  }
+  
+  /**
+   * Sets the default column background. If set to {@code null}
+   * the color is reset to the default L&F background for {@link JTable}.  
+   */
+  public void setDefaultColumnBackground(Color color) {
+    if ( color == null )
+      color = SwingUtil.getDefaultBackground(this);
+    this.columnBackgroundDefault = color;
+  }
+  
+  /**
+   * Returns the default column background. 
+   */
+  public Color getDefaultColumnBackground() {
+    return this.columnBackgroundDefault;
+  }
+  
+  /**
+   * Sets the column background for the alternating columns.
+   * If set to {@code null}, alternating column colors are disabled.
+   */
+  public void setAlternatingColumnBackground(Color color) {
+    if ( color == null )
+      color = SwingUtil.getDefaultBackground(this);
+    this.columnBackgroundGreyed = color;
+  }
+  
+  /**
+   * Returns the column background for the alternating columns.
+   * @return {@code null} if alternating column color rendering is disabled.
+   */
+  public Color getAlternatingColumnBackground() {
+    return this.columnBackgroundGreyed;
+  }
 }

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2011-12-12 21:21:10 UTC (rev 1806)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2011-12-16 11:21:13 UTC (rev 1807)
@@ -100,6 +100,7 @@
 import javax.swing.JTree;
 import javax.swing.JViewport;
 import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
 import javax.swing.table.TableCellEditor;
 import javax.swing.table.TableCellRenderer;
 import javax.swing.text.JTextComponent;
@@ -1640,6 +1641,26 @@
 	}
 
     /**
+     * Returns the default background color for a component 
+     * in the current L&F.
+     */
+    public static Color getDefaultBackground(Component comp) {
+      return getDefaultBackground(comp == null ? null : comp.getClass());
+      
+    }
+    /**
+     * Returns the default background color for a component type
+     * in the current L&F.
+     */
+    public static Color getDefaultBackground(Class<? extends Component> compClass) {
+      if ( compClass == null )
+        return getDefaultBackground();
+      if ( JTable.class.isAssignableFrom(compClass) )
+        return UIManager.getColor("Table.background");
+      throw new IllegalArgumentException("Component not yet supported: "+LangUtil.getSimpleClassName(compClass));
+    }
+
+    /**
      * Creates a menu item.
      * 
      * @param title

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/temp/BaseTypeUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/temp/BaseTypeUtil.java	2011-12-12 21:21:10 UTC (rev 1806)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/temp/BaseTypeUtil.java	2011-12-16 11:21:13 UTC (rev 1807)
@@ -30,8 +30,11 @@
 package de.schmitzm.temp;
 
 import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.List;
 
 import de.schmitzm.data.property.Properties;
+import de.schmitzm.lang.LangUtil;
 
 // nur fuer Doku
 
@@ -549,4 +552,83 @@
 		return strValue;
 	}
 
+	/**
+	 * Checks wheather an object represents a native
+	 * array.
+	 */
+	public static boolean isNativeArray(Object array) {
+	  return array instanceof short[]   ||
+	         array instanceof byte[]    ||
+	         array instanceof int[]     ||
+	         array instanceof long[]    ||
+	         array instanceof float[]   ||
+             array instanceof double[]  ||
+             array instanceof boolean[] ||
+	         array instanceof char[];
+	}
+
+	
+    /**
+     * Treats {@code array} as native array and puts its elements
+     * in a new typed list.
+     * @param array native array
+     * @param listType type for destination list
+     */
+    public static <E> List<E> copyToList(Object array, Class<E> listType) {
+      List<?> tempList = copyToGenericList(array, null);
+      List<E> destList = new ArrayList<E>();
+      // convert elements to adaquate type
+      for ( Object e : tempList ) {
+        if ( isNumeric(listType) )
+          destList.add( (E)convertNumber((Number)e, (Class<Number>)listType) );
+        else if ( isCharacter(listType) )
+          destList.add( (E)(Character)e );
+        else if ( isBoolean(listType) )
+          destList.add( (E)(Boolean)e );
+        else 
+          destList.add( (E)e );
+      }
+      return destList;
+    }
+
+    /**
+     * Treats {@code array} as native array and puts its elements
+     * in the un-typed list.
+     * @param array native array
+     * @param list destination list (if {@code null} a new list
+     *             will be created)
+     */
+    public static List copyToGenericList(Object array, List list) {
+      if ( !isNativeArray(array) )
+        throw new IllegalArgumentException("Object does not represent a native array: "+LangUtil.getSimpleClassName(array));
+	  if (list == null)
+	    list = new ArrayList();
+	  
+	  if ( array instanceof boolean[] )
+        for ( boolean e : (boolean[])array )
+          list.add(e);
+	  else if ( array instanceof short[] )
+	    for ( short e : (short[])array )
+	      list.add(e);
+	  else if ( array instanceof byte[] )
+        for ( byte e : (byte[])array )
+          list.add(e);
+	  else if ( array instanceof int[] )
+	    for ( int e : (int[])array )
+	      list.add(e);
+	  else if ( array instanceof long[] )
+        for ( long e : (long[])array )
+          list.add(e);
+	  else if ( array instanceof float[] )
+        for ( float e : (float[])array )
+          list.add(e);
+	  else if ( array instanceof double[] )
+        for ( double e : (double[])array )
+          list.add(e);
+	  else if ( array instanceof char[] )
+        for ( char e : (char[])array )
+          list.add(e);
+
+	  return list;
+	}
 }



More information about the Schmitzm-commits mailing list