components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/common/smf/PropertyGroupManagedObject.java
changeset 827 0944d8c0158b
child 1410 ca9946e5736c
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panel.common.smf;
       
    27 
       
    28 import java.util.*;
       
    29 import javax.swing.*;
       
    30 import com.oracle.solaris.scf.common.ScfException;
       
    31 import com.oracle.solaris.vp.panel.common.api.smf_old.*;
       
    32 import com.oracle.solaris.vp.panel.common.model.AbstractManagedObject;
       
    33 import com.oracle.solaris.vp.util.misc.*;
       
    34 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    35 import com.oracle.solaris.vp.util.swing.HasIcon;
       
    36 
       
    37 public class PropertyGroupManagedObject
       
    38     extends AbstractManagedObject<PropertyManagedObject> implements HasIcon {
       
    39 
       
    40     //
       
    41     // Static data
       
    42     //
       
    43 
       
    44     protected static final List<ImageIcon> icons = Finder.getIcons(
       
    45 	"images/propertygroup-16.png",
       
    46 	"images/propertygroup-22.png",
       
    47 	"images/propertygroup-24.png",
       
    48 	"images/propertygroup-32.png",
       
    49 	"images/propertygroup-48.png",
       
    50 	"images/propertygroup-256.png");
       
    51 
       
    52     public static final String ID = "propertygroup";
       
    53 
       
    54     public static final Comparator<Object> COMPARATOR =
       
    55 	new Comparator<Object>() {
       
    56 	    @Override
       
    57 	    public int compare(Object o1, Object o2) {
       
    58 		if (o1 == null) {
       
    59 		    return o2 == null ? 0 : -1;
       
    60 		}
       
    61 
       
    62 		if (o2 == null) {
       
    63 		    return 1;
       
    64 		}
       
    65 
       
    66 		SmfMutableProperty prop1 =
       
    67 		    o1 instanceof PropertyManagedObject ?
       
    68                     ((PropertyManagedObject)o1).getProperty() :
       
    69 		    o1 instanceof SmfMutableProperty ?
       
    70                     (SmfMutableProperty)o1 : null;
       
    71 
       
    72 		SmfMutableProperty prop2 =
       
    73 		    o2 instanceof PropertyManagedObject ?
       
    74                     ((PropertyManagedObject)o2).getProperty() :
       
    75 		    o2 instanceof SmfMutableProperty ?
       
    76                     (SmfMutableProperty)o2 : null;
       
    77 
       
    78                 int cmpVal = ObjectUtil.compare(prop1.getPropertyName(),
       
    79 		    prop2.getPropertyName());
       
    80 
       
    81 		if (cmpVal != 0) {
       
    82 		    return cmpVal;
       
    83 		}
       
    84 
       
    85 		Class<?> class1 = prop1.getClass();
       
    86 		Class<?> class2 = prop2.getClass();
       
    87 
       
    88 		if (!class1.equals(class2)) {
       
    89 		    if (class1.isAssignableFrom(class2)) {
       
    90 			return -1;
       
    91 		    }
       
    92 		    if (class2.isAssignableFrom(class1)) {
       
    93 			return 1;
       
    94 		    }
       
    95 		}
       
    96 
       
    97 		return 0;
       
    98 	    }
       
    99 	};
       
   100 
       
   101     //
       
   102     // Instance data
       
   103     //
       
   104 
       
   105     private PropertyGroupsManagedObject parent;
       
   106     private PropertyGroup group;
       
   107 
       
   108     //
       
   109     // Constructors
       
   110     //
       
   111 
       
   112     PropertyGroupManagedObject(PropertyGroupsManagedObject parent,
       
   113 	PropertyGroup group) throws ScfException {
       
   114 
       
   115 	super(group.getName());
       
   116 
       
   117 	this.parent = parent;
       
   118 	this.group = group;
       
   119 
       
   120 	setComparator(COMPARATOR);
       
   121 	refresh(true);
       
   122     }
       
   123 
       
   124     //
       
   125     // HasIcon methods
       
   126     //
       
   127 
       
   128     @Override
       
   129     public Icon getIcon(int height) {
       
   130 	return IconUtil.getClosestIcon(icons, height);
       
   131     }
       
   132 
       
   133     //
       
   134     // PropertyGroupManagedObject methods
       
   135     //
       
   136 
       
   137     public PropertyManagedObject getChild(SmfMutableProperty property) {
       
   138 	synchronized (children) {
       
   139 	    int index = Collections.binarySearch(
       
   140 		getChildren(), property, COMPARATOR);
       
   141 
       
   142 	    if (index >= 0) {
       
   143 		return children.get(index);
       
   144 	    }
       
   145 
       
   146 	    return null;
       
   147 	}
       
   148     }
       
   149 
       
   150     public PropertyGroup getPropertyGroup() {
       
   151 	return group;
       
   152     }
       
   153 
       
   154     public void refresh(boolean force) throws ScfException {
       
   155 	ServiceTracker tracker = parent.getServiceTracker();
       
   156 	ServiceMXBean service = tracker.getService();
       
   157 	if (service == null) {
       
   158 	    return;
       
   159 	}
       
   160 
       
   161 	String pgName = group.getName();
       
   162 
       
   163 	synchronized (children) {
       
   164 	    List<PropertyManagedObject> toRemove =
       
   165 		new LinkedList<PropertyManagedObject>(children);
       
   166 
       
   167 	    for (String pName : service.getPropertyNames(pgName)) {
       
   168 		String locale = Locale.getDefault().getLanguage();
       
   169 		Template templ = getPropertyTemplate(service, pgName, pName,
       
   170 		    locale);
       
   171 
       
   172 		if (templ != null &&
       
   173 		    templ.getVisibility() == PropertyVisibility.HIDDEN) {
       
   174 		    continue;
       
   175 		}
       
   176 
       
   177 		SmfPropertyInfo info = new SimpleSmfPropertyInfo(tracker,
       
   178 		    pgName, null);
       
   179 
       
   180 		BasicSmfMutableProperty property = null;
       
   181 
       
   182 		if (templ != null) {
       
   183 		    List<String> allowed = templ.getAllowed();
       
   184 		    if (allowed != null && allowed.size() > 0) {
       
   185 			property = new ChoiceSmfProperty(pName, info, allowed);
       
   186 		    }
       
   187 		}
       
   188 
       
   189 		PropertyType type = service.getPropertyType(pgName, pName);
       
   190 		if (property == null) {
       
   191 		    switch (type) {
       
   192 		    case BOOLEAN:
       
   193 			property = new BooleanSmfProperty(pName, info);
       
   194 			break;
       
   195 
       
   196 		    case COUNT:
       
   197 		    case INTEGER:
       
   198 			property = new IntegerSmfProperty(pName, info);
       
   199 			break;
       
   200 
       
   201 		    default:
       
   202 			property = new StringSmfProperty(pName, info);
       
   203 		    }
       
   204 		}
       
   205 		property.setType(type);
       
   206 
       
   207 		PropertyManagedObject child = getChild(property);
       
   208 		if (child == null) {
       
   209 		    child = new PropertyManagedObject(this, property);
       
   210 		    addChildren(child);
       
   211 		} else {
       
   212 		    toRemove.remove(child);
       
   213 		    child.refresh(force);
       
   214 		}
       
   215 	    }
       
   216 
       
   217 	    // Minimize IntervalEvents by removing en masse
       
   218 	    if (!toRemove.isEmpty()) {
       
   219 		removeChildren(toRemove.toArray(
       
   220 		    new PropertyManagedObject[toRemove.size()]));
       
   221 	    }
       
   222 	}
       
   223     }
       
   224 
       
   225     //
       
   226     // Static methods
       
   227     //
       
   228 
       
   229     private static Template getPropertyTemplate(ServiceMXBean service,
       
   230 	String pgName, String pName, String locale) throws ScfException {
       
   231 
       
   232 	Template templ = null;
       
   233 	try {
       
   234 	    templ = service.getPropertyTemplate(pgName, pName, locale);
       
   235 	} catch (ScfException e) {
       
   236 	    if (e.getError() != SmfErrorCode.NOT_FOUND) {
       
   237 		throw e;
       
   238 	    }
       
   239 	}
       
   240 	return templ;
       
   241     }
       
   242 }