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.border.*;
  32 import javax.swing.event.*;
  33 import javax.swing.table.*;
  34 
  35 import java.awt.*;
  36 import java.awt.event.*;
  37 import java.util.*;
  38 
  39 import com.sun.dhcpmgr.data.*;
  40 import com.sun.dhcpmgr.ui.*;
  41 
  42 /**
  43  * This dialog allows the user to view the contents of a macro,
  44  */
  45 public class ViewMacroDialog extends JDialog {
  46 
  47     class MacroTableModel extends AbstractTableModel {
  48         private Macro macro;
  49         
  50         public MacroTableModel() {
  51             setMacro(new Macro());
  52         }
  53         
  54         public MacroTableModel(Macro m) {
  55             super();
  56             setMacro(m);
  57         }
  58         
  59         public void setMacro(Macro m) {
  60             macro = m;
  61             fireTableDataChanged();
  62         }
  63         
  64         public int getRowCount() {
  65             return macro.optionCount();
  66         }
  67         
  68         public int getColumnCount() {
  69             return 2;
  70         }
  71         
  72         public Object getValueAt(int row, int column) {
  73             OptionValue v = null;       
  74             try {
  75                 v = macro.getOptionAt(row);
  76             } catch (ArrayIndexOutOfBoundsException e) {
  77                 return null;
  78             }
  79             if (v == null) {
  80                 return null;
  81             }
  82             switch (column) {
  83             case 0:
  84                 return v.getName();
  85             case 1:
  86                 return v.getValue();
  87             default:
  88                 return null;
  89             }
  90         }
  91         
  92         public Class getColumnClass(int column) {
  93             switch (column) {
  94             case 0:
  95             case 1:
  96                 return String.class;
  97             default:
  98                 super.getColumnClass(column);
  99             }
 100             return null;
 101         }
 102         
 103         public String getColumnName(int column) {
 104             switch (column) {
 105             case 0:
 106                 return ResourceStrings.getString("option_column");
 107             case 1:
 108                 return ResourceStrings.getString("value_column");
 109             default:
 110                 super.getColumnName(column);
 111             }
 112             return null;
 113         }
 114     }
 115     
 116     private JTextField name;
 117     private AutosizingTable macroTable;
 118     private MacroTableModel macroTableModel;
 119     private JButton closeButton;
 120     
 121     /**
 122      * Construct the dialog.
 123      * @arg owner The owning dialog
 124      * @arg c The component relative to which we should be positioned
 125      * @arg macro The macro we're viewing
 126      */
 127     public ViewMacroDialog(Dialog owner, Component c, Macro macro) {
 128         super(owner);
 129         setLocationRelativeTo(c);
 130         
 131         setTitle(ResourceStrings.getString("view_macro_title"));
 132         
 133         getContentPane().setLayout(new BoxLayout(getContentPane(),
 134             BoxLayout.Y_AXIS));
 135         JPanel mainPanel = new JPanel(new BorderLayout());
 136         mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
 137         
 138         name = new JTextField(30);
 139         name.setEditable(false);
 140         JPanel panel = new JPanel();
 141 
 142         Mnemonic mnName =
 143             new Mnemonic(ResourceStrings.getString("name_label"));
 144         JLabel nLbl = new JLabel(mnName.getString());
 145         nLbl.setLabelFor(panel);
 146         nLbl.setToolTipText(mnName.getString());
 147         nLbl.setDisplayedMnemonic(mnName.getMnemonic());
 148         panel.add(nLbl);
 149         panel.add(name);
 150         mainPanel.add(panel, BorderLayout.NORTH);
 151 
 152         JPanel contentsPanel = new JPanel();
 153         contentsPanel.setLayout(new BorderLayout());
 154         Border b = BorderFactory.createCompoundBorder(
 155             BorderFactory.createLineBorder(Color.black),
 156             BorderFactory.createEmptyBorder(5, 10, 5, 10));
 157         contentsPanel.setBorder(BorderFactory.createTitledBorder(b,
 158             ResourceStrings.getString("contents_label")));
 159         contentsPanel.setToolTipText(
 160             ResourceStrings.getString("contents_label"));
 161         macroTableModel = new MacroTableModel();
 162         macroTable = new AutosizingTable(macroTableModel);
 163         macroTable.getTableHeader().setReorderingAllowed(false);
 164         macroTable.getTableHeader().setResizingAllowed(false);
 165 
 166         JScrollPane macroTablePane = new JScrollPane(macroTable);
 167         // Resize table as otherwise it asks for a huge area
 168         Dimension d = macroTable.getPreferredScrollableViewportSize();
 169         d.height = 100;
 170         d.width = 300;
 171         macroTable.setPreferredScrollableViewportSize(d);
 172         
 173         contentsPanel.add(macroTablePane, BorderLayout.CENTER);
 174         mainPanel.add(contentsPanel, BorderLayout.CENTER);
 175                 
 176         getContentPane().add(mainPanel);
 177         getContentPane().add(new JSeparator());
 178         
 179         JPanel buttonPanel = new JPanel();
 180 
 181         Mnemonic mnOK = 
 182             new Mnemonic(ResourceStrings.getString("ok"));
 183         closeButton = new JButton(mnOK.getString());
 184         closeButton.setToolTipText(mnOK.getString());
 185         closeButton.setMnemonic(mnOK.getMnemonic());
 186 
 187         buttonPanel.add(closeButton);
 188         closeButton.addActionListener(new ActionListener() {
 189             public void actionPerformed(ActionEvent e) {
 190                 setVisible(false);
 191                 dispose();
 192             }
 193         });
 194         
 195         getContentPane().add(buttonPanel);
 196         
 197         name.setText(macro.getKey());
 198         macroTableModel.setMacro(macro);
 199     }
 200 }