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 javax.swing.*;
  31 import javax.swing.table.*;
  32 import java.util.*;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import java.net.*;
  36 
  37 import com.sun.dhcpmgr.data.*;
  38 import com.sun.dhcpmgr.ui.*;
  39 import com.sun.dhcpmgr.server.DhcpNetMgr;
  40 
  41 /**
  42  * This dialog allows the user to modify multiple addresses
  43  */
  44 public class ModifyAddressesDialog extends MultipleOperationDialog {
  45     private DhcpClientRecord [] recs;
  46     private String table;
  47     private JLabel numberLabel;
  48     private JComboBox server;
  49     private JTextField comment;
  50     private JComboBox macro;
  51     private JRadioButton bootpCurrent, bootpAll, bootpNone;
  52     private JRadioButton unusableCurrent, unusableAll, unusableNone;
  53     private JRadioButton leaseCurrent, leaseDynamic, leasePermanent;
  54     private static final String keepString =
  55         ResourceStrings.getString("modify_multiple_keep");
  56     
  57     class ServerListModel extends AbstractListModel implements ComboBoxModel {
  58         private String [] servers;
  59         private Object currentValue;
  60         
  61         public ServerListModel() {
  62             servers = new String[2];
  63             servers[0] = keepString;
  64             servers[1] = DataManager.get().getShortServerName();
  65         }
  66         
  67         public int getSize() {
  68             return servers.length;
  69         }
  70         
  71         public Object getElementAt(int index) {
  72             return servers[index];
  73         }
  74         
  75         public void setSelectedItem(Object anItem) {
  76             currentValue = anItem;
  77             fireContentsChanged(this, -1, -1);
  78         }
  79         
  80         public Object getSelectedItem() {
  81             return currentValue;
  82         }
  83     }
  84     
  85     class MacroListModel extends AbstractListModel implements ComboBoxModel {
  86         private String [] macros;
  87         private Object currentValue;
  88         
  89         public MacroListModel() {
  90             Macro [] macs = new Macro[0];
  91             try {
  92                 macs = DataManager.get().getMacros(false);
  93                 macros = new String[macs.length + 1];
  94             } catch (Throwable e) {
  95                 macros = new String[1];
  96             }
  97             macros[0] = keepString;
  98             for (int i = 0; i < macs.length; ++i) {
  99                 macros[i+1] = macs[i].getKey();
 100             }
 101         }
 102         
 103         public int getSize() {
 104             return macros.length;
 105         }
 106         
 107         public Object getElementAt(int index) {
 108             return macros[index];
 109         }
 110         
 111         public void setSelectedItem(Object anItem) {
 112             currentValue = anItem;
 113             fireContentsChanged(this, -1, -1);
 114         }
 115         
 116         public Object getSelectedItem() {
 117             return currentValue;
 118         }
 119     }
 120     
 121     public ModifyAddressesDialog(Frame f, DhcpClientRecord [] clients,
 122             String table) {
 123         // Create dialog with reset button
 124         super(f, true);
 125 
 126         this.table = table;
 127         recs = clients;
 128         numberLabel.setText(String.valueOf(recs.length));
 129     }
 130 
 131     public String getTitle() {
 132         return ResourceStrings.getString("modify_multiple_addresses");
 133     }
 134 
 135     protected JPanel getMainPanel() {
 136         JPanel mainPanel = new JPanel();
 137         mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
 138         GridBagLayout bag = new GridBagLayout();
 139         mainPanel.setLayout(bag);
 140         GridBagConstraints con = new GridBagConstraints();
 141         con.gridx = con.gridy = 0;
 142         con.weightx = con.weighty = 1.0;
 143         con.insets = new Insets(5, 5, 5, 5);
 144         con.gridwidth = con.gridheight = 1;
 145         
 146         // Number of addresses
 147         Mnemonic mnAddrSel =
 148             new Mnemonic(ResourceStrings.getString("modify_multiple_number"));
 149         JLabel label = new JLabel(mnAddrSel.getString());
 150         label.setLabelFor(mainPanel);
 151         label.setDisplayedMnemonic(mnAddrSel.getMnemonic());
 152         label.setToolTipText(mnAddrSel.getString());
 153 
 154         con.anchor = GridBagConstraints.EAST;
 155         bag.setConstraints(label, con);
 156         mainPanel.add(label);
 157 
 158         numberLabel = new JLabel("100");
 159         numberLabel.setForeground(Color.black);
 160         ++con.gridx;
 161         con.anchor = GridBagConstraints.WEST;
 162         bag.setConstraints(numberLabel, con);
 163         mainPanel.add(numberLabel);
 164         
 165         // Server
 166         Mnemonic mnManServ =
 167             new Mnemonic(ResourceStrings.getString("modify_multiple_server"));
 168         label = new JLabel(mnManServ.getString());
 169         label.setDisplayedMnemonic(mnManServ.getMnemonic());          
 170         label.setToolTipText(mnManServ.getString());  
 171         con.anchor = GridBagConstraints.EAST;
 172         con.gridx = 0;
 173         con.gridy += 2;
 174         bag.setConstraints(label, con);
 175         mainPanel.add(label);
 176         
 177         server = new JComboBox(new ServerListModel());
 178         label.setLabelFor(server);
 179         server.setEditable(true);
 180         con.anchor = GridBagConstraints.WEST;
 181         ++con.gridx;
 182         bag.setConstraints(server, con);
 183         mainPanel.add(server);
 184         
 185         // Comment
 186         Mnemonic mnComm =
 187             new Mnemonic(ResourceStrings.getString("modify_multiple_comment"));
 188         label = new JLabel(mnComm.getString());
 189         label.setDisplayedMnemonic(mnComm.getMnemonic());          
 190         label.setToolTipText(mnComm.getString());  
 191         con.anchor = GridBagConstraints.EAST;
 192         con.gridx = 0;
 193         ++con.gridy;
 194         bag.setConstraints(label, con);
 195         mainPanel.add(label);
 196 
 197         comment = new JTextField(20);
 198         label.setLabelFor(comment);
 199         con.anchor = GridBagConstraints.WEST;
 200         ++con.gridx;
 201         bag.setConstraints(comment, con);
 202         mainPanel.add(comment);
 203         
 204         // Macro
 205         Mnemonic mnMac =
 206             new Mnemonic(ResourceStrings.getString("modify_multiple_macro"));
 207         label = new JLabel(mnMac.getString());
 208         label.setDisplayedMnemonic(mnMac.getMnemonic());
 209         label.setToolTipText(mnMac.getString());
 210         con.anchor = GridBagConstraints.EAST;
 211         con.gridx = 0;
 212         ++con.gridy;
 213         bag.setConstraints(label, con);
 214         mainPanel.add(label);
 215 
 216         macro = new JComboBox(new MacroListModel());
 217         label.setLabelFor(macro);
 218         con.anchor = GridBagConstraints.WEST;
 219         ++con.gridx;
 220         bag.setConstraints(macro, con);
 221         mainPanel.add(macro);
 222         
 223         // BootP
 224         Mnemonic mnBootP =
 225             new Mnemonic(ResourceStrings.getString("modify_multiple_bootp"));
 226         label = new JLabel(mnBootP.getString());
 227         label.setDisplayedMnemonic(mnBootP.getMnemonic());
 228         label.setToolTipText(mnBootP.getString());
 229         con.anchor = GridBagConstraints.EAST;
 230         con.gridx = 0;
 231         con.gridy += 2;
 232         bag.setConstraints(label, con);
 233         mainPanel.add(label);
 234 
 235         ButtonGroup bootpGroup = new ButtonGroup();
 236         bootpCurrent = new JRadioButton(keepString);
 237         label.setLabelFor(bootpCurrent);
 238         bootpCurrent.setToolTipText(keepString);
 239         bootpGroup.add(bootpCurrent);
 240         con.anchor = GridBagConstraints.WEST;
 241         ++con.gridx;
 242         bag.setConstraints(bootpCurrent, con);
 243         mainPanel.add(bootpCurrent);
 244         
 245         bootpAll = new JRadioButton(
 246             ResourceStrings.getString("modify_multiple_bootp_all"));
 247         bootpAll.setToolTipText(
 248             ResourceStrings.getString("modify_multiple_bootp_all"));
 249         bootpGroup.add(bootpAll);
 250         ++con.gridy;
 251         bag.setConstraints(bootpAll, con);
 252         mainPanel.add(bootpAll);
 253         
 254         bootpNone = new JRadioButton(
 255             ResourceStrings.getString("modify_multiple_bootp_none"));
 256         bootpNone.setToolTipText(
 257             ResourceStrings.getString("modify_multiple_bootp_none"));
 258         bootpGroup.add(bootpNone);
 259         ++con.gridy;
 260         bag.setConstraints(bootpNone, con);
 261         mainPanel.add(bootpNone);
 262         
 263         // Unusable
 264         Mnemonic mnUnusable =
 265             new Mnemonic(ResourceStrings.getString("modify_multiple_unusable"));
 266         label = new JLabel(mnUnusable.getString());
 267         label.setDisplayedMnemonic(mnUnusable.getMnemonic());
 268         label.setToolTipText(mnUnusable.getString());
 269         con.anchor = GridBagConstraints.EAST;
 270         con.gridx = 0;
 271         con.gridy += 2;
 272         bag.setConstraints(label, con);
 273         mainPanel.add(label);
 274 
 275         ButtonGroup unusableGroup = new ButtonGroup();
 276         unusableCurrent = new JRadioButton(keepString);
 277         label.setLabelFor(unusableCurrent);
 278         unusableCurrent.setToolTipText(keepString);
 279         unusableGroup.add(unusableCurrent);
 280         con.anchor = GridBagConstraints.WEST;
 281         ++con.gridx;
 282         bag.setConstraints(unusableCurrent, con);
 283         mainPanel.add(unusableCurrent);
 284         
 285         unusableAll = new JRadioButton(
 286             ResourceStrings.getString("modify_multiple_unusable_all"));
 287         unusableAll.setToolTipText(
 288             ResourceStrings.getString("modify_multiple_unusable_all"));
 289         unusableGroup.add(unusableAll);
 290         ++con.gridy;
 291         bag.setConstraints(unusableAll, con);
 292         mainPanel.add(unusableAll);
 293         
 294         unusableNone = new JRadioButton(
 295             ResourceStrings.getString("modify_multiple_unusable_none"));
 296         unusableNone.setToolTipText(
 297             ResourceStrings.getString("modify_multiple_unusable_none"));
 298         unusableGroup.add(unusableNone);
 299         ++con.gridy;
 300         bag.setConstraints(unusableNone, con);
 301         mainPanel.add(unusableNone);
 302         
 303         // Lease
 304         Mnemonic mnLease =
 305             new Mnemonic(ResourceStrings.getString("modify_multiple_lease"));
 306         label = new JLabel(mnLease.getString());
 307         label.setDisplayedMnemonic(mnLease.getMnemonic());
 308         label.setToolTipText(mnLease.getString());
 309         con.anchor = GridBagConstraints.EAST;
 310         con.gridx = 0;
 311         con.gridy += 2;
 312         bag.setConstraints(label, con);
 313         mainPanel.add(label);
 314 
 315         ButtonGroup leaseGroup = new ButtonGroup();
 316         leaseCurrent = new JRadioButton(keepString);
 317         label.setLabelFor(leaseCurrent);
 318         leaseCurrent.setToolTipText(keepString);
 319         leaseGroup.add(leaseCurrent);
 320         con.anchor = GridBagConstraints.WEST;
 321         ++con.gridx;
 322         bag.setConstraints(leaseCurrent, con);
 323         mainPanel.add(leaseCurrent);
 324         
 325         leaseDynamic = new JRadioButton(
 326             ResourceStrings.getString("modify_multiple_dynamic"));
 327         leaseDynamic.setToolTipText(
 328             ResourceStrings.getString("modify_multiple_dynamic"));
 329         leaseGroup.add(leaseDynamic);
 330         ++con.gridy;
 331         bag.setConstraints(leaseDynamic, con);
 332         mainPanel.add(leaseDynamic);
 333         
 334         leasePermanent = new JRadioButton(
 335             ResourceStrings.getString("modify_multiple_permanent"));
 336         leasePermanent.setToolTipText(
 337             ResourceStrings.getString("modify_multiple_permanent"));
 338         leaseGroup.add(leasePermanent);
 339         ++con.gridy;
 340         bag.setConstraints(leasePermanent, con);
 341         mainPanel.add(leasePermanent);
 342         
 343         buttonPanel.setOkEnabled(true);
 344         doReset();
 345 
 346         return mainPanel;
 347     }
 348     
 349     protected void doReset() {
 350         server.setSelectedIndex(0);
 351         comment.setText(keepString);
 352         macro.setSelectedIndex(0);
 353         bootpCurrent.setSelected(true);
 354         unusableCurrent.setSelected(true);
 355         leaseCurrent.setSelected(true);
 356     }
 357     
 358     protected String getProgressMessage() {
 359         return ResourceStrings.getString("modify_multiple_progress");
 360     }
 361 
 362     protected int getProgressLength() {
 363         return recs.length;
 364     }
 365 
 366     protected String getErrorHeading() {
 367         return ResourceStrings.getString("address_column");
 368     }
 369 
 370     protected Class getErrorClass() {
 371         return IPAddress.class;
 372     }
 373 
 374     protected Thread getOperationThread() {
 375         return new Thread() {
 376             public void run() {
 377                 DhcpNetMgr netMgr = DataManager.get().getDhcpNetMgr();
 378                 for (int i = 0; i < recs.length; ++i) {
 379                     DhcpClientRecord client = (DhcpClientRecord)recs[i].clone();
 380                     try {
 381                         String s = (String)server.getSelectedItem();
 382                         if (!s.equals(keepString)) {
 383                             // Change server if necessary
 384                             client.setServerIP(new IPAddress(s));
 385                         }
 386                         s = comment.getText();
 387                         if (!s.equals(keepString)) {
 388                             client.setComment(s);
 389                         }
 390                         if (macro.getSelectedIndex() != 0) {
 391                             client.setMacro((String)macro.getSelectedItem());
 392                         }
 393                         if (bootpAll.isSelected()) {
 394                             client.setBootp(true);
 395                         } else if (bootpNone.isSelected()) {
 396                             client.setBootp(false);
 397                         }
 398                         if (unusableAll.isSelected()) {
 399                             client.setUnusable(true);
 400                         } else if (unusableNone.isSelected()) {
 401                             client.setUnusable(false);
 402                         }
 403                         if (leaseDynamic.isSelected()) {
 404                             client.setPermanent(false);
 405                         } else if (leasePermanent.isSelected()) {
 406                             client.setPermanent(true);
 407                         }
 408                         netMgr.modifyClient(recs[i], client, table);
 409                         updateProgress(i+1, client.getClientIPAddress());
 410                     } catch (InterruptedException e) {
 411                         closeDialog();
 412                         return;
 413                     } catch (Throwable e) {
 414                         addError(recs[i].getClientIP(), e.getMessage());
 415                     }
 416                 }
 417                 // Errors occurred, display them
 418                 if (errorsOccurred()) {
 419                     displayErrors(
 420                         ResourceStrings.getString("modify_multiple_error"));
 421                 }
 422                 closeDialog();
 423             }
 424         };
 425     }
 426     
 427     protected String getHelpKey() {
 428         return "modify_multiple_addresses";
 429     }
 430     
 431     protected void fireActionPerformed() {
 432         fireActionPerformed(this, DialogActions.OK);
 433     }
 434 }