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  * Copyright 1998-2002 by Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 package com.sun.dhcpmgr.client;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.text.MessageFormat;
  31 import java.util.*;
  32 import javax.swing.*;
  33 import javax.swing.table.*;
  34 
  35 import com.sun.dhcpmgr.server.*;
  36 import com.sun.dhcpmgr.data.*;
  37 import com.sun.dhcpmgr.ui.*;
  38 import com.sun.dhcpmgr.bridge.BridgeException;
  39 
  40 /**
  41  * This dialog is used to delete one or more addresses from a network
  42  */
  43 
  44 public class DeleteAddressDialog extends MultipleOperationDialog {
  45     private DhcpClientRecord [] recs;
  46     private String table;
  47 
  48     // Model for the list of addresses to be deleted
  49     class AddressTableModel extends AbstractTableModel {
  50         public int getRowCount() {
  51             if (recs == null) {
  52                 return 0;
  53             } else {
  54                 return recs.length;
  55             }
  56         }
  57 
  58         public int getColumnCount() {
  59             return 2;
  60         }
  61 
  62         public Object getValueAt(int row, int column) {
  63             if (column == 0) {
  64                 return recs[row].getClientIP();
  65             } else {
  66                 if (recs[row].getClientName().equals(
  67                         recs[row].getClientIPAddress())) {
  68                     // Name returned is IP address, so there is no name
  69                     return "";
  70                 } else {
  71                     return recs[row].getClientName();
  72                 }
  73             }
  74         }
  75 
  76         public Class getColumnClass(int column) {
  77             if (column == 0) {
  78                 return IPAddress.class;
  79             } else {
  80                 return String.class;
  81             }
  82         }
  83 
  84         public String getColumnName(int column) {
  85             if (column == 0) {
  86                 return ResourceStrings.getString("address_column");
  87             } else {
  88                 return ResourceStrings.getString("client_name_column");
  89             }
  90         }
  91     }
  92 
  93     public DeleteAddressDialog(Frame f, DhcpClientRecord [] clients,
  94             String table) {
  95         // Create the dialog without a reset button
  96         super(f, false);
  97         recs = clients;
  98         this.table = table;
  99     }
 100 
 101     public String getTitle() {
 102         return ResourceStrings.getString("delete_address_title");
 103     }
 104 
 105     protected JPanel getMainPanel() {
 106         JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
 107         mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
 108 
 109         // Place a message at the top of the display
 110         JLabel message = new JLabel(
 111             ResourceStrings.getString("delete_address_confirm"));
 112 
 113         message.setLabelFor(mainPanel);
 114         message.setToolTipText(
 115             ResourceStrings.getString("delete_address_confirm"));
 116 
 117         mainPanel.add(message, BorderLayout.NORTH);
 118 
 119         // Now show the list of addresses to be deleted in a table in the middle
 120         JTable addressTable = new JTable(new AddressTableModel());
 121         JScrollPane scrollPane = new JScrollPane(addressTable);
 122         Dimension d = addressTable.getPreferredScrollableViewportSize();
 123         d.height = 100;
 124         addressTable.setPreferredScrollableViewportSize(d);
 125         addressTable.setDefaultRenderer(IPAddress.class,
 126             new ExtendedCellRenderer());
 127         mainPanel.add(scrollPane, BorderLayout.CENTER);
 128 
 129         buttonPanel.setOkEnabled(true);
 130         return mainPanel;
 131     }
 132 
 133     protected String getProgressMessage() {
 134         return ResourceStrings.getString("delete_addr_progress");
 135     }
 136 
 137     protected int getProgressLength() {
 138         return recs.length;
 139     }
 140 
 141     protected String getErrorHeading() {
 142         return ResourceStrings.getString("address_column");
 143     }
 144 
 145     protected Class getErrorClass() {
 146         return IPAddress.class;
 147     }
 148 
 149     protected Thread getOperationThread() {
 150         // Create the thread we'll use
 151         return new Thread() {
 152             public void run() {
 153                 DhcpNetMgr server = DataManager.get().getDhcpNetMgr();
 154                 for (int i = 0; i < recs.length; ++i) {
 155                     try {
 156                         server.deleteClient(recs[i], table);
 157                         updateProgress(i+1, recs[i].getClientIPAddress());
 158                     } catch (InterruptedException e) {
 159                         // User asked us to stop
 160                         closeDialog();
 161                         return;
 162                     } catch (Throwable e) {
 163                         addError(recs[i].getClientIP(), e.getMessage());
 164                     }
 165                 }
 166                 // Errors occurred, display them
 167                 if (errorsOccurred()) {
 168                     displayErrors(
 169                         ResourceStrings.getString("delete_address_error"));
 170                 }
 171                 closeDialog();
 172             }
 173         };
 174     }
 175 
 176     protected String getHelpKey() {
 177         return "delete_address";
 178     }
 179 
 180     protected void fireActionPerformed() {
 181         fireActionPerformed(this, DialogActions.DELETE);
 182     }
 183 }