⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lwtree.java

📁 Zaval Light-Weight Visual Components Library (LwVCL) is a pure Java alternative to humble AWT-based
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
  /**   * The method is overrided to calculate metrical characteristics of the tree view   * component.   */   protected /*C#override*/ void recalc()   {     if (max == null)     {       Insets i = getInsets();       max = new Dimension (i.left, i.top);       recalc (i.left, i.top, null, data.getRoot());       max.width  -= i.left;       max.height -= i.top;     }   }  /**   * Adds the specified selection listener to be notified whenever the tree item has been   * selected, deselected.   * @param <code>l</code> the specified selection listener.   */   public void addSelectionListener(LwActionListener l) {     if (selection == null) selection = new LwActionSupport();     selection.addListener(l);   }  /**   * Removes the specified selection listener.   * @param <code>l</code> the specified selection listener.   */   public void removeSelectionListener(LwActionListener l) {     if (selection != null) selection.removeListener(l);   }   public LwView getView(Drawable d, Object obj) {     defaultRender.getTextModel().setText(obj.toString());     return defaultRender;   }   public void focusGained  (LwFocusEvent e) {}   public void focusLost    (LwFocusEvent e) {}   public void mouseClicked (LwMouseEvent e) {}   public void mouseEntered (LwMouseEvent e) {}   public void mouseExited  (LwMouseEvent e) {}   public void mouseReleased(LwMouseEvent e) {}   public void keyReleased  (LwKeyEvent e) {}   public void keyTyped     (LwKeyEvent e) {}   public void keyPressed (LwKeyEvent e)   {     if (selected != null)     {       switch (e.getKeyChar())       {         case '+' : if (!isOpen(selected)) toggle(selected); return;         case '-' : if (isOpen(selected)) toggle(selected); return;       }     }     Item newSelection = null;     switch (e.getKeyCode())     {       case KeyEvent.VK_DOWN :       case KeyEvent.VK_RIGHT: newSelection = findNext (selected); break;       case KeyEvent.VK_UP   :       case KeyEvent.VK_LEFT : newSelection = findPrev (selected); break;       case KeyEvent.VK_HOME : if ((e.getMask() & KeyEvent.CTRL_MASK) > 0) select(data.getRoot());                               break;       case KeyEvent.VK_END  : if ((e.getMask() & KeyEvent.CTRL_MASK) > 0) select(findLast(data.getRoot()));                               break;       case KeyEvent.VK_PAGE_DOWN : if (selected != null) select(nextPage(selected, 1));                                    break;       case KeyEvent.VK_PAGE_UP   : if (selected != null) select(nextPage(selected, -1));                                    break;     }     if (newSelection != null) select(newSelection);   }   public void mousePressed(LwMouseEvent e)   {     if (LwToolkit.isActionMask(e.getMask()))     {       int x = e.getX() - dx, y = e.getY() - dy;       Item root = getItemAt(firstVisible, x, y);       if (root != null)       {         Rectangle r = getToggleBounds(root);         if (r.contains(x, y)) toggle(root);         else if (x > r.x + r.width) select(root);       }     }   }  /**   * Sets the selection marker color for the given tree view component state.   * @param <code>c</code> the selection marker color. Use <code>null</code> if the marker should not be   * rendered.   * @param <code>isHasFocus</code> the given tree view component focus state. Use <code>true</code>   * to identify the selection color when the component has focus, otherwise use <code>false</code>   */   public void setSelectionColor (Color c, boolean isHasFocus)   {      Color rc = getSelectionColor(isHasFocus);      if (rc != c || (rc == null || !rc.equals(c)))      {        if (isHasFocus) selectColor1 = c;        else            selectColor2 = c;        repaint();      }   }  /**   * Gets the selection marker color for the specified component focus state.   * @param <code>isHasFocus</code> the given tree view component focus state. Use <code>true</code>   * to identify the selection color when the component has focus, otherwise use <code>false</code>   * @return a selection marker color.   */   public Color getSelectionColor (boolean isHasFocus) {     return isHasFocus?selectColor1:selectColor2;   }  /**   * Switches a toggle state for the specified tree item. If the toggle state   * is "on" than the method switches it to "off" and the "off" state will be   * switched to "on" state.   * @param <code>item</code> the specified tree item.   */   public void toggle(Item item)   {     validate();     ItemMetrics node = getIM (item);     node.isOpen = !node.isOpen;     if (actions != null)  actions.perform (new LwActionEvent (this, item));     invalidateMetrics();     if (!node.isOpen && selected != null && isParentFor(item, selected)) select(item);     else repaint();   }  /**   * Selects the specified node of the tree.   * @param <code>item</code> the specified node.   */   public void select(Item item)   {     if (isSelectionEnabled())     {       if (item != selected)       {         selected = item;         if (selected != null)         {           validate();           Rectangle r = getViewBounds(selected);           Point o = LwToolkit.calcOrigin(r.x, r.y, r.width, r.height, this);           if (man != null) man.scrollObjMoved(o.x, o.y);           else             setSOLocation(o.x, o.y);           if (selection != null)             selection.perform (new LwActionEvent(this, selected));         }         repaint();       }     }   }  /**   * Gets the item that is selected at the moment.   * @return a selected item.   */   public Item getSelectedItem() {     return selected;   }  /**   * Adds the specified action listener to receive action events from this tree.   * The event is generated whenever the tree node has been expanded or collapsed.   * The node can be got by <code>getData</code> method of the action event.   * @param <code>l</code> the specified action listener.   */   public void addActionListener(LwActionListener l) {     if (actions == null) actions = new LwActionSupport();     actions.addListener(l);   }  /**   * Removes the specified action listener.   * @param <code>l</code> the specified action listener.   */   public void removeActionListener(LwActionListener l) {     if (actions != null) actions.removeListener(l);   }   public void itemInserted(TreeEvent e) {     invalidateMetrics();     repaint();   }   public void itemRemoved (TreeEvent e)   {     if (nodes != null)     {       nodes.remove(e.getItem());       invalidateMetrics();       repaint();     }   }   public void itemModified(TreeEvent e)   {     if (nodes != null)     {       ItemMetrics node = getIM(e.getItem());       if (node != null) node.viewWidth = -1;       invalidateMetrics();       repaint();     }   }   public /*C#override*/ void invalidate () {     bits = MathBox.getBits(bits, VALVIS_BIT, false);     super.invalidate();   }   public /*C#override*/ Point getOrigin() {     return new Point(dx, dy);   }   public Point getSOLocation () {     return getOrigin();   }   public void setSOLocation (int x, int y)   {     if (x != dx || y != dy)     {       dx = x;       int prevDy = dy;       dy = y;       if (isVisibilityValid())         firstVisible = (y < prevDy)?nextVisible(firstVisible):prevInVisible(firstVisible);       repaint();     }   }   public Dimension getSOSize() {     return getPreferredSize();   }   public void setScrollMan (ScrollMan m) {     man = m;   }   public boolean moveContent() {     return true;   }  /**   * Checks if the tree item is expanded. The item is expanded if it is expanded and   * all parent tree items are exapnded too.   * @param <code>i</code> the specified tree item.   * @return <code>true</code> if the item and its parent item is expanded too;   * <code>false</code> otherwise.   */   public boolean isOpen(Item i) {     validate();     return isOpen_(i);   }  /**   * Gets the item metric for the specified item.   * @param <code>i</code> the specified item.   * @return an item metric.   */   public ItemMetrics getItemMetrics (Item i) {     validate();     return getIM (i);   }  /**   * Gets the preferred size of this component. The method is overrided with the   * component to returns the "pure" preferred size basing on the tree structure.   * @return a dimension object indicating this component preferred size.   */   protected /*C#override*/ Dimension calcPreferredSize() {     return max == null?super.calcPreferredSize():new Dimension (max.width, max.height);   }   private int recalc(int x, int y, Item parent, Item root)   {     ItemMetrics node = getIM(root);     if (node == null)     {       node = new ItemMetrics(isOpenVal);       if (nodes == null) nodes = new Hashtable();       nodes.put(root, node);     }     if (node.viewWidth < 0)     {       LwView nodeView = provider.getView(this, root);       Dimension viewSize = nodeView.getPreferredSize();       node.viewWidth  = viewSize.width == 0?5:viewSize.width;       node.viewHeight = viewSize.height;     }     Dimension imageSize  = getImageSize (root);     Dimension toggleSize = getToggleSize(root);     if (parent != null)     {       Rectangle pImg = getImageBounds(parent);       x = pImg.x + (pImg.width - toggleSize.width)/2;     }     node.x      = x;     node.y      = y;     node.width  = toggleSize.width + imageSize.width + node.viewWidth +                   (toggleSize.width>0?gapx:0) + (imageSize.width>0?gapx:0);     node.height = Math.max(Math.max (toggleSize.height, imageSize.height), node.viewHeight);     if (node.x + node.width > max.width ) max.width  = node.x + node.width;     if (node.y + node.height> max.height) max.height = node.y + node.height;     x  = node.x + toggleSize.width + (toggleSize.width>0?gapx:0);     y += (node.height + gapy);     //!!!     // Take care that it is necessary to use method isOpen(Item), but the method     // implementation works correctly if the field isOpenVal is used. The feature allows     // speed up performance for the method.     //!!!     if (node.isOpen)     {       int count = data.getChildrenCount(root);       for (int i=0; i<count; i++) y = recalc(x, y, root, data.getChildAt(root, i));     }     return y;   }   private boolean isOpen_(Item i) {     return (i == null || (data.hasChildren(i) && getIM(i).isOpen && isOpen_(data.getParent(i))));   }   private ItemMetrics getIM(Item i) {     return (nodes == null)?null:(ItemMetrics)nodes.get(i);   }   private Item getItemAt (Item root, int x, int y)   {     if (isVisibilityValid() && y + dy >= visibleArea.y && y + dy < visibleArea.y + visibleArea.height)     {       Item found = getItemAtInBranch(root, x, y);       if (found != null) return found;       Item parent = data.getParent(root);       while(parent != null)       {         int index = data.getChildIndex(root);         int count = data.getChildrenCount(parent);         for (int i=index + 1; i<count; i++)         {           found = getItemAtInBranch (data.getChildAt(parent, i), x, y);           if (found != null) return found;         }         root   = parent;         parent = data.getParent(root);       }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -