Search in sources :

Example 26 with KeyListener

use of java.awt.event.KeyListener in project org.alloytools.alloy by AlloyTools.

the class OurDialog method getInput.

/**
 * Display a modal dialog window containing the "objects"; returns true iff the
 * user clicks Ok.
 */
public static boolean getInput(String title, Object... objects) {
    // If there is a JTextField or a JTextArea here, then let the first
    // JTextField or JTextArea be the initially focused widget
    Object main = "Ok";
    for (Object obj : objects) if (obj instanceof JTextField || obj instanceof JTextArea) {
        main = obj;
        break;
    }
    // Construct the dialog panel
    final JOptionPane pane = new JOptionPane(objects, QUESTION_MESSAGE, YES_NO_OPTION, null, new Object[] { "Ok", "Cancel" }, main);
    final JDialog dialog = pane.createDialog(null, title);
    // VK_ENTER and treat it as if the user clicked OK
    for (Object obj : objects) if (obj instanceof JTextField || obj instanceof JCheckBox) {
        ((JComponent) obj).addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    pane.setValue("Ok");
                    dialog.dispose();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyTyped(KeyEvent e) {
            }
        });
    }
    dialog.setAlwaysOnTop(true);
    // This method blocks until the user either
    dialog.setVisible(true);
    // chooses something or cancels the dialog.
    dialog.dispose();
    return pane.getValue() == "Ok";
}
Also used : JCheckBox(javax.swing.JCheckBox) KeyEvent(java.awt.event.KeyEvent) JTextArea(javax.swing.JTextArea) KeyListener(java.awt.event.KeyListener) JTextField(javax.swing.JTextField) JOptionPane(javax.swing.JOptionPane) JDialog(javax.swing.JDialog)

Example 27 with KeyListener

use of java.awt.event.KeyListener in project CodenameOne by codenameone.

the class JavaSEPort method editString.

/**
 * @inheritDoc
 */
