[Schmitzm-commits] r2293 - in trunk: schmitzm-core/src/main/java/de/schmitzm/swing schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Thu Apr 18 17:27:51 CEST 2013


Author: mojays
Date: 2013-04-18 17:27:51 +0200 (Thu, 18 Apr 2013)
New Revision: 2293

Added:
   trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/BasicTypeCellRenderer.java
   trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/BasicTypeComboBox.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/GUIBuilder.java
Log:
- BasicTypeComboBox and BasicTypeCellRenderer moved to SCHMITZM
- GUIBuilder: String conversion methods

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/GUIBuilder.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/GUIBuilder.java	2013-04-18 15:26:48 UTC (rev 2292)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/GUIBuilder.java	2013-04-18 15:27:51 UTC (rev 2293)
@@ -37,11 +37,17 @@
 import javax.swing.Action;
 import javax.swing.Icon;
 import javax.swing.JCheckBoxMenuItem;
+import javax.swing.JFormattedTextField;
 import javax.swing.JLabel;
 import javax.swing.JMenuItem;
+import javax.swing.JTextField;
 import javax.swing.JToggleButton;
 import javax.swing.JToolBar;
 
+import org.apache.commons.lang.StringUtils;
+
+import de.schmitzm.temp.BaseTypeUtil;
+
 /**
  * This class provides some methods to build GUI components a common way (application
  * wide).
@@ -165,4 +171,62 @@
     return label;
   }
 
+  
+  /**
+   * Converts an object (String, Integer, Double) to String
+   * 
+   * @return {@code null} if source object is {@code null}
+   */
+  public String convertToGUIString(Object obj) {
+      if (obj == null)
+          return null;
+//      if (obj instanceof Date)
+//          return DATE_FORMAT.format((Date) obj);
+      return obj.toString();
+  }
+
+  /**
+   * Converts a {@link Boolean} to a native {@code boolean}.
+   * @return {@code false} if source object is {@code null}
+   */
+  public boolean convertToBoolean(Boolean obj) {
+      return obj != null && obj;
+  }
+
+  /**
+   * Converts a string to object
+   * 
+   * @return {@code null} if {@code null} or empty string is given
+   * @see BaseTypeUtil#convertFromString(String, Class)
+   * 
+   */
+  public <T> T convertFromGUIString(String str,
+          Class<? extends T> destType, T... defaultValue) {
+      T defValue = defaultValue != null && defaultValue.length > 0 ? defaultValue[0] : null;
+      if (str == null || str.trim().isEmpty())
+          return defValue;
+      return BaseTypeUtil.convertFromString(str, destType);
+  }
+
+  /**
+   * Converts a string to object
+   * 
+   * @return {@code null} if {@code null} or empty string is given
+   * @see BaseTypeUtil#convertFromString(String, Class)
+   * 
+   */
+  public <T> T convertFromGUIString(JTextField textField,
+          Class<? extends T> destType, T... defaultValue) {
+      T defValue = defaultValue != null && defaultValue.length > 0 ? defaultValue[0] : null;
+      
+      if (textField == null || StringUtils.isBlank(textField.getText()) )
+          return defValue;
+      if ( textField instanceof JFormattedTextField ) {
+        Object obj = ((JFormattedTextField)textField).getValue();
+        if ( obj instanceof Number && Number.class.isAssignableFrom(destType) )
+          return (T)BaseTypeUtil.convertNumber((Number)obj, (Class<Number>)destType);
+        
+      }
+      return convertFromGUIString(textField.getText(), destType);
+  }
 }

