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) 1999 by Sun Microsystems, Inc. 26 * All rights reserved. 27 */ 28 package com.sun.dhcpmgr.client; 29 30 import javax.swing.JDialog; 31 import javax.swing.BoxLayout; 32 import javax.swing.JSeparator; 33 import javax.swing.JPanel; 34 import java.awt.event.ActionEvent; 35 import java.awt.event.ActionListener; 36 import java.awt.Frame; 37 import java.util.Vector; 38 import java.util.Enumeration; 39 40 import com.sun.dhcpmgr.ui.ButtonPanel; 41 import com.sun.dhcpmgr.ui.ButtonPanelListener; 42 43 /** 44 * This abstract class provides a framework for building all of the dialogs 45 * used by DHCP Mgr. Subclasses must implement the abstract methods defined 46 * here as well as override any other appropriate methods. Most of these 47 * methods are declared protected because they are implementation details 48 * which need not be visible outside the dialog we're actually implementing. 49 */ 50 public abstract class DhcpmgrDialog extends JDialog 51 implements ButtonPanelListener { 52 // Listeners receive action events when user presses OK 53 private Vector listeners; 54 // ButtonPanel is protected so subclasses can manipulate directly 55 protected ButtonPanel buttonPanel; 56 57 public DhcpmgrDialog(Frame f, boolean allowsReset) { 58 super(f); 59 listeners = new Vector(); 60 // Layout is subclass main panel, then a separator, then buttons 61 getContentPane().setLayout(new BoxLayout(getContentPane(), 62 BoxLayout.Y_AXIS)); 63 // Create buttonPanel first so subclasses can modify it if need be 64 buttonPanel = new ButtonPanel(allowsReset); 65 buttonPanel.addButtonPanelListener(this); 66 getContentPane().add(getMainPanel()); 67 getContentPane().add(new JSeparator()); 68 getContentPane().add(buttonPanel); 69 // Position relative to our owning frame 70 setLocationRelativeTo(f); 71 } 72 73 /** 74 * Return the main display panel 75 */ 76 protected abstract JPanel getMainPanel(); 77 78 public void addActionListener(ActionListener l) { 79 listeners.addElement(l); 80 } 81 82 public void removeActionListener(ActionListener l) { 83 listeners.removeElement(l); 84 } 85 86 /** 87 * fire the action event 88 */ 89 protected abstract void fireActionPerformed(); 90 91 protected void fireActionPerformed(Object source, String command) { 92 ActionEvent e = new ActionEvent(source, ActionEvent.ACTION_PERFORMED, 93 command); 94 Enumeration en = listeners.elements(); 95 while (en.hasMoreElements()) { 96 ActionListener l = (ActionListener)en.nextElement(); 97 l.actionPerformed(e); 98 } 99 } 100 101 /** 102 * Handle user clicks on the dialog buttons 103 */ 104 public void buttonPressed(int buttonId) { 105 switch (buttonId) { 106 case OK: 107 doOk(); 108 break; 109 case CANCEL: 110 doCancel(); 111 break; 112 case HELP: 113 doHelp(); 114 break; 115 case RESET: 116 doReset(); 117 break; 118 default: 119 break; 120 } 121 } 122 123 /** 124 * Handle user pressing OK button 125 */ 126 protected abstract void doOk(); 127 128 /** 129 * Handle user pressing Cancel button, default is to disappear 130 */ 131 protected void doCancel() { 132 setVisible(false); 133 dispose(); 134 } 135 136 /** 137 * Handle user pressing Reset; this default implementation does 138 * nothing, subclasses should override. 139 */ 140 protected void doReset() { 141 // Do nothing 142 } 143 144 /** 145 * Handle user pressing Help 146 */ 147 protected void doHelp() { 148 DhcpmgrApplet.showHelp(getHelpKey()); 149 } 150 151 /** 152 * Return the help key 153 */ 154 protected abstract String getHelpKey(); 155 }