components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/swing/action/SwingManagedObjectAction.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     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.swing.action;
       
    27 
       
    28 import java.beans.*;
       
    29 import java.util.*;
       
    30 import javax.swing.Icon;
       
    31 import com.oracle.solaris.vp.panel.common.ClientContext;
       
    32 import com.oracle.solaris.vp.panel.common.model.ManagedObject;
       
    33 import com.oracle.solaris.vp.panel.swing.control.SwingControl;
       
    34 import com.oracle.solaris.vp.util.misc.ObjectUtil;
       
    35 import com.oracle.solaris.vp.util.swing.HasComponent;
       
    36 
       
    37 @SuppressWarnings({"serial"})
       
    38 public class SwingManagedObjectAction<S extends ManagedObject, I, O>
       
    39     extends SwingStructuredAction<List<S>, I, O> {
       
    40 
       
    41     //
       
    42     // Instance data
       
    43     //
       
    44 
       
    45     private PropertyChangeListener refreshListener =
       
    46 	new PropertyChangeListener() {
       
    47 	    @Override
       
    48 	    public void propertyChange(PropertyChangeEvent e) {
       
    49 		refresh();
       
    50 	    }
       
    51 	};
       
    52 
       
    53     //
       
    54     // Constructors
       
    55     //
       
    56 
       
    57     @SuppressWarnings({"unchecked"})
       
    58     public SwingManagedObjectAction(String text,
       
    59 	Collection<? extends Icon> icons, ClientContext context,
       
    60 	HasComponent hasComponent) {
       
    61 
       
    62 	super(text, (List<S>)(List)Collections.emptyList(), icons, context,
       
    63 	    hasComponent);
       
    64     }
       
    65 
       
    66     @SuppressWarnings({"unchecked"})
       
    67     public SwingManagedObjectAction(String text,
       
    68 	Collection<? extends Icon> icons, SwingControl control) {
       
    69 
       
    70 	super(text, (List<S>)(List)Collections.emptyList(), icons, control);
       
    71     }
       
    72 
       
    73     //
       
    74     // StructuredAction methods
       
    75     //
       
    76 
       
    77     @Override
       
    78     public void setPresetInput(List<S> selection) {
       
    79 	List<S> oldSelection = getPresetInput();
       
    80         if (!ObjectUtil.equals(oldSelection, selection)) {
       
    81 	    if (oldSelection != null) {
       
    82 		for (S object : oldSelection) {
       
    83 		    object.removePropertyChangeListener(refreshListener);
       
    84 		}
       
    85 	    }
       
    86 
       
    87 	    if (selection != null) {
       
    88 		for (S object : selection) {
       
    89 		    object.addPropertyChangeListener(refreshListener);
       
    90 		}
       
    91             }
       
    92 
       
    93             super.setPresetInput(selection);
       
    94         }
       
    95     }
       
    96 }