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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * 23 * ident "%Z%%M% %I% %E% SMI" 24 * 25 * Copyright(c) 1999 by Sun Microsystems, Inc. 26 * All rights reserved. 27 * 28 * pmLogDisplay.java 29 * Command Log implementation 30 */ 31 32 package com.sun.admin.pm.client; 33 34 import javax.swing.*; 35 36 import java.awt.*; 37 import java.util.*; 38 import java.awt.event.*; 39 40 import com.sun.admin.pm.server.*; 41 42 public class pmLogDisplay extends pmFrame { 43 44 pmButton helpButton = null; 45 pmButton okButton = null; 46 pmButton clearButton = null; 47 pmTop theTop = null; 48 String helpTag = null; 49 JTextArea theList = null; 50 String theContents = null; 51 pmLogDisplay()52 public pmLogDisplay() { 53 this(null, null); 54 } 55 56 pmLogDisplay(pmTop t, String h)57 public pmLogDisplay(pmTop t, String h) { 58 super(pmUtility.getResource("SPM:Command-Line.Console")); 59 60 theTop = t; 61 helpTag = h; 62 63 setLocation(150, 200); // relative to parent frame 64 65 // top panel 66 JPanel p = new JPanel(); 67 p.setLayout(new BorderLayout()); 68 69 theContents = new String(); 70 theList = new JTextArea(12, 36); 71 theList.setLineWrap(false); 72 theList.setEditable(false); 73 74 theList.registerKeyboardAction(new ActionListener() { 75 public void actionPerformed(ActionEvent e) { 76 copyPressed(); 77 }}, 78 KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, Event.CTRL_MASK), 79 JComponent.WHEN_IN_FOCUSED_WINDOW); 80 81 theList.registerKeyboardAction(new ActionListener() { 82 public void actionPerformed(ActionEvent e) { 83 copyPressed(); 84 }}, 85 KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK), 86 JComponent.WHEN_IN_FOCUSED_WINDOW); 87 88 89 JScrollPane scroll = new JScrollPane(); 90 scroll.getViewport().setView(theList); 91 92 p.add(scroll, "Center"); 93 94 this.getContentPane().add(p, "Center"); 95 96 p = new JPanel(); 97 98 okButton = new pmButton( 99 pmUtility.getResource("Dismiss")); 100 okButton.setMnemonic( 101 pmUtility.getIntResource("Dismiss.mnemonic")); 102 p.add(okButton); 103 104 clearButton = new pmButton( 105 pmUtility.getResource("Clear")); 106 clearButton.setMnemonic( 107 pmUtility.getIntResource("Clear.mnemonic")); 108 p.add(clearButton); 109 110 if (theTop != null && helpTag != null) { 111 helpButton = new pmButton( 112 pmUtility.getResource("Help")); 113 helpButton.setMnemonic( 114 pmUtility.getIntResource("Help.mnemonic")); 115 p.add(helpButton); 116 helpButton.addActionListener(new ActionListener() { 117 public void actionPerformed(ActionEvent evt) { 118 Debug.message("Help button event"); 119 theTop.showHelpItem(helpTag); 120 } 121 }); 122 } 123 124 this.getContentPane().add(p, "South"); 125 126 this.pack(); 127 128 this.addWindowListener(new WindowAdapter() { 129 public void windowClosing(WindowEvent evt) { 130 returnValue = JOptionPane.CLOSED_OPTION; 131 pmLogDisplay.this.setVisible(false); 132 if (pmLogDisplay.this.theTop != null) 133 pmLogDisplay.this.theTop.setLogOption(false); 134 } 135 }); 136 137 okButton.addActionListener(new ActionListener() { 138 public void actionPerformed(ActionEvent evt) { 139 okPressed(); 140 } 141 }); 142 143 clearButton.addActionListener(new ActionListener() { 144 public void actionPerformed(ActionEvent evt) { 145 pmLogDisplay.this.clear(); 146 } 147 }); 148 149 // handle Esc as dismiss in any case 150 getRootPane().registerKeyboardAction(new ActionListener() { 151 public void actionPerformed(ActionEvent e) { 152 Debug.message("CLNT: default cancel action"); 153 okPressed(); 154 }}, 155 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false), 156 JComponent.WHEN_IN_FOCUSED_WINDOW); 157 158 // getRootPane().setDefaultButton(okButton); 159 okButton.setAsDefaultButton(); 160 161 theList.requestFocus(); 162 163 } 164 copyPressed()165 protected void copyPressed() { 166 theList.copy(); 167 } 168 169 okPressed()170 protected void okPressed() { 171 returnValue = JOptionPane.OK_OPTION; 172 pmLogDisplay.this.setVisible(false); 173 if (pmLogDisplay.this.theTop != null) 174 pmLogDisplay.this.theTop.setLogOption(false); 175 } 176 getValue()177 public int getValue() { 178 // Debug.message("getValue"); 179 return returnValue; 180 } 181 182 // i.e. a solid line, or spaces, or... addSeparator()183 public void addSeparator() { 184 theContents = theContents + "\n\r"; 185 theList.setText(theContents); 186 } 187 188 // tricky: s may have embedded newlines... addText(String s)189 public void addText(String s) { 190 theContents = theContents + s; 191 192 /* 193 * StringTokenizer st = new StringTokenizer(s, "\n\r", false); 194 * try { 195 * while(st.hasMoreTokens()) { 196 * String ss = st.nextToken(); 197 * theContents.addElement(ss); 198 * } 199 * } catch(Exception x) { 200 * Debug.warning("CLNT: Log addText caught: " + x); 201 * } 202 */ 203 204 /* 205 * Debug.message("Log contents len = " + theContents.size()); 206 * for (int i = 0; i < theContents.size(); ++i) 207 * Debug.message("\t" + i + ": " + theContents.elementAt(i)); 208 */ 209 210 // conveniently, this forces the last line to be scrolled to. 211 theList.setText(theContents); 212 213 } 214 clear()215 public void clear() { 216 theContents = null; 217 theContents = new String(); 218 theList.setText(theContents); 219 } 220 disableText(boolean d)221 public void disableText(boolean d) { 222 // theText.setEnabled(!d); 223 } 224 main(String[] args)225 public static void main(String[] args) { 226 JFrame f = new JFrame("Test Dialog"); 227 f.setSize(300, 100); 228 229 f.addWindowListener(new WindowAdapter() { 230 public void windowClosing(WindowEvent evt) { 231 System.exit(0); 232 } 233 }); 234 235 f.setVisible(true); 236 237 pmLogDisplay d = new pmLogDisplay(); 238 d.addText("A\nB\nC\nD\nE\nF\nG\nH\nI\nJ"); 239 d.setVisible(true); 240 } 241 242 243 protected int returnValue = JOptionPane.CLOSED_OPTION; 244 } 245