use of java.awt.event.KeyAdapter in project intellij-community by JetBrains.
the class MavenSelectProjectPopup method attachToButton.
public static void attachToButton(@NotNull final MavenProjectsManager projectsManager, @NotNull final JButton button, @NotNull final Consumer<MavenProject> callback) {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
List<MavenProject> projectList = projectsManager.getProjects();
if (projectList.isEmpty()) {
JBPopupFactory.getInstance().createMessage("Maven projects not found").showUnderneathOf(button);
return;
}
DefaultMutableTreeNode root = buildTree(projectList);
final Map<MavenProject, String> projectsNameMap = MavenProjectNamer.generateNameMap(projectList);
final Tree projectTree = new Tree(root);
projectTree.setRootVisible(false);
projectTree.setCellRenderer(new NodeRenderer() {
@Override
public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
if (value instanceof DefaultMutableTreeNode) {
MavenProject mavenProject = (MavenProject) ((DefaultMutableTreeNode) value).getUserObject();
value = projectsNameMap.get(mavenProject);
setIcon(MavenIcons.MavenProject);
}
super.customizeCellRenderer(tree, value, selected, expanded, leaf, row, hasFocus);
}
});
new TreeSpeedSearch(projectTree, new Convertor<TreePath, String>() {
@Override
public String convert(TreePath o) {
Object lastPathComponent = o.getLastPathComponent();
if (!(lastPathComponent instanceof DefaultMutableTreeNode))
return null;
Object userObject = ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
//noinspection SuspiciousMethodCalls
return projectsNameMap.get(userObject);
}
});
final Ref<JBPopup> popupRef = new Ref<>();
final Runnable clickCallBack = () -> {
TreePath path = projectTree.getSelectionPath();
if (path == null)
return;
Object lastPathComponent = path.getLastPathComponent();
if (!(lastPathComponent instanceof DefaultMutableTreeNode))
return;
Object object = ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
// may be it's the root
if (object == null)
return;
callback.consume((MavenProject) object);
popupRef.get().closeOk(null);
};
projectTree.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
clickCallBack.run();
e.consume();
}
}
});
JBPopup popup = new PopupChooserBuilder(projectTree).setTitle("Select maven project").setResizable(true).setItemChoosenCallback(clickCallBack).setAutoselectOnMouseMove(true).setCloseOnEnter(false).createPopup();
popupRef.set(popup);
popup.showUnderneathOf(button);
}
private DefaultMutableTreeNode buildTree(List<MavenProject> projectList) {
MavenProject[] projects = projectList.toArray(new MavenProject[projectList.size()]);
Arrays.sort(projects, new MavenProjectNamer.MavenProjectComparator());
Map<MavenProject, DefaultMutableTreeNode> projectsToNode = new HashMap<>();
for (MavenProject mavenProject : projects) {
projectsToNode.put(mavenProject, new DefaultMutableTreeNode(mavenProject));
}
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
for (MavenProject mavenProject : projects) {
DefaultMutableTreeNode parent;
MavenProject aggregator = projectsManager.findAggregator(mavenProject);
if (aggregator != null) {
parent = projectsToNode.get(aggregator);
} else {
parent = root;
}
parent.add(projectsToNode.get(mavenProject));
}
return root;
}
});
}
use of java.awt.event.KeyAdapter in project intellij-community by JetBrains.
the class MavenSelectProjectPopup method attachToWorkingDirectoryField.
public static void attachToWorkingDirectoryField(@NotNull final MavenProjectsManager projectsManager, final JTextField workingDirectoryField, final JButton showModulesButton, @Nullable final JComponent focusAfterSelection) {
attachToButton(projectsManager, showModulesButton, project -> {
workingDirectoryField.setText(project.getDirectory());
if (focusAfterSelection != null) {
ApplicationManager.getApplication().invokeLater(() -> {
if (workingDirectoryField.hasFocus()) {
IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
IdeFocusManager.getGlobalInstance().requestFocus(focusAfterSelection, true);
});
}
});
}
});
workingDirectoryField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (!e.isConsumed()) {
// May be consumed by path completion.
e.consume();
showModulesButton.doClick();
}
}
}
});
}
use of java.awt.event.KeyAdapter in project intellij-community by JetBrains.
the class PyDataViewerPanel method createEditorField.
@NotNull
private EditorTextField createEditorField() {
return new EditorTextField(EditorFactory.getInstance().createDocument(""), myProject, PythonFileType.INSTANCE, false, true) {
@Override
protected EditorEx createEditor() {
EditorEx editor = super.createEditor();
editor.getContentComponent().addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
apply(mySliceTextField.getText());
}
}
});
return editor;
}
};
}
use of java.awt.event.KeyAdapter in project intellij-community by JetBrains.
the class IpnbEditablePanel method createEditablePanel.
private JTextArea createEditablePanel() {
final JTextArea textArea = new JTextArea(getRawCellText());
textArea.setLineWrap(true);
textArea.setEditable(true);
textArea.setBorder(BorderFactory.createLineBorder(JBColor.lightGray));
textArea.setBackground(IpnbEditorUtil.getEditablePanelBackground());
textArea.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
setEditing(true);
final Container parent = getParent();
parent.repaint();
if (parent instanceof IpnbFilePanel) {
((IpnbFilePanel) parent).setSelectedCellPanel(IpnbEditablePanel.this);
IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
IdeFocusManager.getGlobalInstance().requestFocus(textArea, true);
});
}
}
}
});
textArea.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
setEditing(false);
final Container parent = getParent();
if (parent instanceof IpnbFilePanel) {
parent.repaint();
UIUtil.requestFocus((IpnbFilePanel) parent);
}
}
}
});
return textArea;
}
use of java.awt.event.KeyAdapter in project intellij-community by JetBrains.
the class EmmetAbbreviationBalloon method show.
public void show(@NotNull final CustomTemplateCallback customTemplateCallback) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
if (ourTestingAbbreviation == null) {
throw new RuntimeException("Testing abbreviation is not set. See EmmetAbbreviationBalloon#setTestingAbbreviation");
}
myCallback.onEnter(ourTestingAbbreviation);
return;
}
final TextFieldWithStoredHistory field = new TextFieldWithStoredHistory(myAbbreviationsHistoryKey);
final Dimension fieldPreferredSize = field.getPreferredSize();
field.setPreferredSize(new Dimension(Math.max(220, fieldPreferredSize.width), fieldPreferredSize.height));
field.setHistorySize(10);
final JBPopupFactory popupFactory = JBPopupFactory.getInstance();
final BalloonImpl balloon = (BalloonImpl) popupFactory.createDialogBalloonBuilder(field, myTitle).setCloseButtonEnabled(false).setBlockClicksThroughBalloon(true).setAnimationCycle(0).setHideOnKeyOutside(true).setHideOnClickOutside(true).createBalloon();
final DocumentAdapter documentListener = new DocumentAdapter() {
@Override
protected void textChanged(DocumentEvent e) {
if (!isValid(customTemplateCallback)) {
balloon.hide();
return;
}
validateTemplateKey(field, balloon, field.getText(), customTemplateCallback);
}
};
field.addDocumentListener(documentListener);
final KeyAdapter keyListener = new KeyAdapter() {
@Override
public void keyPressed(@NotNull KeyEvent e) {
if (!field.isPopupVisible()) {
if (!isValid(customTemplateCallback)) {
balloon.hide();
return;
}
switch(e.getKeyCode()) {
case KeyEvent.VK_ENTER:
final String abbreviation = field.getText();
if (validateTemplateKey(field, balloon, abbreviation, customTemplateCallback)) {
myCallback.onEnter(abbreviation);
PropertiesComponent.getInstance().setValue(myLastAbbreviationKey, abbreviation);
field.addCurrentTextToHistory();
balloon.hide();
}
break;
case KeyEvent.VK_ESCAPE:
balloon.hide(false);
break;
}
}
}
};
field.addKeyboardListener(keyListener);
balloon.addListener(new JBPopupListener.Adapter() {
@Override
public void beforeShown(LightweightWindowEvent event) {
field.setText(PropertiesComponent.getInstance().getValue(myLastAbbreviationKey, ""));
}
@Override
public void onClosed(LightweightWindowEvent event) {
field.removeKeyListener(keyListener);
field.removeDocumentListener(documentListener);
super.onClosed(event);
}
});
balloon.show(popupFactory.guessBestPopupLocation(customTemplateCallback.getEditor()), Balloon.Position.below);
final IdeFocusManager focusManager = IdeFocusManager.getInstance(customTemplateCallback.getProject());
focusManager.doWhenFocusSettlesDown(() -> {
focusManager.requestFocus(field, true);
field.selectText();
});
}
Aggregations