[Schmitzm-commits] r1180 - in trunk/src/schmitzm/swing: . resource/locales

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Oct 26 16:10:48 CEST 2010


Author: alfonx
Date: 2010-10-26 16:10:48 +0200 (Tue, 26 Oct 2010)
New Revision: 1180

Modified:
   trunk/src/schmitzm/swing/SwingUtil.java
   trunk/src/schmitzm/swing/resource/locales/SwingResourceBundle.properties
Log:
Improved logging stuff

Modified: trunk/src/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/src/schmitzm/swing/SwingUtil.java	2010-10-26 14:10:25 UTC (rev 1179)
+++ trunk/src/schmitzm/swing/SwingUtil.java	2010-10-26 14:10:48 UTC (rev 1180)
@@ -34,6 +34,7 @@
 import java.awt.Component;
 import java.awt.Container;
 import java.awt.Cursor;
+import java.awt.Desktop;
 import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.Frame;
@@ -44,19 +45,23 @@
 import java.awt.Rectangle;
 import java.awt.Toolkit;
 import java.awt.Window;
+import java.awt.event.ActionEvent;
 import java.awt.event.MouseWheelListener;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
+import java.net.URISyntaxException;
 import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Random;
 import java.util.StringTokenizer;
 
 import javax.imageio.ImageIO;
+import javax.swing.AbstractAction;
 import javax.swing.AbstractButton;
 import javax.swing.ImageIcon;
 import javax.swing.JComboBox;
@@ -64,7 +69,10 @@
 import javax.swing.JFrame;
 import javax.swing.JInternalFrame;
 import javax.swing.JLabel;
+import javax.swing.JMenu;
+import javax.swing.JMenuItem;
 import javax.swing.JOptionPane;
+import javax.swing.JRadioButtonMenuItem;
 import javax.swing.JScrollPane;
 import javax.swing.JTable;
 import javax.swing.JTree;
@@ -74,7 +82,11 @@
 import javax.swing.tree.TreePath;
 
 import org.apache.commons.lang.SystemUtils;
+import org.apache.log4j.Appender;
+import org.apache.log4j.FileAppender;
+import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
+import org.geotools.data.DataUtilities;
 
 import schmitzm.lang.LangUtil;
 import schmitzm.lang.ResourceProvider;
@@ -94,7 +106,6 @@
 			.getClass().getName());
 	private static final JLabel DUMMY_LABEL = new JLabel("dummy");
 
-	
 	/**
 	 * {@link ResourceProvider}, der die Lokalisation fuer GUI-Komponenten des
 	 * Package {@code schmitzm.swing} zur Verfuegung stellt. Diese sind in
@@ -124,9 +135,10 @@
 		/** Auswahl "Immer ueberschreiben". */
 		OVERWRITE_ALL
 	}
-	
+
 	/** A default font. */
-	public static final Font DEFAULT_FONT = new Font(Font.DIALOG,Font.PLAIN,12);
+	public static final Font DEFAULT_FONT = new Font(Font.DIALOG, Font.PLAIN,
+			12);
 
 	// ****************************************************************************
 	// Diese Icons sind auf Basis der Icons von Gimp erstellt
@@ -385,8 +397,8 @@
 	 * @param parent
 	 *            Component
 	 * @return {@code false} wenn {@code child == null}, {@code true} wenn
-	 *         {@code child == parent}, {@code
-	 *         isChildComponent(child.getParent(),parent)} sonst
+	 *         {@code child == parent},
+	 *         {@code isChildComponent(child.getParent(),parent)} sonst
 	 */
 	public static boolean isChildComponent(Component child, Component parent) {
 		if (child == null)
@@ -501,8 +513,10 @@
 			frameSize.width = screenSize.width;
 		}
 		// Fenster positionieren
-		window.setLocation(relLocation.x
-				+ Math.round((relSize.width - frameSize.width) * (float) relX),
+		window.setLocation(
+				relLocation.x
+						+ Math.round((relSize.width - frameSize.width)
+								* (float) relX),
 				relLocation.y
 						+ Math.round((relSize.height - frameSize.height)
 								* (float) relY));
@@ -960,42 +974,44 @@
 		// combine RGB to Hex string
 		return result.toString();
 	}