Added: trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/BasicTypeCellRenderer.java
===================================================================
--- trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/BasicTypeCellRenderer.java	                        (rev 0)
+++ trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/BasicTypeCellRenderer.java	2013-04-18 15:27:51 UTC (rev 2293)
@@ -0,0 +1,52 @@
+package de.schmitzm.db.hibernate.gui;
+
+import java.awt.Component;
+
+import javax.swing.JTable;
+import javax.swing.table.TableCellRenderer;
+
+import de.schmitzm.db.hibernate.types.BasicType;
+import de.schmitzm.swing.table.ComponentRenderer;
+
+/**
+ * {@link TableCellRenderer} which uses a {@link BasicTypeComboBox}. 
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class BasicTypeCellRenderer<E extends BasicType> extends ComponentRenderer {
+  protected boolean nullable = false;
+  protected Class<E> basicType = null;
+  
+  /**
+   * Creates a new cell renderer.
+   */
+  public BasicTypeCellRenderer(Class<E> basicType, boolean nullable) {
+    super();
+    this.nullable = nullable;
+    this.basicType = basicType;
+  }
+
+  /**
+   * Returns the {@link BasicTypeComboBox}, with the cell value as selected
+   * value.
+   * @param table      Table of the cell
+   * @param value      cell value
+   * @param isSelected indicated whether cell is selected
+   * @param hasFocus   indicated whether cell is has the focus
+   * @param row        table row of the cell
+   * @param column     table column of the cell
+   */
+  public Component createRendererComponent( JTable table,
+                                            Object value,
+                                            boolean isSelected,
+                                            boolean hasFocus,
+                                            int row,
+                                            int column) {
+    BasicTypeComboBox<E> comboBox = new BasicTypeComboBox<E>(basicType, nullable);
+    if ( value instanceof BasicType )
+      comboBox.setSelectedItem((BasicType)value);
+    else
+      comboBox.setSelectedItem(null);
+    return comboBox;
+  }
+
+}