public void editString(final Component cmp, int maxSize, int constraint, String text, int keyCode) {
    if (scrollWheeling) {
        return;
    }
    if (System.getProperty("TextCompatMode") != null) {
        editStringLegacy(cmp, maxSize, constraint, text, keyCode);
        return;
    }
    // a workaround to fix an issue where the previous Text Component wasn't removed properly.
    java.awt.Component[] cmps = canvas.getComponents();
    for (int i = 0; i < cmps.length; i++) {
        java.awt.Component cmp1 = cmps[i];
        if (cmp1 instanceof JScrollPane || cmp1 instanceof javax.swing.text.JTextComponent) {
            canvas.remove(cmp1);
        }
    }
    checkEDT();
    javax.swing.text.JTextComponent swingT;
    if (((com.codename1.ui.TextArea) cmp).isSingleLineTextArea()) {
        JTextComponent t;
        if (isDesktop() && (constraint & TextArea.PASSWORD) == TextArea.PASSWORD) {
            t = new JPasswordField() {

                public void repaint(long tm, int x, int y, int width, int height) {
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            cmp.repaint();
                        }
                    });
                }
            };
        } else {
            t = new JTextField() {

                public void repaint(long tm, int x, int y, int width, int height) {
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            cmp.repaint();
                        }
                    });
                }
            };
        /*
                ((JTextField)t).addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (cmp instanceof com.codename1.ui.TextField) {
                            final com.codename1.ui.TextField tf = (com.codename1.ui.TextField)cmp;
                            if (tf.getDoneListener() != null) {
                                Display.getInstance().callSerially(new Runnable() {
                                    public void run() {
                                        if (tf.getDoneListener() != null) {
                                            tf.fireDoneEvent();
                                        }
                                    }
                                });
                            }
                        }
                    }
                    
                });
                */
        }
        swingT = t;
        textCmp = swingT;
    } else {
        final com.codename1.ui.TextArea ta = (com.codename1.ui.TextArea) cmp;
        JTextArea t = new JTextArea(ta.getLines(), ta.getColumns()) {

            public void repaint(long tm, int x, int y, int width, int height) {
                int marginTop = cmp.getSelectedStyle().getPadding(Component.TOP);
                int marginLeft = cmp.getSelectedStyle().getPadding(Component.LEFT);
                int marginRight = cmp.getSelectedStyle().getPadding(Component.RIGHT);
                int marginBottom = cmp.getSelectedStyle().getPadding(Component.BOTTOM);
                Rectangle bounds;
                if (getSkin() != null) {
                    bounds = new Rectangle((int) ((cmp.getAbsoluteX() + cmp.getScrollX() + getScreenCoordinates().x + canvas.x + marginLeft) * zoomLevel), (int) ((cmp.getAbsoluteY() + cmp.getScrollY() + getScreenCoordinates().y + canvas.y + marginTop) * zoomLevel), (int) ((cmp.getWidth() - marginLeft - marginRight) * zoomLevel), (int) ((cmp.getHeight() - marginTop - marginBottom) * zoomLevel));
                } else {
                    bounds = new Rectangle(cmp.getAbsoluteX() + cmp.getScrollX() + marginLeft, cmp.getAbsoluteY() + cmp.getScrollY() + marginTop, cmp.getWidth() - marginRight - marginLeft, cmp.getHeight() - marginTop - marginBottom);
                }
                if (textCmp != null && !textCmp.getBounds().equals(bounds)) {
                    textCmp.setBounds(bounds);
                }
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        cmp.repaint();
                    }
                });
            }
        };
        t.setWrapStyleWord(true);
        t.setLineWrap(true);
        swingT = t;
        JScrollPane pane = new JScrollPane(swingT);
        pane.setBorder(null);
        pane.setOpaque(false);
        pane.getViewport().setOpaque(false);
        pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        textCmp = pane;
    }
    DefaultCaret caret = (DefaultCaret) swingT.getCaret();
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    swingT.setFocusTraversalKeysEnabled(false);
    TextEditUtil.setCurrentEditComponent(cmp);
    final javax.swing.text.JTextComponent txt = swingT;
    txt.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_TAB) {
                TextEditUtil.editNextTextArea();
            }
        }
    });
    swingT.setBorder(null);
    swingT.setOpaque(false);
    swingT.setForeground(new Color(cmp.getUnselectedStyle().getFgColor()));
    final javax.swing.text.JTextComponent tf = swingT;
    if (keyCode > 0) {
        text += ((char) keyCode);
        setText(tf, text);
        setCaretPosition(tf, text.length());
        if (cmp instanceof com.codename1.ui.TextField) {
            ((com.codename1.ui.TextField) cmp).setText(getText(tf));
        }
    } else {
        setText(tf, text);
    }
    textCmp.setBorder(null);
    textCmp.setOpaque(false);
    canvas.add(textCmp);
    int marginTop = cmp.getSelectedStyle().getPadding(Component.TOP);
    int marginLeft = cmp.getSelectedStyle().getPadding(Component.LEFT);
    int marginRight = cmp.getSelectedStyle().getPadding(Component.RIGHT);
    int marginBottom = cmp.getSelectedStyle().getPadding(Component.BOTTOM);
    if (getSkin() != null) {
        textCmp.setBounds((int) ((cmp.getAbsoluteX() + cmp.getScrollX() + getScreenCoordinates().x + canvas.x + marginLeft) * zoomLevel), (int) ((cmp.getAbsoluteY() + cmp.getScrollY() + getScreenCoordinates().y + canvas.y + marginTop) * zoomLevel), (int) ((cmp.getWidth() - marginLeft - marginRight) * zoomLevel), (int) ((cmp.getHeight() - marginTop - marginBottom) * zoomLevel));
        java.awt.Font f = font(cmp.getStyle().getFont().getNativeFont());
        tf.setFont(f.deriveFont(f.getSize2D() * zoomLevel));
    } else {
        textCmp.setBounds(cmp.getAbsoluteX() + cmp.getScrollX() + marginLeft, cmp.getAbsoluteY() + cmp.getScrollY() + marginTop, cmp.getWidth() - marginRight - marginLeft, cmp.getHeight() - marginTop - marginBottom);
        tf.setFont(font(cmp.getStyle().getFont().getNativeFont()));
    }
    setCaretPosition(tf, getText(tf).length());
    tf.requestFocus();
    tf.setSelectionStart(0);
    tf.setSelectionEnd(0);
    class Listener implements ActionListener, FocusListener, KeyListener, TextListener, Runnable, DocumentListener {

        public synchronized void run() {
            while (textCmp.getParent() != null) {
                try {
                    wait(20);
                } catch (InterruptedException ex) {
                }
            }
        }

        public void actionPerformed(ActionEvent e) {
            String txt = getText(tf);
            if (testRecorder != null) {
                testRecorder.editTextFieldCompleted(cmp, txt);
            }
            Display.getInstance().onEditingComplete(cmp, txt);
            if (e != null && cmp instanceof com.codename1.ui.TextField) {
                final com.codename1.ui.TextField cn1Tf = (com.codename1.ui.TextField) cmp;
                if (cmp != null && cn1Tf.getDoneListener() != null) {
                    cn1Tf.fireDoneEvent();
                }
            }
            if (tf instanceof JTextField) {
                ((JTextField) tf).removeActionListener(this);
            }
            ((JTextComponent) tf).getDocument().removeDocumentListener(this);
            tf.removeFocusListener(this);
            canvas.remove(textCmp);
            synchronized (this) {
                notify();
            }
            canvas.repaint();
        }

        public void focusGained(FocusEvent e) {
            setCaretPosition(tf, getText(tf).length());
        }

        public void focusLost(FocusEvent e) {
            actionPerformed(null);
        }

        public void keyTyped(KeyEvent e) {
            String t = getText(tf);
            if (t.length() >= ((TextArea) cmp).getMaxSize()) {
                e.consume();
            }
        }

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                if (tf instanceof JTextField) {
                    actionPerformed(null);
                } else {
                    if (getCaretPosition(tf) >= getText(tf).length() - 1) {
                        actionPerformed(null);
                    }
                }
                return;
            }
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                if (tf instanceof JTextField) {
                    actionPerformed(null);
                } else {
                    if (getCaretPosition(tf) <= 2) {
                        actionPerformed(null);
                    }
                }
                return;
            }
        }

        public void textValueChanged(TextEvent e) {
            if (cmp instanceof com.codename1.ui.TextField) {
                updateText();
            }
        }

        private void updateText() {
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    if (cmp instanceof com.codename1.ui.TextField) {
                        ((com.codename1.ui.TextField) cmp).setText(getText(tf));
                    }
                }
            });
        }

        public void insertUpdate(DocumentEvent e) {
            updateText();
        }

        public void removeUpdate(DocumentEvent e) {
            updateText();
        }

        public void changedUpdate(DocumentEvent e) {
            updateText();
        }
    }
    ;
    final Listener l = new Listener();
    if (tf instanceof JTextField) {
        ((JTextField) tf).addActionListener(l);
    }
    ((JTextComponent) tf).getDocument().addDocumentListener(l);
    tf.addKeyListener(l);
    tf.addFocusListener(l);
    if (simulateAndroidKeyboard) {
        java.util.Timer t = new java.util.Timer();
        TimerTask tt = new TimerTask() {

            @Override
            public void run() {
                if (!Display.getInstance().isEdt()) {
                    Display.getInstance().callSerially(this);
                    return;
                }
                if (tf.getParent() != null) {
                    final int height = getScreenCoordinates().height;
                    JavaSEPort.this.sizeChanged(getScreenCoordinates().width, height / 2);
                    new UITimer(new Runnable() {

                        public void run() {
                            if (tf.getParent() != null) {
                                new UITimer(this).schedule(100, false, Display.getInstance().getCurrent());
                            } else {
                                JavaSEPort.this.sizeChanged(getScreenCoordinates().width, height);
                            }
                        }
                    }).schedule(100, false, Display.getInstance().getCurrent());
                }
            }
        };
        t.schedule(tt, 300);
    }
    Display.getInstance().invokeAndBlock(l);
}
Also used : DocumentListener(javax.swing.event.DocumentListener) FocusListener(java.awt.event.FocusListener) TextListener(java.awt.event.TextListener) KeyListener(java.awt.event.KeyListener) ActionListener(java.awt.event.ActionListener) AdjustmentListener(java.awt.event.AdjustmentListener) MouseMotionListener(java.awt.event.MouseMotionListener) MouseWheelListener(java.awt.event.MouseWheelListener) DocumentListener(javax.swing.event.DocumentListener) ItemListener(java.awt.event.ItemListener) WindowListener(java.awt.event.WindowListener) MouseListener(java.awt.event.MouseListener) MenuListener(javax.swing.event.MenuListener) HierarchyBoundsListener(java.awt.event.HierarchyBoundsListener) TextArea(com.codename1.ui.TextArea) UITimer(com.codename1.ui.util.UITimer) ActionEvent(java.awt.event.ActionEvent) Rectangle(java.awt.Rectangle) JTextComponent(javax.swing.text.JTextComponent) FocusEvent(java.awt.event.FocusEvent) KeyEvent(java.awt.event.KeyEvent) DefaultCaret(javax.swing.text.DefaultCaret) FontRenderContext(java.awt.font.FontRenderContext) BrowserComponent(com.codename1.ui.BrowserComponent) Component(com.codename1.ui.Component) JTextComponent(javax.swing.text.JTextComponent) PeerComponent(com.codename1.ui.PeerComponent) TextEvent(java.awt.event.TextEvent) Color(java.awt.Color) TextListener(java.awt.event.TextListener) DocumentEvent(javax.swing.event.DocumentEvent) JTextComponent(javax.swing.text.JTextComponent) Point(java.awt.Point) ActionListener(java.awt.event.ActionListener) java.util(java.util) UITimer(com.codename1.ui.util.UITimer) KeyListener(java.awt.event.KeyListener) TextArea(com.codename1.ui.TextArea) FocusListener(java.awt.event.FocusListener) java.awt(java.awt)

