[Schmitzm-commits] r1290 - trunk/src/skrueger/i8n

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun Nov 21 21:58:12 CET 2010


Author: keeb
Date: 2010-11-21 21:58:12 +0100 (Sun, 21 Nov 2010)
New Revision: 1290

Modified:
   trunk/src/skrueger/i8n/SwitchLanguageDialog.java
Log:
Refactored SwitchLanguageDialog to use MigLayout. Thereby optimized few methods.
See http://wald.intevation.org/tracker/?func=detail&aid=1612&group_id=37&atid=295 

Modified: trunk/src/skrueger/i8n/SwitchLanguageDialog.java
===================================================================
--- trunk/src/skrueger/i8n/SwitchLanguageDialog.java	2010-11-19 10:54:01 UTC (rev 1289)
+++ trunk/src/skrueger/i8n/SwitchLanguageDialog.java	2010-11-21 20:58:12 UTC (rev 1290)
@@ -44,6 +44,8 @@
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 
+import net.miginfocom.swing.MigLayout;
+
 import org.apache.log4j.Logger;
 
 import schmitzm.swing.SwingUtil;
@@ -58,235 +60,166 @@
  * @author Stefan A. Tzeggai
  */
 public class SwitchLanguageDialog extends AtlasDialog {
-	protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class);
+    protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class);
 
-	private JPanel jContentPane = null;
+    private JPanel jContentPane = null;
 
-	private JLabel jLabelFlagimage = null;
+    private JLabel jLabelFlagimage = null;
 
-	private JPanel jPanel = null;
+    private JPanel jPanel = null;
 
-	private JButton jButton = null;
+    private JLabel jLabel = null;
 
-	private JPanel jPanel1 = null;
+    private LanguagesComboBox jComboBox = null;
 
-	private JLabel jLabel = null;
+    private final List<String> languages;
 
-	private LanguagesComboBox jComboBox = null;
+    /**
+     * if <code>true</code>, the default locale will also be changed during a
+     * language selection
+     **/
+    protected boolean setLocale;
 
-	private final List<String> languages;
+    /**
+     * A dialog to select one of the available languages. If only one language
+     * is available, select it directly. Creating this object automatically
+     * makes it visible, unless there is only one language to choose from.. it
+     * that case it disposes itself automatically.
+     * 
+     * @param setLocale if <code>true</code>, the default locale will also be
+     *        changed during a language selection
+     */
+    public SwitchLanguageDialog(final Component owner,
+            final List<String> languages, boolean setLocale) {
+        super(owner);
+        this.languages = languages;
+        this.setLocale = setLocale;
 
-	/**
-	 * if <code>true</code>, the default locale will also be changed during a
-	 * language selection
-	 **/
-	protected boolean setLocale;
+        if (languages == null || languages.size() == 0) {
+            // No language is available.
+            return;
+        }
 
-	/**
-	 * A dialog to select one of the available languages. If only one language
-	 * is available, select it directly. Creating this object automatically
-	 * makes it visible, unless there is only one language to choose from.. it
-	 * that case it disposes itself automatically.
-	 * 
-	 * @param setLocale
-	 *            if <code>true</code>, the default locale will also be changed
-	 *            during a language selection
-	 */
-	public SwitchLanguageDialog(final Component owner,
-			final List<String> languages, boolean setLocale) {
-		super(owner);
-		this.languages = languages;
-		this.setLocale = setLocale;
+        Translation.setActiveLang(languages.get(0), setLocale);
+        if (languages.size() == 1) {
+            // Only language one language is available. It has been selected
+            // automatically.
+            return;
+        }
 
-		if (languages == null || languages.size() == 0) {
-			// No language is available.
-			return;
-		}
+        initialize();
+    }
 
-		Translation.setActiveLang(languages.get(0), setLocale);
-		if (languages.size() == 1) {
-			// Only language one language is available. It has been selected
-			// automatically.
-			return;
-		}
+    @Override
+    /**
+     * This modal dialog will not appear if there is <= one language to select from.
+     */
+    public void setVisible(boolean b) {
+        if (b == true && (languages == null || languages.size() <= 1)) {
+            return;
+        }
+        super.setVisible(b);
+    }
 
-		initialize();
-	}
+    /**
+     * This method initializes this
+     * 
+     * @return void
+     */
+    private void initialize() {
+        this.setContentPane(getJContentPane());
 
-	@Override
-	/**
-	 * This modal dialog will not appear if there is <= one language to select from.
-	 */
-	public void setVisible(boolean b) {
-		if (b == true && (languages == null || languages.size() <= 1)) {
-			return;
-		}
-		super.setVisible(b);
-	}
+        pack();
 
-	/**
-	 * This method initializes this
-	 * 
-	 * @return void
-	 */
-	private void initialize() {
-		this.setContentPane(getJContentPane());
+        SwingUtil.centerFrameOnScreenRandom(this);
+        setModal(true);
+    }
 
-		pack();
+    public boolean close() {
+        // Only close by ESC and window-close if a valid selection is made
+        if (jComboBox.getSelectedIndex() == languages.size()
+                || jComboBox.getSelectedIndex() == -1)
+            return false;
+        else
+            return super.close();
+    }
 
-		SwingUtil.centerFrameOnScreenRandom(this);
-		setModal(true);
-	}
+    /**
+     * This method initializes jContentPane
+     * 
+     * @return javax.swing.JPanel
+     */
+    private JPanel getJContentPane() {
+        if (jContentPane == null) {
+            jLabelFlagimage = new JLabel(new ImageIcon(
+                    TranslationEditJPanel.class
+                            .getResource("resource/flags.jpg")));
+            jContentPane = new JPanel();
+            MigLayout migLayout = new MigLayout("wrap 1", "[center]", "[]0[]");
+            jContentPane.setLayout(migLayout);
+            jContentPane.add(jLabelFlagimage, "north");
+            jContentPane.add(getLanguageCombobox());
+            jContentPane.add(getOkButton(), // AtlasDialog.getOkButton()
+                    "right, w 50!, gaptop 5, gapright 5, gapbottom 5");
+        }
+        return jContentPane;
+    }
 
