<commit_before>/*
   Copyright 2008-2010 CNR-ISTI, http://isti.cnr.it
   Institute of Information Science and Technologies 
   of the Italian National Research Council 


   See the NOTICE file distributed with this work for additional 
   information regarding copyright ownership

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.persona.zigbee.tester.gui;

import it.cnr.isti.zigbee.api.ZigBeeDevice;
import it.cnr.isti.zigbee.ha.cluster.glue.Cluster;
import it.cnr.isti.zigbee.ha.driver.core.HADevice;
import it.cnr.isti.zigbee.zcl.library.api.core.Attribute;

import java.awt.Color;
import java.awt.Component;
import java.awt.image.ImageObserver;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JToolTip;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.persona.zigbee.tester.Activator;
import org.persona.zigbee.tester.discovery.DeviceNode;


/**
 * 
 * @author <a href="mailto:<EMAIL>"><NAME>urfari</a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $LastChangedRevision$ ($LastChangedDate$)
 * @since 0.1.0
 *
 */
public class HADeviceTreeNode extends DefaultMutableTreeNode {

	public final static String HA_DEVICE = "RootDeviceNode";
	public final static String HA_EVENT = "HAEventNode";
	public final static String ZIGBEE_DEVICE = "DeviceNode";
	public final static String SERVICE = "UPnPService";
	public final static String ZCL_COMMAND = "ZCLCommand";
	public final static String ZCL_ATTRIBUTE = "ZCLAttribute";
	public final static String EVENTED_STATE = "ZCLReportableAttribute";
	public final static String SUBSCRIBED_STATE = "ZCLSubcribedAttribute";
	
	protected String category;
	
	public HADeviceTreeNode(String obj) {
		super(obj);
		category = obj.getClass().getName();
	}
	
	public HADeviceTreeNode(DeviceNode node, HADeviceTreeNode root) {
		super(node);
		
		category = HA_DEVICE;
		
		final HADevice device = node.getDevice(Activator.context);
		/* Adding the wrapped ZigBeeDevice as subnode and removing it from the sibling nodes */
		ServiceReference[] sr = null;
        try {
            sr = Activator.context.getServiceReferences( 
                ZigBeeDevice.class.getName(), 
                "(" +  ZigBeeDevice.UUID + "=" + device.getZBDevice().getUniqueIdenfier() + ")"
            );
        } catch ( InvalidSyntaxException e ) {
            //IGNORED 
        }
        this.add( new HADeviceTreeNode( sr[0] ) );    
		
        final long sid = (Long) sr[0].getProperty( Constants.SERVICE_ID );
		for(int i = 0; i < root.getChildCount(); i++){
			final HADeviceTreeNode current = (HADeviceTreeNode) root.getChildAt(i);
			if ( current.category != HADeviceTreeNode.ZIGBEE_DEVICE ) {
				continue;
			}
			final ServiceReference currentSR = ( ServiceReference ) current.getUserObject();
			final long currentSID = (Long) currentSR.getProperty( Constants.SERVICE_ID );
			if ( sid == currentSID ){
				current.removeFromParent();
				break;
			}
		}
		
		
		/* Adding all the clusters of this HADevice as subtree */
		Cluster[] clusters = device.getAvailableCluster();		
		if (clusters != null){
			Arrays.sort(clusters, new Comparator<Cluster>() {
				public int compare(Cluster a, Cluster b) {
					if ( a == b ) return 0;
					if ( a == null ) return -1;
					if ( b == null ) return 1;
					return a.getId() - b.getId();
				}
			});
			
			for (int i=0; i<clusters.length; i++) {
				if ( clusters[i] != null ) {
					this.add(new HADeviceTreeNode(clusters[i]));
				}
			}
		}
		
		Activator.context.ungetService(node.getReference());
	}
	
