diff --git a/izpack-dist/src/main/izpack/install.xml b/izpack-dist/src/main/izpack/install.xml index 98d5245859..56d0c45486 100755 --- a/izpack-dist/src/main/izpack/install.xml +++ b/izpack-dist/src/main/izpack/install.xml @@ -24,7 +24,8 @@ - + + diff --git a/izpack-installer/src/main/java/com/izforge/izpack/installer/gui/InstallerController.java b/izpack-installer/src/main/java/com/izforge/izpack/installer/gui/InstallerController.java index 7b47d53e00..857ba73957 100755 --- a/izpack-installer/src/main/java/com/izforge/izpack/installer/gui/InstallerController.java +++ b/izpack-installer/src/main/java/com/izforge/izpack/installer/gui/InstallerController.java @@ -2,6 +2,7 @@ import javax.swing.SwingUtilities; +import com.izforge.izpack.api.exception.IzPackException; import com.izforge.izpack.installer.base.InstallDataConfiguratorWithRules; /** @@ -25,43 +26,53 @@ public InstallerController(InstallDataConfiguratorWithRules installDataRulesEngi public InstallerController buildInstallation() { - try + + run(new Runnable() { - SwingUtilities.invokeAndWait(new Runnable() + @Override + public void run() { - @Override - public void run() - { - installerFrame.buildGUI(); - installerFrame.sizeFrame(); - } - }); - } - catch (Exception exception) - { - throw new IllegalStateException(exception); - } + installerFrame.buildGUI(); + installerFrame.sizeFrame(); + } + }); return this; } public void launchInstallation() { - try + run(new Runnable() { - SwingUtilities.invokeAndWait(new Runnable() + @Override + public void run() { - @Override - public void run() - { - installerFrame.setVisible(true); - installerFrame.navigateNext(); - } - }); + installerFrame.setVisible(true); + installerFrame.navigateNext(); + } + }); + } + + /** + * Runs a {@code Runnable} inside the event dispatch thread. + * + * @param action the action to run + */ + private void run(Runnable action) + { + if (SwingUtilities.isEventDispatchThread()) + { + action.run(); } - catch (Exception exception) + else { - throw new IllegalStateException(exception); + try + { + SwingUtilities.invokeAndWait(action); + } + catch (Exception exception) + { + throw new IzPackException(exception); + } } } - } diff --git a/izpack-installer/src/main/java/com/izforge/izpack/installer/language/LanguageDialog.java b/izpack-installer/src/main/java/com/izforge/izpack/installer/language/LanguageDialog.java index f88179909c..7116d1e15c 100755 --- a/izpack-installer/src/main/java/com/izforge/izpack/installer/language/LanguageDialog.java +++ b/izpack-installer/src/main/java/com/izforge/izpack/installer/language/LanguageDialog.java @@ -1,8 +1,25 @@ +/* + * IzPack - Copyright 2001-2012 Julien Ponge, All Rights Reserved. + * + * http://izpack.org/ + * http://izpack.codehaus.org/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.izforge.izpack.installer.language; import java.awt.Component; import java.awt.Dimension; -import java.awt.Font; import java.awt.GraphicsEnvironment; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; @@ -12,10 +29,11 @@ import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.util.List; +import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.TreeMap; +import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.GrayFilter; @@ -46,29 +64,19 @@ * @author Christian Murphy * @author Klaus Bartz */ -public class LanguageDialog extends JDialog implements ActionListener +public class LanguageDialog extends JDialog { private static final long serialVersionUID = 3256443616359887667L; - private static transient final Logger logger = Logger.getLogger(LanguageDialog.class.getName()); - - /** - * The combo box. - */ - private JComboBox comboBox; - /** - * Holds language to ISO-3 language code translation. + * The parent frame. */ - private Map isoTable; + private final JFrame frame; - private GUIInstallData installData; /** - * defined modifier for language display type. + * The installation data. */ - private static final String[] LANGUAGE_DISPLAY_TYPES = {"iso3", "native", "default"}; - private JFrame frame; - private RequirementsChecker requirements; + private final GUIInstallData installData; /** * The resources. @@ -80,31 +88,81 @@ public class LanguageDialog extends JDialog implements ActionListener */ private final Locales locales; + /** + * Installation requirements checker. + */ + private final RequirementsChecker requirements; + + /** + * Maps ISO3 codes to the corresponding language display values. + */ + private Map displayNames = new HashMap(); /** - * The constructor. + * The combo box. + */ + private JComboBox comboBox; + + /** + * The logger. + */ + private static final Logger logger = Logger.getLogger(LanguageDialog.class.getName()); + + + /** + * Constructs a {@code LanguageDialog}. * - * @param installDataGUI - * @param requirements + * @param frame the parent frame + * @param resources the resources + * @param locales the locales + * @param installData the installation data + * @param requirements the installation requirements */ - public LanguageDialog(JFrame frame, Resources resources, Locales locales, GUIInstallData installDataGUI, - RequirementsChecker requirements) throws Exception + public LanguageDialog(JFrame frame, Resources resources, Locales locales, GUIInstallData installData, + RequirementsChecker requirements) { super(frame); this.frame = frame; this.resources = resources; this.locales = locales; - this.installData = installDataGUI; + this.installData = installData; this.requirements = requirements; this.setName(GuiId.DIALOG_PICKER.id); - initLanguageDialog(); + initialise(); } - private void initLanguageDialog() + /** + * Displays the dialog. + * + * @throws Exception for any error + */ + public void initLangPack() throws Exception { + // Loads the suitable langpack + if (locales.getLocales().size() > 1) + { + frame.setVisible(false); + setVisible(true); + } + + // check installer conditions + if (!requirements.check()) + { + logger.info("Not all installer requirements are fulfilled."); + System.exit(-1); + } + } + + /** + * Initialises the dialog. + */ + private void initialise() + { + JPanel contentPane = (JPanel) getContentPane(); + Languages languages = new Languages(locales, installData, contentPane.getFont()); + // We build the GUI addWindowListener(new WindowHandler()); - JPanel contentPane = (JPanel) getContentPane(); setTitle("Language Selection"); GridBagLayout layout = new GridBagLayout(); contentPane.setLayout(layout); @@ -124,9 +182,9 @@ private void initLanguageDialog() contentPane.add(imgLabel); String firstMessage = "Please select your language"; - if (getLangType().equals(LANGUAGE_DISPLAY_TYPES[0])) - // iso3 + if (languages.getDisplayType() == Languages.DisplayType.ISO3) { + // TODO - Not sure why this is specific to ISO3. Should be localised too. firstMessage = "Please select your language below"; } @@ -137,14 +195,19 @@ private void initLanguageDialog() contentPane.add(label1); gbConstraints.insets = new Insets(5, 5, 5, 5); - Object[] langPacks = reviseItems(locales.getLocales()); + displayNames = languages.getDisplayNames(); - comboBox = new JComboBox(langPacks); + comboBox = new JComboBox(displayNames.keySet().toArray()); comboBox.setName(GuiId.COMBO_BOX_LANG_FLAG.id); if (useFlags()) { - comboBox.setRenderer(new FlagRenderer(resources)); + comboBox.setRenderer(new FlagRenderer()); + } + else + { + comboBox.setRenderer(new LanguageRenderer()); } + gbConstraints.gridy = 3; layout.addLayoutComponent(comboBox, gbConstraints); contentPane.add(comboBox); @@ -152,7 +215,14 @@ private void initLanguageDialog() gbConstraints.insets = new Insets(15, 5, 15, 5); JButton okButton = new JButton("OK"); okButton.setName(GuiId.BUTTON_LANG_OK.id); - okButton.addActionListener(this); + okButton.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + onOK(); + } + }); gbConstraints.fill = GridBagConstraints.NONE; gbConstraints.gridy = 4; gbConstraints.anchor = GridBagConstraints.CENTER; @@ -172,92 +242,9 @@ private void initLanguageDialog() Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); setLocation(center.x - frameSize.width / 2, center.y - frameSize.height / 2 - 10); setResizable(true); - this.setSelection(Locale.getDefault().getISO3Language().toLowerCase()); - this.setModal(true); - this.toFront(); - } - - /** - * Revises iso3 language items depending on the language display type. - * - * @param locales item array to be revised - * @return the revised array - */ - private Object[] reviseItems(List locales) - { - Object[] result; - String langType = getLangType(); - if (langType.equals(LANGUAGE_DISPLAY_TYPES[0])) - { - // iso3 - result = new Object[locales.size()]; - for (int i = 0; i < locales.size(); ++i) - { - result[i] = locales.get(i).getISO3Language(); - } - } - else if (langType.equals(LANGUAGE_DISPLAY_TYPES[1])) - { - // native: get the names as they are written in that language. - result = expandItems(locales, (new JComboBox()).getFont()); - } - else - { - // default: get the names as they are written in the default - // language. - result = expandItems(locales, null); - } - return result; - } - - /** - * Expands the given iso3 codes to language names. If a testFont is given, the codes are - * tested whether they can displayed or not. If not, or no font given, the language name - * will be returned as written in the default language of this VM. - * - * @param locales item array to be expanded to the language name - * @param testFont font to test wheter a name is displayable - * @return aray of expanded items - */ - private String[] expandItems(List locales, Font testFont) - { - String[] result = new String[locales.size()]; - int i; - for (i = 0; i < locales.size(); i++) - { - Locale locale = locales.get(i); - String it = expandItem(locale, testFont); - result[i] = it; - } - return result; - } - - /** - * Expands the given iso3 code to a language name. If a testFont is given, the code will be - * tested whether it is displayable or not. If not, or no font given, the language name will - * be returned as written in the default language of this VM. - * - * @param locale locale to be expanded to the language name - * @param testFont font to test wheter the name is displayable - * @return expanded locale - */ - private String expandItem(Locale locale, Font testFont) - { - if (testFont == null) - { - // Return the language name in the spelling of the default locale. - return locale.getDisplayLanguage(); - } - // Get the language name in the spelling of that language. - String str = locale.getDisplayLanguage(locale); - int cdut = testFont.canDisplayUpTo(str); - if (cdut > -1) - { - // Test font cannot render it; - // use language name in the spelling of the default locale. - str = locale.getDisplayLanguage(); - } - return (str); + comboBox.setSelectedItem(Locale.getDefault().getISO3Language().toLowerCase()); + setModal(true); + toFront(); } /** @@ -265,7 +252,7 @@ private String expandItem(Locale locale, Font testFont) * * @return The image icon. */ - public ImageIcon getImage() + private ImageIcon getImage() { ImageIcon img; try @@ -279,55 +266,9 @@ public ImageIcon getImage() return img; } - /** - * Gets the selected object. - * - * @return The selected item. - */ - public Object getSelection() + private void onOK() { - Object retval = null; - if (isoTable != null) - { - retval = isoTable.get((String) comboBox.getSelectedItem()); - } - return (retval != null) ? retval : comboBox.getSelectedItem(); - } - - /** - * Sets the selection. - * - * @param item The item to be selected. - */ - public void setSelection(Object item) - { - Object mapped = null; - if (isoTable != null) - { - for (String key : isoTable.keySet()) - { - if (isoTable.get(key).equals(item)) - { - mapped = key; - break; - } - } - } - if (mapped == null) - { - mapped = item; - } - comboBox.setSelectedItem(mapped); - } - - /** - * Closer. - * - * @param e The event. - */ - public void actionPerformed(ActionEvent e) - { - String selectedPack = (String) this.getSelection(); + String selectedPack = (String) comboBox.getSelectedItem(); if (selectedPack == null) { throw new RuntimeException("installation canceled"); @@ -338,20 +279,13 @@ public void actionPerformed(ActionEvent e) // Configure buttons after locale has been loaded installData.configureGuiButtons(); } - catch (Exception e1) + catch (Exception exception) { - e1.printStackTrace(); + logger.log(Level.SEVERE, exception.getMessage(), exception); } dispose(); } - public void runPicker() - { - // frame.setVisible(true); - frame.setVisible(false); - this.setVisible(true); - } - /** * The window events handler. * @@ -371,16 +305,80 @@ public void windowClosing(WindowEvent e) } } + /** + * Returns whether flags should be used in the language selection dialog or not. + * + * @return whether flags should be used in the language selection dialog or not + */ + private boolean useFlags() + { + if (installData.guiPrefs.modifier.containsKey("useFlags") + && "no".equalsIgnoreCase(installData.guiPrefs.modifier.get("useFlags"))) + { + return (false); + } + return (true); + } + + + /** + * Sets the selected locale on the installation data. + * + * @param code the locale ISO code + * @throws ResourceException for any resource exception + */ + private void propagateLocale(String code) + { + Locale locale = locales.getLocale(code); + locales.setLocale(locale); + installData.setLocale(locale); + installData.setMessages(locales.getMessages()); + } + + /** + * A list cell renderer to display the language given is ISO3 code. + */ + private class LanguageRenderer extends JLabel implements ListCellRenderer + { + + /** + * Return a component that has been configured to display the specified value. + * + * @param list the list + * @param value the value to display + * @param index the cells index. + * @param isSelected true if the specified cell was selected. + * @param cellHasFocus true if the specified cell has the focus + * @return a component to render the value + */ + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, + boolean cellHasFocus) + { + String code = (String) value; + setText(displayNames.get(code)); + if (isSelected) + { + setForeground(list.getSelectionForeground()); + setBackground(list.getSelectionBackground()); + } + else + { + setForeground(list.getForeground()); + setBackground(list.getBackground()); + } + return this; + } + } + /** * A list cell renderer that adds the flags on the display. * * @author Julien Ponge */ - private class FlagRenderer extends JLabel implements ListCellRenderer + private class FlagRenderer extends LanguageRenderer { - private static final long serialVersionUID = 3832899961942782769L; - /** * Icons cache. */ @@ -391,11 +389,11 @@ private class FlagRenderer extends JLabel implements ListCellRenderer */ private TreeMap grayIcons = new TreeMap(); - private Resources resources; - - public FlagRenderer(Resources resources) + /** + * Default constructor. + */ + public FlagRenderer() { - this.resources = resources; setOpaque(true); } @@ -412,117 +410,34 @@ public FlagRenderer(Resources resources) public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { - // We put the label - String iso3 = (String) value; - setText(iso3); - if (isoTable != null) - { - iso3 = isoTable.get(iso3); - } - if (isSelected) - { - setForeground(list.getSelectionForeground()); - setBackground(list.getSelectionBackground()); - } - else - { - setForeground(list.getForeground()); - setBackground(list.getBackground()); - } - // We put the icon + Component result = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); - if (!icons.containsKey(iso3)) + String code = (String) value; + if (!icons.containsKey(code)) { - ImageIcon icon; - icon = resources.getImageIcon("flag." + iso3); - icons.put(iso3, icon); - icon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage())); - grayIcons.put(iso3, icon); + try + { + ImageIcon icon; + icon = resources.getImageIcon("flag." + code); + icons.put(code, icon); + icon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage())); + grayIcons.put(code, icon); + } + catch (ResourceException exception) + { + logger.log(Level.WARNING, exception.getMessage(), exception); + } } if (isSelected || index == -1) { - setIcon(icons.get(iso3)); + setIcon(icons.get(code)); } else { - setIcon(grayIcons.get(iso3)); + setIcon(grayIcons.get(code)); } - - // We return - return this; - } - } - - /** - * Returns the type in which the language should be displayed in the language selction dialog. - * Possible are "iso3", "native" and "usingDefault". - * - * @return language display type - */ - protected String getLangType() - { - if (installData.guiPrefs.modifier.containsKey("langDisplayType")) - { - String val = installData.guiPrefs.modifier.get("langDisplayType"); - val = val.toLowerCase(); - // Verify that the value is valid, else return the default. - for (String aLANGUAGE_DISPLAY_TYPES : LANGUAGE_DISPLAY_TYPES) - { - if (val.equalsIgnoreCase(aLANGUAGE_DISPLAY_TYPES)) - { - return (val); - } - } - logger.fine("Value for language display type not valid; value: " + val); - } - return (LANGUAGE_DISPLAY_TYPES[0]); - } - - - /** - * Returns whether flags should be used in the language selection dialog or not. - * - * @return whether flags should be used in the language selection dialog or not - */ - protected boolean useFlags() - { - if (installData.guiPrefs.modifier.containsKey("useFlags") - && "no".equalsIgnoreCase(installData.guiPrefs.modifier.get("useFlags"))) - { - return (false); + return result; } - return (true); - } - - - public void initLangPack() throws Exception - { - // Loads the suitable langpack - if (locales.getLocales().size() > 1) - { - runPicker(); - } - - // check installer conditions - if (!requirements.check()) - { - logger.info("Not all installer requirements are fulfilled."); - System.exit(-1); - } - } - - /** - * Sets the selected locale on {@link } - * - * @param code the locale ISO code - * @throws ResourceException for any resource exception - */ - private void propagateLocale(String code) - { - Locale locale = locales.getLocale(code); - locales.setLocale(locale); - installData.setLocale(locale); - installData.setMessages(locales.getMessages()); } } diff --git a/izpack-installer/src/main/java/com/izforge/izpack/installer/language/Languages.java b/izpack-installer/src/main/java/com/izforge/izpack/installer/language/Languages.java new file mode 100755 index 0000000000..e0d0451ea4 --- /dev/null +++ b/izpack-installer/src/main/java/com/izforge/izpack/installer/language/Languages.java @@ -0,0 +1,207 @@ +/* + * IzPack - Copyright 2001-2012 Julien Ponge, All Rights Reserved. + * + * http://izpack.org/ + * http://izpack.codehaus.org/ + * + * Copyright 2012 Tim Anderson + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.izforge.izpack.installer.language; + +import java.awt.Font; +import java.util.LinkedHashMap; +import java.util.Locale; +import java.util.Map; +import java.util.logging.Logger; + +import com.izforge.izpack.api.resource.Locales; +import com.izforge.izpack.installer.data.GUIInstallData; + + +/** + * Helper to map ISO3 codes to their corresponding display names according to the configured "langDisplayType" + * modifier. + * + * @author Tim Anderson + */ +class Languages +{ + + enum DisplayType + { + ISO3, // indicates to display ISO3 language codes + NATIVE, // indicates to display the native name for a language. + DEFAULT // Indicates to display the default name for a language. + } + + /** + * The display type. One of "iso3", "native" or "default". + */ + private DisplayType displayType; + + /** + * The language display names, keyed on their ISO3 codes. + */ + private final Map displayNames = new LinkedHashMap(); + + /** + * The logger. + */ + private static final Logger logger = Logger.getLogger(Languages.class.getName()); + + + /** + * Constructs a {@code Languages}. + * + * @param locales the locales + * @param installData the installation data + * @param font the font to verify that language display names can be displayed. May be {@code null} + */ + public Languages(Locales locales, GUIInstallData installData, Font font) + { + displayType = getDisplayType(installData); + if (displayType == DisplayType.NATIVE && font == null) + { + logger.info("Cannot render native language display names - no font supplied for verification"); + displayType = DisplayType.DEFAULT; + } + + DisplayNameCollector collector; + if (displayType == DisplayType.NATIVE) + { + collector = new NativeDisplayNameCollector(font); + } + else if (displayType == DisplayType.DEFAULT) + { + collector = new DefaultDisplayNameCollector(); + } + else + { + collector = new ISO3CodeCollector(); + } + for (Locale locale : locales.getLocales()) + { + collector.addDisplayName(locale, displayNames); + } + } + + /** + * Returns the type in which the languages will be displayed. + * + * @return the display type + */ + public DisplayType getDisplayType() + { + return displayType; + } + + /** + * Returns the language display names, keyed on their ISO3 codes. + * + * @return the language display names + */ + public Map getDisplayNames() + { + return displayNames; + } + + /** + * Returns the type in which the language should be displayed. + * + * @return the language display type + */ + private DisplayType getDisplayType(GUIInstallData installData) + { + DisplayType result = DisplayType.DEFAULT; + Map modifier = installData.guiPrefs.modifier; + String langDisplayType = modifier.get("langDisplayType"); + if (langDisplayType != null && langDisplayType.length() != 0) + { + try + { + result = DisplayType.valueOf(langDisplayType.toUpperCase()); + } + catch (IllegalArgumentException exception) + { + logger.warning("Invalid langDisplayType: " + langDisplayType); + } + } + return result; + } + + /** + * Collects display names. + */ + private interface DisplayNameCollector + { + void addDisplayName(Locale locale, Map displayNames); + } + + /** + * Collects ISO3 codes. + */ + private class ISO3CodeCollector implements DisplayNameCollector + { + + @Override + public void addDisplayName(Locale locale, Map displayNames) + { + String code = locale.getISO3Language(); + displayNames.put(code, code); + } + } + + /** + * Collects display names as they are written in the default locale. + */ + private class DefaultDisplayNameCollector implements DisplayNameCollector + { + @Override + public void addDisplayName(Locale locale, Map displayNames) + { + displayNames.put(locale.getISO3Language(), locale.getDisplayLanguage()); + } + } + + /** + * Collects display names in the language of the supplied locale. If the language is not displayable by the + * supplied font, the language name will be returned as written in the default locale. + */ + private class NativeDisplayNameCollector implements DisplayNameCollector + { + + private final Font font; + + public NativeDisplayNameCollector(Font font) + { + this.font = font; + } + + @Override + public void addDisplayName(Locale locale, Map displayNames) + { + String name = locale.getDisplayLanguage(locale); + if (font.canDisplayUpTo(name) > -1) + { + // Font cannot render it; use language name in the spelling of the default locale. + name = locale.getDisplayLanguage(); + } + displayNames.put(locale.getISO3Language(), name); + } + + } + +} diff --git a/izpack-installer/src/test/java/com/izforge/izpack/installer/container/TestLanguageContainer.java b/izpack-installer/src/test/java/com/izforge/izpack/installer/container/TestLanguageContainer.java index 7f17db4054..4c88d69023 100755 --- a/izpack-installer/src/test/java/com/izforge/izpack/installer/container/TestLanguageContainer.java +++ b/izpack-installer/src/test/java/com/izforge/izpack/installer/container/TestLanguageContainer.java @@ -1,19 +1,13 @@ package com.izforge.izpack.installer.container; -import java.awt.Dimension; -import java.awt.Toolkit; import java.util.Arrays; import javax.swing.ImageIcon; -import javax.swing.JFrame; -import org.fest.swing.fixture.DialogFixture; import org.mockito.Mockito; -import org.picocontainer.Characteristics; import org.picocontainer.MutablePicoContainer; import org.picocontainer.PicoException; import org.picocontainer.injectors.ProviderAdapter; -import org.picocontainer.parameters.ComponentParameter; import com.izforge.izpack.api.container.Container; import com.izforge.izpack.api.data.Variables; @@ -26,8 +20,6 @@ import com.izforge.izpack.installer.container.provider.IconsProvider; import com.izforge.izpack.installer.data.UninstallData; import com.izforge.izpack.installer.data.UninstallDataWriter; -import com.izforge.izpack.installer.language.LanguageDialog; -import com.izforge.izpack.installer.requirement.RequirementsChecker; import com.izforge.izpack.merge.resolve.PathResolver; import com.izforge.izpack.test.provider.GUIInstallDataMockProvider; @@ -65,17 +57,12 @@ protected void fillContainer(MutablePicoContainer container) DefaultLocales locales = new DefaultLocales(resourceManager); container.addComponent(Variables.class, DefaultVariables.class) .addComponent(resourceManager) - .addComponent(Mockito.mock(RequirementsChecker.class)) .addComponent(Mockito.mock(UninstallData.class)) .addComponent(Mockito.mock(UninstallDataWriter.class)) .addComponent(Mockito.mock(AutomatedInstaller.class)) .addComponent(Mockito.mock(PathResolver.class)) .addComponent(locales) - .addComponent(DialogFixture.class, DialogFixture.class, new ComponentParameter(LanguageDialog.class)) - .addComponent(Container.class, this) - .as(Characteristics.USE_NAMES).addComponent(LanguageDialog.class) - .addConfig("frame", initFrame()) - .addConfig("title", "testPanel"); + .addComponent(Container.class, this); container .addAdapter(new ProviderAdapter(new GUIInstallDataMockProvider())) .addAdapter(new ProviderAdapter(new IconsProvider())); @@ -88,14 +75,4 @@ protected void fillContainer(MutablePicoContainer container) getClass().getResourceAsStream("/com/izforge/izpack/bin/langpacks/installer/eng.xml")); } - private JFrame initFrame() - { - // Dummy Frame - JFrame frame = new JFrame(); - Dimension frameSize = frame.getSize(); - Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); - frame.setLocation((screenSize.width - frameSize.width) / 2, - (screenSize.height - frameSize.height) / 2 - 10); - return frame; - } } diff --git a/izpack-installer/src/test/java/com/izforge/izpack/installer/language/LanguageDialogTest.java b/izpack-installer/src/test/java/com/izforge/izpack/installer/language/LanguageDialogTest.java index 4c49a90ce6..4a7bb72f5d 100755 --- a/izpack-installer/src/test/java/com/izforge/izpack/installer/language/LanguageDialogTest.java +++ b/izpack-installer/src/test/java/com/izforge/izpack/installer/language/LanguageDialogTest.java @@ -1,60 +1,176 @@ +/* + * IzPack - Copyright 2001-2012 Julien Ponge, All Rights Reserved. + * + * http://izpack.org/ + * http://izpack.codehaus.org/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.izforge.izpack.installer.language; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import java.util.Locale; + +import javax.swing.JFrame; + import org.fest.swing.fixture.DialogFixture; import org.hamcrest.core.Is; import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import com.izforge.izpack.api.GuiId; -import com.izforge.izpack.api.container.Container; import com.izforge.izpack.api.resource.Locales; +import com.izforge.izpack.api.resource.Resources; import com.izforge.izpack.installer.container.TestLanguageContainer; +import com.izforge.izpack.installer.data.GUIInstallData; +import com.izforge.izpack.installer.requirement.RequirementsChecker; +import com.izforge.izpack.test.Container; import com.izforge.izpack.test.junit.PicoRunner; /** - * Test for an installation using mock data + * Tests the {@link LanguageDialog}. + * + * @author Anthonin Bonnefoy + * @author Tim Anderson */ @RunWith(PicoRunner.class) -@com.izforge.izpack.test.Container(TestLanguageContainer.class) +@Container(TestLanguageContainer.class) public class LanguageDialogTest { - private Container installerContainer; - private DialogFixture dialogFixture; - private Locales locales; + /** + * The resources. + */ + private final Resources resources; + + /** + * The installation data. + */ + private final GUIInstallData installData; + + /** + * The locales. + */ + private final Locales locales; + + /** + * The dialog fixture. + */ + private DialogFixture fixture; - public LanguageDialogTest(Container installerContainer, DialogFixture dialogFixture, Locales locales) + /** + * Constructs {@code LanguageDialogTest}. + * + * @param resources the resources + * @param installData the installation data + * @param locales the locales. Must contain locales "eng" and "fra" + */ + public LanguageDialogTest(Resources resources, GUIInstallData installData, Locales locales) { - this.installerContainer = installerContainer; - this.dialogFixture = dialogFixture; + this.resources = resources; + this.installData = installData; this.locales = locales; } + /** + * Cleans up after the test. + */ @After public void tearBinding() { - installerContainer.dispose(); - if (dialogFixture != null) + if (fixture != null) { - dialogFixture.cleanUp(); - dialogFixture = null; + fixture.cleanUp(); + fixture = null; } } + /** + * Tests the "default" language display type. + */ + @Test + public void testDefaultDisplayType() + { + fixture = new DialogFixture(createDialog("default")); + + String eng = locales.getLocale("eng").getDisplayLanguage(); + String fra = locales.getLocale("fra").getDisplayLanguage(); + + checkSelectLanguage(eng, fra); + } + + /** + * Tests the "native" language display type. + */ @Test - public void testLangPickerChoseFra() throws Exception + public void testNativeDisplayType() { - dialogFixture.show(); - assertThat(dialogFixture.comboBox(GuiId.COMBO_BOX_LANG_FLAG.id).contents(), Is.is(new String[]{"eng", "fra"})); - dialogFixture.comboBox(GuiId.COMBO_BOX_LANG_FLAG.id).selectItem(1); - dialogFixture.button(GuiId.BUTTON_LANG_OK.id).click(); + LanguageDialog dialog = createDialog("native"); + fixture = new DialogFixture(dialog); + + Locale engLocale = locales.getLocale("eng"); + Locale fraLocale = locales.getLocale("fra"); + String eng = engLocale.getDisplayLanguage(engLocale); + String fra = fraLocale.getDisplayLanguage(fraLocale); + + checkSelectLanguage(eng, fra); + } + + /** + * Tests the "iso3" language display type. + */ + @Test + public void testISO3DisplayType() + { + fixture = new DialogFixture(createDialog("iso3")); + checkSelectLanguage("eng", "fra"); + } + + /** + * Verifies that the combo box has the correct elements for English and French and that the locale is set correctly + * when French is selected. + * + * @param englishDisplayName the expected display name for English + * @param frenchDisplayName the expected display name for French + */ + private void checkSelectLanguage(String englishDisplayName, String frenchDisplayName) + { + fixture.show(); + assertThat(fixture.comboBox(GuiId.COMBO_BOX_LANG_FLAG.id).contents(), + Is.is(new String[]{englishDisplayName, frenchDisplayName})); + fixture.comboBox(GuiId.COMBO_BOX_LANG_FLAG.id).selectItem(1); + fixture.button(GuiId.BUTTON_LANG_OK.id).click(); assertNotNull(locales.getLocale()); assertEquals("fra", locales.getLocale().getISO3Language()); } + /** + * Creates a new {@link LanguageDialog} that displays language names according to the supplied {@code + * langDisplayType}. + * + * @param langDisplayType one of "native", "default", "iso3" + * @return a new dialog + */ + private LanguageDialog createDialog(String langDisplayType) + { + installData.guiPrefs.modifier.put("langDisplayType", langDisplayType); + JFrame frame = new JFrame(); + frame.setLocationRelativeTo(null); + return new LanguageDialog(frame, resources, locales, installData, Mockito.mock(RequirementsChecker.class)); + } } \ No newline at end of file