com.intellij.openapi.editor.actions.ContentChooser Maven / Gradle / Ivy
/*
* Copyright 2000-2015 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.openapi.editor.actions;
import com.intellij.CommonBundle;
import com.intellij.icons.AllIcons;
import com.intellij.ide.ui.SplitterProportionsDataImpl;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.EditorFontType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.SplitterProportionsData;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.*;
import com.intellij.ui.components.JBList;
import com.intellij.ui.speedSearch.FilteringListModel;
import com.intellij.ui.speedSearch.ListWithFilter;
import com.intellij.util.Alarm;
import com.intellij.util.Function;
import com.intellij.util.ObjectUtils;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class ContentChooser extends DialogWrapper {
private List myAllContents;
private Editor myViewer;
private final boolean myUseIdeaEditor;
private final JList myList;
private final JBSplitter mySplitter;
private final Project myProject;
private final boolean myAllowMultipleSelections;
private final Alarm myUpdateAlarm;
private Icon myListEntryIcon = AllIcons.FileTypes.Text;
public ContentChooser(Project project, String title, boolean useIdeaEditor) {
this(project, title, useIdeaEditor, false);
}
public ContentChooser(Project project, String title, boolean useIdeaEditor, boolean allowMultipleSelections) {
super(project, true);
myProject = project;
myUseIdeaEditor = useIdeaEditor;
myAllowMultipleSelections = allowMultipleSelections;
myUpdateAlarm = new Alarm(getDisposable());
mySplitter = new JBSplitter(true, 0.3f);
mySplitter.setSplitterProportionKey(getDimensionServiceKey() + ".splitter");
myList = new JBList(new CollectionListModel- ());
setOKButtonText(CommonBundle.getOkButtonText());
setTitle(title);
init();
}
public void setContentIcon(@Nullable Icon icon) {
myListEntryIcon = icon;
}
public void setSplitterOrientation(boolean vertical) {
mySplitter.setOrientation(vertical);
}
@Override
public JComponent getPreferredFocusedComponent() {
return myList;
}
@Override
protected JComponent createCenterPanel() {
final int selectionMode = myAllowMultipleSelections ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
: ListSelectionModel.SINGLE_SELECTION;
myList.setSelectionMode(selectionMode);
if (myUseIdeaEditor) {
EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
myList.setFont(scheme.getFont(EditorFontType.PLAIN));
Color fg = ObjectUtils.chooseNotNull(scheme.getDefaultForeground(), UIUtil.getListForeground());
Color bg = ObjectUtils.chooseNotNull(scheme.getDefaultBackground(), UIUtil.getListBackground());
myList.setForeground(fg);
myList.setBackground(bg);
}
new DoubleClickListener() {
@Override
protected boolean onDoubleClick(MouseEvent e) {
close(OK_EXIT_CODE);
return true;
}
}.installOn(myList);
myList.setCellRenderer(new MyListCellRenderer());
myList.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DELETE) {
int newSelectionIndex = -1;
for (Object o : myList.getSelectedValues()) {
int i = ((Item)o).index;
removeContentAt(myAllContents.get(i));
if (newSelectionIndex < 0) {
newSelectionIndex = i;
}
}
rebuildListContent();
if (myAllContents.isEmpty()) {
close(CANCEL_EXIT_CODE);
return;
}
newSelectionIndex = Math.min(newSelectionIndex, myAllContents.size() - 1);
myList.setSelectedIndex(newSelectionIndex);
}
else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
doOKAction();
}
else {
final char aChar = e.getKeyChar();
if (aChar >= '0' && aChar <= '9') {
int idx = aChar == '0' ? 9 : aChar - '1';
if (idx < myAllContents.size()) {
myList.setSelectedIndex(idx);
e.consume();
doOKAction();
}
}
}
}
});
mySplitter.setFirstComponent(ListWithFilter.wrap(myList, ScrollPaneFactory.createScrollPane(myList), new Function
© 2015 - 2025 Weber Informatics LLC | Privacy Policy