synthtreeui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,971 行 · 第 1/5 页
JAVA
1,971 行
} tree = (JTree)c; drawingCache = new HashMap(7); leftToRight = tree.getComponentOrientation().isLeftToRight(); stopEditingWhenSelectionChanges = true; validCachedPreferredSize = false; installDefaults(); installKeyboardActions(); installComponents(); installListeners(); } protected void installDefaults() { // Installs the renderer, and potentially the editor updateRenderer(); fetchStyle(tree); // This has to be here so that createLayoutCache doesn't get an NPE // when the renderer calls into the NodeDimensions. rendererPane = createCellRendererPane(); tree.add(rendererPane); treeState = createLayoutCache(); configureLayoutCache(); TransferHandler th = tree.getTransferHandler(); if (th == null || th instanceof UIResource) { tree.setTransferHandler(defaultTransferHandler); } DropTarget dropTarget = tree.getDropTarget(); if (dropTarget instanceof UIResource) { if (defaultDropTargetListener == null) { defaultDropTargetListener = new TreeDropTargetListener(); } try { dropTarget.addDropTargetListener(defaultDropTargetListener); } catch (TooManyListenersException tmle) { // should not happen... swing drop target is multicast } } } private void fetchStyle(JTree tree) { SynthContext context = getContext(tree, ENABLED); SynthStyle oldStyle = style; style = SynthLookAndFeel.updateStyle(context, this); if (style != oldStyle) { drawHorizontalLegs = style.getBoolean( context, "Tree.drawHorizontalLegs",true); drawVerticalLegs = style.getBoolean( context, "Tree.drawVerticalLegs", true); tree.setRowHeight(style.getInt(context, "Tree.rowHeight", -1)); tree.setScrollsOnExpand(style.getBoolean( context, "Tree.scrollsOnExpand", true)); largeModel = (tree.isLargeModel() && tree.getRowHeight() > 0); expandedIcon = style.getIcon(context, "Tree.expandedIcon"); collapsedIcon = style.getIcon(context, "Tree.collapsedIcon"); trailingLegBufferOffset = style.getInt( context, "Tree.trailingLegBufferOffset", 0); trailingControlOffset = style.getInt( context, "Tree.trailingControlOffset", 0); controlSize = style.getInt(context, "Tree.controlSize", 0); indent = style.getInt(context, "Tree.indent", 0); updateRootOffset(); drawsFocusBorder = style.getBoolean( context, "Tree.drawsFocusBorder", true); } context.dispose(); context = getContext(tree, Region.TREE_CELL, ENABLED); cellStyle = SynthLookAndFeel.updateStyle(context, this); context.dispose(); } protected void installListeners() { tree.addPropertyChangeListener(this); tree.addMouseListener(defaultDragRecognizer); tree.addMouseMotionListener(defaultDragRecognizer); if ((mouseListener = createMouseListener()) != null) { tree.addMouseListener(mouseListener); if (mouseListener instanceof MouseMotionListener) { tree.addMouseMotionListener( (MouseMotionListener)mouseListener); } } if ((focusListener = createFocusListener()) != null) { tree.addFocusListener(focusListener); } if ((keyListener = createKeyListener()) != null) { tree.addKeyListener(keyListener); } if ((treeExpansionListener = createTreeExpansionListener()) != null) { tree.addTreeExpansionListener(treeExpansionListener); } // TreeModel can be null TreeModel model = tree.getModel(); if ((treeModelListener = createTreeModelListener()) != null && model != null) { model.addTreeModelListener(treeModelListener); } TreeSelectionModel treeSelectionModel = tree.getSelectionModel(); if ((treeSelectionListener = createTreeSelectionListener()) != null) { treeSelectionModel.addTreeSelectionListener(treeSelectionListener); } TreeCellEditor editor = tree.getCellEditor(); if (editor != null) { cellEditorListener = createCellEditorListener(); if (cellEditorListener != null) { editor.addCellEditorListener(cellEditorListener); } } } protected void installKeyboardActions() { InputMap km = getInputMap(JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); SwingUtilities.replaceUIInputMap(tree, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, km); km = getInputMap(JComponent.WHEN_FOCUSED); SwingUtilities.replaceUIInputMap(tree, JComponent.WHEN_FOCUSED, km); LazyActionMap.installLazyActionMap(tree, this); } InputMap getInputMap(int condition) { SynthContext context = getContext(tree, ENABLED); SynthStyle style = context.getStyle(); InputMap map = null; if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { map = (InputMap)style.get(context, "Tree.ancestorInputMap"); } else if (condition == JComponent.WHEN_FOCUSED) { map = (InputMap)style.get(context, "Tree.focusInputMap"); InputMap rtlKeyMap; if (!leftToRight && ((rtlKeyMap = (InputMap)style.get(context, "Tree.focusInputMap.RightToLeft")) != null)) { rtlKeyMap.setParent(map); map = rtlKeyMap; } } context.dispose(); return map; } public void loadActionMap(JComponent c, ActionMap map) { map.put("selectPrevious", new IncrementAction("selectPrevious", -1, false, true)); map.put("selectPreviousChangeLead", new IncrementAction ("selectPreviousLead", -1, false, false)); map.put("selectPreviousExtendSelection", new IncrementAction ("selectPreviousExtendSelection", -1, true, true)); map.put("selectNext", new IncrementAction ("selectNext", 1, false, true)); map.put("selectNextChangeLead", new IncrementAction ("selectNextLead", 1, false, false)); map.put("selectNextExtendSelection", new IncrementAction ("selectNextExtendSelection", 1, true, true)); map.put("selectChild", new TraverseAction ("selectChild", 1, true)); map.put("selectChildChangeLead", new TraverseAction ("selectChildLead", 1, false)); map.put("selectParent", new TraverseAction ("selectParent", -1, true)); map.put("selectParentChangeLead", new TraverseAction ("selectParentLead", -1, false)); map.put("scrollUpChangeSelection", new ScrollAndSelectAction ("scrollUpChangeSelection", SwingConstants.NORTH,false, true)); map.put("scrollUpChangeLead", new ScrollAndSelectAction ("scrollUpChangeLead", SwingConstants.NORTH, false, false)); map.put("scrollUpExtendSelection", new ScrollAndSelectAction ("scrollUpExtendSelection", SwingConstants.NORTH, true, true)); map.put("scrollDownChangeSelection", new ScrollAndSelectAction ("scrollDownChangeSelection", SwingConstants.SOUTH, false, true)); map.put("scrollDownExtendSelection", new ScrollAndSelectAction ("scrollDownExtendSelection", SwingConstants.SOUTH, true, true)); map.put("scrollDownChangeLead", new ScrollAndSelectAction ("scrollDownChangeLead", SwingConstants.SOUTH, false, false)); map.put("selectFirst", new HomeAction ("selectFirst", -1, false, true)); map.put("selectFirstChangeLead", new HomeAction ("selectFirst", -1, false, false)); map.put("selectFirstExtendSelection",new HomeAction ("selectFirstExtendSelection", -1, true, true)); map.put("selectLast", new HomeAction ("selectLast", 1, false, true)); map.put("selectLastChangeLead", new HomeAction ("selectLast", 1, false, false)); map.put("selectLastExtendSelection", new HomeAction ("selectLastExtendSelection", 1, true, true)); map.put("toggle", new ToggleAction("toggle")); map.put("cancel", new CancelEditingAction("cancel")); map.put("startEditing", new EditAction("startEditing")); map.put("selectAll", new SelectAllAction("selectAll", true)); map.put("clearSelection", new SelectAllAction ("clearSelection", false)); map.put("toggleSelectionPreserveAnchor", new AddSelectionAction("toggleSelectionPreserveAnchor", false)); map.put("toggleSelection", new AddSelectionAction("toggleSelection", true)); map.put("extendSelection", new ExtendSelectionAction ("extendSelection")); map.put("scrollLeft", new ScrollAction ("scrollLeft", SwingConstants.HORIZONTAL, -10)); map.put("scrollLeftExtendSelection", new ScrollAndSelectAction ("scrollLeftExtendSelection", SwingConstants.WEST,true, true)); map.put("scrollRight", new ScrollAction ("scrollRight", SwingConstants.HORIZONTAL, 10)); map.put("scrollRightExtendSelection", new ScrollAndSelectAction ("scrollRightExtendSelection", SwingConstants.EAST,true,true)); map.put("scrollRightChangeLead", new ScrollAndSelectAction ("scrollRightChangeLead", SwingConstants.EAST, false, false)); map.put("scrollLeftChangeLead", new ScrollAndSelectAction ("scrollLeftChangeLead", SwingConstants.WEST, false, false)); map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction()); map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction()); map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction()); } /** * Intalls the subcomponents of the tree, which is the renderer pane. */ protected void installComponents() { } public SynthContext getContext(JComponent c) { return getContext(c, getComponentState(c)); } private SynthContext getContext(JComponent c, int state) { return SynthContext.getContext(SynthContext.class, c, SynthLookAndFeel.getRegion(c), style, state); } private Region getRegion(JTree c) { return SynthLookAndFeel.getRegion(c); } private int getComponentState(JComponent c) { return SynthLookAndFeel.getComponentState(c); } private SynthContext getContext(JComponent c, Region region) { return getContext(c, region, getComponentState(c, region)); } private SynthContext getContext(JComponent c, Region region, int state) { return SynthContext.getContext(SynthContext.class, c, region, cellStyle, state); } private int getComponentState(JComponent c, Region region) { // Always treat the cell as selected, will be adjusted appropriately // when painted. return ENABLED | SELECTED; } // // Create methods. // /** * Adjusts the bounds, as returned from the TreeState to the insets * and orientation of the Tree. */ private void adjustCellBounds(JTree tree, Rectangle bounds, Insets i){ if (bounds != null) { if (i == null) { i = tree.getInsets(); } bounds.y += i.top; if (leftToRight) { bounds.x += i.left; } else { bounds.x = tree.getWidth() - i.right - bounds.x - bounds.width; } } } /** * Creates an instance of NodeDimensions that is able to determine * the size of a given node in the tree. */ protected AbstractLayoutCache.NodeDimensions createNodeDimensions() { return new NodeDimensionsHandler(); } /** * Creates the listener responsible for updating the selection based on * mouse events. */ protected MouseListener createMouseListener() { return new MouseHandler(); } /** * Creates a listener that is responsible for updating the display * when focus is lost/gained. */ protected FocusListener createFocusListener() { return new FocusHandler(); } /** * Creates the listener reponsible for getting key events from * the tree. */ protected KeyListener createKeyListener() { return new KeyHandler(); } /** * Creates the listener that updates the display based on selection change * methods. */ protected TreeSelectionListener createTreeSelectionListener() { return new TreeSelectionHandler(); } /** * Creates a listener to handle events from the current editor. */ protected CellEditorListener createCellEditorListener() { return new CellEditorHandler(); } /** * Creates and returns a new ComponentHandler. This is used for * the large model to mark the validCachedPreferredSize as invalid * when the component moves. */ protected ComponentListener createComponentListener() { return new ComponentHandler(); } /** * Creates and returns the object responsible for updating the treestate * when nodes expanded state changes. */ protected TreeExpansionListener createTreeExpansionListener() { return new TreeExpansionHandler(); } /** * Creates the object responsible for managing what is expanded, as * well as the size of nodes. */ protected AbstractLayoutCache createLayoutCache() { if (largeModel && tree.getRowHeight() > 0) { return new FixedHeightLayoutCache(); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?