	public HADeviceTreeNode(Cluster obj) {
		super(obj);
		category = SERVICE;
		Attribute[] variables = obj.getAttributes();
		if (variables != null) {
			for (int i=0;i<variables.length;i++)
				this.add(new HADeviceTreeNode(variables[i]));
		}
		Method[] methods = obj.getClass().getMethods();
		for (int i = 0; i < methods.length; i++) {
			Class<?> declaringClass = methods[i].getDeclaringClass();
			if( declaringClass == Cluster.class || declaringClass == Object.class ) {
				continue;
			}
			String methodName = methods[i].getName();
			if ( methodName.startsWith("get") || methodName.equals("subscribe") || methodName.equals("unsubscribe") ){
				//We skip method that are only a way to access method available at level of attribute
				continue;
			} else if ( methodName.startsWith("add") || methodName.startsWith("remove") ) {
			    Class<?>[] args = methods[i].getParameterTypes();
               if( args.length == 1 && args[0].getName().endsWith( "Listener" ) ){
	                String pairName = null;<commit_msg>CommandActionPanel does not display Command that starts with "get" even if are not related to an attribute ( refs #140 )<commit_after>/*
   Copyright 2008-2010 CNR-ISTI, http://isti.cnr.it
   Institute of Information Science and Technologies 
   of the Italian National Research Council 


   See the NOTICE file distributed with this work for additional 
   information regarding copyright ownership

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.persona.zigbee.tester.gui;

import it.cnr.isti.zigbee.api.ZigBeeDevice;
import it.cnr.isti.zigbee.ha.cluster.glue.Cluster;
import it.cnr.isti.zigbee.ha.driver.core.HADevice;
import it.cnr.isti.zigbee.zcl.library.api.core.Attribute;

import java.awt.Color;
import java.awt.Component;
import java.awt.image.ImageObserver;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JToolTip;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.persona.zigbee.tester.Activator;
import org.persona.zigbee.tester.discovery.DeviceNode;


/**
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $LastChangedRevision$ ($LastChangedDate$)
 * @since 0.1.0
 *
 */
public class HADeviceTreeNode extends DefaultMutableTreeNode {

	public final static String HA_DEVICE = "RootDeviceNode";
	public final static String HA_EVENT = "HAEventNode";
	public final static String ZIGBEE_DEVICE = "DeviceNode";
	public final static String SERVICE = "UPnPService";
	public final static String ZCL_COMMAND = "ZCLCommand";
	public final static String ZCL_ATTRIBUTE = "ZCLAttribute";
	public final static String EVENTED_STATE = "ZCLReportableAttribute";
	public final static String SUBSCRIBED_STATE = "ZCLSubcribedAttribute";
	
	protected String category;
	
	public HADeviceTreeNode(String obj) {
		super(obj);
		category = obj.getClass().getName();
	}
	
	public HADeviceTreeNode(DeviceNode node, HADeviceTreeNode root) {
		super(node);
		
		category = HA_DEVICE;
		
		final HADevice device = node.getDevice(Activator.context);
		/* Adding the wrapped ZigBeeDevice as subnode and removing it from the sibling nodes */
		ServiceReference[] sr = null;
        try {
            sr = Activator.context.getServiceReferences( 
                ZigBeeDevice.class.getName(), 
                "(" +  ZigBeeDevice.UUID + "=" + device.getZBDevice().getUniqueIdenfier() + ")"
            );
        } catch ( InvalidSyntaxException e ) {
            //IGNORED 
        }
        this.add( new HADeviceTreeNode( sr[0] ) );    
		
        final long sid = (Long) sr[0].getProperty( Constants.SERVICE_ID );
		for(int i = 0; i < root.getChildCount(); i++){
			final HADeviceTreeNode current = (HADeviceTreeNode) root.getChildAt(i);
			if ( current.category != HADeviceTreeNode.ZIGBEE_DEVICE ) {
				continue;
			}
			final ServiceReference currentSR = ( ServiceReference ) current.getUserObject();
			final long currentSID = (Long) currentSR.getProperty( Constants.SERVICE_ID );
			if ( sid == currentSID ){
				current.removeFromParent();
				break;
			}
		}
		
		
		/* Adding all the clusters of this HADevice as subtree */
		Cluster[] clusters = device.getAvailableCluster();		
		if (clusters != null){
			Arrays.sort(clusters, new Comparator<Cluster>() {
				public int compare(Cluster a, Cluster b) {
					if ( a == b ) return 0;
					if ( a == null ) return -1;
					if ( b == null ) return 1;
					return a.getId() - b.getId();
				}
			});
			
			for (int i=0; i<clusters.length; i++) {
				if ( clusters[i] != null ) {
					this.add(new HADeviceTreeNode(clusters[i]));
				}
			}
		}
		
		Activator.context.ungetService(node.getReference());
	}
	
	public HADeviceTreeNode(Cluster obj) {
		super(obj);
		category = SERVICE;
		Attribute[] variables = obj.getAttributes();
		if (variables != null) {
			for (int i=0;i<variables.length;i++)
				this.add(new HADeviceTreeNode(variables[i]));
		}
		Method[] methods = obj.getClass().getMethods();
		for (int i = 0; i < methods.length; i++) {
			Class<?> declaringClass = methods[i].getDeclaringClass();
			if( declaringClass == Cluster.class || declaringClass == Object.class ) {
				continue;
			}
			String methodName = methods[i].getName();
			if ( methodName.equals("subscribe") || methodName.equals("unsubscribe") ){
				//We skip method that are only a way to access method available at level of attribute
				continue;
			} else if ( methodName.startsWith("get") && Attribute.class.isAssignableFrom(methods[i].getReturnType()) ) {
				//We skip method that are only a way to access method available at level of attribute
				continue;				
			} else if ( methodName.startsWith("add") || methodName.startsWith("remove") ) {
			    Class<?>[] args = methods[i].getParameterTypes();
               if( args.length == 1 && args[0].getName().endsWith( "Listener" ) ){
	                String pairName = null;
{'fraction_non_alphanumeric': 0.11532584269662921, 'fraction_numerical': 0.004943820224719101, 'mean_word_length': 3.6610808546292417, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2772955', 'n_tokens_mistral': 3705, 'n_tokens_neox': 3330, 'n_words': 912}