[Schmitzm-commits] r38 - in trunk: dist src/skrueger/i8n src/skrueger/swing src/skrueger/swing/resource/locales

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun Apr 5 17:06:56 CEST 2009


Author: alfonx
Date: 2009-04-05 17:06:56 +0200 (Sun, 05 Apr 2009)
New Revision: 38

Modified:
   trunk/dist/
   trunk/dist/schmitzm.jar
   trunk/src/skrueger/i8n/I8NUtil.java
   trunk/src/skrueger/i8n/Translation.java
   trunk/src/skrueger/swing/TranslationAskJDialog.java
   trunk/src/skrueger/swing/TranslationEditJPanel.java
   trunk/src/skrueger/swing/resource/locales/SwingResourceBundle.properties
   trunk/src/skrueger/swing/resource/locales/SwingResourceBundle_de.properties
Log:
* Further improved the TranslationAskJDialog
* Removed deprecated stuff from TranslationEditJPanel




Property changes on: trunk/dist
___________________________________________________________________
Name: svn:ignore
   + schmitzm.jar.zip


Modified: trunk/dist/schmitzm.jar
===================================================================
(Binary files differ)

Modified: trunk/src/skrueger/i8n/I8NUtil.java
===================================================================
--- trunk/src/skrueger/i8n/I8NUtil.java	2009-03-30 00:43:08 UTC (rev 37)
+++ trunk/src/skrueger/i8n/I8NUtil.java	2009-04-05 15:06:56 UTC (rev 38)
@@ -1,14 +1,14 @@
 package skrueger.i8n;
 
-import java.util.LinkedList;
-import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
 
 public class I8NUtil {
 	
-	public static List<String> languageCodes = new LinkedList<String>();
+	private static Set<String> languageCodes = new TreeSet<String>();
 	static {
         for (String code : java.util.Locale.getISOLanguages()) {
-        	languageCodes.add(code);
+        	getLanguageCodes().add(code);
         }
     }
 //	
@@ -42,7 +42,15 @@
 	 * @return true if the code paramter is a valid ISO Language code
 	 */
 	public static boolean isValidISOLangCode(String code) {
-		return languageCodes.contains(code);
+		return getLanguageCodes().contains(code);
 	}
+
+
+	/**
+	 * @return All language codes available in java.util.Locale.getISOLanguages() without duplicates.
+	 */
+	public static Set<String> getLanguageCodes() {
+		return languageCodes;
+	}
 }
 

Modified: trunk/src/skrueger/i8n/Translation.java
===================================================================
--- trunk/src/skrueger/i8n/Translation.java	2009-03-30 00:43:08 UTC (rev 37)
+++ trunk/src/skrueger/i8n/Translation.java	2009-04-05 15:06:56 UTC (rev 38)
@@ -108,7 +108,9 @@
 			put(DEFAULT_KEY, defaultTranslation);
 		}
 		else for (String code : languages){
-			put(code, defaultTranslation);
+			if (code.equals(getActiveLang())) {
+				put(code, defaultTranslation);
+			}
 		}
 	}
 

Modified: trunk/src/skrueger/swing/TranslationAskJDialog.java
===================================================================
--- trunk/src/skrueger/swing/TranslationAskJDialog.java	2009-03-30 00:43:08 UTC (rev 37)
+++ trunk/src/skrueger/swing/TranslationAskJDialog.java	2009-04-05 15:06:56 UTC (rev 38)
@@ -1,7 +1,7 @@
 package skrueger.swing;
 
 import java.awt.BorderLayout;
-import java.awt.Dialog;
+import java.awt.Component;
 import java.awt.FlowLayout;
 import java.awt.Window;
 import java.awt.event.ActionEvent;
@@ -9,7 +9,6 @@
 import java.awt.event.KeyEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
-import java.util.ArrayList;
 import java.util.Locale;
 
 import javax.swing.AbstractAction;
@@ -17,6 +16,7 @@
 import javax.swing.Box;
 import javax.swing.JComponent;
 import javax.swing.JDialog;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JRootPane;
 import javax.swing.KeyStroke;
@@ -45,7 +45,7 @@
 	public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";
 	public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";
 
-	private final JComponent[] translationEditJPanelsOrJustComponents;
+	private JComponent[] translationEditJPanelsOrJustComponents;
 
 	private boolean hasBeenCanceled;
 