Example 28 with KeyListener

use of java.awt.event.KeyListener in project CodenameOne by codenameone.

the class JavaSEPort method editStringLegacy.

public void editStringLegacy(final Component cmp, int maxSize, int constraint, String text, int keyCode) {
    checkEDT();
    java.awt.Component awtTf;
    if (cmp instanceof com.codename1.ui.TextField) {
        java.awt.TextField t = new java.awt.TextField();
        awtTf = t;
        t.setSelectionEnd(0);
        t.setSelectionStart(0);
    } else {
        java.awt.TextArea t = new java.awt.TextArea("", 0, 0, java.awt.TextArea.SCROLLBARS_NONE);
        ;
        awtTf = t;
        t.setSelectionEnd(0);
        t.setSelectionStart(0);
    }
    final java.awt.Component tf = awtTf;
    if (keyCode > 0) {
        text += ((char) keyCode);
        setText(tf, text);
        setCaretPosition(tf, text.length());
        ((com.codename1.ui.TextField) cmp).setText(getText(tf));
    } else {
        setText(tf, text);
    }
    canvas.add(tf);
    if (getSkin() != null) {
        tf.setBounds((int) ((cmp.getAbsoluteX() + getScreenCoordinates().x + canvas.x) * zoomLevel), (int) ((cmp.getAbsoluteY() + getScreenCoordinates().y + canvas.y) * zoomLevel), (int) (cmp.getWidth() * zoomLevel), (int) (cmp.getHeight() * zoomLevel));
        java.awt.Font f = font(cmp.getStyle().getFont().getNativeFont());
        tf.setFont(f.deriveFont(f.getSize2D() * zoomLevel));
    } else {
        tf.setBounds(cmp.getAbsoluteX(), cmp.getAbsoluteY(), cmp.getWidth(), cmp.getHeight());
        tf.setFont(font(cmp.getStyle().getFont().getNativeFont()));
    }
    setCaretPosition(tf, getText(tf).length());
    tf.requestFocus();
    class Listener implements ActionListener, FocusListener, KeyListener, TextListener, Runnable {

        public synchronized void run() {
            while (tf.getParent() != null) {
                try {
                    wait(20);
                } catch (InterruptedException ex) {
                }
            }
        }

        public void actionPerformed(ActionEvent e) {
            String txt = getText(tf);
            if (testRecorder != null) {
                testRecorder.editTextFieldCompleted(cmp, txt);
            }
            Display.getInstance().onEditingComplete(cmp, txt);
            if (tf instanceof java.awt.TextField) {
                ((java.awt.TextField) tf).removeActionListener(this);
            }
            ((TextComponent) tf).removeTextListener(this);
            tf.removeFocusListener(this);
            canvas.remove(tf);
            synchronized (this) {
                notify();
            }
            canvas.repaint();
        }

        public void focusGained(FocusEvent e) {
            setCaretPosition(tf, getText(tf).length());
        }

        public void focusLost(FocusEvent e) {
            actionPerformed(null);
        }

        public void keyTyped(KeyEvent e) {
            String t = getText(tf);
            if (t.length() >= ((TextArea) cmp).getMaxSize()) {
                e.consume();
            }
        }

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                if (tf instanceof java.awt.TextField) {
                    actionPerformed(null);
                } else {
                    if (getCaretPosition(tf) >= getText(tf).length() - 1) {
                        actionPerformed(null);
                    }
                }
                return;
            }
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                if (tf instanceof java.awt.TextField) {
                    actionPerformed(null);
                } else {
                    if (getCaretPosition(tf) <= 2) {
                        actionPerformed(null);
                    }
                }
                return;
            }
        }

        public void textValueChanged(TextEvent e) {
            if (cmp instanceof com.codename1.ui.TextField) {
                ((com.codename1.ui.TextField) cmp).setText(getText(tf));
            }
        }
    }
    ;
    final Listener l = new Listener();
    if (tf instanceof java.awt.TextField) {
        ((java.awt.TextField) tf).addActionListener(l);
    }
    ((TextComponent) tf).addTextListener(l);
    tf.addKeyListener(l);
    tf.addFocusListener(l);
    if (simulateAndroidKeyboard) {
        java.util.Timer t = new java.util.Timer();
        TimerTask tt = new TimerTask() {

            @Override
            public void run() {
                if (!Display.getInstance().isEdt()) {
                    Display.getInstance().callSerially(this);
                    return;
                }
                if (tf.getParent() != null) {
                    final int height = getScreenCoordinates().height;
                    JavaSEPort.this.sizeChanged(getScreenCoordinates().width, height / 2);
                    new UITimer(new Runnable() {

                        public void run() {
                            if (tf.getParent() != null) {
                                new UITimer(this).schedule(100, false, Display.getInstance().getCurrent());
                            } else {
                                JavaSEPort.this.sizeChanged(getScreenCoordinates().width, height);
                            }
                        }
                    }).schedule(100, false, Display.getInstance().getCurrent());
                }
            }
        };
        t.schedule(tt, 300);
    }
    Display.getInstance().invokeAndBlock(l);
}
Also used : FocusListener(java.awt.event.FocusListener) TextListener(java.awt.event.TextListener) KeyListener(java.awt.event.KeyListener) ActionListener(java.awt.event.ActionListener) AdjustmentListener(java.awt.event.AdjustmentListener) MouseMotionListener(java.awt.event.MouseMotionListener) MouseWheelListener(java.awt.event.MouseWheelListener) DocumentListener(javax.swing.event.DocumentListener) ItemListener(java.awt.event.ItemListener) WindowListener(java.awt.event.WindowListener) MouseListener(java.awt.event.MouseListener) MenuListener(javax.swing.event.MenuListener) HierarchyBoundsListener(java.awt.event.HierarchyBoundsListener) TextArea(com.codename1.ui.TextArea) UITimer(com.codename1.ui.util.UITimer) ActionEvent(java.awt.event.ActionEvent) FocusEvent(java.awt.event.FocusEvent) KeyEvent(java.awt.event.KeyEvent) JTextComponent(javax.swing.text.JTextComponent) TextEvent(java.awt.event.TextEvent) TextListener(java.awt.event.TextListener) Point(java.awt.Point) ActionListener(java.awt.event.ActionListener) java.util(java.util) UITimer(com.codename1.ui.util.UITimer) KeyListener(java.awt.event.KeyListener) FocusListener(java.awt.event.FocusListener) java.awt(java.awt)

