[Schmitzm-commits] r2330 - in trunk: schmitzm-core/src/main/java/de/schmitzm/swing/event schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Jun 7 18:01:16 CEST 2013


Author: mojays
Date: 2013-06-07 18:01:16 +0200 (Fri, 07 Jun 2013)
New Revision: 2330

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/FontZoomListener.java
Modified:
   trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagFieldSequencePanel.java
Log:
New (very very sweet!!) FontZoomListener which listens on MouseWheel and +/- keyboard key and resizes a components font (zoom); useful e.g. for JTable or JTextArea/JTextPane

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/FontZoomListener.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/FontZoomListener.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/FontZoomListener.java	2013-06-07 16:01:16 UTC (rev 2330)
@@ -0,0 +1,153 @@
+package de.schmitzm.swing.event;
+
+import java.awt.Component;
+import java.awt.Font;
+import java.awt.event.InputEvent;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseWheelEvent;
+import java.awt.event.MouseWheelListener;
+
+import javax.swing.JList;
+import javax.swing.JScrollPane;
+import javax.swing.JTable;
+
+/**
+ * Listener which listens to [Alt] +/- key and [Alt] mouse wheel on a component and
+ * reacts with resizing the components {@linkplain Component#setFont(Font) font}.<br>
+ * <b>Special case:</b> For {@link JTable} also the row height is increased/decreased
+ * automatically to fit the new font size.
+ * @see Component#setFont(Font)  
+ * @author Martin O.J. Schmitz
+ */
+public class FontZoomListener extends KeyAdapter implements MouseWheelListener {
+  /** Holds  component to resize the font for. */
+  protected Component comp = null;
+  /** Indicates whether the listener reacts on plain +/- keys (should be {@code false}
+   *  e.g. for editable text fields!) */
+  protected boolean reactOnPlainKey = false;
+  /** Indicates whether the listener reacts on plain mouse wheel (should be {@code false}
+   *  e.g. for all components displayed in {@link JScrollPane}!) */
+  protected boolean reactOnPlainWheel = false;
+  
+  /**
+   * Creates new listener. Usually the listener has to be added as
+   * {@link KeyListener} and/or {@link MouseWheelListener} to this component.
+   * This is not done automatically by this constructor! The {@link #initComponent()}
+   * method can be called to add the listeners to component.
+   * @param comp component to zoom the font for.
+   * @param reactOnPlainKey indicates whether the listener should react on plain +/- keys
+   *                        (should be {@code false} e.g. for editable text fields!)
+   * @param reactOnPlainWheel indicates whether the listener should react on plain mouse wheel
+   *                          (should be {@code false} e.g. for all components displayed in
+   *                          {@link JScrollPane}!)
+   */
+  public FontZoomListener(Component comp, boolean reactOnPlainKey, boolean reactOnPlainWheel) {
+    super();
+    this.comp = comp;
+    this.reactOnPlainKey = reactOnPlainKey;
+  }
+  
+  /**
+   * Adds this listener to the component as {@link KeyListener} and {@link MouseWheelListener}.
+   */
+  public void initComponent() {
+    comp.addKeyListener(this);
+    comp.addMouseWheelListener(this);
+  }
+
+  /**
+   * Performs the font resizing.
+   * @param resizeFactor 1 to increase the font, and -1 to descrease
+   */
+  protected void resizeFont(int resizeFactor) {
+    if (resizeFactor == 0)
+      return;
+    
+    final int FONT_INCREASE = 1;     // resize font by +/-1 
+    final int TABLEROW_INCREASE = 1; // resize table row height by +/-1px 
+//    final int LISTROW_INCREASE = 1;  // resize list row height by +/-1px
+
+    // Resize font 
+    Font  font = comp.getFont();
+    float newFontSize = font.getSize() + FONT_INCREASE*resizeFactor; // IMPORTANT: size must be float!
+    if ( newFontSize <= 0 )
+      return;
+    font = font.deriveFont(newFontSize);
+    comp.setFont( font );
+
+    // Special cases to automatically resize row height in
+    // table and list
+    if ( comp instanceof JTable )
+      ((JTable)comp).setRowHeight( ((JTable)comp).getRowHeight() + TABLEROW_INCREASE*resizeFactor );
+// COMPLETELY NOT NECESSARY
+// becasuse JList automatically resizes the row height when Font changes!
+//    if ( comp instanceof JList ) {
+//      JList<?> list = (JList<?>)comp;
+//      int rowHeight = list.getFixedCellHeight();
+//      // if no fixed row height set, try to determine row height
+//      // from 
+//      if ( rowHeight <= 0 && list.getCellBounds(0, 0) != null)
+//        rowHeight = list.getCellBounds(0, 0).height;
+//      if ( rowHeight <= 0 )
+//        rowHeight = (int)newFontSize;
+//      list.setFixedCellHeight( rowHeight + LISTROW_INCREASE*resizeFactor );
+//    }
+  }
+  
+  /**
+   * Reacts on +/- and Alt +/- key pressed and resizes the
+   * font by 1.
+   */
+  @Override
+  public void keyTyped(KeyEvent e) {
+    final boolean altPressed = (e.getModifiers() == InputEvent.ALT_MASK); // NOTE: InputEvent.ALT_MASK does not work!
+    final boolean plusPressed = (e.getKeyChar() == '+');
+    final boolean minusPressed = (e.getKeyChar() == '-');
+    // if neither + nor - was pressed, do nothing
+    if ( !plusPressed && !minusPressed )
+      return;
+    // if ALT was not pressed and no reaction on plain keys is
+    // configured, do nothing
+    if ( !reactOnPlainKey && !altPressed )
+      return;
+    
+    // determine factor whether to increase or decrease
+    int  resizeFactor = 0;
+    if ( plusPressed )
+      resizeFactor = 1;
+    if ( minusPressed ) 
+      resizeFactor = -1;
+
+    resizeFont(resizeFactor);
+  }
+
+  /**
+   * Reacts mouse wheel with resizing the font.
+   */
+  @Override
+  public void mouseWheelMoved(MouseWheelEvent e) {
+    final boolean altPressed = (e.getModifiers() == InputEvent.ALT_MASK); // NOTE: InputEvent.ALT_MASK does not work!
+    // if ALT was not pressed and no reaction on plain keys is
+    // configured, do nothing
+    if ( !reactOnPlainWheel && !altPressed ) {
+      // IMPORTANT:
+      // If not consumed here, pipe event to parent component.
+      // Otherwise a possible JScrollPane will not act anymore as expected!
+      e.getComponent().getParent().dispatchEvent(e);
+      return;
+    }
+    
+    // determine factor whether to increase or decrease
+    int  resizeFactor = 0;
+    if ( e.getPreciseWheelRotation() < 0 )
+      resizeFactor = 1;
+    if ( e.getPreciseWheelRotation() > 0 ) 
+      resizeFactor = -1;
+
+    resizeFont(resizeFactor);
+    
+  }
+
+}

Modified: trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagFieldSequencePanel.java
===================================================================
--- trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagFieldSequencePanel.java	2013-06-07 14:49:48 UTC (rev 2329)
+++ trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagFieldSequencePanel.java	2013-06-07 16:01:16 UTC (rev 2330)
@@ -50,6 +50,7 @@
 import de.schmitzm.swing.ListMaintainancePanel;
 import de.schmitzm.swing.dnd.JListTransferHandler;
 import de.schmitzm.swing.dnd.ListingTransferHandler;
+import de.schmitzm.swing.event.FontZoomListener;
 
 /**
  * Panel to define a sequence of {@link ID3TagField} to specify the format
@@ -162,6 +163,9 @@
     
     clearSequence();
     
+    new FontZoomListener(availableFields.getList(), true, false).initComponent();
+    new FontZoomListener(fieldSequence.getList(), true, false).initComponent();
+    
     add(fieldSequenceCaption,"");
     add(availableFieldsCaption,"");
     add(fieldSequence,"sgxFIELDS, growx, growy");



More information about the Schmitzm-commits mailing list