Added: trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/BasicTypeComboBox.java
===================================================================
--- trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/BasicTypeComboBox.java	                        (rev 0)
+++ trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/BasicTypeComboBox.java	2013-04-18 15:27:51 UTC (rev 2293)
@@ -0,0 +1,184 @@
+package de.schmitzm.db.hibernate.gui;
+
+import java.awt.event.KeyEvent;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.swing.JComboBox;
+import javax.swing.JTextField;
+
+import org.hibernate.Session;
+import org.hibernate.criterion.Criterion;
+
+import de.schmitzm.db.hibernate.DBUtil;
+import de.schmitzm.db.hibernate.HibernateSessionFactory;
+import de.schmitzm.db.hibernate.types.BasicType;
+import de.schmitzm.swing.Disposable;
+import de.schmitzm.swing.EditableComboBox;
+import de.schmitzm.swing.LimitedDocument;
+import de.schmitzm.swing.ToolTipComboBoxRenderer;
+import de.schmitzm.swing.ToolTipItemListener;
+
+/**
+ * {@link JComboBox}, which options depends on the instances
+ * of a {@link BasicType} in the database.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class BasicTypeComboBox<F extends BasicType> extends EditableComboBox implements Disposable {
+  
+  /** Contains the {@link BasicType} (Enum) of the combo box. */
+  protected Class<F> basicType;
+  /** If {@code true}, the combo box options are automatically extended by
+   *  empty options. */
+  protected boolean nullable = false;
+  
+  /**
+   * Creates a new ComboBox.
+   * @param basicType type to retrieve the options from
+   * @param defValue default value
+   * @param nullable if {@code true} the combo box has
+   *                 automatically an empty entry
+   * @param editable indicates whether the combo box is editable 
+   */
+  public BasicTypeComboBox(Class<F> basicType, boolean nullable, F defValue, boolean editable) {
+    super();
+    this.basicType = basicType;
+    this.nullable  = nullable;
+    updateOptions();
+    setEditable(editable);
+    setSelectedItem(defValue);
+    addItemListener( ToolTipItemListener.INSTANCE ); // Tooltip depending on selected item
+//    // Limit input
+//    JTextField textField = (JTextField)getEditor().getEditorComponent();
+//    textField.setDocument( new LimitedDocument(DBUtil.getBasicTypeDescrMaxSize(basicType)) );
+  }
+  
+  /**
+   * Sets a restriction for the manual input of the combo box.
+   * @param limit maximum number of allowed characters
+   * @param style character restriction (see {@link LimitedDocument})
+   * @see #getEditor()
+   * @see JTextField#setDocument(javax.swing.text.Document)
+   * @see LimitedDocument
+   */
+  public void limitInput(int limit, int style) {
+    // Limit input
+    JTextField textField = (JTextField)getEditor().getEditorComponent();
+    textField.setDocument( new LimitedDocument(limit, style) );
+  }
+  
+  /**
+   * Sets the tooltip to the manually entered value.
+   */
+  @Override
+  public void keyReleased(KeyEvent e) {
+    super.keyReleased(e);
+    setToolTipText(((JTextField)getEditor().getEditorComponent()).getText());
+  }
+
+
+  /**
+   * Creates a new ComboBox.
+   * @param basicType type to retrieve the options from
+   * @param defValue default value
+   * @param editable indicates whether the combo box is editable 
+   */
+  public BasicTypeComboBox(Class<F> basicType, boolean nullable, boolean editable) {
+    this(basicType,nullable,null,editable);
+  }
+
+  /**
+   * Creates a new ComboBox, which is NOT editable.
+   * @param basicType type to retrieve the options from
+   * @param defValue default value
+   * @param nullable if {@code true} the combo box has
+   *                 automatically an empty entry 
+   */
+  public BasicTypeComboBox(Class<F> basicType, boolean nullable, F defValue) {
+    this(basicType,nullable,defValue,false);
+  }
+
+  /**
+   * Creates a new ComboBox, which is NOT editable.
+   * @param basicType type to retrieve the options from
+   * @param defValue default value
+   */
+  public BasicTypeComboBox(Class<F> basicType, boolean nullable) {
+    this(basicType,nullable,null);
+  }
+
+  /**
+   * Returns the {@link BasicType} of the combo box.
+   */
+  public Class<F> getBasicType() {
+    return basicType;
+  }
+
+  /**
+   * Returns whether empty selection is allowed.
+   */
+  public boolean isNullable() {
+    return nullable;
+  }
+  
+  /**
+   * Sets whether empty selection is allowed.
+   */
+  public void setNullable(boolean nullable) {
+    this.nullable = nullable;
+    updateOptions();
+  }
+  
+  /**
+   * Returns {@code super.getSelectedItem()} automatically
+   * casted to the {@link BasicType}.
+   */
+  @Override
+  public F getSelectedItem() {
+    Object value = super.getSelectedItem();
+    if ( value instanceof String ) {
+      // Sometimes (especially if backspace was the last input) the value
+      // is String although there already is an corresponding list item.
+      // Thus at this point we double-check, whether there already is such
+      // an item and add it only if it was created (no ID yet!)
+      value = DBUtil.getOrCreateBasicType(getBasicType(), (String)value, false);
+      setSelectedItem(value); // Important! Otherwise addItem(.) results in recursion! 
+      if ( value != null && ((BasicType)value).getId() == null )
+        addItem(value);
+    }
+    return (F)value;
+  }
+
+  /**
+   * Updates the combo box selection options from database.
+   */
+  public void updateOptions(Criterion filter) {
+    removeAllItems();
+    if ( isNullable() )
+      addItem(null);
+    Map<Object,String> tooltips = new HashMap<Object, String>();
+    for ( F basicTypeInst : DBUtil.determineAllBasicTypes(null, getBasicType(),filter) ) {
+      tooltips.put(basicTypeInst,basicTypeInst.toString());
+      addItem(basicTypeInst);
+    }
+    setRenderer(new ToolTipComboBoxRenderer(tooltips));
+  }
+  
+  /**
+   * Updates the combo box selection options from database.
+   */
+  public void updateOptions() {
+    updateOptions(null);
+  }
+
+  /**
+   * Calls {@link Session#evict(Object)} for all entry of the combo box.
+   */
+  public void dispose() {
+    for (int i=0; i<getItemCount(); i++) {
+      Object basicTypeInst = getItemAt(i);
+      if ( basicTypeInst != null )
+        HibernateSessionFactory.getGlobalFactory().getSession().evict(basicTypeInst);
+    }    
+  }
+}



More information about the Schmitzm-commits mailing list