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-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


/*
 * ServerRegNode.java -- synopsis
 *
 */
package org.netbeans.modules.j2ee.deployment.impl.ui;

import org.netbeans.modules.j2ee.deployment.impl.ui.actions.RefreshAction;
import org.openide.nodes.*;
import org.openide.util.actions.CookieAction;
import org.openide.util.actions.SystemAction;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;

import javax.management.*;
import javax.swing.Action;
import javax.management.NotificationListener;
import java.util.*;

/**
 * @author George FinKlang
 */

public class ManagementNode extends AbstractNode {
    
    public ManagementNode(ManagedObject mo) {
        super(new ManagementChildren(mo));
        setDisplayName(mo.getDisplayName());
        getCookieSet().add(mo);
        Refresher refresher = new Refresher();
        getCookieSet().add(refresher);
        mo.registerListener(refresher, null);

        String icon = mo.getIconBase();
        if (icon != null)
            setIconBase(icon);
    }
    
    public javax.swing.Action[] getActions(boolean b) {
        ArrayList actions = new ArrayList(Arrays.asList(getManagedObject().getStandardActions()));
        actions.add(SystemAction.get(RefreshAction.class));
        
        actions.add(null);
        actions.add(getManagedObject().getExpertOperationsAction());
        return (Action[]) actions.toArray(new Action[actions.size()]);
    }
    
    private class Refresher implements NotificationListener, RefreshAction.RefreshCookie {
        //Force refresh need to remove all reference to the listener
        public void refresh() {
            refreshSubtree();
            getManagedObject().updateSheet(getSheet());
        }
        
        void refreshSubtree() {
            Children old = getChildren();
            Children replacing = new ManagementChildren(getManagedObject());
            setChildren(replacing);
            
            //PENDING: traverse old subtree to remove listener.
        }
        
        public void handleNotification(javax.management.Notification notification, Object obj) {
            if ("j2ee.object.created".equals(notification.getType())) {
                //PENDIN: to refine with more surgical action if possible
                refreshSubtree();
            } else if ("j2ee.object.deleted".equals(notification.getType())) {
                //PENDIN: to refine with more surgical action if possible
                refreshSubtree();
            }
            else if ("j2ee.attribute.changed".equals(notification.getType()))
                //PENDIN: to refine with more surgical action if possible
                getManagedObject().updateSheet(getSheet());
        }
    }
    
    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
    }
    
    void addListener() {
        ManagedObject mo = getManagedObject();
        mo.registerListener(new AttributesListener(), new AttributeNotificationFilter());
    }
    private class AttributesListener implements NotificationListener {
        public void handleNotification(Notification notification, Object handback) {
            getManagedObject().updateSheet(getSheet());
        }
    }
    private class AttributeNotificationFilter implements NotificationFilter {
        public boolean isNotificationEnabled(Notification notification) {
            if ("j2ee.management.attribute".equals(notification.getType())) //NOI18N
                return true;
            return false;
        }
    }
    
    public Sheet createSheet() {
        Sheet sheet = Sheet.createDefault();
        getManagedObject().updateSheet(sheet);
        addListener();
        return sheet;
    }
    
    ManagedObject getManagedObject() {
        return (ManagedObject) getCookie(ManagedObject.class);
    }
    
    private static class ManagementChildren extends Children.Keys {
        private ManagedObject mo;
        ManagementChildren(ManagedObject mo) {
            this.mo = mo;
        }
        protected void addNotify() {
            setKeys(mo.getChildren());
        }
        protected void removeNotify() {
            setKeys(java.util.Collections.EMPTY_SET);
        }
        protected org.openide.nodes.Node[] createNodes(Object obj) {
            ManagedObject child = (ManagedObject) obj;
            return new Node[] { new ManagementNode(child) };
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.