components/visual-panels/apache/src/java/vpanels/app/apache/com/oracle/solaris/vp/panels/apache/client/swing/Module.java
changeset 827 0944d8c0158b
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) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panels.apache.client.swing;
       
    27 
       
    28 import java.util.List;
       
    29 import javax.swing.*;
       
    30 import com.oracle.solaris.scf.common.ScfException;
       
    31 import com.oracle.solaris.vp.panel.common.model.*;
       
    32 import com.oracle.solaris.vp.panel.common.smf.*;
       
    33 import com.oracle.solaris.vp.util.misc.IconUtil;
       
    34 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    35 import com.oracle.solaris.vp.util.misc.property.*;
       
    36 import com.oracle.solaris.vp.util.swing.HasIcon;
       
    37 
       
    38 @SuppressWarnings({"serial"})
       
    39 public class Module extends AbstractManagedObject<ManagedObject>
       
    40     implements HasIcon, Cloneable, SmfPropertyInfo {
       
    41 
       
    42     //
       
    43     // Static data
       
    44     //
       
    45 
       
    46     public static final String PROPERTY_FILE = "file";
       
    47 
       
    48     public static final String SMF_PROPERTY_PREFIX = "module-";
       
    49 
       
    50     protected static final List<ImageIcon> icons = Finder.getIcons(
       
    51 	"images/modules-32.png",
       
    52 	"images/modules-48.png",
       
    53 	"images/modules-96.png");
       
    54 
       
    55     //
       
    56     // Instance data
       
    57     //
       
    58 
       
    59     private Modules parent;
       
    60     private String propertyName;
       
    61 
       
    62     private MutableProperty<String> nameProperty =
       
    63 	new BasicMutableProperty<String>(PROPERTY_NAME);
       
    64 
       
    65     private MutableProperty<String> fileProperty =
       
    66 	new BasicMutableProperty<String>(PROPERTY_FILE);
       
    67 
       
    68     // This is the property that does the actual work of persisting this
       
    69     // Module's data to the SMF repository
       
    70     private BasicSmfMutableProperty<String> smfProperty =
       
    71 	new StringSmfProperty(null, this);
       
    72 
       
    73     //
       
    74     // Constructors
       
    75     //
       
    76 
       
    77     public Module(Modules parent) {
       
    78 	this.parent = parent;
       
    79 
       
    80 	nameProperty.setValue("");
       
    81 	addProperties(nameProperty);
       
    82 
       
    83 	fileProperty.setValue("");
       
    84 	addProperties(fileProperty);
       
    85 
       
    86 	// Synchronize the properties holding this Module's data with the
       
    87 	// property responsible for persisting it to the SMF repository
       
    88 	new ModulePropertySynchronizer(smfProperty, this, false);
       
    89     }
       
    90 
       
    91     public Module(Modules parent, String propertyName) throws ScfException {
       
    92 	this(parent);
       
    93 	this.propertyName = propertyName;
       
    94 	refresh(true);
       
    95     }
       
    96 
       
    97     //
       
    98     // HasService methods
       
    99     //
       
   100 
       
   101     @Override
       
   102     public AggregatedRefreshService getService() {
       
   103 	return parent.getApacheInfo().getService();
       
   104     }
       
   105 
       
   106     //
       
   107     // SmfPropertyGroupInfo methods
       
   108     //
       
   109 
       
   110     @Override
       
   111     public String getPropertyGroupName() throws ScfException {
       
   112 	return parent.getApacheInfo().getPropertyGroupName();
       
   113     }
       
   114 
       
   115     //
       
   116     // SmfPropertyInfo methods
       
   117     //
       
   118 
       
   119     @Override
       
   120     public String getPropertyName() throws ScfException {
       
   121 	if (propertyName == null) {
       
   122 	    propertyName = ApacheUtil.getNextAvailablePropertyName(
       
   123 		parent.getApacheInfo(), SMF_PROPERTY_PREFIX);
       
   124 	}
       
   125 	return propertyName;
       
   126     }
       
   127 
       
   128     //
       
   129     // Object methods
       
   130     //
       
   131 
       
   132     @Override
       
   133     public Module clone() {
       
   134 	Module clone = new Module(getParent());
       
   135 	for (MutableProperty<?> property : getProperties()) {
       
   136 	    String name = property.getPropertyName();
       
   137 
       
   138 	    @SuppressWarnings({"unchecked"})
       
   139 	    MutableProperty<Object> oldProperty =
       
   140 		(MutableProperty<Object>)property;
       
   141 
       
   142 	    @SuppressWarnings({"unchecked"})
       
   143 	    MutableProperty<Object> newProperty =
       
   144 		(MutableProperty<Object>)clone.getProperty(name);
       
   145 
       
   146 	    newProperty.setValue(oldProperty.getValue());
       
   147 	}
       
   148 
       
   149 	return clone;
       
   150     }
       
   151 
       
   152     //
       
   153     // ManagedObject methods
       
   154     //
       
   155 
       
   156     @Override
       
   157     public String getId() {
       
   158 	return nameProperty.getValue();
       
   159     }
       
   160 
       
   161     @Override
       
   162     public String getName() {
       
   163 	return getId();
       
   164     }
       
   165 
       
   166     //
       
   167     // HasIcon methods
       
   168     //
       
   169 
       
   170     @Override
       
   171     public Icon getIcon(int height) {
       
   172 	return IconUtil.getClosestIcon(icons, height);
       
   173     }
       
   174 
       
   175     //
       
   176     // Module methods
       
   177     //
       
   178 
       
   179     public MutableProperty<String> getFileProperty() {
       
   180 	return fileProperty;
       
   181     }
       
   182 
       
   183     public MutableProperty<String> getNameProperty() {
       
   184 	return nameProperty;
       
   185     }
       
   186 
       
   187     public Modules getParent() {
       
   188 	return parent;
       
   189     }
       
   190 
       
   191     public boolean isInRepo() {
       
   192 	return propertyName != null;
       
   193     }
       
   194 
       
   195     public void refresh(boolean force) throws ScfException {
       
   196 	smfProperty.updateFromRepo(force);
       
   197     }
       
   198 
       
   199     protected void removeFromRepo() throws ScfException {
       
   200 	if (isInRepo()) {
       
   201 	    smfProperty.removeFromRepo();
       
   202 	}
       
   203     }
       
   204 
       
   205     public void saveToRepo() throws ScfException {
       
   206 	ApacheUtil.saveToRepo(getService(),
       
   207 	    new ScfRunnable() {
       
   208 		@Override
       
   209 		public void run() throws ScfException {
       
   210 		    if (smfProperty.isChanged()) {
       
   211 			smfProperty.saveToRepo();
       
   212 		    }
       
   213 		}
       
   214 	    });
       
   215     }
       
   216 
       
   217     //
       
   218     // Static methods
       
   219     //
       
   220 
       
   221     public static boolean isValidName(String name) {
       
   222 	return !name.isEmpty() && !name.matches(".*\\s.*");
       
   223     }
       
   224 }