[Schmitzm-commits] r2136 - in trunk/schmitzm-core/src/main/java/de/schmitzm/swing: . plaf

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Sun Nov 11 17:37:49 CET 2012


Author: mojays
Date: 2012-11-11 17:37:49 +0100 (Sun, 11 Nov 2012)
New Revision: 2136

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/plaf/
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/plaf/AdaptableBackgroundBasicTextAreaUI.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
Log:
SwingUtil: method to create limited JTextArea
new AdaptableBackgroundBasicTextAreaUI to make JTextArea look grayed (according to UI constant ".inactiveBackground") in not-editable state; revert BugFix 6420149 (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6420149)

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-11-10 17:41:26 UTC (rev 2135)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-11-11 16:37:49 UTC (rev 2136)
@@ -207,7 +207,10 @@
 	public static Font DEFAULT_FONT = new Font(Font.DIALOG, Font.PLAIN,
 			12);
 	
-	/** Font attributes for an underlined font.
+    /** The default font of {@link JTextPane}. */
+    public static Font DEFAULT_FONT_TEXTPANE = new JTextPane().getFont();
+
+    /** Font attributes for an underlined font.
 	 *  @see Font#deriveFont(Map) */
 	public static Map<TextAttribute,Integer> FONT_ATTR_UNDERLINED = new HashMap<TextAttribute, Integer>();
 	static {
@@ -1458,7 +1461,8 @@
 	  }
 	  
 	  // Increase default font
-	  DEFAULT_FONT = DEFAULT_FONT.deriveFont( (float)Math.round(DEFAULT_FONT.getSize() * factor) );
+      DEFAULT_FONT = DEFAULT_FONT.deriveFont( (float)Math.round(DEFAULT_FONT.getSize() * factor) );
+      DEFAULT_FONT_TEXTPANE = DEFAULT_FONT_TEXTPANE.deriveFont( (float)Math.round(DEFAULT_FONT_TEXTPANE.getSize() * factor) );
 	  
 	  // Store factor on global field
 	  uiFontSizeFactor = factor;
@@ -2001,15 +2005,28 @@
       return field;
 	}
 	
-	/**
+    /**
+     * Creates limited text input field.
+     * @param limit limits the character count
+     * @param style see styled in {@link LimitedDocument}
+     */
+    public static JTextField createLimitedTextField(int limit, int style) {
+      LimitedDocument doc = new LimitedDocument(limit, style);
+      JTextField field = new JTextField();
+      field.setDocument(doc);
+      return field;
+    }
+
+    /**
 	 * Creates limited text input field.
 	 * @param limit limits the character count
 	 * @param style see styled in {@link LimitedDocument}
 	 */
-	public static JTextField createLimitedTextField(int limit, int style) {
+	public static JTextArea createLimitedTextArea(int limit, int style) {
 	  LimitedDocument doc = new LimitedDocument(limit, style);
-	  JTextField field = new JTextField();
+	  JTextArea field = new JTextArea();
 	  field.setDocument(doc);
+	  field.setFont(DEFAULT_FONT_TEXTPANE);
 	  return field;
 	}
 	

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/plaf/AdaptableBackgroundBasicTextAreaUI.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/plaf/AdaptableBackgroundBasicTextAreaUI.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/plaf/AdaptableBackgroundBasicTextAreaUI.java	2012-11-11 16:37:49 UTC (rev 2136)
@@ -0,0 +1,112 @@
+/**
+ * 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.plaf;
+
+import java.awt.Color;
+
+import javax.swing.JComponent;
+import javax.swing.JTextArea;
+import javax.swing.UIManager;
+import javax.swing.plaf.ComponentUI;
+import javax.swing.plaf.UIResource;
+import javax.swing.plaf.basic.BasicTextAreaUI;
+
+/**
+ * Acts like {@link BasicTextAreaUI} with the exception
+ * that the "BugFix" 6420149 (see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6420149)
+ * is reverted to handle the background of an non-editable {@link JTextArea}
+ * as defined in UI constant "TextArea.inactiveBackground" (instead of
+ * ignoring {@link JTextArea} completely in private {@code BasicTextUI.updateBackground(.)}).<br>
+ * <br>
+ * To use this {@link JTextArea} implementation, it has to be installed in {@link UIManager} by<br>
+ * {@code UIManager.put("TextAreaUI",AdaptableBackgroundBasicTextAreaUI.class.getName());}<br>
+ * <br>
+ * This class is predominantly copied from Paul Ebermann's
+ * solution (https://groups.google.com/forum/#!msg/de.comp.lang.java/jYqUSQarnwg/WXjjyln7DKAJ).
+ * @author Martin O.J. Schmitz
+ *
+ */
+public class AdaptableBackgroundBasicTextAreaUI extends BasicTextAreaUI {
+
+  /**
+   * Constructor method.
+   */
+  public static ComponentUI createUI(JComponent c) {
+      return new AdaptableBackgroundBasicTextAreaUI();
+  }
+
+  /**
+   * Installer method.
+   */
+  public void installUI(JComponent c) {
+      super.installUI(c);
+      updateBackground((JTextArea)c);
+  }
+
+
+  /**
+   * Reacts on editable/enabled change with call of {@link #updateBackground(JTextArea)}.
+   */
+  protected void propertyChange(java.beans.PropertyChangeEvent evt) {
+    super.propertyChange(evt);
+    if (evt.getPropertyName().equals("editable") ||
+        evt.getPropertyName().equals("enabled")) {
+          updateBackground((JTextArea)evt.getSource());
+    }
+  }
+
+  /**
+   * Updates the background according to UI constants ".disabledBackground",
+   * ".inactiveBackground" and ".background".
+   */
+  protected void updateBackground(JTextArea c) {
+    Color background = c.getBackground();
+    if (background instanceof UIResource) {
+      String prefix = getPropertyPrefix();
+      Color disabledBG = UIManager.getColor(prefix + ".disabledBackground");
+      Color inactiveBG = UIManager.getColor(prefix + ".inactiveBackground");
+      Color bg = UIManager.getColor(prefix + ".background");
+
+      Color newColor = null;
+      if (!c.isEnabled()) {
+        newColor = disabledBG;
+      }
+      if (newColor == null && !c.isEditable()) {
+        newColor = inactiveBG;
+      }
+      if (newColor == null) {
+        newColor = bg;
+      }
+      if (newColor != null && newColor != background) {
+        c.setBackground(newColor);
+      }
+    } 
+  }
+}



More information about the Schmitzm-commits mailing list