[Schmitzm-commits] r2109 - in trunk/schmitzm-core/src/main/java/de/schmitzm: lang swing swing/table
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Sun Oct 14 23:31:52 CEST 2012
Author: mojays
Date: 2012-10-14 23:31:50 +0200 (Sun, 14 Oct 2012)
New Revision: 2109
Modified:
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/swing/table/ComponentRenderer.java
Log:
SelectableJTable: set static field for application wide table row height
SwingUtil: method to restore original font sizes
LangUtil: method to format file size in Bytes, KB, MB or GB
ComponentRenderer: set alignment for default renderer
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java 2012-10-10 23:20:08 UTC (rev 2108)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java 2012-10-14 21:31:50 UTC (rev 2109)
@@ -112,6 +112,8 @@
public static final long KB_BYTES = 1024;
/** Ein Megabyte (MB) in Bytes */
public static final long MB_BYTES = 1024 * KB_BYTES;
+ /** Ein Gigabyte (GB) in Bytes */
+ public static final long GB_BYTES = 1024 * MB_BYTES;
final static Pattern onlyZerosRegEx = Pattern.compile("^([\\s0.,]*)$");
@@ -2560,4 +2562,21 @@
Thread.currentThread().interrupt();
}
}
+
+ /**
+ * Returns the given file size in an adequate format.
+ */
+ public static String formatFileSize(long bytes) {
+ if ( bytes < LangUtil.KB_BYTES )
+ return bytes + " Byte";
+
+ int kBytes = (int)Math.round( bytes*1.0/LangUtil.KB_BYTES );
+ if ( bytes < LangUtil.MB_BYTES )
+ return kBytes + " KB";
+ int mBytes = (int)Math.round( bytes*1.0/LangUtil.MB_BYTES );
+ if ( bytes < LangUtil.GB_BYTES )
+ return kBytes + " MB";
+ int gBytes = (int)Math.round( bytes*1.0/LangUtil.GB_BYTES );
+ return kBytes + " GB";
+ }
}
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java 2012-10-10 23:20:08 UTC (rev 2108)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java 2012-10-14 21:31:50 UTC (rev 2109)
@@ -53,6 +53,15 @@
* column background. */
public static final Color VERY_LIGHT_GRAY = new Color(238,236,225);
+ /** Row height used for a "normal" {@link JTable}. */
+ public static final int STANDARD_ROW_HEIGHT = new JTable().getRowHeight();
+ /** Row height initially used for instances of {@link SelectableJTable}. This variable
+ * can be changed e.g. to generally use an alternative row height for all
+ * {@link SelectableJTable} in an application. E.g. if default font size
+ * was increased. */
+ public static int DEFAULT_ROW_HEIGHT = STANDARD_ROW_HEIGHT;
+
+
/** Indicates whether column width automatically grows with
* data (Default: false). */
protected boolean autoGrowColumns = false;
@@ -125,7 +134,8 @@
* functionalities.
*/
protected void initTable() {
- SwingUtil.setAutoStopTableCellEditing(this, true);
+ setRowHeight(DEFAULT_ROW_HEIGHT);
+ SwingUtil.setAutoStopTableCellEditing(this, true);
}
/**
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java 2012-10-10 23:20:08 UTC (rev 2108)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java 2012-10-14 21:31:50 UTC (rev 2109)
@@ -156,6 +156,15 @@
public class SwingUtil {
private static final Logger LOGGER = Logger.getLogger(SwingUtil.class
.getClass().getName());
+
+ /** Holds the factor the font sizes are already increased with. By using the
+ * factor provided by {@link #getUIFontSizeFactor()} its is possible to
+ * revert to original font size.
+ * @see #increaseUIFontSize(double)
+ * @see #getUIFontSizeFactor() */
+ private static double uiFontSizeFactor = 1.0;
+
+
/** A dummy label for default color and font */
public static final JLabel DUMMY_LABEL = new JLabel("dummy");
@@ -1003,8 +1012,11 @@
public static void setEditable(Component component, boolean editable) {
// simple components
if ( component instanceof JTextComponent )
-// ((JTextComponent)component).setEditable(editable);
- ((JTextComponent)component).setEnabled(editable);
+// MS:2012-10-12: "somebody" (probably me in r1572, 2011-05-05) changed this from setEditable(.) to
+// setEnabled(.). Why?? Disadvantage: On disabled text component the text
+// can not be marked, so in my opinion setEnabled(.) is the proper way!
+ ((JTextComponent)component).setEditable(editable);
+// ((JTextComponent)component).setEnabled(editable); // ???
if ( component instanceof JComboBox )
((JComboBox)component).setEnabled(editable);
if ( component instanceof JCheckBox )
@@ -1410,10 +1422,10 @@
*/
public static void increaseUIFontSize(double factor) {
// TODO: also increase:
- // - table row height
- // - list row height
+ // - table row height --> implemented by DEFAULT_ROW_HEIGTH in SelectableJTable
+ // - list row height
// - check box size
- // - JDateChooser width
+ // - JDateChooser width --> no change, increases automatically with font size
UIDefaults defaults = UIManager.getDefaults();
int i = 0;
for (Enumeration e = defaults.keys(); e.hasMoreElements(); i++) {
@@ -1432,8 +1444,27 @@
// Increase default font
DEFAULT_FONT = DEFAULT_FONT.deriveFont( (float)Math.round(DEFAULT_FONT.getSize() * factor) );
+
+ // Store factor on global field
+ uiFontSizeFactor = factor;
}
+
+ /**
+ * Reverts a former call of {@link #increaseUIFontSize(double)} so that the font
+ * are reset to original size.
+ */
+ public static void revertUIFontSizeIncrease() {
+ increaseUIFontSize( 1/getUIFontSizeFactor() );
+ }
+ /**
+ * Returns the factor the font sizes are already increased with. By using this
+ * value it is possible do revert the resize!
+ */
+ public static double getUIFontSizeFactor() {
+ return uiFontSizeFactor;
+ }
+
/**
* Prueft, ob eine Datei existiert und fordert gegenfalls zum Bestaetigen
* des Ueberschreiben auf.
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/ComponentRenderer.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/ComponentRenderer.java 2012-10-10 23:20:08 UTC (rev 2108)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/ComponentRenderer.java 2012-10-14 21:31:50 UTC (rev 2109)
@@ -165,8 +165,12 @@
int row,
int column) {
- if ( !(value instanceof Component) )
- return new DefaultTableCellRenderer().getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
+ if ( !(value instanceof Component) ) {
+ DefaultTableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer();
+ defaultTableCellRenderer.setHorizontalAlignment(getHorizontalAlignment());
+ defaultTableCellRenderer.setVerticalAlignment(getVerticalAlignment());
+ return defaultTableCellRenderer.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
+ }
return (Component)value;
}
More information about the Schmitzm-commits
mailing list