Example 29 with KeyListener

use of java.awt.event.KeyListener in project azure-tools-for-java by Microsoft.

the class ServerExplorerToolWindowFactory method createToolWindowContent.

@Override
@AzureOperation(name = "common|explorer.initialize", type = AzureOperation.Type.SERVICE)
public void createToolWindowContent(@NotNull final Project project, @NotNull final ToolWindow toolWindow) {
    // initialize azure service module
    AzureModule azureModule = new AzureModuleImpl(project);
    HDInsightUtil.setHDInsightRootModule(azureModule);
    azureModule.setSparkServerlessModule(new CosmosSparkClusterRootModuleImpl(azureModule));
    azureModule.setArcadiaModule(new ArcadiaSparkClusterRootModuleImpl(azureModule));
    // initialize aris service module
    SqlBigDataClusterModule arisModule = new SqlBigDataClusterModule(project);
    // initialize with all the service modules
    DefaultTreeModel treeModel = new DefaultTreeModel(initRoot(project, ImmutableList.of(azureModule, arisModule)));
    treeModelMap.put(project, treeModel);
    // initialize tree
    final JTree tree = new Tree(treeModel);
    tree.setRootVisible(false);
    tree.setCellRenderer(new NodeTreeCellRenderer());
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    new TreeSpeedSearch(tree);
    final DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel.getRoot();
    final DefaultMutableTreeNode azureRoot = (DefaultMutableTreeNode) root.getChildAt(0);
    final List<? extends com.microsoft.azure.toolkit.intellij.common.component.Tree.TreeNode<?>> modules = AzureExplorer.getModules().stream().map(m -> new com.microsoft.azure.toolkit.intellij.common.component.Tree.TreeNode<>(m, tree)).collect(Collectors.toList());
    modules.forEach(azureRoot::add);
    azureModule.setClearResourcesListener(() -> modules.forEach(m -> m.clearChildren()));
    com.microsoft.azure.toolkit.intellij.common.component.Tree.installPopupMenu(tree);
    treeModel.reload();
    DataManager.registerDataProvider(tree, dataId -> {
        if (StringUtils.equals(dataId, Action.SOURCE)) {
            final DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if (Objects.nonNull(selectedNode)) {
                return selectedNode.getUserObject();
            }
        }
        return null;
    });
    // add a click handler for the tree
    tree.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(final MouseEvent e) {
            if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
                treeNodeDblClicked(e, tree, project);
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            treeMousePressed(e, tree);
        }
    });
    // add keyboard handler for the tree
    tree.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
            TreePath treePath = tree.getAnchorSelectionPath();
            if (treePath == null) {
                return;
            }
            final Object raw = treePath.getLastPathComponent();
            if (raw instanceof com.microsoft.azure.toolkit.intellij.common.component.Tree.TreeNode || raw instanceof LoadingNode) {
                return;
            }
            SortableTreeNode treeNode = (SortableTreeNode) raw;
            Node node = (Node) treeNode.getUserObject();
            Rectangle rectangle = tree.getRowBounds(tree.getRowForPath(treePath));
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                if (!node.isLoading()) {
                    node.getClickAction().fireNodeActionEvent();
                }
            } else if (e.getKeyCode() == KeyEvent.VK_CONTEXT_MENU) {
                if (node.hasNodeActions()) {
                    JPopupMenu menu = createPopupMenuForNode(node);
                    menu.show(e.getComponent(), (int) rectangle.getX(), (int) rectangle.getY());
                }
            }
        }
    });
    // add the tree to the window
    toolWindow.getComponent().add(new JBScrollPane(tree));
    // set tree and tree path to expand the node later
    azureModule.setTree(tree);
    azureModule.setTreePath(tree.getPathForRow(0));
    // setup toolbar icons
    addToolbarItems(toolWindow, project, azureModule);
}
Also used : AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager) ObservableList(com.microsoft.tooling.msservices.helpers.collections.ObservableList) ListChangedEvent(com.microsoft.tooling.msservices.helpers.collections.ListChangedEvent) NodeAction(com.microsoft.tooling.msservices.serviceexplorer.NodeAction) StringUtils(org.apache.commons.lang3.StringUtils) AzureModule(com.microsoft.tooling.msservices.serviceexplorer.azure.AzureModule) SqlBigDataClusterModule(com.microsoft.azure.sqlbigdata.serverexplore.SqlBigDataClusterModule) AzureIconSymbol(com.microsoft.tooling.msservices.serviceexplorer.AzureIconSymbol) TreeSpeedSearch(com.intellij.ui.TreeSpeedSearch) UIHelperImpl(com.microsoft.intellij.helpers.UIHelperImpl) StorageModule(com.microsoft.tooling.msservices.serviceexplorer.azure.storage.StorageModule) ProjectManager(com.intellij.openapi.project.ProjectManager) Map(java.util.Map) MouseAdapter(java.awt.event.MouseAdapter) RefreshableNode(com.microsoft.tooling.msservices.serviceexplorer.RefreshableNode) AzureExplorer(com.microsoft.azure.toolkit.intellij.explorer.AzureExplorer) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) ProjectManagerListener(com.intellij.openapi.project.ProjectManagerListener) ToolWindow(com.intellij.openapi.wm.ToolWindow) LoadingNode(com.intellij.ui.LoadingNode) AzureModuleImpl(com.microsoft.intellij.serviceexplorer.azure.AzureModuleImpl) TreePath(javax.swing.tree.TreePath) Collection(java.util.Collection) KeyEvent(java.awt.event.KeyEvent) Collectors(java.util.stream.Collectors) MutableTreeNode(javax.swing.tree.MutableTreeNode) JBScrollPane(com.intellij.ui.components.JBScrollPane) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) PropertyChangeListener(java.beans.PropertyChangeListener) CosmosSparkClusterRootModuleImpl(com.microsoft.azure.cosmosspark.serverexplore.cosmossparknode.CosmosSparkClusterRootModuleImpl) Action(com.microsoft.azure.toolkit.lib.common.action.Action) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) AzureTaskManager(com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager) KeyListener(java.awt.event.KeyListener) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) TreeNode(javax.swing.tree.TreeNode) AzureIconLoader(com.microsoft.intellij.helpers.AzureIconLoader) Node(com.microsoft.tooling.msservices.serviceexplorer.Node) HDInsightUtil(com.microsoft.azure.hdinsight.common.HDInsightUtil) HashMap(java.util.HashMap) ToolWindowFactory(com.intellij.openapi.wm.ToolWindowFactory) NodeRenderer(com.intellij.ide.util.treeView.NodeRenderer) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) ArcadiaSparkClusterRootModuleImpl(com.microsoft.azure.arcadia.serverexplore.ArcadiaSparkClusterRootModuleImpl) Project(com.intellij.openapi.project.Project) Tree(com.intellij.ui.treeStructure.Tree) DefaultLoader(com.microsoft.tooling.msservices.components.DefaultLoader) DataManager(com.intellij.ide.DataManager) PropertyChangeEvent(java.beans.PropertyChangeEvent) AnAction(com.intellij.openapi.actionSystem.AnAction) AzurePlugin(com.microsoft.intellij.AzurePlugin) AzureSignInAction(com.microsoft.intellij.actions.AzureSignInAction) SelectSubscriptionsAction(com.microsoft.intellij.actions.SelectSubscriptionsAction) TreeSelectionModel(javax.swing.tree.TreeSelectionModel) ListChangeListener(com.microsoft.tooling.msservices.helpers.collections.ListChangeListener) VMArmModule(com.microsoft.tooling.msservices.serviceexplorer.azure.vmarm.VMArmModule) MouseEvent(java.awt.event.MouseEvent) java.awt(java.awt) ToolWindowEx(com.intellij.openapi.wm.ex.ToolWindowEx) Comparator(java.util.Comparator) javax.swing(javax.swing) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeSpeedSearch(com.intellij.ui.TreeSpeedSearch) RefreshableNode(com.microsoft.tooling.msservices.serviceexplorer.RefreshableNode) LoadingNode(com.intellij.ui.LoadingNode) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeNode(javax.swing.tree.TreeNode) Node(com.microsoft.tooling.msservices.serviceexplorer.Node) KeyEvent(java.awt.event.KeyEvent) ArcadiaSparkClusterRootModuleImpl(com.microsoft.azure.arcadia.serverexplore.ArcadiaSparkClusterRootModuleImpl) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeNode(javax.swing.tree.TreeNode) AzureModule(com.microsoft.tooling.msservices.serviceexplorer.azure.AzureModule) Tree(com.intellij.ui.treeStructure.Tree) MouseEvent(java.awt.event.MouseEvent) CosmosSparkClusterRootModuleImpl(com.microsoft.azure.cosmosspark.serverexplore.cosmossparknode.CosmosSparkClusterRootModuleImpl) MouseAdapter(java.awt.event.MouseAdapter) LoadingNode(com.intellij.ui.LoadingNode) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) SqlBigDataClusterModule(com.microsoft.azure.sqlbigdata.serverexplore.SqlBigDataClusterModule) AzureModuleImpl(com.microsoft.intellij.serviceexplorer.azure.AzureModuleImpl) TreePath(javax.swing.tree.TreePath) KeyListener(java.awt.event.KeyListener) JBScrollPane(com.intellij.ui.components.JBScrollPane) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 30 with KeyListener

