コード例 #1
0
  public RegistryUi() {
    myContent.setLayout(new BorderLayout(UIUtil.DEFAULT_HGAP, UIUtil.DEFAULT_VGAP));

    myModel = new MyTableModel();
    myTable = new JBTable(myModel);
    myTable.setCellSelectionEnabled(true);
    myTable.setEnableAntialiasing(true);
    final MyRenderer r = new MyRenderer();

    final TableColumn c0 = myTable.getColumnModel().getColumn(0);
    c0.setCellRenderer(r);
    c0.setMaxWidth(RESTART_ICON.getIconWidth() + 12);
    c0.setMinWidth(RESTART_ICON.getIconWidth() + 12);
    c0.setHeaderValue(null);

    final TableColumn c1 = myTable.getColumnModel().getColumn(1);
    c1.setCellRenderer(r);
    c1.setHeaderValue("Key");

    final TableColumn c2 = myTable.getColumnModel().getColumn(2);
    c2.setCellRenderer(r);
    c2.setHeaderValue("Value");
    c2.setCellEditor(new MyEditor());
    myTable.setStriped(true);

    myDescriptionLabel = new JTextArea(3, 50);
    myDescriptionLabel.setEditable(false);
    final JScrollPane label = ScrollPaneFactory.createScrollPane(myDescriptionLabel);
    final JPanel descriptionPanel = new JPanel(new BorderLayout());
    descriptionPanel.add(label, BorderLayout.CENTER);
    descriptionPanel.setBorder(
        IdeBorderFactory.createTitledBorder("Description", false, false, true));

    myContent.add(ScrollPaneFactory.createScrollPane(myTable), BorderLayout.CENTER);
    myContent.add(descriptionPanel, BorderLayout.SOUTH);

    myTable
        .getSelectionModel()
        .addListSelectionListener(
            new ListSelectionListener() {
              public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) return;

                final int selected = myTable.getSelectedRow();
                if (selected != -1) {
                  final RegistryValue value = myModel.getRegistryValue(selected);
                  String desc = value.getDescription();
                  if (value.isRestartRequired()) {
                    String required = "Requires IDE restart.";
                    if (desc.endsWith(".")) {
                      desc += required;
                    } else {
                      desc += (". " + required);
                    }
                  }
                  myDescriptionLabel.setText(desc);
                } else {
                  myDescriptionLabel.setText(null);
                }
              }
            });

    myRestoreDefaultsAction = new RestoreDefaultsAction();

    final DefaultActionGroup tbGroup = new DefaultActionGroup();
    tbGroup.add(new EditAction());
    tbGroup.add(new RevertAction());

    final ActionToolbar tb =
        ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, tbGroup, true);
    tb.setTargetComponent(myTable);

    myContent.add(tb.getComponent(), BorderLayout.NORTH);
    new TableSpeedSearch(myTable).setComparator(new SpeedSearchComparator(false));
  }
  @SuppressWarnings("unchecked")
  private void createUIComponents() {
    final JBList projectExistLocalesList = new JBList();
    final MyExistLocalesListModel existLocalesListModel = new MyExistLocalesListModel();
    projectExistLocalesList.setModel(existLocalesListModel);
    projectExistLocalesList.setCellRenderer(getLocaleRenderer());
    myProjectExistLocalesPanel =
        ToolbarDecorator.createDecorator(projectExistLocalesList)
            .disableRemoveAction()
            .disableUpDownActions()
            .createPanel();
    myProjectExistLocalesPanel.setBorder(
        IdeBorderFactory.createTitledBorder("Project locales", false));

    final JBList localesToAddList = new JBList();

    final List<Locale> locales;
    final List<Locale> restrictedLocales;
    if (myResourceBundle == null) {
      locales = Collections.singletonList(PropertiesUtil.DEFAULT_LOCALE);
      restrictedLocales = Collections.emptyList();
    } else {
      locales = Collections.emptyList();
      restrictedLocales =
          ContainerUtil.map(myResourceBundle.getPropertiesFiles(), PropertiesFile::getLocale);
    }
    myLocalesModel =
        new CollectionListModel<Locale>(locales) {
          @Override
          public void add(@NotNull List<? extends Locale> elements) {
            final List<Locale> currentItems = getItems();
            elements =
                ContainerUtil.filter(
                    elements,
                    locale ->
                        !restrictedLocales.contains(locale) && !currentItems.contains(locale));
            super.add(elements);
          }
        };
    localesToAddList.setModel(myLocalesModel);
    localesToAddList.setCellRenderer(getLocaleRenderer());
    localesToAddList.addFocusListener(
        new FocusAdapter() {
          @Override
          public void focusGained(FocusEvent e) {
            projectExistLocalesList.clearSelection();
          }
        });
    projectExistLocalesList.addFocusListener(
        new FocusAdapter() {
          @Override
          public void focusGained(FocusEvent e) {
            localesToAddList.clearSelection();
          }
        });

    myNewBundleLocalesPanel =
        ToolbarDecorator.createDecorator(localesToAddList)
            .setAddAction(
                new AnActionButtonRunnable() {
                  @Override
                  public void run(AnActionButton button) {
                    final String rawAddedLocales =
                        Messages.showInputDialog(
                            myProject,
                            PropertiesBundle.message(
                                "create.resource.bundle.dialog.add.locales.validator.message"),
                            PropertiesBundle.message(
                                "create.resource.bundle.dialog.add.locales.validator.title"),
                            null,
                            null,
                            new InputValidatorEx() {
                              @Nullable
                              @Override
                              public String getErrorText(String inputString) {
                                return checkInput(inputString) ? null : "Invalid locales";
                              }

                              @Override
                              public boolean checkInput(String inputString) {
                                return extractLocalesFromString(inputString) != null;
                              }

                              @Override
                              public boolean canClose(String inputString) {
                                return checkInput(inputString);
                              }
                            });
                    if (rawAddedLocales != null) {
                      final List<Locale> locales = extractLocalesFromString(rawAddedLocales);
                      LOG.assertTrue(locales != null);
                      myLocalesModel.add(locales);
                    }
                  }
                })
            .setAddActionName("Add locales by suffix")
            .disableUpDownActions()
            .createPanel();
    myNewBundleLocalesPanel.setBorder(IdeBorderFactory.createTitledBorder("Locales to add", false));

    myAddLocaleFromExistButton = new JButton(AllIcons.Actions.Forward);
    new ClickListener() {
      @Override
      public boolean onClick(@NotNull MouseEvent event, int clickCount) {
        if (clickCount == 1) {
          myLocalesModel.add(
              ContainerUtil.map(projectExistLocalesList.getSelectedValues(), o -> (Locale) o));
          return true;
        }
        return false;
      }
    }.installOn(myAddLocaleFromExistButton);

    projectExistLocalesList.addListSelectionListener(
        new ListSelectionListener() {
          @Override
          public void valueChanged(ListSelectionEvent e) {
            final List<Locale> currentItems = myLocalesModel.getItems();
            for (Object o : projectExistLocalesList.getSelectedValues()) {
              Locale l = (Locale) o;
              if (!restrictedLocales.contains(l) && !currentItems.contains(l)) {
                myAddLocaleFromExistButton.setEnabled(true);
                return;
              }
            }
            myAddLocaleFromExistButton.setEnabled(false);
          }
        });
    myAddLocaleFromExistButton.setEnabled(false);

    myAddAllButton = new JButton("Add All");
    new ClickListener() {
      @Override
      public boolean onClick(@NotNull MouseEvent event, int clickCount) {
        if (clickCount == 1) {
          myLocalesModel.add(existLocalesListModel.getLocales());
        }
        return false;
      }
    }.installOn(myAddAllButton);
  }