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 (c) 2001 by Sun Microsystems, Inc.
  26  * All rights reserved.
  27  */
  28 package com.sun.dhcpmgr.client;
  29 
  30 import java.awt.*;
  31 import java.awt.event.*;
  32 import java.util.*;
  33 import java.text.MessageFormat;
  34 
  35 import javax.swing.*;
  36 
  37 import com.sun.dhcpmgr.data.DhcpDatastore;
  38 import com.sun.dhcpmgr.server.*;
  39 import com.sun.dhcpmgr.ui.*;
  40 
  41 /**
  42  * This class defines a Wizard that configures a data store.
  43  */
  44 public abstract class DSWizard extends Wizard implements DSModuleListener {
  45 
  46     /**
  47      * The collection of valid DSConf objects.
  48      */
  49     protected DSConfList dsconfList = null;
  50 
  51     /**
  52      * The selected DSConf
  53      */
  54     private DSConf dsconf = null;
  55 
  56     /**
  57      * This class is a holder for the set of DSConf objects.
  58      */
  59     protected class DSConfList extends ArrayList {
  60 
  61         /**
  62          * Initializes the set of supported DSConfList.
  63          * @param server handle to a service manager server
  64          */
  65         public void init(DhcpServiceMgr server) {
  66 
  67             DhcpDatastore [] dsArray = null;
  68 
  69             try {
  70                 dsArray = server.getDataStores();
  71             } catch (Throwable e) {
  72                 // ignore for now
  73             }
  74 
  75             for (int i = 0;
  76                 dsArray != null && i < dsArray.length; i++) {
  77                 String dsResource = dsArray[i].getResource();
  78                 try {
  79                     
  80                     String className = server.getDataStoreClassname(dsResource);
  81                     DSConf dsconf = new DSConf(dsArray[i], className);
  82                     dsconfList.add(dsconf);
  83                 } catch (Throwable e) {
  84                     MessageFormat form = new MessageFormat(
  85                             ResourceStrings.getString("ds_wiz_init_error"));
  86                     Object args = new Object[] {
  87                         dsResource,
  88                         e.getMessage()
  89                     };
  90                     JOptionPane.showMessageDialog(DSWizard.this,
  91                         form.format(args),
  92                         ResourceStrings.getString("server_error_title"),
  93                         JOptionPane.ERROR_MESSAGE);
  94                 }
  95             }
  96         } // init
  97 
  98         /**
  99          * Finds and returns a DSConf by resource.
 100          * @param resource the resource of the DSConf.
 101          * @return the DSConf if found or null if not found.
 102          */
 103         public DSConf findDsconf(String resource) {
 104 
 105             DSConf entry = null;
 106             for (int i = 0; i < size(); i++) {
 107                 DSConf dsconf = (DSConf) get(i);
 108                 if (dsconf.getDS().getResource().equals(resource)) {
 109                     entry = dsconf;
 110                     break;
 111                 }
 112             }
 113 
 114             return entry;
 115 
 116         } // findDSConf
 117 
 118     } // DSConfList
 119      
 120     /**
 121      * This class is a simple holder for a data store
 122      * and the module used to manage the data store.
 123      */
 124     protected class DSConf {
 125 
 126         /**
 127          * DHCP datastore information.
 128          */
 129         private DhcpDatastore ds = null;
 130 
 131         /**
 132          * The module used to manage the data store.
 133          */
 134         private DSModule dsm = null;
 135 
 136         /**
 137          * Constructs a DSConf from a name and a classname.
 138          * @param ds DHCP data store.
 139          * @param className of the DSModule classname.
 140          */
 141         public DSConf(DhcpDatastore ds, String className)
 142             throws Exception {
 143 
 144             Class dataStoreClass = Class.forName(className);
 145 
 146             dsm = (DSModule)dataStoreClass.newInstance();
 147             this.ds = ds;
 148         } // constructor
 149 
 150         /**
 151          * Returns the DhcpDatastore for this DSConf
 152          * @return the DhcpDatastore for this DSConf
 153          */
 154         public DhcpDatastore getDS() {
 155             return ds;
 156         } // getDS
 157 
 158         /**
 159          * Returns the module used to manage the data store.
 160          * @return the module used to manage the data store.
 161          */
 162         public DSModule getModule() {
 163             return dsm;
 164         } // getModule
 165 
 166 
 167         /**
 168          * Sets the location from the module into the DhcpDatastore.
 169          */
 170         public void setLocation() {
 171             ds.setLocation(dsm.getPath());
 172         } // setLocation
 173 
 174         /**
 175          * Sets the location from the module into the DhcpDatastore.
 176          */
 177         public void setConfig() {
 178             ds.setConfig(dsm.getAdditionalInfo());
 179         } // setConfig
 180 
 181     } // DSConf
 182 
 183     /**
 184      * This class maps a radio button and a DSConf.
 185      */
 186     private class DSConfButton extends JRadioButton {
 187 
 188         /**
 189          * The data store to link to the radio button.
 190          */
 191         DSConf dsconf = null;
 192 
 193         /**
 194          * Constructs a DSConfButton from a DSConf and determines
 195          * whether the button should be selected using the boolean argument.
 196          * @param dsconf the data store to map to the radio button.
 197          * @param selected select the radio button?
 198          */
 199         public DSConfButton(DSConf dsconf, boolean selected) {
 200             super(dsconf.getModule().getDescription(), selected);
 201             setEnabled(dsconf.getDS().isEnabled());
 202             this.dsconf = dsconf;
 203         } // constructor
 204 
 205         /**
 206          * Returns the DSConf mapped to the radio button.
 207          * @return the DSConf mapped to the radio button.
 208          */
 209         public DSConf getDsconf() {
 210             return dsconf;
 211         } // getDsconf
 212 
 213     } // DSConfButton
 214 
 215 
 216     /**
 217      * This class is the wizard step that presents the choice of
 218      * data stores to the user for selection.
 219      */
 220     protected class DatastoreStep implements WizardStep {
 221 
 222         /**
 223          * The component provided to the wizard.
 224          */
 225         private Box stepBox;
 226         
 227         /**
 228          * The group of DSConfButton objects.
 229          */
 230         private ButtonGroup buttonGroup;
 231 
 232         /**
 233          * The basic constructor for the wizard step.
 234          * @param wizardText the main explanatory text for the wizard.
 235          * @param stepText the explanatory text for the step.
 236          */
 237         public DatastoreStep(String wizardText, String stepText) {
 238 
 239             super();
 240 
 241             stepBox = Box.createVerticalBox();
 242 
 243             // Explanatory wizard intro text
 244             //
 245             JComponent c = Wizard.createTextArea(wizardText, 2, 45);
 246             c.setAlignmentX(Component.LEFT_ALIGNMENT);
 247             stepBox.add(c);
 248             stepBox.add(Box.createVerticalStrut(5));
 249             
 250             // Explanatory step text
 251             //
 252             c = Wizard.createTextArea(stepText, 3, 45);
 253             c.setAlignmentX(Component.LEFT_ALIGNMENT);
 254             stepBox.add(c);
 255             stepBox.add(Box.createVerticalStrut(5));
 256 
 257             // Create button listener, that will set the selected
 258             // data store when the button is selected.
 259             //
 260             ActionListener buttonListener = new ActionListener() {
 261                 public void actionPerformed(ActionEvent e) {
 262                     DSConfButton button = (DSConfButton)e.getSource();
 263                     if (button.isSelected()) {
 264                         setDsconf(button.getDsconf());
 265                     }
 266                 }
 267             };
 268 
 269             // Create panel that will contain the buttons.
 270             //
 271             JPanel boxPanel = new JPanel();
 272             boxPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
 273             boxPanel.setLayout(new BoxLayout(boxPanel, BoxLayout.Y_AXIS));
 274             boxPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 20));
 275 
 276             // List data store choices.
 277             //
 278             buttonGroup = new ButtonGroup();
 279             for (int i = 0; i < dsconfList.size(); ++i) {
 280                 DSConf dsconf = (DSConf)dsconfList.get(i);
 281                 DSConfButton radioButton =
 282                     new DSConfButton(dsconf, false);
 283                 radioButton.setAlignmentX(Component.LEFT_ALIGNMENT);
 284                 radioButton.addActionListener(buttonListener);
 285                 buttonGroup.add(radioButton);
 286                 boxPanel.add(radioButton);
 287             }
 288 
 289             // Add the panel to the stepBox component.
 290             //
 291             stepBox.add(boxPanel);
 292             stepBox.add(Box.createVerticalStrut(20));
 293             stepBox.add(Box.createVerticalGlue());
 294 
 295         } // constructor
 296         
 297         public String getDescription() {
 298             return ResourceStrings.getString("ds_wiz_datastore_desc");
 299         } // getDescription
 300         
 301         public Component getComponent() {
 302             return stepBox;
 303         } // getComponent
 304         
 305         public void setActive(int direction) {
 306             if (getDsconf() != null) {
 307                 setForwardEnabled(true);
 308             } else {
 309                 setForwardEnabled(false);
 310             }
 311         } // setActive
 312         
 313         public boolean setInactive(int direction) {
 314             return true;
 315         } // setInactive
 316 
 317 
 318         public void enableButton(String resource, boolean enable) {
 319 
 320             DSConfButton button = null;
 321             Enumeration en = buttonGroup.getElements();
 322             while (en.hasMoreElements()) {
 323                 DSConfButton enButton = (DSConfButton)en.nextElement();
 324                 DSConf DSConf = enButton.getDsconf();
 325                 if (dsconf.getDS().getResource().equals(resource)) {
 326                     button = enButton;
 327                     break;
 328                 }
 329             }
 330 
 331             if (button != null) {
 332                 button.setEnabled(enable);
 333             }
 334 
 335         } // enableButton
 336 
 337     } // DatastoreStep
 338     
 339     /**
 340      * This class is the wizard step that presents the data store module
 341      * bean to the user for data store configuration.
 342      */
 343     protected class DatastoreModuleStep implements WizardStep {
 344         
 345         /**
 346          * The component provided to the wizard.
 347          */
 348         private Box stepBox;
 349 
 350         /**
 351          * Basic constructor. The component for the step will actually be
 352          * built in the setActive method, as this step is dependant upon
 353          * the data store selection made by the user in the DatastoreStep
 354          * wizard step.
 355          */
 356         public DatastoreModuleStep() {
 357             stepBox = Box.createVerticalBox();
 358             stepBox.add(Box.createVerticalGlue());
 359         } // constructor
 360         
 361         public String getDescription() {
 362             return ResourceStrings.getString("ds_wiz_datastore_parm_desc");
 363         } // getDescription
 364         
 365         public Component getComponent() {
 366             return stepBox;
 367         } // getComponent
 368         
 369         public void setActive(int direction) {
 370             if (direction > 0) {
 371                 stepBox.removeAll();
 372                 Component component =
 373                     getDsconf().getModule().getComponent();
 374                 if (component != null) {
 375                     stepBox.add(component);
 376                     stepBox.add(Box.createVerticalGlue());
 377                     validate();
 378                 }
 379             }
 380 
 381             if (getDsconf().getModule().getForwardEnabled()) {
 382                 setForwardEnabled(true);
 383             } else {
 384                 setForwardEnabled(false);
 385             }
 386         } // setActive
 387         
 388         public boolean setInactive(int direction) {
 389             return true;
 390         } // setInactive
 391 
 392     } // DatastoreModuleStep
 393     
 394     /**
 395      * Simple constructor.
 396      * @param owner frame for wizard.
 397      * @param title title of the wizard.
 398      */
 399     public DSWizard(Frame owner, String title) {
 400         super(owner, title);
 401     } // constructor
 402 
 403     /**
 404      * Sets dsconf.
 405      * @param dsconf the data store config value.
 406      */
 407     public void setDsconf(DSConf dsconf) {
 408         if (this.dsconf != null) {
 409             this.dsconf.getModule().removeDSMListener(this);
 410         }
 411         setForwardEnabled(true);
 412         this.dsconf = dsconf;
 413         this.dsconf.getModule().addDSMListener(this);
 414     } // setDsconf
 415 
 416     /**
 417      * Returns the dsconf.
 418      * @return the dsconf.
 419      */
 420     public DSConf getDsconf() {
 421         return dsconf;
 422     } // getDsconf
 423 
 424     /**
 425      * Invoked when the DSModule has changed its state.
 426      * @param e the event.
 427      */
 428     public void stateChanged(DSModuleEvent e) {
 429         if (e.getState() == DSModuleEvent.DATA_VALID) {
 430             setForwardEnabled(true);
 431         } else {
 432             setForwardEnabled(false);
 433         }
 434     } // stateChanged
 435 
 436 } // DSWizard