alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.examples.modules.sortbysize;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.openide.actions.PropertiesAction;
import org.openide.filesystems.*;
import org.openide.nodes.*;
import org.openide.util.WeakListener;
import org.openide.util.actions.SystemAction;

public class FileViewNode extends AbstractNode implements FileChangeListener {
    
    private final FileObject f;
    
    public FileViewNode(FileObject f) {
        super(f.isFolder() ? new FileViewChildren(f) : Children.LEAF);
        this.f = f;
        setIconBase(f.isFolder() ? "org/netbeans/examples/modules/sortbysize/folder" : "org/netbeans/examples/modules/sortbysize/file");
        setDefaultAction(SystemAction.get(PropertiesAction.class));
        updateNames();
        f.addFileChangeListener(WeakListener.fileChange(this, f));
    }
    
    private void updateNames() {
        setName(f.getNameExt());
        File physical = FileUtil.toFile(f);
        if (physical != null) {
            setShortDescription(physical.getAbsolutePath());
        }
    }
    
    protected Sheet createSheet() {
        Sheet sheet = super.createSheet();
        if (f.isData()) {
            Sheet.Set props = sheet.get(Sheet.PROPERTIES);
            if (props == null) {
                props = Sheet.createPropertiesSet();
                sheet.put(props);
            }
            props.put(new SizeProp());
        }
        return sheet;
    }
    
    public Node cloneNode() {
        return new FileViewNode(f);
    }
    public Node.Handle getHandle() {
        return new H(f);
    }
    
    public void fileChanged(FileEvent fe) {
        if (fe.getFile() == f) {
            firePropertyChange("size", null, null);
        }
    }
    public void fileRenamed(FileRenameEvent fe) {
        if (fe.getFile() == f) {
            updateNames();
        }
    }
    public void fileDeleted(FileEvent fe) {
        if (fe.getFile() == f) {
            fireNodeDestroyed();
        }
    }
    public void fileAttributeChanged(FileAttributeEvent fe) {}
    public void fileDataCreated(FileEvent fe) {}
    public void fileFolderCreated(FileEvent fe) {}
    
    private static final class H implements Node.Handle {
        private static final long serialVersionUID = 5762765672245L;
        private final FileObject f;
        public H(FileObject f) {
            this.f = f;
        }
        public Node getNode() throws IOException {
            return new FileViewNode(f);
        }
    }
    
    private final class SizeProp extends PropertySupport.ReadOnly {
        public SizeProp() {
            super("size", Long.TYPE, "Size", "Size of this file in bytes.");
        }
        public Object getValue() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            return new Long(f.getSize());
        }
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.