components/visual-panels/firewall/src/java/vpanels/app/firewall/com/oracle/solaris/vp/panels/firewall/client/swing/TablePanelPropertySynchronizer.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.panels.firewall.client.swing;
       
    27 
       
    28 import java.util.*;
       
    29 import javax.swing.event.*;
       
    30 import com.oracle.solaris.vp.util.misc.property.*;
       
    31 import com.oracle.solaris.vp.util.swing.*;
       
    32 
       
    33 /**
       
    34  * The {@code TablePanelPropertySynchronizer} class synchronizes a
       
    35  * {@link MutableProperty} with a {@code TablePanel} so that changes in
       
    36  * one will automatically be reflected in the other.
       
    37  */
       
    38 public class TablePanelPropertySynchronizer<T>
       
    39     extends PropertySynchronizer<List<T>, TablePanel> {
       
    40 
       
    41     //
       
    42     // Instance data
       
    43     //
       
    44 
       
    45     private TableModelListener listener =
       
    46 	new TableModelListener() {
       
    47 	    @Override
       
    48 	    public void tableChanged(TableModelEvent e) {
       
    49 		objectChanged();
       
    50 	    }
       
    51 	};
       
    52 
       
    53     //
       
    54     // Constructors
       
    55     //
       
    56 
       
    57     public TablePanelPropertySynchronizer(
       
    58 	MutableProperty<List<T>> property, TablePanel panel,
       
    59 	boolean initFromProp) {
       
    60 
       
    61 	super(property, panel, initFromProp);
       
    62 	panel.getTable().getModel().addTableModelListener(listener);
       
    63     }
       
    64 
       
    65     /**
       
    66      * Constructs a {@code EditableTablePanelPropertySynchronizer} with initial
       
    67      * synchronization from the property to the {@link EditableTablePanel}.
       
    68      */
       
    69     public TablePanelPropertySynchronizer(
       
    70 	MutableProperty<List<T>> property, TablePanel panel) {
       
    71 
       
    72 	this(property, panel, true);
       
    73     }
       
    74 
       
    75     //
       
    76     // PropertySynchronizer methods
       
    77     //
       
    78 
       
    79     @Override
       
    80     public void desynchronize() {
       
    81 	super.desynchronize();
       
    82 	getObject().getTable().getModel().removeTableModelListener(listener);
       
    83     }
       
    84 
       
    85     @Override
       
    86     public List<T> getValue() {
       
    87 	OpenPortsTableModel model =
       
    88 	    (OpenPortsTableModel)getObject().getTable().getModel();
       
    89 
       
    90 	int nRows = model.getRowCount();
       
    91 	List<T> values = new ArrayList<T>(nRows);
       
    92 
       
    93 	for (int row = 0; row < nRows; row++) {
       
    94 	    @SuppressWarnings({"unchecked"})
       
    95 	    T value = (T)model.getValueAt(row);
       
    96 	    values.add(value);
       
    97 	}
       
    98 
       
    99 	return values;
       
   100     }
       
   101 
       
   102     @Override
       
   103     public void setValue(List<T> values) {
       
   104 	OpenPortsTableModel model =
       
   105 	    (OpenPortsTableModel)getObject().getTable().getModel();
       
   106 
       
   107 	if (values == null) {
       
   108 	    values = Collections.emptyList();
       
   109 	}
       
   110 
       
   111 	int nVals = values.size();
       
   112 	int nRows = model.getRowCount();
       
   113 
       
   114 	int row = 0;
       
   115 	for (Object value : values) {
       
   116 	    if (row >= nRows) {
       
   117 		if (model.attemptAddRow(value)) {
       
   118 		    nRows++;
       
   119 		    row++;
       
   120 		}
       
   121 	    } else {
       
   122 		model.setValueAt(value, row++);
       
   123 	    }
       
   124 	}
       
   125 
       
   126 	if (nRows > nVals) {
       
   127 	    for (row = nRows - 1; row >= nVals; row--) {
       
   128 		model.removeRow(row);
       
   129 	    }
       
   130 	}
       
   131     }
       
   132 }