📄 basictreeui.java
字号:
|| e.getActionCommand().equals("selectPrevious") || e.getActionCommand().equals("selectNext") || e.getActionCommand().equals("selectNextExtendSelection") || e.getActionCommand().equals("selectNextChangeLead")) (new TreeIncrementAction(0, "")).actionPerformed(e); else if (e.getActionCommand().equals("selectParent") || e.getActionCommand().equals("selectChild")) (new TreeTraverseAction(0, "")).actionPerformed(e); else if (e.getActionCommand().equals("selectAll")) { TreePath[] paths = new TreePath[tree.getVisibleRowCount()]; Object curr = getNextVisibleNode(treeModel.getRoot()); int i = 0; while (curr != null && i < paths.length) { paths[i] = new TreePath(getPathToRoot(curr, 0)); i++; } tree.addSelectionPaths(paths); } else if (e.getActionCommand().equals("startEditing")) tree.startEditingAtPath(lead); else if (e.getActionCommand().equals("toggle")) { if (tree.isEditing()) tree.stopEditing(); else { Object last = lead.getLastPathComponent(); TreePath path = new TreePath(getPathToRoot(last, 0)); if (!treeModel.isLeaf(last)) toggleExpandState(path); } } else if (e.getActionCommand().equals("clearSelection")) tree.clearSelection(); if (tree.isEditing() && !e.getActionCommand().equals("startEditing")) tree.cancelEditing(); tree.scrollPathToVisible(lead); } } /** * This class is used to mimic the behaviour of the JDK when registering * keyboard actions. It is the same as the private class used in JComponent * for the same reason. This class receives an action event and dispatches it * to the true receiver after altering the actionCommand property of the * event. */ private static class ActionListenerProxy extends AbstractAction { ActionListener target; String bindingCommandName; public ActionListenerProxy(ActionListener li, String cmd) { target = li; bindingCommandName = cmd; } public void actionPerformed(ActionEvent e) { ActionEvent derivedEvent = new ActionEvent(e.getSource(), e.getID(), bindingCommandName, e.getModifiers()); target.actionPerformed(derivedEvent); } } /** * The timer that updates the editor component. */ private class EditorUpdateTimer extends Timer implements ActionListener { /** * Creates a new EditorUpdateTimer object with a default delay of 0.3 * seconds. */ public EditorUpdateTimer() { super(300, null); addActionListener(this); } /** * Lets the caret blink and repaints the table. */ public void actionPerformed(ActionEvent ev) { Caret c = ((JTextField) editingComponent).getCaret(); if (c != null) c.setVisible(!c.isVisible()); tree.repaint(); } /** * Updates the blink delay according to the current caret. */ public void update() { stop(); Caret c = ((JTextField) editingComponent).getCaret(); if (c != null) { setDelay(c.getBlinkRate()); if (((JTextField) editingComponent).isEditable()) start(); else c.setVisible(false); } } } /** * Updates the preferred size when scrolling, if necessary. */ public class ComponentHandler extends ComponentAdapter implements ActionListener { /** * Timer used when inside a scrollpane and the scrollbar is adjusting */ protected Timer timer; /** ScrollBar that is being adjusted */ protected JScrollBar scrollBar; /** * Constructor */ public ComponentHandler() { // Nothing to do here. } /** * Invoked when the component's position changes. * * @param e * the event that occurs when moving the component */ public void componentMoved(ComponentEvent e) { // TODO: What should be done here, if anything? } /** * Creates, if necessary, and starts a Timer to check if needed to resize the * bounds */ protected void startTimer() { // TODO: Implement this properly. } /** * Returns the JScrollPane housing the JTree, or null if one isn't found. * * @return JScrollPane housing the JTree, or null if one isn't found. */ protected JScrollPane getScrollPane() { return null; } /** * Public as a result of Timer. If the scrollBar is null, or not adjusting, * this stops the timer and updates the sizing. * * @param ae * is the action performed */ public void actionPerformed(ActionEvent ae) { // TODO: Implement this properly. } } /** * Listener responsible for getting cell editing events and updating the tree * accordingly. */ public class CellEditorHandler implements CellEditorListener { /** * Constructor */ public CellEditorHandler() { // Nothing to do here. } /** * Messaged when editing has stopped in the tree. Tells the listeners * editing has stopped. * * @param e * is the notification event */ public void editingStopped(ChangeEvent e) { editingPath = null; editingRow = -1; stopEditingInCompleteEditing = false; if (editingComponent != null) { tree.remove(editingComponent.getParent()); editingComponent = null; } if (cellEditor != null) { newVal = ((JTextField) getCellEditor().getCellEditorValue()).getText(); completeEditing(false, false, true); if (cellEditor instanceof DefaultTreeCellEditor) tree.removeTreeSelectionListener((DefaultTreeCellEditor) cellEditor); cellEditor.removeCellEditorListener(cellEditorListener); setCellEditor(null); createdCellEditor = false; } isEditing = false; tree.requestFocusInWindow(false); editorTimer.stop(); validCachedPreferredSize = false; tree.revalidate(); tree.repaint(); } /** * Messaged when editing has been canceled in the tree. This tells the * listeners the editor has canceled editing. * * @param e * is the notification event */ public void editingCanceled(ChangeEvent e) { editingPath = null; editingRow = -1; stopEditingInCompleteEditing = false; if (editingComponent != null) tree.remove(editingComponent.getParent()); editingComponent = null; if (cellEditor != null) { if (cellEditor instanceof DefaultTreeCellEditor) tree.removeTreeSelectionListener((DefaultTreeCellEditor) cellEditor); cellEditor.removeCellEditorListener(cellEditorListener); setCellEditor(null); createdCellEditor = false; } tree.requestFocusInWindow(false); editorTimer.stop(); isEditing = false; validCachedPreferredSize = false; tree.revalidate(); tree.repaint(); } }// CellEditorHandler /** * Repaints the lead selection row when focus is lost/grained. */ public class FocusHandler implements FocusListener { /** * Constructor */ public FocusHandler() { // Nothing to do here. } /** * Invoked when focus is activated on the tree we're in, redraws the lead * row. Invoked when a component gains the keyboard focus. * * @param e * is the focus event that is activated */ public void focusGained(FocusEvent e) { // TODO: Implement this properly. } /** * Invoked when focus is deactivated on the tree we're in, redraws the lead * row. Invoked when a component loses the keyboard focus. * * @param e * is the focus event that is deactivated */ public void focusLost(FocusEvent e) { // TODO: Implement this properly. } } /** * This is used to get multiple key down events to appropriately genereate * events. */ public class KeyHandler extends KeyAdapter { /** Key code that is being generated for. */ protected Action repeatKeyAction; /** Set to true while keyPressed is active */ protected boolean isKeyDown; /** * Constructor */ public KeyHandler() { // Nothing to do here. } /** * Invoked when a key has been typed. Moves the keyboard focus to the first * element whose first letter matches the alphanumeric key pressed by the * user. Subsequent same key presses move the keyboard focus to the next * object that starts with the same letter. * * @param e * the key typed */ public void keyTyped(KeyEvent e) { // TODO: What should be done here, if anything? } /** * Invoked when a key has been pressed. * * @param e * the key pressed */ public void keyPressed(KeyEvent e) { // TODO: What should be done here, if anything? } /** * Invoked when a key has been released * * @param e * the key released */ public void keyReleased(KeyEvent e) { // TODO: What should be done here, if anything? } } /** * MouseListener is responsible for updating the selection based on mouse * events. */ public class MouseHandler extends MouseAdapter implements MouseMotionListener { /** * Constructor */ public MouseHandler() { // Nothing to do here. } /** * Invoked when a mouse button has been pressed on a component. * * @param e * is the mouse event that occured */ public void mousePressed(MouseEvent e) { Point click = e.getPoint(); TreePath path = getClosestPathForLocation(tree, click.x, click.y); if (path != null) { Rectangle bounds = getPathBounds(tree, path); int row = getRowForPath(tree, path); boolean cntlClick = isLocationInExpandControl(path, click.x, click.y); boolean isLeaf = isLeaf(row); TreeCellRenderer tcr = getCellRenderer(); Icon icon; if (isLeaf) icon = UIManager.getIcon("Tree.leafIcon"); else if (tree.isExpanded(path)) icon = UIManager.getIcon("Tree.openIcon"); else icon = UIManager.getIcon("Tree.closedIcon"); if (tcr instanceof DefaultTreeCellRenderer) { Icon tmp = ((DefaultTreeCellRenderer) tcr).getIcon(); if (tmp != null) icon = tmp; } // add gap*2 for the space before and after the text if (icon != null) bounds.width += icon.getIconWidth() + gap*2; boolean inBounds = bounds.contains(click.x, click.y); if ((inBounds || cntlClick) && tree.isVisible(path)) { if (inBounds) { selectPath(tree, path); if (e.getClickCount() == 2 && !isLeaf(row)) toggleExpandState(path); } if (cntlClick) { handleExpandControlClick(path, click.x, click.y); if (cellEditor != null) cellEditor.cancelCellEditing(); tree.scrollPathToVisible(path); } else if (tree.isEditable()) startEditing(path, e); } } } /** * Invoked when a mouse button is pressed on a component and then dragged. * MOUSE_DRAGGED events will continue to be delivered to the component where * the drag originated until the mouse button is released (regardless of * whether the mouse position is within the bounds of the component). * * @param e * is the mouse event that occured */ public void mouseDragged(MouseEvent e) { // TODO: What should be done here, if anything? } /** * Invoked when the mouse button has been moved on a component (with no * buttons no down). *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -