示例#1
0
  private static JRadioButton[] buildLangButtons() {
    java.util.List<JRadioButton> btns = new java.util.ArrayList<JRadioButton>();

    for (Language lng : LANGS) {
      JRadioButton btn = new JRadioButton(lng.toString());
      btn.setText(lng.toString());
      btns.add(btn);
    }

    final ButtonGroup group = new ButtonGroup();
    for (final JRadioButton jrb : btns) {
      group.add(jrb);
    }

    JRadioButton[] res = new JRadioButton[btns.size()];
    return btns.toArray(res);
  }
示例#2
0
/** @author steg */
@SuppressWarnings("serial")
public class LanguageView extends JPanel {
  public static final int HORIZONTAL_PANEL = -1;
  public static final int VERTICAL_PANEL = 1;

  public static final Language[] LANGS = Language.values();

  private java.util.List<LanguageListener> langListeners = new ArrayList<LanguageListener>();

  private static JRadioButton[] buildLangButtons() {
    java.util.List<JRadioButton> btns = new java.util.ArrayList<JRadioButton>();

    for (Language lng : LANGS) {
      JRadioButton btn = new JRadioButton(lng.toString());
      btn.setText(lng.toString());
      btns.add(btn);
    }

    final ButtonGroup group = new ButtonGroup();
    for (final JRadioButton jrb : btns) {
      group.add(jrb);
    }

    JRadioButton[] res = new JRadioButton[btns.size()];
    return btns.toArray(res);
  }

  private JRadioButton[] langs = buildLangButtons();
  private int selectedIndex = -1;

  private void setSelected(int index) {
    if (index > -1 && index < langs.length) {
      selectedIndex = index;
      for (int __i = 0; __i < langs.length; ++__i) {
        langs[__i].setSelected(__i == index ? true : false);
      }
    }
  }

  private void mapAction() {
    for (final JRadioButton btn : langs) {
      btn.addActionListener(
          new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
              Language lang = Language.valueOf(btn.getText());
              setSelected(lang);
              for (LanguageListener listener : langListeners) {
                listener.actionPerformed(lang);
              }
            }
          });
    }
  }

  private void buildView(int orientation, String title) {
    BoxLayout layout =
        new BoxLayout(
            this, orientation == HORIZONTAL_PANEL ? BoxLayout.LINE_AXIS : BoxLayout.PAGE_AXIS);
    this.setLayout(layout);
    if (title != null) {
      this.setBorder(BorderFactory.createTitledBorder(title));
    }

    boolean first = true;
    for (JRadioButton btn : langs) {
      if (first) {
        first = false;
      } else {
        this.add(new JLabel(" "));
      }
      this.add(btn);
    }
  }

  public LanguageView(int orientation, String title) {
    setSelected(0);
    mapAction();
    buildView(orientation, title);
  }

  public LanguageView(Language lang, int orientation, String title) {
    setSelected(lang);
    mapAction();
    buildView(orientation, title);
  }

  public ILanguage getSelected() {
    if (selectedIndex < 0) return null;
    return LANGS[selectedIndex];
  }

  public String getSelectedString() {
    if (selectedIndex < 0) return null;
    return LANGS[selectedIndex].toString();
  }

  public String getSelectedValue() {
    if (selectedIndex < 0) return null;
    return LANGS[selectedIndex].getValue();
  }

  public void setSelected(Language lang) {
    if (lang != null) {
      setSelected(Arrays.binarySearch(LANGS, lang));
    }
  }

  public void addLanguageSelectionListener(LanguageListener listener) {
    langListeners.add(listener);
  }

  public static LanguageView createHorizontalPanel() {
    return new LanguageView(HORIZONTAL_PANEL, null);
  }

  public static LanguageView createHorizontalPanel(String title) {
    return new LanguageView(HORIZONTAL_PANEL, title);
  }

  public static LanguageView createHorizontalPanel(Language lang) {
    return new LanguageView(lang, HORIZONTAL_PANEL, null);
  }

  public static LanguageView createHorizontalPanel(Language lang, String title) {
    return new LanguageView(lang, HORIZONTAL_PANEL, title);
  }

  public static LanguageView createVerticalPanel() {
    return new LanguageView(VERTICAL_PANEL, null);
  }

  public static LanguageView createVerticalPanel(String title) {
    return new LanguageView(VERTICAL_PANEL, title);
  }

  public static LanguageView createVerticalPanel(Language lang) {
    return new LanguageView(lang, VERTICAL_PANEL, null);
  }

  public static LanguageView createVerticalPanel(Language lang, String title) {
    return new LanguageView(lang, VERTICAL_PANEL, title);
  }
}