Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common
   * Development and Distribution License("CDDL") (collectively, the
   * "License"). You may not use this file except in compliance with the
  * License. You can obtain a copy of the License at
  * http://www.netbeans.org/cddl-gplv2.html
  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  * specific language governing permissions and limitations under the
  * License.  When distributing the software, include this License Header
  * Notice in each file and include the License file at
  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the GPL Version 2 section of the License file that
  * accompanied this code. If applicable, add the following below the
  * License Header, with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  *
  * If you wish your version of this file to be governed by only the CDDL
  * or only the GPL Version 2, indicate your decision by adding
  * "[Contributor] elects to include this software in this distribution
  * under the [CDDL or GPL Version 2] license." If you do not indicate a
  * single choice of license, a recipient has the option to distribute
  * your version of this file under either the CDDL, the GPL Version 2 or
  * to extend the choice of license to its licensees as provided above.
  * However, if you add GPL Version 2 code and therefore, elected the GPL
  * Version 2 license, then the option applies only if the new code is
  * made subject to such option by the copyright holder.
  *
  * Contributor(s):
  *
  * Portions Copyrighted 2009 Sun Microsystems, Inc.
  */
 
 package org.netbeans.modules.kenai.ui.treelist;
 
 import java.awt.Color;
 import java.util.List;
Representation of a single row in TreeList.
If the node is expandable then its children are created asynchronously in a separate thread to avoid blocking of AWT queue.
Each node provides its own renderer component.

Author(s):
S. Aubrecht
 
 public abstract class TreeListNode {

    
Time in milliseconds to wait for children creation to finish. When the interval elapses then node's renderer shows an error message.
 
     public static final long TIMEOUT_INTERVAL_MILLIS =
             NbPreferences.forModule(TreeListNode.class).getInt("node.expand.timeoutmillis", 5 * 60 * 1000); //NOI18N
 
     private final boolean expandable;
     private final TreeListNode parent;
     private TreeListListener listener;
     private boolean expanded = false;
     private ArrayList<TreeListNodechildren = null;
     private final Object LOCK = new Object();
 
     private RendererPanel renderer;
 
     private ChildrenLoader loader;

    
C'tor

Parameters:
expandable True if the node provides some children
parent Node's parent or null if this node is root.
 
     public TreeListNodeboolean expandableTreeListNode parent ) {
         this. = expandable;
         this. = parent;
     }
 
     public final boolean isExpandable() {
        return ;
    }
    public final TreeListNode getParent() {
        return ;
    }
    public final List<TreeListNodegetChildren() {
        synchronized ) {
            ifnull ==  )
                return Collections.emptyList();
            return new ArrayList<TreeListNode>();
        }
    }

    

Returns:
Actions for popup menu, or null to disable popup menu.
    public Action[] getPopupActions() {
        return null;
    }

    
This method is called outside AWT thread and may block indefinetely. The list of children is cached until the call of refreshChildren() method.

Returns:
Node's children or an empty list if no children are available, never returns null.
    protected abstract List<TreeListNodecreateChildren();

    
Invoke this method when node's children must be reloaded.
    protected final void refreshChildren() {
        SwingUtilities.invokeLaternew Runnable() {
            public void run() {
                synchronized ) {
                    ifnull !=  ) {
                        forTreeListNode node :  )
                            node.dispose();
                         = null;
                        ifnull !=  )
                            .childrenRemoved(TreeListNode.this);
                    }
                    if ) {
                        startLoadingChildren();
                    }
                }
            }
        });
    }
    
    final JComponent getRendererColor foregroundColor backgroundboolean isSelectedboolean hasFocusint rowHeight ) {
        RendererPanel res = null;
        synchronizedthis ) {
            ifnull ==  ) {
                 = new RendererPanelthis );
            }
            res = ;
        }
        res.configure(foregroundbackgroundisSelectedhasFocusgetNestingDepth(), rowHeight);
        return res;
    }

    
Creates component that will render this node in TreeList. The component will be wrapped in another component to add proper background, border and expansion button.

Parameters:
foreground
background
isSelected
hasFocus
Returns:
Component to render this node.
    protected abstract JComponent getComponentColor foregroundColor backgroundboolean isSelectedboolean hasFocus );

    

Returns:
Action to invoke when Enter key is pressed on selected node in TreeList.
    protected ActionListener getDefaultAction() {
        return null;
    }

    
