components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/swing/view/ObjectsView.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.view;
       
    27 
       
    28 import java.awt.Component;
       
    29 import java.beans.*;
       
    30 import java.util.List;
       
    31 import javax.swing.event.*;
       
    32 import com.oracle.solaris.vp.panel.common.action.StructuredAction;
       
    33 import com.oracle.solaris.vp.panel.common.model.ManagedObject;
       
    34 import com.oracle.solaris.vp.util.misc.HasId;
       
    35 import com.oracle.solaris.vp.util.misc.HasName;
       
    36 import com.oracle.solaris.vp.util.misc.event.PropertyChangeListeners;
       
    37 
       
    38 /**
       
    39  * The {@code ObjectsView} class represents a unique view of a {@link
       
    40  * ManagedObject}.  Most implementations will display children or other
       
    41  * descendants thereof.
       
    42  */
       
    43 public abstract class ObjectsView<C extends ManagedObject>
       
    44     implements HasId, HasName {
       
    45 
       
    46     //
       
    47     // Static data
       
    48     //
       
    49 
       
    50     public static final String PROPERTY_OBJECT_COUNT = "objectcount";
       
    51     public static final String PROPERTY_SELECTION = "selection";
       
    52 
       
    53     //
       
    54     // Instance data
       
    55     //
       
    56 
       
    57     private ManagedObject<C> pObject;
       
    58     private StructuredAction<List<C>, ?, ?>[] mActions;
       
    59     private StructuredAction<List<C>, ?, ?> dmAction;
       
    60 
       
    61     private int count;
       
    62     private List<C> selection;
       
    63     private PropertyChangeListeners listeners = new PropertyChangeListeners();
       
    64 
       
    65     // Notify PropertyChangeListeners of changes in a list's selection model
       
    66     protected ListSelectionListener updateActionSelectionListener =
       
    67 	new ListSelectionListener() {
       
    68 	    @Override
       
    69 	    public void valueChanged(ListSelectionEvent e) {
       
    70 		if (!e.getValueIsAdjusting()) {
       
    71 		    fireSelectionChange();
       
    72 		}
       
    73 	    }
       
    74 	};
       
    75 
       
    76     //
       
    77     // Constructors
       
    78     //
       
    79 
       
    80     public ObjectsView(ManagedObject<C> pObject,
       
    81 	StructuredAction<List<C>, ?, ?>[] mActions,
       
    82 	StructuredAction<List<C>, ?, ?> dmAction) {
       
    83 
       
    84 	this.pObject = pObject;
       
    85 	this.mActions = mActions;
       
    86 	this.dmAction = dmAction;
       
    87     }
       
    88 
       
    89     //
       
    90     // Object methods
       
    91     //
       
    92 
       
    93     @Override
       
    94     public String toString() {
       
    95 	return getName();
       
    96     }
       
    97 
       
    98     //
       
    99     // ObjectsView methods
       
   100     //
       
   101 
       
   102     public void addPropertyChangeListener(PropertyChangeListener l) {
       
   103 	listeners.add(l);
       
   104     }
       
   105 
       
   106     /**
       
   107      * If the {@link #getObjectCount object count} has changed, creates and
       
   108      * fires a {@code PropertyChangeEvent} for the {@link
       
   109      * #PROPERTY_OBJECT_COUNT} property.
       
   110      */
       
   111     protected void fireObjectCountChange() {
       
   112 	int count = getObjectCount();
       
   113 	if (count != this.count) {
       
   114 	    PropertyChangeEvent e = new PropertyChangeEvent(
       
   115 		this, PROPERTY_OBJECT_COUNT, this.count, count);
       
   116 	    this.count = count;
       
   117 	    firePropertyChange(e);
       
   118 	}
       
   119     }
       
   120 
       
   121     protected void firePropertyChange(PropertyChangeEvent e) {
       
   122 	listeners.propertyChange(e);
       
   123     }
       
   124 
       
   125     /**
       
   126      * If the {@link #getSelection object selection} has changed, creates and
       
   127      * fires a {@code PropertyChangeEvent} for the {@link #PROPERTY_SELECTION}
       
   128      * property.
       
   129      */
       
   130     protected void fireSelectionChange() {
       
   131 	List<C> selection = getSelection();
       
   132 	if (!selection.equals(this.selection)) {
       
   133 	    PropertyChangeEvent e = new PropertyChangeEvent(
       
   134 		this, PROPERTY_SELECTION, this.selection, selection);
       
   135 	    this.selection = selection;
       
   136 	    firePropertyChange(e);
       
   137 	}
       
   138     }
       
   139 
       
   140     public StructuredAction<List<C>, ?, ?>[] getActions() {
       
   141 	return mActions;
       
   142     }
       
   143 
       
   144     public abstract Component getComponent();
       
   145 
       
   146     public StructuredAction<List<C>, ?, ?> getDefaultAction() {
       
   147 	return dmAction;
       
   148     }
       
   149 
       
   150     /**
       
   151      * Gets an identifier for this {@code ObjectsView}, sufficiently unique as
       
   152      * to distinguish itself from its peers.
       
   153      * <p>
       
   154      * This default implementation returns the class name.
       
   155      */
       
   156     public String getId() {
       
   157 	return getClass().getName();
       
   158     }
       
   159 
       
   160     public ManagedObject<C> getManagedObject() {
       
   161 	return pObject;
       
   162     }
       
   163 
       
   164     /**
       
   165      * Gets the selected {@code ManagedObject}s in this view.
       
   166      * <p>
       
   167      * Note: Implementations of this class <strong>must</strong> call {@link
       
   168      * #fireSelectionChange} whenever this value changes.
       
   169      */
       
   170     public abstract List<C> getSelection();
       
   171 
       
   172     /**
       
   173      * Gets the number of {@code ManagedObject}s displayed in this view.
       
   174      * <p>
       
   175      * Note: Implementations of this class <strong>must</strong> call {@link
       
   176      * #fireObjectCountChange} whenever this value changes.
       
   177      */
       
   178     public abstract int getObjectCount();
       
   179 
       
   180     public boolean removePropertyChangeListener(PropertyChangeListener l) {
       
   181 	return listeners.remove(l);
       
   182     }
       
   183 }