@@ -74,42 +74,64 @@
 	}
 
 	/**
+	 * The {@link TranslationAskJDialog} fills its content pane with an
+	 * arbitrary number of components. If these {@link Component}s are
+	 * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
+	 * values and restore them if the dialog is canceled. Other
+	 * {@link JComponent}s are just displayed.<br/>
 	 * This class handles the cancel button itself. You may still want to listen
-	 * to PROPERTY_APPLY_AND_CLOSE events.
-	 * 
-	 * This dialog is modal. The dialog has to be set visible afterwards.
+	 * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
+	 * to be set visible afterwards.<br/>
 	 */
-	public TranslationAskJDialog(Dialog owner,
+	public TranslationAskJDialog(Window owner,
 			final JComponent... translationEditJPanels) {
 		super(owner);
-		this.translationEditJPanelsOrJustComponents = translationEditJPanels;
-		init();
+		setComponents(translationEditJPanels);
 	}
 
 	/**
+	 * The {@link TranslationAskJDialog} fills its content pane with an
+	 * arbitrary number of components. If these {@link Component}s are
+	 * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
+	 * values and restore them if the dialog is canceled. Other
+	 * {@link JComponent}s are just displayed.<br/>
 	 * This class handles the cancel button itself. You may still want to listen
 	 * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
-	 * to be set visible afterwards.
+	 * to be set visible afterwards.<br/>
+	 * Using this constructor, you have to call setComponents afterwards.
 	 */
-	public TranslationAskJDialog(Window owner,
-			final JComponent... translationEditJPanels) {
+	public TranslationAskJDialog(Window owner) {
 		super(owner);
+	}
+
+	/**
+	 * The {@link TranslationAskJDialog} fills its content pane with an
+	 * arbitrary number of components. If these {@link Component}s are
+	 * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
+	 * values and restore them if the dialog is canceled. Other
+	 * {@link JComponent}s are just displayed.
+	 * 
+	 * @param translationEditJPanels
+	 *            Arbitrary list of {@link JComponent}s and
+	 *            {@link TranslationEditJPanel}s.
+	 */
+	public void setComponents(final JComponent... translationEditJPanels) {
 		this.translationEditJPanelsOrJustComponents = translationEditJPanels;
-		init();
 
-		// Rememebr backups for all the jtextpanels
+		// Remember backups for all the TranslationEditJPanel
 		int count = 0;
 		for (JComponent component : translationEditJPanelsOrJustComponents) {
 			if (component instanceof TranslationEditJPanel) {
 				TranslationEditJPanel tep = (TranslationEditJPanel) component;
 				Translation orig = tep.getTranslation();
-				
+
 				// We don't want to overwrite the Translation object on
 				// restore(). We just want to change its value.
-
 				backup[count++] = orig.toOneLine();
 			}
 		}
+
+		init();
 	}
 
 	private void init() {
@@ -131,8 +153,6 @@
 		cp.add(getButtons(), BorderLayout.SOUTH);
 		setContentPane(cp);
 
-		// dialog.getRootPane().setDefaultButton(okButton);
-
 		setTitle(RESOURCE.getString("translation_dialog_title")); // i8n
 		setModal(true);
 		pack();
@@ -145,7 +165,7 @@
 		dispose();
 	}
 
-	private void restore() {
+	protected void restore() {
 		int count = 0;
 		for (JComponent component : translationEditJPanelsOrJustComponents) {
 			if (component instanceof TranslationEditJPanel) {
@@ -179,31 +199,26 @@
 				public void actionPerformed(ActionEvent evt) {
 					TranslationAskJDialog.this.firePropertyChange(
 							PROPERTY_APPLY_AND_CLOSE, null, null);
+					
+					if (!checkValidInputs()) return;
+					
 					setVisible(false);
 					dispose();
-					System.out.println("OK button action performed");
 				}
 
 			});
-			// okButton.addKeyListener( new KeyListener() {
-			//
-			// public void keyTyped(KeyEvent e) {
-			// if ()
-			// okButton.action(new KEyPreEvent(), what)
-			// }
-			//				
-			// });
+
 		}
 		jPanel.add(okButton);
 
 		if (cancelButton == null) {
 			cancelButton = new CancelButton(new AbstractAction("") {
 				public void actionPerformed(ActionEvent evt) {
-					// restore();
+					restore();
 					TranslationAskJDialog.this.firePropertyChange(
 							PROPERTY_CANCEL_AND_CLOSE, null, null);
 					setVisible(false);
-					setHasBeenCanceled(true);
+					setCancelled(true);
 					dispose();
 				}
 			});
@@ -213,35 +228,30 @@
 		return jPanel;
 	}
 
-	public static void main(String[] args) {
-		ArrayList<String> lang = new ArrayList<String>();
-		lang.add("de");
-		lang.add("en");
-		lang.add("fr");
-
-		Translation transe = new Translation();
-		transe.put("de", "Terciopelo-Lanzenotter");
-		TranslationEditJPanel p1 = new TranslationEditJPanel(
-				"Name von New Group", transe, lang);
-
-		Translation transe2 = new Translation();
-		transe2
-				.put(
-						"de",
-						"Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart.");
-		TranslationEditJPanel p2 = new TranslationEditJPanel(
-				"Description of Animal:", transe2, lang);
-
-		// JFrame frame = new JFrame();
-		// frame.setContentPane(p1);
-		// frame.pack();
-		// frame.setVisible(true);
-
-		TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2);
-		dialog.setVisible(true);
+	/**
+	 * @return <code>true</code> if none of the translations contains illegal characters.
+	 */
+	protected boolean checkValidInputs() {
+		
+		for (JComponent component : translationEditJPanelsOrJustComponents) {
+			if (component instanceof TranslationEditJPanel) {
+				TranslationEditJPanel tep = (TranslationEditJPanel) component;
+				
+				for (String l : tep.getTranslation().values()){
+					if ( l.contains("{") || l.contains("}")) {
+						JOptionPane.showMessageDialog(this, RESOURCE.getString("ErrorMsg.InvalidCharacterInTranslation"));
+						return false;
+					}
+				}
+				
+			}
+		}
+		
+		
+		return true;
 	}
 
-	private void setHasBeenCanceled(boolean hasBeenCanceled) {
+	private void setCancelled(boolean hasBeenCanceled) {
 		this.hasBeenCanceled = hasBeenCanceled;
 	}
 
@@ -251,7 +261,7 @@
 	 * 
 	 * @return <code>true</code> if the {@link JDialog} has been canceled.
 	 */
-	public boolean isHasBeenCanceled() {
+	public boolean isCancelled() {
 		return hasBeenCanceled;
 	}
 

Modified: trunk/src/skrueger/swing/TranslationEditJPanel.java
===================================================================
--- trunk/src/skrueger/swing/TranslationEditJPanel.java	2009-03-30 00:43:08 UTC (rev 37)
+++ trunk/src/skrueger/swing/TranslationEditJPanel.java	2009-04-05 15:06:56 UTC (rev 38)
@@ -2,19 +2,9 @@
 
 import java.awt.BorderLayout;
 import java.awt.Dimension;
-import java.awt.Frame;
-import java.awt.Window;
-import java.awt.event.ActionEvent;
-import java.awt.event.WindowAdapter;
-import java.awt.event.WindowEvent;
-import java.awt.event.WindowListener;
-import java.util.ArrayList;
 import java.util.List;
 
-import javax.swing.AbstractAction;
 import javax.swing.BorderFactory;
-import javax.swing.Box;
-import javax.swing.JComponent;
 import javax.swing.JDialog;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
@@ -24,7 +14,6 @@
 import org.apache.log4j.Logger;
 
 import schmitzm.swing.SpringUtilities;
-import schmitzm.swing.SwingUtil;
 import skrueger.i8n.Translation;
 
 /**
@@ -33,14 +22,9 @@
  *
  */
 public class TranslationEditJPanel extends JPanel {
-	public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";
-	public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";
-	private static JDialog dialog;
-	private static OkButton okButton;
+	static final protected Logger LOGGER = Logger.getLogger(TranslationEditJPanel.class);
 	
-	private static CancelButton cancelButton;
-	Logger log = Logger.getLogger(TranslationEditJPanel.class);
-	private List<String> languages;
+	private final List<String> languages;
 	private JPanel translationGrid;
 	private Translation trans;
 
@@ -57,8 +41,6 @@
 		this.trans = trans;
 		this.languages = languages_;
 		
-		
-
 		add(getTranslationGrid(), BorderLayout.CENTER);
 
 		if (question != null) {
@@ -99,135 +81,110 @@
 		return translationGrid;
 	}
 
-	/**
-	 * Merges a few {@link TranslationEditJPanel}s and shows them together..
-	 * So far this is working on the Translation object directly, Cancel not possible.
-	 * @param translationEditJPanel
-	 * 
-	 * @deprecated Use {@link TranslationAskJDialog}
-	 */
-	public static void ask(Frame parentFrame, final TranslationEditJPanel... translationEditJPanels) {
-		dialog = new JDialog(parentFrame);
-	//	backup( translationEditJPanels );
-		showDialog(dialog, translationEditJPanels);
-	}
-
+//	/**
+//	 * Merges a few {@link TranslationEditJPanel}s and shows them together..
+//	 * So far this is working on the Translation object directly, Cancel not possible.
+//	 * @param translationEditJPanel
+//	 * 
+//	 * @deprecated Use {@link TranslationAskJDialog}
+//	 */
+//	public static void ask(Frame parentFrame, final TranslationEditJPanel... translationEditJPanels) {
+//		dialog = new JDialog(parentFrame);
+//	//	backup( translationEditJPanels );
+//		showDialog(dialog, translationEditJPanels);
+//	}
+//
+//	
+//	/**
+//	 * Merges a few {@link TranslationEditJPanel}s and shows them together..
+//	 * So far this is working on the Translation object directly, Cancel not possible.
+//	 * @param translationEditJPanel
+//
+//	 * @deprecated User {@link TranslationAskJDialog}	 * 
+//	 */
+//	public static void ask(Window parentWindow, TranslationEditJPanel... translationEditJPanels) {
+//		dialog = new JDialog(parentWindow);
+//	//	backup( translationEditJPanels );
+//		showDialog(dialog, translationEditJPanels);
+//	}
+//	
+//	/**
+//	 * Merges a few {@link TranslationEditJPanel}s and shows them together..
+//	 * So far this is working on the Translation object directly, Cancel not possible.
+//	 * @param translationEditJPanel
+//	 * 
+//	 * 	 * @deprecated User {@link TranslationAskJDialog}
+//	 */
+//	public static void ask(JDialog parentDialog, TranslationEditJPanel... translationEditJPanels) {
+//		dialog = new JDialog(parentDialog);
+//	//	backup( translationEditJPanels );
+//		showDialog(dialog, translationEditJPanels);
+//	}
+//	
+//	
+//	/**
+//	 * 	 * @deprecated User {@link TranslationAskJDialog}
+//	 * @param d
+//	 * @param translationEditJPanels 
+//	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons Kr&uuml;ger</a>
+//	 */
+//	public static void showDialog(JDialog d, JComponent... translationEditJPanels) {
+//		dialog = d;
+//		dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
+//		SwingUtil.centerFrameOnScreen(dialog);
+//		Box box = Box.createVerticalBox();
+//		for (JComponent panel : translationEditJPanels) {
+//			box.add(panel);
+//		}
+//		JPanel cp = new JPanel( new BorderLayout());
+//		cp.add( box, BorderLayout.CENTER);
+//		cp.add( getButtons(), BorderLayout.SOUTH  );
+//		dialog.setContentPane(cp);
+//		
+//		// dialog.getRootPane().setDefaultButton(okButton);
+//		
+//		dialog.setTitle("Please translate"); //i8n
+//		dialog.setModal(true);
+//		dialog.pack();
+//		dialog.setVisible(true);
+//	}
+//
+//	/**
+//	 * @deprecated Use TranslationAskJDialog
+//	 * @return 
+//	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons Kr&uuml;ger</a>
+//	 */
+//	private static JComponent getButtons() {
+//		JPanel jPanel = new JPanel();
+//		if (okButton == null) {
+//			okButton = new OkButton(new AbstractAction("enter") {
+//	            public void actionPerformed(ActionEvent evt) {
+//	            	firePropertyChange(PROPERTY_APPLY_AND_CLOSE, null, null);
+//	            	dialog.dispose();
+//	            }
+//	        } );
+//		}
+//		jPanel.add(okButton);
+//
+//		if (cancelButton == null) {
+//			cancelButton = new CancelButton(new AbstractAction("") {
+//	            public void actionPerformed(ActionEvent evt) {
+//	            	// restore();
+//	            	firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
+//	            	dialog.dispose();
+//	            }
+//	        } );
+//		}
+//		jPanel.add(okButton);
+//
+//		return jPanel;
+//	}
 	
 	/**
-	 * Merges a few {@link TranslationEditJPanel}s and shows them together..
-	 * So far this is working on the Translation object directly, Cancel not possible.
-	 * @param translationEditJPanel
-
-	 * @deprecated User {@link TranslationAskJDialog}	 * 
-	 */
-	public static void ask(Window parentWindow, TranslationEditJPanel... translationEditJPanels) {
-		dialog = new JDialog(parentWindow);
-	//	backup( translationEditJPanels );
-		showDialog(dialog, translationEditJPanels);
-	}
-	
-	/**
-	 * Merges a few {@link TranslationEditJPanel}s and shows them together..
-	 * So far this is working on the Translation object directly, Cancel not possible.
-	 * @param translationEditJPanel
-	 * 
-	 * 	 * @deprecated User {@link TranslationAskJDialog}
-	 */
-	public static void ask(JDialog parentDialog, TranslationEditJPanel... translationEditJPanels) {
-		dialog = new JDialog(parentDialog);
-	//	backup( translationEditJPanels );
-		showDialog(dialog, translationEditJPanels);
-	}
-	
-	
-	/**
-	 * 	 * @deprecated User {@link TranslationAskJDialog}
-	 * @param d
-	 * @param translationEditJPanels 
-	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons Kr&uuml;ger</a>
-	 */
-	public static void showDialog(JDialog d, JComponent... translationEditJPanels) {
-		dialog = d;
-		dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
-		SwingUtil.centerFrameOnScreen(dialog);
-		Box box = Box.createVerticalBox();
-		for (JComponent panel : translationEditJPanels) {
-			box.add(panel);
-		}
-		JPanel cp = new JPanel( new BorderLayout());
-		cp.add( box, BorderLayout.CENTER);
-		cp.add( getButtons(), BorderLayout.SOUTH  );
-		dialog.setContentPane(cp);
-		
-		// dialog.getRootPane().setDefaultButton(okButton);
-		
-		dialog.setTitle("Please translate"); //i8n
-		dialog.setModal(true);
-		dialog.pack();
-		dialog.setVisible(true);
-	}
-
-	/**
-	 * @deprecated Use TranslationAskJDialog
-	 * @return 
-	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons Kr&uuml;ger</a>
-	 */
-	private static JComponent getButtons() {
-		JPanel jPanel = new JPanel();
-		if (okButton == null) {
-			okButton = new OkButton(new AbstractAction("enter") {
-	            public void actionPerformed(ActionEvent evt) {
-	            	firePropertyChange(PROPERTY_APPLY_AND_CLOSE, null, null);
-	            	dialog.dispose();
-	            }
-	        } );
-		}
-		jPanel.add(okButton);
-
-		if (cancelButton == null) {
-			cancelButton = new CancelButton(new AbstractAction("") {
-	            public void actionPerformed(ActionEvent evt) {
-	            	// restore();
-	            	firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
-	            	dialog.dispose();
-	            }
-	        } );
-		}
-		jPanel.add(okButton);
-
-		return jPanel;
-	}
-	
-	/**
 	 * @return The {@link Translation} that this {@link TranslationEditJPanel} deals with.
 	 */
 	public Translation getTranslation() {
 		return trans;
 	}
-//
-//	
-//	public static void main(String[] args) {
-//		ArrayList<String> lang = new ArrayList <String> ();
-//		lang.add("de");
-//		lang.add("en");
-//		lang.add("fr");
-//		
-//		Translation transe = new Translation();
-//		transe.put("de", "Terciopelo-Lanzenotter");
-//		TranslationEditJPanel p1 = new TranslationEditJPanel("Name von New Group",
-//				transe, lang);
-//		
-//		
-//		Translation transe2 = new Translation();
-//		transe2.put("de", "Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart.");
-//		TranslationEditJPanel p2 = new TranslationEditJPanel("Description of Animal:",
-//				transe2, lang);
-//		
-////		JFrame frame = new JFrame();
-////		frame.setContentPane(p1);
-////		frame.pack();
-////		frame.setVisible(true);
-//		
-//		ask(new Frame(),p1,p2);
-//	}
+
 }

Modified: trunk/src/skrueger/swing/resource/locales/SwingResourceBundle.properties
===================================================================
--- trunk/src/skrueger/swing/resource/locales/SwingResourceBundle.properties	2009-03-30 00:43:08 UTC (rev 37)
+++ trunk/src/skrueger/swing/resource/locales/SwingResourceBundle.properties	2009-04-05 15:06:56 UTC (rev 38)
@@ -1 +1,2 @@
-translation_dialog_title=Please translate
\ No newline at end of file
+translation_dialog_title=Please translate
+ErrorMsg.InvalidCharacterInTranslation = Sorry, but you must not use characters { and } in any text label.
\ No newline at end of file

Modified: trunk/src/skrueger/swing/resource/locales/SwingResourceBundle_de.properties
===================================================================
--- trunk/src/skrueger/swing/resource/locales/SwingResourceBundle_de.properties	2009-03-30 00:43:08 UTC (rev 37)
+++ trunk/src/skrueger/swing/resource/locales/SwingResourceBundle_de.properties	2009-04-05 15:06:56 UTC (rev 38)
@@ -1 +1,2 @@
-translation_dialog_title=Bitte übersetzen
\ No newline at end of file
+translation_dialog_title=Bitte übersetzen
+ErrorMsg.InvalidCharacterInTranslation = Die Zeichen { und } dürfen Sie leider nicht benutzen.
\ No newline at end of file



More information about the Schmitzm-commits mailing list