use of javax.swing.event.ListSelectionListener in project intellij-community by JetBrains.
the class AbstractExternalSystemConfigurable method prepareProjectSettings.
@SuppressWarnings("unchecked")
private void prepareProjectSettings(@NotNull SystemSettings s) {
myProjectsModel = new DefaultListModel();
myProjectsList = new JBList(myProjectsModel);
myProjectsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
addTitle(ExternalSystemBundle.message("settings.title.linked.projects", myExternalSystemId.getReadableName()));
myComponent.add(new JBScrollPane(myProjectsList), ExternalSystemUiUtil.getFillLineConstraints(1));
addTitle(ExternalSystemBundle.message("settings.title.project.settings"));
List<ProjectSettings> settings = ContainerUtilRt.newArrayList(s.getLinkedProjectsSettings());
myProjectsList.setVisibleRowCount(Math.max(3, Math.min(5, settings.size())));
ContainerUtil.sort(settings, (s1, s2) -> getProjectName(s1.getExternalProjectPath()).compareTo(getProjectName(s2.getExternalProjectPath())));
myProjectSettingsControls.clear();
for (ProjectSettings setting : settings) {
ExternalSystemSettingsControl<ProjectSettings> control = createProjectSettingsControl(setting);
control.fillUi(myComponent, 1);
myProjectsModel.addElement(getProjectName(setting.getExternalProjectPath()));
myProjectSettingsControls.add(control);
if (control instanceof AbstractExternalProjectSettingsControl<?>) {
((AbstractExternalProjectSettingsControl) control).setCurrentProject(myProject);
}
control.showUi(false);
}
myProjectsList.addListSelectionListener(new ListSelectionListener() {
@SuppressWarnings("unchecked")
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
return;
}
int i = myProjectsList.getSelectedIndex();
if (i < 0) {
return;
}
if (myActiveProjectSettingsControl != null) {
myActiveProjectSettingsControl.showUi(false);
}
myActiveProjectSettingsControl = myProjectSettingsControls.get(i);
myActiveProjectSettingsControl.showUi(true);
}
});
if (!myProjectsModel.isEmpty()) {
myProjectsList.setSelectedIndex(0);
}
}
use of javax.swing.event.ListSelectionListener in project intellij-community by JetBrains.
the class LookupImpl method addListeners.
private void addListeners() {
myEditor.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
public void documentChanged(DocumentEvent e) {
if (!myChangeGuard && !myFinishing) {
hideLookup(false);
}
}
}, this);
final CaretListener caretListener = new CaretAdapter() {
@Override
public void caretPositionChanged(CaretEvent e) {
if (!myChangeGuard && !myFinishing) {
hideLookup(false);
}
}
};
final SelectionListener selectionListener = new SelectionListener() {
@Override
public void selectionChanged(final SelectionEvent e) {
if (!myChangeGuard && !myFinishing) {
hideLookup(false);
}
}
};
final EditorMouseListener mouseListener = new EditorMouseAdapter() {
@Override
public void mouseClicked(EditorMouseEvent e) {
e.consume();
hideLookup(false);
}
};
myEditor.getCaretModel().addCaretListener(caretListener);
myEditor.getSelectionModel().addSelectionListener(selectionListener);
myEditor.addEditorMouseListener(mouseListener);
Disposer.register(this, new Disposable() {
@Override
public void dispose() {
myEditor.getCaretModel().removeCaretListener(caretListener);
myEditor.getSelectionModel().removeSelectionListener(selectionListener);
myEditor.removeEditorMouseListener(mouseListener);
}
});
JComponent editorComponent = myEditor.getContentComponent();
if (editorComponent.isShowing()) {
Disposer.register(this, new UiNotifyConnector(editorComponent, new Activatable() {
@Override
public void showNotify() {
}
@Override
public void hideNotify() {
hideLookup(false);
}
}));
}
myList.addListSelectionListener(new ListSelectionListener() {
private LookupElement oldItem = null;
@Override
public void valueChanged(@NotNull ListSelectionEvent e) {
if (!myUpdating) {
final LookupElement item = getCurrentItem();
fireCurrentItemChanged(oldItem, item);
oldItem = item;
}
}
});
new ClickListener() {
@Override
public boolean onClick(@NotNull MouseEvent e, int clickCount) {
setFocusDegree(FocusDegree.FOCUSED);
markSelectionTouched();
if (clickCount == 2) {
CommandProcessor.getInstance().executeCommand(myProject, () -> finishLookup(NORMAL_SELECT_CHAR), "", null);
}
return true;
}
}.installOn(myList);
}
use of javax.swing.event.ListSelectionListener in project intellij-community by JetBrains.
the class LookupUi method addListeners.
private void addListeners() {
myList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (myLookup.isLookupDisposed())
return;
myHintAlarm.cancelAllRequests();
final LookupElement item = myLookup.getCurrentItem();
if (item != null) {
updateHint(item);
}
}
});
final Alarm alarm = new Alarm(myLookup);
myScrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
if (myLookup.myUpdating || myLookup.isLookupDisposed())
return;
alarm.addRequest(() -> myLookup.refreshUi(false, false), 300, myModalityState);
}
});
}
use of javax.swing.event.ListSelectionListener in project intellij-community by JetBrains.
the class MacrosDialog method addListeners.
private void addListeners() {
myMacrosList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
Macro macro = getSelectedMacro();
if (macro == null) {
myPreviewTextarea.setText("");
setOKActionEnabled(false);
} else {
myPreviewTextarea.setText(macro.preview());
setOKActionEnabled(true);
}
}
});
new DoubleClickListener() {
@Override
protected boolean onDoubleClick(MouseEvent e) {
if (getSelectedMacro() != null) {
close(OK_EXIT_CODE);
return true;
}
return false;
}
}.installOn(myMacrosList);
}
use of javax.swing.event.ListSelectionListener in project intellij-community by JetBrains.
the class ChooseByNameBase method initUI.
/**
* @param modalityState - if not null rebuilds list in given {@link ModalityState}
*/
protected void initUI(final ChooseByNamePopupComponent.Callback callback, final ModalityState modalityState, final boolean allowMultipleSelection) {
myPreviouslyFocusedComponent = WindowManagerEx.getInstanceEx().getFocusedComponent(myProject);
myActionListener = callback;
myTextFieldPanel.setLayout(new BoxLayout(myTextFieldPanel, BoxLayout.Y_AXIS));
final JPanel hBox = new JPanel();
hBox.setLayout(new BoxLayout(hBox, BoxLayout.X_AXIS));
JPanel caption2Tools = new JPanel(new BorderLayout());
if (myModel.getPromptText() != null) {
JLabel label = new JLabel(myModel.getPromptText());
label.setFont(UIUtil.getLabelFont().deriveFont(Font.BOLD));
caption2Tools.add(label, BorderLayout.WEST);
}
caption2Tools.add(hBox, BorderLayout.EAST);
// space between checkbox and filter/show all in view buttons
myCardContainer.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 4));
final String checkBoxName = myModel.getCheckBoxName();
myCheckBox.setText(checkBoxName != null ? checkBoxName + (myCheckBoxShortcut != null && myCheckBoxShortcut.getShortcuts().length > 0 ? " (" + KeymapUtil.getShortcutsText(myCheckBoxShortcut.getShortcuts()) + ")" : "") : "");
myCheckBox.setAlignmentX(SwingConstants.RIGHT);
if (!SystemInfo.isMac) {
myCheckBox.setBorder(null);
}
myCheckBox.setSelected(myModel.loadInitialCheckBoxState());
if (checkBoxName == null) {
myCheckBox.setVisible(false);
}
addCard(myCheckBox, CHECK_BOX_CARD);
addCard(new HintLabel(myModel.getNotInMessage()), NOT_FOUND_IN_PROJECT_CARD);
addCard(new HintLabel(IdeBundle.message("label.choosebyname.no.matches.found")), NOT_FOUND_CARD);
JPanel searching = new JPanel(new BorderLayout(5, 0));
searching.add(new AsyncProcessIcon("searching"), BorderLayout.WEST);
searching.add(new HintLabel(IdeBundle.message("label.choosebyname.searching")), BorderLayout.CENTER);
addCard(searching, SEARCHING_CARD);
myCard.show(myCardContainer, CHECK_BOX_CARD);
if (isCheckboxVisible()) {
hBox.add(myCardContainer);
}
final DefaultActionGroup group = new DefaultActionGroup();
group.add(new ShowFindUsagesAction() {
@Override
public PsiElement[][] getElements() {
final List<Object> objects = myListModel.getItems();
final List<PsiElement> prefixMatchElements = new ArrayList<>(objects.size());
final List<PsiElement> nonPrefixMatchElements = new ArrayList<>(objects.size());
List<PsiElement> curElements = prefixMatchElements;
for (Object object : objects) {
if (object instanceof PsiElement) {
curElements.add((PsiElement) object);
} else if (object instanceof DataProvider) {
final PsiElement psi = CommonDataKeys.PSI_ELEMENT.getData((DataProvider) object);
if (psi != null) {
curElements.add(psi);
}
} else if (object == NON_PREFIX_SEPARATOR) {
curElements = nonPrefixMatchElements;
}
}
return new PsiElement[][] { PsiUtilCore.toPsiElementArray(prefixMatchElements), PsiUtilCore.toPsiElementArray(nonPrefixMatchElements) };
}
});
final ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, group, true);
actionToolbar.setLayoutPolicy(ActionToolbar.NOWRAP_LAYOUT_POLICY);
// we need valid ActionToolbar.getPreferredSize() to calc size of popup
actionToolbar.updateActionsImmediately();
final JComponent toolbarComponent = actionToolbar.getComponent();
toolbarComponent.setBorder(null);
if (myToolArea == null) {
myToolArea = new JLabel(JBUI.scale(EmptyIcon.create(1, 24)));
}
hBox.add(myToolArea);
hBox.add(toolbarComponent);
myTextFieldPanel.add(caption2Tools);
new MyCopyReferenceAction().registerCustomShortcutSet(ActionManager.getInstance().getAction(IdeActions.ACTION_COPY).getShortcutSet(), myTextField);
myTextFieldPanel.add(myTextField);
Font editorFont = EditorUtil.getEditorFont();
myTextField.setFont(editorFont);
if (checkBoxName != null) {
if (myCheckBoxShortcut != null) {
new DumbAwareAction("change goto check box", null, null) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
myCheckBox.setSelected(!myCheckBox.isSelected());
}
}.registerCustomShortcutSet(myCheckBoxShortcut, myTextField);
}
}
if (isCloseByFocusLost()) {
myTextField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(@NotNull final FocusEvent e) {
if (Registry.is("focus.follows.mouse.workarounds")) {
if (myFocusPoint != null) {
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
if (pointerInfo != null && myFocusPoint.equals(pointerInfo.getLocation())) {
// Ignore the loss of focus if the mouse hasn't moved between the last dropdown resize
// and the loss of focus event. This happens in focus follows mouse mode if the mouse is
// over the dropdown and it resizes to leave the mouse outside the dropdown.
IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
myFocusPoint = null;
return;
}
}
myFocusPoint = null;
}
// cancel thread as early as possible
cancelListUpdater();
myHideAlarm.addRequest(() -> {
JBPopup popup = JBPopupFactory.getInstance().getChildFocusedPopup(e.getComponent());
if (popup != null) {
popup.addListener(new JBPopupListener.Adapter() {
@Override
public void onClosed(@NotNull LightweightWindowEvent event) {
if (event.isOk()) {
hideHint();
}
}
});
} else {
Component oppositeComponent = e.getOppositeComponent();
if (oppositeComponent == myCheckBox) {
IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
return;
}
if (oppositeComponent != null && !(oppositeComponent instanceof JFrame) && myList.isShowing() && (oppositeComponent == myList || SwingUtilities.isDescendingFrom(myList, oppositeComponent))) {
// Otherwise me may skip some KeyEvents
IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
return;
}
if (isDescendingFromTemporarilyFocusableToolWindow(oppositeComponent)) {
// Allow toolwindows to gain focus (used by QuickDoc shown in a toolwindow)
return;
}
hideHint();
}
}, 5);
}
});
}
myCheckBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(@NotNull ItemEvent e) {
rebuildList(false);
}
});
myCheckBox.setFocusable(false);
myTextField.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(DocumentEvent e) {
rebuildList(false);
}
});
final Set<KeyStroke> upShortcuts = getShortcuts(IdeActions.ACTION_EDITOR_MOVE_CARET_UP);
final Set<KeyStroke> downShortcuts = getShortcuts(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN);
myTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(@NotNull KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER && (e.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
myClosedByShiftEnter = true;
close(true);
}
if (!myListScrollPane.isVisible()) {
return;
}
final int keyCode;
// Add support for user-defined 'caret up/down' shortcuts.
KeyStroke stroke = KeyStroke.getKeyStrokeForEvent(e);
if (upShortcuts.contains(stroke)) {
keyCode = KeyEvent.VK_UP;
} else if (downShortcuts.contains(stroke)) {
keyCode = KeyEvent.VK_DOWN;
} else {
keyCode = e.getKeyCode();
}
switch(keyCode) {
case KeyEvent.VK_DOWN:
ScrollingUtil.moveDown(myList, e.getModifiersEx());
break;
case KeyEvent.VK_UP:
ScrollingUtil.moveUp(myList, e.getModifiersEx());
break;
case KeyEvent.VK_PAGE_UP:
ScrollingUtil.movePageUp(myList);
break;
case KeyEvent.VK_PAGE_DOWN:
ScrollingUtil.movePageDown(myList);
break;
case KeyEvent.VK_TAB:
close(true);
break;
case KeyEvent.VK_ENTER:
if (myList.getSelectedValue() == EXTRA_ELEM) {
myMaximumListSizeLimit += myListSizeIncreasing;
rebuildList(myList.getSelectedIndex(), myRebuildDelay, ModalityState.current(), null);
e.consume();
}
break;
}
if (myList.getSelectedValue() == NON_PREFIX_SEPARATOR) {
if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_PAGE_UP) {
ScrollingUtil.moveUp(myList, e.getModifiersEx());
} else {
ScrollingUtil.moveDown(myList, e.getModifiersEx());
}
}
}
});
myTextField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(@NotNull ActionEvent actionEvent) {
doClose(true);
}
});
myList.setFocusable(false);
myList.setSelectionMode(allowMultipleSelection ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION : ListSelectionModel.SINGLE_SELECTION);
new ClickListener() {
@Override
public boolean onClick(@NotNull MouseEvent e, int clickCount) {
if (!myTextField.hasFocus()) {
IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
}
if (clickCount == 2) {
int selectedIndex = myList.getSelectedIndex();
Rectangle selectedCellBounds = myList.getCellBounds(selectedIndex, selectedIndex);
if (selectedCellBounds != null && selectedCellBounds.contains(e.getPoint())) {
// Otherwise it was reselected in the selection listener
if (myList.getSelectedValue() == EXTRA_ELEM) {
myMaximumListSizeLimit += myListSizeIncreasing;
rebuildList(selectedIndex, myRebuildDelay, ModalityState.current(), null);
} else {
doClose(true);
}
}
return true;
}
return false;
}
}.installOn(myList);
myList.setCellRenderer(myModel.getListCellRenderer());
myList.setFont(editorFont);
myList.addListSelectionListener(new ListSelectionListener() {
private int myPreviousSelectionIndex = 0;
@Override
public void valueChanged(@NotNull ListSelectionEvent e) {
if (myList.getSelectedValue() != NON_PREFIX_SEPARATOR) {
myPreviousSelectionIndex = myList.getSelectedIndex();
chosenElementMightChange();
updateDocumentation();
} else if (allowMultipleSelection) {
myList.setSelectedIndex(myPreviousSelectionIndex);
}
}
});
myListScrollPane = ScrollPaneFactory.createScrollPane(myList);
myListScrollPane.setViewportBorder(JBUI.Borders.empty());
myTextFieldPanel.setBorder(JBUI.Borders.empty(5));
showTextFieldPanel();
myInitialized = true;
if (modalityState != null) {
rebuildList(myInitialIndex, 0, modalityState, null);
}
}
Aggregations