package skrueger.swing; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JOptionPane; import net.miginfocom.swing.MigLayout; import schmitzm.swing.JPanel; import schmitzm.swing.SwingUtil; import skrueger.i8n.Translation; public class TranslationsAskJPanel extends JPanel implements Cancellable, Checkable { private String[] backup = new String[50]; // Maximum 50 languages private final JComponent[] translationEditJPanelsOrJustComponents; public TranslationsAskJPanel( JComponent... translationEditJPanelsOrJustComponents_) { super(new MigLayout("wrap 1, width 100%", "[]")); this.translationEditJPanelsOrJustComponents = translationEditJPanelsOrJustComponents_; backup(); // Box box = Box.createVerticalBox(); // for (JComponent panel : translationEditJPanelsOrJustComponents) { // panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT); // panel.setBorder(BorderFactory.createEmptyBorder(5, 6, 5, 6)); // box.add(panel); // // } // add(box); for (JComponent panel : translationEditJPanelsOrJustComponents) { panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT); panel.setBorder(BorderFactory.createEmptyBorder(5, 6, 5, 6)); add(panel); } } /** * Stores the original values of all {@link TranslationEditJPanel}s so * cancel works. */ protected void backup() { // 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(); } } } /** * Used to restore all the values when cancel has been pressed. */ @Override public void cancel() { int count = 0; for (JComponent component : translationEditJPanelsOrJustComponents) { if (component instanceof TranslationEditJPanel) { TranslationEditJPanel tep = (TranslationEditJPanel) component; tep.getTranslation().fromOneLine(backup[count++]); } } } /** * @return true if none of the translations contains illegal * characters. */ public boolean checkValidInputs() { for (JComponent component : translationEditJPanelsOrJustComponents) { if (component instanceof TranslationEditJPanel) { TranslationEditJPanel tep = (TranslationEditJPanel) component; final Translation translationToCheck = tep.getTranslation(); if (!Translation.checkValid(translationToCheck)) { JOptionPane .showMessageDialog( this, SwingUtil .R("TranslationAskJDialog.ErrorMsg.InvalidCharacterInTranslation")); return false; } } } return true; } }