Notification that the loading of this node's children has started. The method may get called several times without corresponding childrenLoadingFinished() or childrenLoadingTimedout() calls as the loading thread may get cancelled.
    protected void childrenLoadingStarted() {
    }

    
Notification that the loading of this node's children is finished.
    protected void childrenLoadingFinished() {
    }

    
Notification that the loading of this node's children has timed out.
    protected void childrenLoadingTimedout() {
    }
    final void setListenerTreeListListener listener ) {
        this. = listener;
    }

    
Invoked when the node is removed from the model. All listeners should be removed here. Always call super implementation to ensure that children node's (if any) get disposed properly as well.
    protected void dispose() {
        synchronized ) {
            this. = null;
            ifnull !=  ) {
                forTreeListNode node :  )
                    node.dispose();
            }
        }
    }
    final boolean isDescendantOf(TreeListNode grandParent) {
        ifnull ==  )
            return false;
        if.equalsgrandParent ) )
            return true;
        return .isDescendantOf(grandParent);
    }
    final boolean isExpanded() {
        return  && isExpandable();
    }
    public final void setExpandedboolean expanded ) {
        if( !isExpandable() )
            throw new IllegalStateException();
        ifthis. == expanded )
            return;
        this. = expanded;
        ifnull !=  ) {
            ifthis. ) {
                boolean childrenLoaded = true;
                synchronized ) {
                    ifnull ==  ) {
                        childrenLoaded = false;
                        startLoadingChildren();
                    }
                }
                ifchildrenLoaded )
                    .childrenAdded(this);
            } else {
                synchronized ) {
                    ifnull !=  ) {
                        .cancel();
                        childrenLoadingFinished();
                    }
                }
                .childrenRemoved(this);
            }
        }
    }
    final protected void fireContentChanged() {
        synchronizedthis ) {
             = null;
        }
        ifnull !=  )
            .contentChanged(this);
    }
    final protected JLabel createProgressLabel() {
        return createProgressLabel(NbBundle.getMessage(TreeListNode.class"LBL_LoadingInProgress")); //NOI18N
    }
    final protected JLabel createProgressLabelString text ) {
        return new ProgressLabel(text);
    }
    final int getNestingDepth() {
        ifnull == getParent() )
            return 0;
        return getParent().getNestingDepth() + 1;
    }
    private void startLoadingChildren() {
        childrenLoadingStarted();
        ifnull !=  )
            .cancel();
         = new ChildrenLoader();
        RequestProcessor.getDefault().post();
    }
    private class ChildrenLoader implements RunnableCancellable {
        private boolean cancelled = false;
        private Thread t = null;
        public void run() {
            final List<TreeListNoderes[] = new ArrayList[1];
            Runnable r = new Runnable() {
                public void run() {
                    res[0] = createChildren();
                }
            };
             = new Threadr );
            .start();
            try {
                .join );
            } catchInterruptedException iE ) {
                //ignore
            }
            
            if )
                return;
            
            ifnull == res[0] ) {
                childrenLoadingTimedout();
                return;
            }
            synchronized ) {
                 = new ArrayList<TreeListNode>(res[0]);
            }
            SwingUtilities.invokeLaternew Runnable() {
                public void run() {
                    ifnull !=  ) {
                        .childrenAdded(TreeListNode.this);
                    }
                }
            });
            childrenLoadingFinished();
        }
        public boolean cancel() {
             = true;
            ifnull !=  ) {
                .interrupt();
            }
            return true;
        }
    }
    private class ProgressLabel extends TreeLabel {
        private int frame = 0;
        private Timer t;
        final BusyPainter painter;
        public ProgressLabelString text ) {
            supertext );
             = new BusyPainter(16);
            PainterIcon icon = new PainterIcon(new Dimension(16, 16));
            icon.setPainter();
            setIcon(icon );
             = new Timer(100, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                     = (+1)%.getPoints();
                    .setFrame();
                    ProgressLabel.this.repaint();
                    fireContentChanged();
                }
            });
            .setRepeats(true);
            super.setVisible(false);
        }
        @Override
        public void setVisibleboolean visible ) {
            boolean old = isVisible();
            super.setVisible(visible);
            ifold != visible ) {
                ifvisible )
                    .start();
                else {
                    .stop();
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X