Пример #1
0
 @Override
 public void setEnabled(boolean enabled) {
   super.setEnabled(enabled);
   locales.setEnabled(enabled);
   value.setEnabled(enabled);
   if (scrollPane != null) scrollPane.setEnabled(enabled);
 }
Пример #2
0
  /**
   * @param strings The list of translatable strings.
   * @param textField The component to be used for typing in the translations.
   * @param scrollPane If not <code>null</code>, the textField will be added to the scrollPane.
   */
  public JLangStringListTextField(List<LangString> strings, T textField, JScrollPane scrollPane) {
    if (strings == null)
      throw new NullPointerException("Cannot create JLangStringListTextField with a null list.");

    this.strings = strings;

    this.scrollPane = scrollPane;

    setLayout(new FormLayout("0px:grow,$lcgap,pref", "pref"));

    CellConstraints cc = new CellConstraints();
    value = textField;
    if (scrollPane == null) {
      add(value, cc.xy(1, 1));
    } else {
      scrollPane.setViewportView(value);
      add(scrollPane, cc.xy(1, 1));
    }

    locales = new JLocaleComboBox();
    updateLocales();
    add(locales, cc.xy(3, 1));

    locales.addPopupMenuListener(
        new PopupMenuListener() {
          @Override
          public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}

          @Override
          public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            value.requestFocusInWindow();
          }

          @Override
          public void popupMenuCanceled(PopupMenuEvent e) {
            value.requestFocusInWindow();
          }
        });

    locales.setAction(
        new AbstractAction() {
          /** */
          private static final long serialVersionUID = -5477415299077842204L;

          @Override
          public void actionPerformed(ActionEvent e) {
            if (isUpdatingLocales) return;

            isLocaleChanging = true;
            Locale l = (Locale) locales.getSelectedItem();
            LangString string = getLangString(l);
            if (string == null) {
              value.setText("");
            } else {
              value.setText(string.getValue());
            }
            isLocaleChanging = false;
          }
        });
    locales.setSelectedIndex(0);

    final DocumentListener textChangeListener =
        new UniversalDocumentListener() {
          @Override
          protected void update(DocumentEvent e) {
            if (isLocaleChanging) return;

            LangString string = getLangString((Locale) locales.getSelectedItem());

            if (string == null) {
              string = (LangString) new ObjectFactory().createLangString();
              string.setLang((Locale) locales.getSelectedItem());
              string.setValue("");
              JLangStringListTextField.this.strings.add(string);
              updateLocales();
            }

            int index = JLangStringListTextField.this.strings.indexOf(string);

            if (value.getText() != null && value.getText().length() > 0) {
              string.setValue(value.getText());
              JLangStringListTextField.this.strings.set(
                  index, string); // fire a change notification for
              // observable lists
            } else {
              JLangStringListTextField.this.strings.remove(string);
              updateLocales();
            }
          }
        };

    value.getDocument().addDocumentListener(textChangeListener);
    value.addPropertyChangeListener(
        "document",
        new PropertyChangeListener() {
          @Override
          public void propertyChange(PropertyChangeEvent evt) {
            value.getDocument().addDocumentListener(textChangeListener);
          }
        });

    setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
  }
Пример #3
0
 /** @param strings the strings to set */
 public void setStrings(List<LangString> strings) {
   this.strings = strings;
   updateLocales();
   locales.setSelectedIndex(0);
 }
Пример #4
0
 /** Updates the combobox with locales to have the suggested locales moved to the top. */
 protected void updateLocales() {
   isUpdatingLocales = true;
   locales.updateSuggested(getSuggestedLocales());
   isUpdatingLocales = false;
 }