-	public boolean close() {
-		// Only close by ESC and window-close if a valid selection is made
-		if (jComboBox.getSelectedIndex() == languages.size()
-				|| jComboBox.getSelectedIndex() == -1)
-			return false;
-		else
-			return super.close();
-	}
+    /**
+     * This method initializes the JPanel that carries the select language
+     * combobox
+     * 
+     * @return javax.swing.JPanel
+     */
+    private JPanel getLanguageCombobox() {
+        if (jPanel == null) {
+            jLabel = new JLabel();
+            jLabel.setText("Select language: "); // i8n!?! Maybe replace with an
+            // icon of an index finger
+            jPanel = new JPanel();
+            jPanel.add(jLabel);
+            jPanel.add(getJComboBox());
+        }
+        return jPanel;
+    }
 
-	/**
-	 * This method initializes jContentPane
-	 * 
-	 * @return javax.swing.JPanel
-	 */
-	private JPanel getJContentPane() {
-		if (jContentPane == null) {
-			final GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
-			gridBagConstraints11.gridx = 1;
-			gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
-			gridBagConstraints11.gridy = 1;
-			final GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
-			gridBagConstraints3.gridx = 1;
-			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
-			gridBagConstraints3.gridy = 2;
-			final GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
-			gridBagConstraints1.gridx = 0;
-			gridBagConstraints1.fill = GridBagConstraints.BOTH;
-			gridBagConstraints1.gridwidth = 2;
-			gridBagConstraints1.anchor = GridBagConstraints.NORTH;
-			gridBagConstraints1.gridy = 0;
-			jLabelFlagimage = new JLabel(new ImageIcon(
-					TranslationEditJPanel.class
-							.getResource("resource/flags.jpg")));
-			jContentPane = new JPanel();
-			jContentPane.setLayout(new GridBagLayout());
-			jContentPane.add(jLabelFlagimage, gridBagConstraints1);
-			jContentPane.add(getJPanel(), gridBagConstraints3);
-			jContentPane.add(getJPanel1(), gridBagConstraints11);
-		}
-		return jContentPane;
-	}
+    /**
+     * This method initializes the Select Language Combobox
+     * 
+     * @return javax.swing.JComboBox
+     */
+    private JComboBox getJComboBox() {
+        if (jComboBox == null) {
+            ArrayList<String> languagesPlusOne = new ArrayList<String>(
+                    languages);
+            languagesPlusOne.add("?");
 
-	/**
-	 * This method initializes jPanel
-	 * 
-	 * @return javax.swing.JPanel
-	 */
-	private JPanel getJPanel() {
-		if (jPanel == null) {
-			final GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
-			gridBagConstraints4.gridx = 0;
-			gridBagConstraints4.anchor = GridBagConstraints.EAST;
-			gridBagConstraints4.weightx = 1.0;
-			gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
-			gridBagConstraints4.gridy = 0;
-			jPanel = new JPanel();
-			jPanel.setLayout(new GridBagLayout());
-			jPanel.add(getJButton(), gridBagConstraints4);
-		}
-		return jPanel;
-	}
+            jComboBox = new LanguagesComboBox(languages);
 
-	/**
-	 * This method initializes jButton
-	 * 
-	 * @return javax.swing.JButton
-	 */
-	private JButton getJButton() {
-		if (jButton == null) {
-			jButton = new OkButton();
-			jButton.setEnabled(false);
+            SwingUtil.addMouseWheelForCombobox(jComboBox);
+            jComboBox.addActionListener(new ActionListener() {
 
-			jButton.addActionListener(new ActionListener() {
+                public void actionPerformed(final ActionEvent e) {
+                    if (jComboBox.getSelectedIndex() == languages.size()) {
+                        getOkButton().setEnabled(false);
+                        return;
+                    }
 
-				public void actionPerformed(ActionEvent e) {
-					dispose();
-				}
+                    String l = languages.get(jComboBox.getSelectedIndex());
+                    try {
+                        Translation.setActiveLang(l, setLocale);
 
-			});
-		}
-		return jButton;
-	}
+                        getOkButton().setEnabled(true);
+                    } catch (java.lang.IllegalArgumentException ee) {
+                        LOGGER.warn("The language " + l + " is not valid", ee);
+                        getOkButton().setEnabled(false);
+                    }
 
-	/**
-	 * This method initializes jPanel1
-	 * 
-	 * @return javax.swing.JPanel
-	 */
-	private JPanel getJPanel1() {
-		if (jPanel1 == null) {
-			final GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
-			gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
-			gridBagConstraints2.gridy = 0;
-			gridBagConstraints2.weightx = 1.0;
-			gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
-			gridBagConstraints2.anchor = GridBagConstraints.WEST;
-			gridBagConstraints2.gridx = 1;
-			final GridBagConstraints gridBagConstraints = new GridBagConstraints();
-			gridBagConstraints.gridx = 0;
-			gridBagConstraints.insets = new Insets(0, 5, 0, 0);
-			gridBagConstraints.gridy = 0;
-			jLabel = new JLabel();
-			jLabel.setText("Select language: "); // i8n!?! Maybe replace with an
-			// icon of an index finger
-			jPanel1 = new JPanel();
-			jPanel1.setLayout(new GridBagLayout());
-			jPanel1.add(jLabel, gridBagConstraints);
-			jPanel1.add(getJComboBox(), gridBagConstraints2);
-		}
-		return jPanel1;
-	}
+                }
 
-	/**
-	 * This method initializes jComboBox
-	 * 
-	 * @return javax.swing.JComboBox
-	 */
-	private JComboBox getJComboBox() {
-		if (jComboBox == null) {
-			ArrayList<String> languagesPlusOne = new ArrayList<String>(
-					languages);
-			languagesPlusOne.add("?");
+            });
+        }
+        return jComboBox;
+    }
 
-			jComboBox = new LanguagesComboBox(languages);
-
-			SwingUtil.addMouseWheelForCombobox(jComboBox);
-			jComboBox.addActionListener(new ActionListener() {
-
-				public void actionPerformed(final ActionEvent e) {
-					if (jComboBox.getSelectedIndex() == languages.size()) {
-						getJButton().setEnabled(false);
-						return;
-					}
-
-					String l = languages.get(jComboBox.getSelectedIndex());
-					try {
-						Translation.setActiveLang(l, setLocale);
-
-						getJButton().setEnabled(true);
-					} catch (java.lang.IllegalArgumentException ee) {
-						LOGGER.warn("The language " + l + " is not valid", ee);
-						getJButton().setEnabled(false);
-					}
-
-				}
-
-			});
-		}
-		return jComboBox;
-	}
-
 }



More information about the Schmitzm-commits mailing list