[Schmitzm-commits] r2147 - trunk/schmitzm-core/src/main/java/de/schmitzm/swing
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Wed Nov 28 16:39:47 CET 2012
Author: mojays
Date: 2012-11-28 16:39:47 +0100 (Wed, 28 Nov 2012)
New Revision: 2147
Added:
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ToolTipItemListener.java
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java
Log:
LimitedDocument: trim input for some limit types (helps to ignore white spaces on copy/paste!)
ToolTipItemListener: new class to set tooltip for combo boxes (depending on selected item)
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java 2012-11-24 11:00:46 UTC (rev 2146)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/LimitedDocument.java 2012-11-28 15:39:47 UTC (rev 2147)
@@ -4,6 +4,8 @@
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
+import org.apache.commons.lang.StringUtils;
+
/**
* A document which size can be limited and reduced to
* upper oder lowercase contents.
@@ -112,8 +114,20 @@
*/
@Override
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
- if (str == null) return;
+ // in case of INTEGER and DECIMAL, automatically remove
+ // white spaces (BEFORE length check!)
+ switch ( getStyle() ) {
+ case STYLE_DECIMAL:
+ case STYLE_DECIMAL_COMMA:
+ case STYLE_INTEGER:
+ case STYLE_BASIC_STRING:
+ str = StringUtils.trimToNull(str);
+ break;
+ }
+ if (str == null)
+ return;
+
// Check limit
if ( getLimit() >= 0 && (getLength() + str.length()) > getLimit() )
return;
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ToolTipItemListener.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ToolTipItemListener.java (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ToolTipItemListener.java 2012-11-28 15:39:47 UTC (rev 2147)
@@ -0,0 +1,85 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ *
+ * This file is part of the SCHMITZM library - a collection of utility
+ * classes based on Java 1.6, focusing (not only) on Java Swing
+ * and the Geotools library.
+ *
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser 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 Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ *
+ * Contributors:
+ * Martin O. J. Schmitz - initial API and implementation
+ * Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.swing;
+
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
+
+import javax.swing.JComboBox;
+import javax.swing.JComponent;
+
+/**
+ * {@link ItemListener} which listens to item changes e.g. of a {@link JComboBox} and
+ * sets the tooltip to the selected item.<br>
+ * Because this class works independent of a concrete {@link JComponent} (just by the
+ * data provided by the {@link ItemEvent}), it is not necessary to instantiate this
+ * listener for each {@link JComponent} individually. Instead the singleton {@link #INSTANCE}
+ * can be used for all {@link JComponent JComponents}!
+ * @author Martin O.J. Schmitz
+ */
+public class ToolTipItemListener implements ItemListener {
+ /**
+ * Constant instance of this listener. Because {@link #itemStateChanged(ItemEvent)} gets
+ * all necessary data from {@link ItemEvent}, it is not necessary to instantiate this
+ * listener for each {@link JComponent}!
+ */
+ public static final ToolTipItemListener INSTANCE = new ToolTipItemListener();
+
+ /**
+ * If new item is selected, {@link #getTooltip(Object)} is called for
+ * the new selected item and the tooltip is set.
+ * Does nothing if event source is not a {@link JComponent}!
+ */
+ @Override
+ public void itemStateChanged(ItemEvent e) {
+ if ( !(e.getSource() instanceof JComponent) )
+ return;
+ JComponent comp = (JComponent)e.getSource();
+ if ( e.getStateChange() == ItemEvent.SELECTED )
+ // new item selected
+ comp.setToolTipText( getTooltip( e.getItem() ) );
+ else
+ // previous item deselected
+ comp.setToolTipText( "" );
+ }
+
+ /**
+ * Returns the This implementation simply returns {@code obj.toString()}.
+ * Sub-classes can overwrite this method to return an alternative
+ * value, depending of the individual object!
+ * @return empty string for {@code null} object
+ */
+ public String getTooltip(Object obj) {
+ if (obj == null)
+ return "";
+ return obj.toString();
+ }
+
+}
More information about the Schmitzm-commits
mailing list