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, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * ident        "%Z%%M% %I%     %E% SMI"
  24  *
  25  * Copyright 1998-2002 by Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 package com.sun.dhcpmgr.client;
  29 
  30 import java.awt.*;
  31 import java.awt.event.*;
  32 import java.text.*;
  33 import java.util.*;
  34 import javax.swing.*;
  35 import javax.swing.table.*;
  36 
  37 import com.sun.dhcpmgr.server.*;
  38 import com.sun.dhcpmgr.data.*;
  39 import com.sun.dhcpmgr.ui.*;
  40 
  41 /**
  42  * This dialog is used to release one or more addresses.  Release is defined
  43  * to mean resetting the client id to 00 and setting the lease date to 0.
  44  */
  45 public class ReleaseAddressDialog extends MultipleOperationDialog {
  46     private DhcpClientRecord [] recs;
  47     private String table;
  48     boolean showAddresses;
  49     
  50     class AddressTableModel extends AbstractTableModel {
  51 
  52         public int getRowCount() {
  53             if (recs == null) {
  54                 return 0;
  55             } else {
  56                 return recs.length;
  57             }
  58         }
  59         
  60         public int getColumnCount() {
  61             return 3;
  62         }
  63         
  64         public Object getValueAt(int row, int column) {
  65             switch (column) {
  66             case 0:
  67                 if (showAddresses) {
  68                     return recs[row].getClientIP();
  69                 } else {
  70                     return recs[row].getClientName();
  71                 }
  72             case 1:
  73                 return recs[row].getClientId();
  74             case 2:
  75                 return recs[row].getExpiration();
  76             default:
  77                 return null;
  78             }
  79         }
  80             
  81         public Class getColumnClass(int column) {
  82             switch (column) {
  83             case 0:
  84                 if (showAddresses) {
  85                     return IPAddress.class;
  86                 } else {
  87                     return String.class;
  88                 }
  89             case 1:
  90                 return String.class;
  91             case 2:
  92                 return Date.class;
  93             default:
  94                 return Object.class;
  95             }
  96         }
  97 
  98         public String getColumnName(int column) {
  99             switch (column) {
 100             case 0:
 101                 if (showAddresses) {
 102                     return ResourceStrings.getString("address_column");
 103                 } else {
 104                     return ResourceStrings.getString("client_name_column");
 105                 }
 106             case 1:
 107                 return ResourceStrings.getString("client_column");
 108             case 2:
 109                 return ResourceStrings.getString("expires_column");
 110             default:
 111                 return null;
 112             }
 113         }
 114     }    
 115     
 116     public ReleaseAddressDialog(Frame f, DhcpClientRecord [] clients,
 117             String table, boolean showAddresses) {
 118         super(f, false);
 119         recs = clients;
 120         this.table = table;
 121         this.showAddresses = showAddresses;
 122     }
 123     
 124     public String getTitle() {
 125         return ResourceStrings.getString("release_address_title");
 126     }
 127 
 128     protected JPanel getMainPanel() {
 129         JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
 130         mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
 131         
 132         JLabel message = new JLabel(
 133             ResourceStrings.getString("release_address_confirm"));
 134 
 135         message.setLabelFor(mainPanel);
 136         message.setToolTipText(
 137             ResourceStrings.getString("release_address_confirm"));
 138 
 139         mainPanel.add(message, BorderLayout.NORTH);
 140         
 141         JTable addressTable = new JTable(new AddressTableModel());
 142         JScrollPane scrollPane = new JScrollPane(addressTable);
 143         Dimension d = addressTable.getPreferredScrollableViewportSize();
 144         d.height = 100;
 145         addressTable.setPreferredScrollableViewportSize(d);
 146         ExtendedCellRenderer renderer = new ExtendedCellRenderer();
 147         addressTable.setDefaultRenderer(Date.class, renderer);
 148         addressTable.setDefaultRenderer(IPAddress.class, renderer);
 149         
 150         mainPanel.add(scrollPane, BorderLayout.CENTER);
 151         buttonPanel.setOkEnabled(true);
 152         return mainPanel;
 153     }
 154 
 155     protected String getProgressMessage() {
 156         return ResourceStrings.getString("release_addr_progress");
 157     }
 158 
 159     protected int getProgressLength() {
 160         return recs.length;
 161     }
 162 
 163     protected String getErrorHeading() {
 164         return ResourceStrings.getString("address_column");
 165     }
 166 
 167     protected Class getErrorClass() {
 168         return IPAddress.class;
 169     }
 170 
 171     protected Thread getOperationThread() {
 172         return new Thread() {
 173             public void run() {
 174                 DhcpNetMgr server = DataManager.get().getDhcpNetMgr();
 175                 for (int i = 0; i < recs.length; ++i) {
 176                     DhcpClientRecord client = (DhcpClientRecord)recs[i].clone();
 177                     Date emptyDate = new Date(0);
 178                     try {
 179                         // Clear client id and lease date
 180                         client.setClientId(DhcpClientRecord.DEFAULT_CLIENT_ID);
 181                         client.setFlags(DhcpClientRecord.DEFAULT_FLAGS);
 182                         client.setExpiration(emptyDate);
 183                         server.modifyClient(recs[i], client, table);
 184                         // Update progress meter
 185                         updateProgress(i+1, recs[i].getClientIPAddress());
 186                     } catch (InterruptedException e) {
 187                         // User asked to stop
 188                         closeDialog();
 189                         return;
 190                     } catch (Throwable e) {
 191                         addError(recs[i].getClientIP(), e.getMessage());
 192                     }
 193                 }
 194                 // Errors occurred, display them now
 195                 if (errorsOccurred()) {
 196                     displayErrors(
 197                         ResourceStrings.getString("release_address_error"));
 198                 }
 199                 closeDialog();
 200             }
 201         };
 202     }
 203     
 204     protected String getHelpKey() {
 205         return "release_addresses";
 206     }
 207 
 208     protected void fireActionPerformed() {
 209         fireActionPerformed(this, DialogActions.OK);
 210     }
 211 }