use of java.awt.event.KeyListener in project bytecode-viewer by Konloch.

the class ResourceListPane method attachTreeListeners.

public void attachTreeListeners() {
    tree.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.isMetaDown()) {
                ResourceTree tree = (ResourceTree) e.getSource();
                TreePath selPath = ResourceListPane.this.tree.getClosestPathForLocation(e.getX(), e.getY());
                if (selPath == null)
                    return;
                showContextMenu(tree, selPath, e.getX(), e.getY());
            }
        }
    });
    this.open.addActionListener(e -> {
        final TreeNode root = (TreeNode) tree.getModel().getRoot();
        expandAll(tree, new TreePath(root), true);
    });
    this.close.addActionListener(e -> {
        final TreeNode root = (TreeNode) tree.getModel().getRoot();
        final TreePath path = new TreePath(root);
        expandAll(tree, path, false);
        tree.expandPath(path);
    });
    this.tree.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            if (// right-click
            e.getButton() == MouseEvent.BUTTON1)
                openPath(tree.getPathForLocation(e.getX(), e.getY()));
        }
    });
    /*this.tree.addTreeSelectionListener(arg0 -> {
            if (cancel) {
                cancel = false;
                return;
            }
            
            openPath(arg0.getPath());
        });*/
    this.tree.addKeyListener(new KeyListener() {

        @Override
        public void keyReleased(KeyEvent e) {
        }

        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                if (e.getSource() instanceof ResourceTree) {
                    ResourceTree tree = (ResourceTree) e.getSource();
                    openPath(tree.getSelectionPath());
                }
            } else if ((int) e.getKeyChar() != 0 && (int) e.getKeyChar() != 8 && (int) e.getKeyChar() != 127 && (int) e.getKeyChar() != 65535 && !e.isControlDown() && !e.isAltDown()) {
                quickSearch.grabFocus();
                quickSearch.setText("" + e.getKeyChar());
                cancel = true;
            } else if (// ctrl + f
            e.isControlDown() && (int) e.getKeyChar() == 6)
                quickSearch.grabFocus();
            else
                cancel = true;
        }
    });
}
Also used : KeyEvent(java.awt.event.KeyEvent) MouseEvent(java.awt.event.MouseEvent) TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) MutableTreeNode(javax.swing.tree.MutableTreeNode) MouseAdapter(java.awt.event.MouseAdapter) KeyListener(java.awt.event.KeyListener)

Aggregations

KeyListener (java.awt.event.KeyListener)64 KeyEvent (java.awt.event.KeyEvent)53 JPanel (javax.swing.JPanel)20 ActionEvent (java.awt.event.ActionEvent)18 ActionListener (java.awt.event.ActionListener)17 GridBagConstraints (java.awt.GridBagConstraints)15 JLabel (javax.swing.JLabel)14 JTextField (javax.swing.JTextField)13 GridBagLayout (java.awt.GridBagLayout)12 Insets (java.awt.Insets)11 BorderLayout (java.awt.BorderLayout)8 Dimension (java.awt.Dimension)8 FocusEvent (java.awt.event.FocusEvent)8 FocusListener (java.awt.event.FocusListener)8 JButton (javax.swing.JButton)8 Point (java.awt.Point)7 MouseAdapter (java.awt.event.MouseAdapter)6 MouseEvent (java.awt.event.MouseEvent)6 JComponent (javax.swing.JComponent)5 Color (java.awt.Color)4