-	
+
 	/**
-	 * Converts a {@link Font} to a string, which vise-versa can be
-	 * decoded by {@link Font#decode(String)}.
-	 * @param font a font
-     * @return {@code null} if {@code font} is {@code null}
+	 * Converts a {@link Font} to a string, which vise-versa can be decoded by
+	 * {@link Font#decode(String)}.
+	 * 
+	 * @param font
+	 *            a font
+	 * @return {@code null} if {@code font} is {@code null}
 	 */
 	public static String convertFontToString(Font font) {
-	  if ( font == null )
-	    return null;
-	  StringBuffer sb = new StringBuffer();
-	  // Format: fontname-style-size
-	  sb.append( font.getName() )
-	    .append("-")
-	    .append( convertFontStyleToString(font) )
-	    .append("-")
-	    .append( font.getSize() );
-	  
-	  return sb.toString();
+		if (font == null)
+			return null;
+		StringBuffer sb = new StringBuffer();
+		// Format: fontname-style-size
+		sb.append(font.getName()).append("-")
+				.append(convertFontStyleToString(font)).append("-")
+				.append(font.getSize());
+
+		return sb.toString();
 	}
 
 	/**
 	 * Converts the style of a {@link Font} to string according to
 	 * {@link Font#decode(String)}.
-	 * @param font a font
+	 * 
+	 * @param font
+	 *            a font
 	 * @return {@code "PLAIN"}, {@code "BOLD"}, {@code "ITALIC"} or
 	 *         {@code "BOLDITALIC"}
 	 */
 	public static String convertFontStyleToString(Font font) {
-	  if ( font.isBold() && font.isItalic() )
-	    return "BOLDITALIC";
-      if ( font.isBold() )
-        return "BOLD";
-      if ( font.isItalic() )
-        return "ITALIC";
-      return "PLAIN";
+		if (font.isBold() && font.isItalic())
+			return "BOLDITALIC";
+		if (font.isBold())
+			return "BOLD";
+		if (font.isItalic())
+			return "ITALIC";
+		return "PLAIN";
 	}
 
 	/**
@@ -1087,10 +1103,10 @@
 		else if (comp instanceof JDialog)
 			((JDialog) comp).setTitle(newLabel.toString());
 		else
-			throw new UnsupportedOperationException(SwingUtil.class
-					.getSimpleName()
-					+ "resetCaption(.) can not be applied to "
-					+ comp.getClass().getSimpleName());
+			throw new UnsupportedOperationException(
+					SwingUtil.class.getSimpleName()
+							+ "resetCaption(.) can not be applied to "
+							+ comp.getClass().getSimpleName());
 	}
 
 	/**
@@ -1229,8 +1245,8 @@
 		if (minWidth != null)
 			table.getColumnModel().getColumn(idx).setMinWidth(minWidth);
 		if (preferredWidth != null)
-			table.getColumnModel().getColumn(idx).setPreferredWidth(
-					preferredWidth);
+			table.getColumnModel().getColumn(idx)
+					.setPreferredWidth(preferredWidth);
 		if (maxWidth != null)
 			table.getColumnModel().getColumn(idx).setMaxWidth(maxWidth);
 
@@ -1271,30 +1287,30 @@
 
 		// Clear left of the painted area if needed
 		if (clearArea.getMinX() < paintArea.getMinX()) {
-			graphics.clearRect((int) clearArea.getMinX(), (int) clearArea
-					.getMinY(), (int) paintArea.getMinX(), (int) clearArea
-					.getMaxY());
+			graphics.clearRect((int) clearArea.getMinX(),
+					(int) clearArea.getMinY(), (int) paintArea.getMinX(),
+					(int) clearArea.getMaxY());
 		}
 
 		// Clear right of the painted area if needed
 		if (clearArea.getMaxX() > paintArea.getMaxX()) {
-			graphics.clearRect((int) paintArea.getMaxX(), (int) clearArea
-					.getMinY(), (int) clearArea.getMaxX(), (int) clearArea
-					.getMaxY());
+			graphics.clearRect((int) paintArea.getMaxX(),
+					(int) clearArea.getMinY(), (int) clearArea.getMaxX(),
+					(int) clearArea.getMaxY());
 		}
 
 		// Clear above of the painted area if needed
 		if (clearArea.getMinY() < paintArea.getMinY()) {
-			graphics.clearRect((int) clearArea.getMinX(), (int) clearArea
-					.getMinY(), (int) clearArea.getMaxX(), (int) paintArea
-					.getMinY());
+			graphics.clearRect((int) clearArea.getMinX(),
+					(int) clearArea.getMinY(), (int) clearArea.getMaxX(),
+					(int) paintArea.getMinY());
 		}
 
 		// Clear below of the painted area if needed
 		if (clearArea.getMaxY() > paintArea.getMaxY()) {
-			graphics.clearRect((int) clearArea.getMinX(), (int) paintArea
-					.getMaxY(), (int) clearArea.getMaxX(), (int) clearArea
-					.getMaxY());
+			graphics.clearRect((int) clearArea.getMinX(),
+					(int) paintArea.getMaxY(), (int) clearArea.getMaxX(),
+					(int) clearArea.getMaxY());
 		}
 
 	}
@@ -1312,8 +1328,7 @@
 			final boolean upIsUp) {
 
 		if (comboBox.getMouseWheelListeners().length > 0) {
-			LOGGER
-					.warn("There already exists another MouseWheelListener. I will not add mine!");
+			LOGGER.warn("There already exists another MouseWheelListener. I will not add mine!");
 			return;
 		}
 
@@ -1330,16 +1345,14 @@
 						// + comboBox.getSelectedIndex() + " to "
 						// + (comboBox.getSelectedIndex() + 1));
 
-						comboBox
-								.setSelectedIndex(comboBox.getSelectedIndex() + 1);
+						comboBox.setSelectedIndex(comboBox.getSelectedIndex() + 1);
 					}
 				} else {
 					if (comboBox.getSelectedIndex() > 0) {
 						// System.out.println("switching from "
 						// + comboBox.getSelectedIndex() + " to "
 						// + (comboBox.getSelectedIndex() - 1));
-						comboBox
-								.setSelectedIndex(comboBox.getSelectedIndex() - 1);
+						comboBox.setSelectedIndex(comboBox.getSelectedIndex() - 1);
 					}
 				}
 			}
@@ -1434,7 +1447,6 @@
 			LOGGER.info("failed", e);
 		}
 	}
-	
 
 	/**
 	 * Throws a {@link RuntimeException} if this method is called on the EDT.
@@ -1454,12 +1466,74 @@
 			throw new RuntimeException("Not on EDT!");
 		}
 	}
-	
-  /**
-   * Returns the default background color for the current L&F.
-   */
-  public static Color getDefaultBackground() {
-    // Quick and dirty:
-    return DUMMY_LABEL.getBackground();
-  }
+
+	/**
+	 * Returns the default background color for the current L&F.
+	 */
+	public static Color getDefaultBackground() {
+		// Quick and dirty:
+		return DUMMY_LABEL.getBackground();
+	}
+
+	/**
+	 * Create a {@link JMenu} that allows to switch the Log-Level of the root
+	 * logger.
+	 */
+	public static JMenu createChangeLog4JLevelJMenu() {
+
+		JMenu logMenu = new JMenu(R("Menu.Logging"));
+
+		final Logger rootLogger = Logger.getRootLogger();
+
+		{
+			// FInd FileAppender
+			Enumeration allAppenders = rootLogger.getAllAppenders();
+			while (allAppenders.hasMoreElements()) {
+				Appender app = (Appender) allAppenders.nextElement();
+				if (app instanceof FileAppender) {
+					FileAppender fa = (FileAppender) app;
+					final File logFile = new File(fa.getFile());
+					JMenuItem jMenuItem = new JMenuItem(new AbstractAction(R(
+							"Menu.Logging.OpenFile",
+							logFile.getAbsolutePath())) {
+
+						@Override
+						public void actionPerformed(ActionEvent e) {
+							try {
+								Desktop.getDesktop().browse(
+										DataUtilities.fileToURL(logFile)
+												.toURI());
+							} catch (Exception e1) {
+								ExceptionDialog.show(e1);
+							}
+						}
+					});
+					logMenu.add(jMenuItem);
+				}
+			}
+		}
+
+		ButtonGroup bg = new ButtonGroup();
+
+		for (final Level l : new Level[] { Level.DEBUG, Level.INFO, Level.WARN,
+				Level.ERROR }) {
+
+			JRadioButtonMenuItem rbMi = new JRadioButtonMenuItem(
+					new AbstractAction(R("Menu.Logging." + l.toString())) {
+
+						@Override
+						public void actionPerformed(ActionEvent e) {
+							rootLogger.setLevel(l);
+						}
+					});
+
+			bg.add(rbMi);
+
+			rbMi.setSelected(l.equals(rootLogger.getLevel()));
+			logMenu.add(rbMi);
+		}
+
+		return logMenu;
+
+	}
 }

Modified: trunk/src/schmitzm/swing/resource/locales/SwingResourceBundle.properties
===================================================================
--- trunk/src/schmitzm/swing/resource/locales/SwingResourceBundle.properties	2010-10-26 14:10:25 UTC (rev 1179)
+++ trunk/src/schmitzm/swing/resource/locales/SwingResourceBundle.properties	2010-10-26 14:10:48 UTC (rev 1180)
@@ -243,3 +243,9 @@
 ResourceProviderManagerFrame.UpdateMess=${0} resource bundles updated...
 
 
+Menu.Logging=Logging...
+Menu.Logging.DEBUG=Log every message
+Menu.Logging.INFO=Log information, warning and errors
+Menu.Logging.WARN=Log warnings and errors
+Menu.Logging.ERROR=Log only errors
+Menu.Logging.OpenFile=Open ${0}
\ No newline at end of file



More information about the Schmitzm-commits mailing list