components/visual-panels/firewall/src/java/vpanels/app/firewall/com/oracle/solaris/vp/panels/firewall/client/swing/OpenPortsTableModel.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.Vector;
       
    29 import javax.swing.table.DefaultTableModel;
       
    30 
       
    31 @SuppressWarnings({"serial"})
       
    32 public class OpenPortsTableModel extends DefaultTableModel {
       
    33 
       
    34     private static final String TCP_PREFIX = "tcp:";
       
    35     private static final String UDP_PREFIX = "udp:";
       
    36     private static final String TCP_UDP_PREFIX = ":";
       
    37 
       
    38     //
       
    39     // Constructors
       
    40     //
       
    41 
       
    42     public OpenPortsTableModel() {
       
    43 	this(new String[0]);
       
    44     }
       
    45 
       
    46     public OpenPortsTableModel(Object[] values, String[] columns) {
       
    47 	for (String col : columns) {
       
    48 	    addColumn(col);
       
    49 	}
       
    50 
       
    51 	for (Object v : values) {
       
    52 	    String data[] = ((String) v).split("\\:");
       
    53 	    addRow(data);
       
    54 	}
       
    55     }
       
    56 
       
    57     public OpenPortsTableModel(Object[] values) {
       
    58 	this(values, new String[] {"", "", ""});
       
    59     }
       
    60 
       
    61     //
       
    62     // TableModel methods
       
    63     //
       
    64 
       
    65     @Override
       
    66     public Class<?> getColumnClass(int columnIndex) {
       
    67 	return (columnIndex <= 1 ? Boolean.class : String.class);
       
    68     }
       
    69 
       
    70     //
       
    71     // DefaultTableModel methods
       
    72     //
       
    73 
       
    74     @Override
       
    75     public boolean isCellEditable(int row, int column) {
       
    76 	return false;
       
    77     }
       
    78 
       
    79     //
       
    80     // OpenPortsTableModel methods
       
    81     //
       
    82 
       
    83     public void setValueAt(Object aValue, int row) {
       
    84 	Object vals[] = valueToArray(aValue);
       
    85 	super.setValueAt(vals[0], row, 0);
       
    86 	super.setValueAt(vals[1], row, 1);
       
    87 	super.setValueAt(vals[2], row, 2);
       
    88     }
       
    89 
       
    90     //
       
    91     // Transform a {proto}:port tuple to a three columns array of data
       
    92     //
       
    93     public Object[] valueToArray(Object value) {
       
    94 	int idx = ((String) value).indexOf(":");
       
    95 
       
    96 	Object values[];
       
    97 	if (idx == -1) {
       
    98 	    //
       
    99 	    // This is an error case
       
   100 	    //
       
   101 	    values = new Object[] { new Boolean("false"),
       
   102 		new Boolean("false"), ((String) value)};
       
   103 	} else if (idx == 0) {
       
   104 	    values = new Object[] { new Boolean("true"),
       
   105 		new Boolean("true"), ((String) value).substring(idx + 1)};
       
   106 	} else {
       
   107 	    if (((String)value).startsWith(TCP_PREFIX))
       
   108 		values = new Object[] {new Boolean("true"),
       
   109 		    new Boolean("false"), ((String) value).substring(idx + 1)};
       
   110 	    else
       
   111 		values = new Object[] {new Boolean("false"),
       
   112 		    new Boolean("true"), ((String) value).substring(idx + 1)};
       
   113 	}
       
   114 
       
   115 	return values;
       
   116     }
       
   117 
       
   118     //
       
   119     // Inverse-transformation a three columns array of data to a
       
   120     // {proto}:port tuple
       
   121     //
       
   122     public Object getValueAt(int row) {
       
   123 	Boolean tcp = ((Boolean) super.getValueAt(row, 0));
       
   124 	Boolean udp = ((Boolean) super.getValueAt(row, 1));
       
   125 	String port = ((String) super.getValueAt(row, 2));
       
   126 
       
   127 	if (tcp == null || udp == null)
       
   128 	    return null;
       
   129 
       
   130 	// This is an error case
       
   131 	if (tcp.booleanValue() == false && udp.booleanValue() == false)
       
   132 	    return (port);
       
   133 
       
   134 	if (tcp.booleanValue() == true && udp.booleanValue() == true)
       
   135 	    return TCP_UDP_PREFIX.concat(port);
       
   136 
       
   137 	return (tcp.booleanValue() == true ? TCP_PREFIX.concat(port) :
       
   138 	    UDP_PREFIX.concat(port));
       
   139     }
       
   140 
       
   141     public boolean attemptAddRow(Object rowData) {
       
   142 	Object vals[] = valueToArray(rowData);
       
   143 	addRow(vals);
       
   144 	return true;
       
   145     }
       
   146 
       
   147     public boolean attemptInsertRow(int row, Object rowData) {
       
   148 	Object vals[] = valueToArray(rowData);
       
   149 	insertRow(row, vals);
       
   150 	return true;
       
   151     }
       
   152 
       
   153     public Object[] getTemplateRow() {
       
   154 	Object[] row = new Object[getColumnCount()];
       
   155 	for (int col = 0; col < row.length; col++) {
       
   156 	    try {
       
   157 		row[col] = getColumnClass(col).newInstance();
       
   158 	    } catch (Throwable e) {
       
   159 		row[col] = null;
       
   160 	    }
       
   161 	}
       
   162 	return row;
       
   163     }
       
   164 }