All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.intellij.util.ui.CheckBoxListModelEditor Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition platform-impl library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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.intellij.util.ui;

import com.intellij.ide.IdeBundle;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.util.Pair;
import com.intellij.ui.AnActionButton;
import com.intellij.ui.AnActionButtonRunnable;
import com.intellij.ui.CheckBoxList;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.util.Consumer;
import com.intellij.util.EditSourceOnDoubleClickHandler;
import com.intellij.util.Function;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class CheckBoxListModelEditor {
  private final CheckBoxList list;
  private final ToolbarDecorator toolbarDecorator;
  private final Function toNameConverter;

  public CheckBoxListModelEditor(@NotNull Function toNameConverter, @NotNull String emptyText) {
    this.toNameConverter = toNameConverter;
    list = new CheckBoxList();
    list.setEmptyText(emptyText);
    // toolbar decorator is responsible for border
    list.setBorder(null);
    toolbarDecorator = ToolbarDecorator.createDecorator(list);
  }

  @NotNull
  public CheckBoxListModelEditor editAction(final @NotNull Function consumer) {
    final Runnable action = new Runnable() {
      @Override
      public void run() {
        T item = getSelectedItem();
        if (item != null) {
          T newItem = consumer.fun(item);
          if (newItem != null) {
            list.updateItem(item, newItem);
          }
          list.repaint();
          list.requestFocus();
        }
      }
    };
    toolbarDecorator.setEditAction(new AnActionButtonRunnable() {
      @Override
      public void run(AnActionButton button) {
        action.run();
      }
    });
    EditSourceOnDoubleClickHandler.install(list, action);
    return this;
  }

  @NotNull
  public CheckBoxListModelEditor copyAction(final @NotNull Consumer consumer) {
    toolbarDecorator.addExtraAction(new ToolbarDecorator.ElementActionButton(IdeBundle.message("button.copy"), PlatformIcons.COPY_ICON) {
      @Override
      public void actionPerformed(AnActionEvent e) {
        int[] indices = list.getSelectedIndices();
        if (indices == null || indices.length == 0) {
          return;
        }

        for (int index : indices) {
          T item = list.getItemAt(index);
          if (item != null) {
            consumer.consume(item);
          }
        }
      }
    });
    return this;
  }

  public ToolbarDecorator getToolbar() {
    return toolbarDecorator;
  }

  @NotNull
  public JComponent createComponent() {
    return toolbarDecorator.createPanel();
  }

  @NotNull
  public DefaultListModel getModel() {
    return ((DefaultListModel)list.getModel());
  }

  public void selectItemByName(@NotNull String name) {
    for (int i = 0; i < list.getItemsCount(); i++) {
      T item = list.getItemAt(i);
      if (item != null && name.equalsIgnoreCase(toNameConverter.fun(item))) {
        list.setSelectedIndex(i);
      }
    }
  }

  @Nullable
  private T getSelectedItem() {
    int index = list.getSelectedIndex();
    return index == -1 ? null : list.getItemAt(index);
  }

  public CheckBoxList getList() {
    return list;
  }

  public void reset(@NotNull List> items) {
    list.clear();
    for (Pair item : items) {
      list.addItem(item.first, toNameConverter.fun(item.first), item.second);
    }
  }

  public boolean isModified(@NotNull List> oldItems) {
    if (oldItems.size() != list.getItemsCount()) {
      return true;
    }

    for (int i = 0; i < list.getItemsCount(); i++) {
      T item = list.getItemAt(i);
      if (item == null) {
        return true;
      }

      Pair oldItem = oldItems.get(i);
      if (oldItem.second != list.isItemSelected(i) || !oldItem.first.equals(item)) {
        return true;
      }
    }
    return false;
  }

  @NotNull
  public List getItems() {
    int count = list.getItemsCount();
    List result = new ArrayList(count);
    for (int i = 0; i < count; i++) {
      T item = list.getItemAt(i);
      if (item != null) {
        result.add(item);
      }
    }
    return result;
  }

  @NotNull
  public List> apply() {
    int count = list.getItemsCount();
    List> result = new ArrayList>(count);
    for (int i = 0; i < count; i++) {
      T item = list.getItemAt(i);
      if (item != null) {
        result.add(Pair.create(item, list.isItemSelected(i)));
      }
    }
    return result;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy