xref: /onnv-gate/usr/src/cmd/print/printmgr/com/sun/admin/pm/client/pmAccess.java (revision 11262:b7ebfbf2359e)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Comment which describes the contents of this file.
27  *
28  * pmAccess.java
29  * Add Access To Printer handling
30  */
31 
32 package com.sun.admin.pm.client;
33 
34 import java.awt.*;
35 import java.awt.event.*;
36 import java.util.*;
37 import java.util.Vector;
38 import javax.swing.JPanel;
39 import javax.swing.*;
40 
41 import com.sun.admin.pm.server.*;
42 
43 
44 /*
45  * Window for Edit -> Add Access to a Printer
46  */
47 
48 public class pmAccess extends pmButtonScreen {
49     JComboBox portCombo;
50     pmFrame frame = new pmFrame(
51 	pmUtility.getResource("SPM:Add.Access.To.Printer"));
52     pmTextField pnameText;
53     pmTextField snameText;
54     pmTextField descText;
55     Boolean makedefault;
56     JCheckBox defaultp;
57     Printer newpr = null;
58     pmTop mytop;
59     String printer = null;
60     String server = null;
61 
62     String cmdLog = null;
63     String errorLog = null;
64     String warnLog = null;
65 
66 
67     final static int OK =  1;
68     final static int APPLY =  2;
69     final static int RESET =  3;
70     final static int CANCEL =  4;
71     final static int HELP =  5;
72 
pmAccess(pmTop mytop)73     public pmAccess(pmTop mytop) {
74 
75         // ensure that pmButton hashtable gets cleaned up
76         frame.setClearButtonsOnClose(true);
77 
78         setLayout(new BorderLayout());
79 
80 	this.mytop = mytop;
81 
82 	// Build the Frame
83 	centerPanel();
84 	southPanel();
85 
86 	/*
87 	 * let's try doing this in Show...
88 	 *
89 	 * // default button is always OK, for now...
90 	 * frame.getRootPane().setDefaultButton(okButton);
91 	 *
92 	 * okButton.setAsDefaultButton();
93 	 */
94 
95         // handle Esc as cancel
96 	this.registerKeyboardAction(new ActionListener() {
97             public void actionPerformed(ActionEvent e) {
98                 Debug.message("CLNT:  default cancel action");
99                 actioncancelButton();
100             }},
101             KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
102             JComponent.WHEN_IN_FOCUSED_WINDOW);
103 
104         // enable the pmFrame to set focus to our default comp on activation
105 	frame.setDefaultComponent(pnameText);
106 
107 
108 	// following is test code, I think...
109 	Component glass = frame.getGlassPane();
110 
111 	glass.addKeyListener(new KeyAdapter() {
112             public void keyPressed(KeyEvent k) {
113                 Debug.info("Glass: " + k);
114             }
115             public void keyReleased(KeyEvent k) {
116                 Debug.info("Glass: " + k);
117             }
118             public void keyTyped(KeyEvent k) {
119                 Debug.info("Glass: " + k);
120             }
121 	});
122 
123     }
124 
centerPanel()125     public void centerPanel() {
126 	// Build center panel
127 	JPanel center = new JPanel();
128 	center.setLayout(new GridBagLayout());
129 	GridBagConstraints c = new GridBagConstraints();
130 
131 	// define center constraints
132 	c.insets = new Insets(8, 5, 5, 5);
133 	c.gridheight = 1;
134 	c.gridwidth = 1;
135 
136 	// build center components
137 	// Create
138 	c.gridx = 0;
139 	c.weightx = c.weighty = 0.0;
140 	c.fill = GridBagConstraints.NONE;
141 	c.anchor = GridBagConstraints.WEST;
142 
143 	// Build the labels
144 	c.gridy = 0;
145 	center.add(new JLabel
146 		(pmUtility.getResource("Printer.Name:")), c);
147 	c.gridy = 1;
148 	center.add(new JLabel
149 		(pmUtility.getResource("Printer.Server:")), c);
150 	c.gridy = 2;
151 	center.add(new JLabel
152 		(pmUtility.getResource("Description:")), c);
153 	c.gridy = 3;
154 	center.add(new JLabel
155 		(pmUtility.getResource("Option:")), c);
156 
157 	// Build the text fields
158 	// Common constraints
159 	c.gridx = 1;
160 	c.ipadx = 15;
161 	c.fill = GridBagConstraints.HORIZONTAL;
162 	c.anchor = GridBagConstraints.WEST;
163 	c.weightx = c.weighty = 1.0;
164 
165 	c.gridy = 0;
166 	pnameText = new pmTextField(14);
167 
168 	center.add(pnameText, c);
169 
170 	c.gridy = 1;
171 	snameText = new pmTextField(25);
172 	center.add(snameText, c);
173 
174 	c.gridy = 2;
175 	descText = new pmTextField(25);
176 	center.add(descText, c);
177 
178 	// Add Choice Menus - ComboBox
179 	c.weightx = c.weighty = 0.0;
180 	c.gridy = 3;
181 
182 	defaultp = new JCheckBox(
183 		pmUtility.getResource("Default.Printer"));
184 	center.add(defaultp, c);
185 
186 	add("Center", center);
187     }
188 
createAccess()189     public void createAccess() throws pmGuiException {
190 	boolean getHostOk = true;
191 	String description = "";
192 
193 	newpr = new Printer(mytop.ns);
194 	Debug.message("CLNT: createAccess()");
195 	pmCalls.debugShowPrinter(newpr);
196 	printer = pnameText.getText().trim();
197 	server = snameText.getText().trim();
198 	description = descText.getText();
199 
200 	if (printer.equals("")) {
201 		pnameText.requestFocus();
202 		Debug.message("CLNT:pmAccess:Printer name required.");
203 		throw new pmIncompleteFormException(
204 			pmUtility.getResource("Printer.name.required."));
205 	}
206 
207 	if (!Valid.remotePrinterName(printer)) {
208 		pnameText.requestFocus();
209 		Debug.message("CLNT:pmAccess:Printer name invalid: " + printer);
210 		throw new pmIncompleteFormException(
211 			pmUtility.getResource("Printer.name.invalid."));
212 	}
213 
214 	if (server.equals("")) {
215 		snameText.requestFocus();
216 		Debug.message("CLNT:pmAccess:Server name required.");
217 		throw new pmIncompleteFormException(
218 			pmUtility.getResource("Server.name.required."));
219 	}
220 
221 	if (!Valid.serverName(server)) {
222 		snameText.requestFocus();
223 		Debug.message("CLNT:pmAccess:Server name invalid.");
224 		throw new pmIncompleteFormException(
225 			pmUtility.getResource("Server.name.invalid."));
226 	}
227 
228 	try {
229 	    if (server.equals(mytop.host.getLocalHostName()) ||
230 		server.equals("localhost")) {
231 		snameText.requestFocus();
232 		getHostOk = false;
233 	    }
234 	} catch (Exception e) {
235 		Debug.warning(
236 		"CLNT: pmAccess:createAccess:getLocalHostName exception");
237 		throw new pmGuiException(
238 			pmUtility.getResource(
239 			"Could.not.get.local.hostname " + e));
240 	}
241 
242 	if (!getHostOk) {
243 		Debug.warning(
244 		"CLNT: pmAccess:createAccess:Server name required.");
245 		throw new pmMustBeRemoteServerException(
246 			pmUtility.getResource("Server.name.required."));
247 	}
248 
249 	boolean exist;
250 	try {
251 		exist = PrinterUtil.exists(printer, mytop.ns);
252 	} catch (Exception e) {
253 		throw new pmGuiException(e.toString());
254 	}
255 
256 	if (exist) {
257 		throw new pmPrinterExistsException();
258 	}
259 
260 	if (mytop.ns.getNameService().equals("nis") == true ||
261 		mytop.ns.getNameService().equals("ldap") == true) {
262 		try {
263 			if (!mytop.ns.isAuth()) {
264 				pmUtility.doLogin(mytop, frame);
265 			}
266 		} catch (pmUserCancelledException e) {
267 			Debug.message("CLNT:pmAccess:user cancelled login");
268 			throw new pmUserCancelledException(
269 				pmUtility.getResource(
270 				"User.cancelled.login."));
271 		} catch (pmGuiException e) {
272 			Debug.message(
273 			    "CLNT:pmAccess:login nis/ldap failed: " + e);
274                		throw new pmLoginFailedException();
275 		} catch (Exception e) {
276 			Debug.message(
277 			    "CLNT:pmAccess:login nis/ldap failed: " + e);
278                		throw new pmLoginFailedException();
279 		}
280 	}
281 
282 	Debug.message("CLNT:pmAccess:checkbox: " + defaultp.isSelected());
283 
284 	// Check for confirmation option
285 	if (((mytop.getConfirmOption() == true) && (confirmAction() == true))
286               	|| (mytop.getConfirmOption() == false)) {
287 
288 		// Set the printer attributes
289 		newpr.setPrinterName(printer);
290 		newpr.setPrintServer(server);
291 		newpr.setComment(description);
292 		if (defaultp.isSelected())
293 			newpr.setIsDefaultPrinter(true);
294 
295 		frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
296 
297 		boolean failed = false;
298 		try {
299 			newpr.addRemotePrinter();
300 		} catch (Exception e) {
301 			failed = true;
302 			Debug.warning(
303 				"CLNT:pmAccess:addRemotePrinter caught:" + e);
304 		}
305 
306 		frame.setCursor(Cursor.getDefaultCursor());
307 
308 		gatherLogs();
309 		dumpLogs("CLNT:pmAccess:createAccess()");
310 
311 		pmCalls.debugShowPrinter(newpr);
312 
313 		mytop.setLogData(cmdLog, errorLog, warnLog);
314 		mytop.showLogData(
315 		    pmUtility.getResource("Add.Access.To.Printer"));
316 
317 		mytop.pmsetPrinterList();
318 		mytop.clearSelected();
319 		mytop.listTable.clearSelection();
320 
321 		if (failed)
322 			throw new pmAddAccessFailedException(errorLog);
323 
324         }
325     }
326 
clearAccessInput()327     public void clearAccessInput()  {
328 	try {
329 		pnameText.setText("");
330 		snameText.setText("");
331 		descText.setText("");
332 		if (defaultp.isSelected())
333 			defaultp.doClick();
334 	} catch (Exception  e) {
335 	// ignore???
336         // throw new pmGuiException("pmAccess: Error clearAccessInput: " + e);
337             	Debug.warning("CLNT:pmAccess: Error clearAccessInput: " + e);
338 	}
339     }
340 
341 
342 
confirmAction()343     public boolean confirmAction() {
344 	if (mytop.getConfirmOption() == true) {
345 		pmOKCancelDialog d = new pmOKCancelDialog(frame,
346 			pmUtility.getResource("Action.Confirmation"),
347 			pmUtility.getResource(
348 				"Continue.creating.access.for.this.printer?"));
349 		d.setVisible(true);
350 		if (d.getValue() != JOptionPane.OK_OPTION) {
351 		    pmMessageDialog m = new pmMessageDialog(frame,
352 			    pmUtility.getResource("Warning"),
353 			    pmUtility.getResource("Operation.Cancelled"));
354 		    m.setVisible(true);
355 		    return false;
356 		}
357 	}
358 	return true;
359     }
360 
gatherLogs()361     void gatherLogs() {
362         cmdLog = newpr.getCmdLog();
363         errorLog = newpr.getErrorLog();
364         warnLog = newpr.getWarnLog();
365     }
366 
367 
dumpLogs(String who)368     void dumpLogs(String who) {
369         Debug.message(who);
370         Debug.message(who + " command: " + cmdLog);
371         Debug.message(who + " warnings: " + warnLog);
372         Debug.message(who + " errors: " + errorLog);
373     }
374 
375     // returns true if success, false otherwise
doAction()376     boolean doAction() {
377         boolean rv = false;
378 
379         try {
380             createAccess();
381 	    rv = true;	// only if it didn't throw!
382         } catch (pmIncompleteFormException ix) {
383 		Debug.warning(
384 			"CLNT:pmAccess:incomplete form " + ix.getMessage());
385             	pmMessageDialog m = new pmMessageDialog(
386                 	frame,
387                 	pmUtility.getResource("Error"),
388                 	ix.getMessage(),
389                 	mytop,
390                 	"AddAccessFailed");
391             m.setVisible(true);
392         } catch (pmPrinterExistsException ex) {
393 		Debug.warning("CLNT:pmAccess:printer exists");
394             pmMessageDialog m = new pmMessageDialog(
395                 frame,
396                 pmUtility.getResource("Error"),
397                 pmUtility.getResource("The.specified.printer.already.exists."));
398             m.setVisible(true);
399         } catch (pmMustBeRemoteServerException rx) {
400 		Debug.warning("CLNT:pmAccess:server must be remove.");
401             	pmMessageDialog m = new pmMessageDialog(
402                 	frame,
403                 	pmUtility.getResource("Error"),
404                 	pmUtility.getResource(
405 				"The.server.must.be.a.remote.server."),
406                 	mytop,
407                 	"RemoteServer");
408 		m.setVisible(true);
409 
410         } catch (pmLoginFailedException lx) {
411 		Debug.warning("CLNT:pmAccess:Required login failed");
412 		pmMessageDialog m = new pmMessageDialog(
413 			frame,
414 			pmUtility.getResource("Error"),
415 			pmUtility.getResource("Required.login.failed."),
416 			mytop,
417 			"LoginFailed");
418             	m.setVisible(true);
419 
420         } catch (pmAddAccessFailedException ax) {
421 		Debug.warning(
422 			"CLNT:pmAccess:add access failed " + ax.getMessage());
423 		pmMessageDialog m = new pmMessageDialog(
424 			frame,
425 			pmUtility.getResource("Error"),
426 			ax.getMessage(),
427 			mytop,
428 			"AddAccessFailed");
429             m.setVisible(true);
430 
431         } catch (pmUserCancelledException cx) {
432 		Debug.message(
433 		"CLNT:pmAccess:createAccess: User cancelled namespace login");
434         } catch (pmGuiException gx) {
435 		Debug.warning(
436 			"CLNT:pmAccess:Application Error" + gx.getMessage());
437 		pmMessageDialog m = new pmMessageDialog(
438 			frame,
439 			pmUtility.getResource("Application.Error"),
440 			gx.getMessage());
441 		m.setVisible(true);
442 
443         } finally {
444             // clearAccessInput();
445         }
446         return rv;
447     }
448 
449 
pmScreendispose()450     public void pmScreendispose() {
451         frame.dispose();
452     }
453 
actionokButton()454     public void actionokButton() {
455         Debug.message("CLNT:pmAccess:actionokButton()");
456 
457         if (doAction() == true) {
458             clearAccessInput();
459 	    mytop.pmsetdefaultpLabel();
460             frame.setVisible(false);
461             frame.repaint();
462             // frame.dispose();
463             mytop.scrollPane.revalidate();
464             mytop.scrollPane.repaint();
465         } else {
466                 Debug.message("CLNT: pmAccess: doAction is false");
467         }
468 
469     }
470 
actionapplyButton()471     public void actionapplyButton() {
472         Debug.message("CLNT:pmAccess:actionapplyButton()");
473 
474         if (doAction() == true) {
475 			mytop.pmsetdefaultpLabel();
476 			mytop.scrollPane.revalidate();
477 			mytop.scrollPane.repaint();
478 
479 		}
480     }
481 
482 
actionresetButton()483     public void actionresetButton() {
484         Debug.message("CLNT:pmAccess:actionresetButton()");
485         clearAccessInput();
486 		pnameText.requestFocus();
487     }
488 
actioncancelButton()489     public void actioncancelButton() {
490         Debug.message("CLNT:pmAccess:actioncancelButton()");
491         clearAccessInput();
492         frame.setVisible(false);
493         frame.repaint();
494         // frame.dispose();
495     }
496 
actionhelpButton()497     public void actionhelpButton() {
498 	Debug.message("CLNT:pmAccess:actionhelpButton()");
499 	mytop.showHelpItem("AddAccess");
500     }
501 
502 
Show()503     public void Show() {
504 
505 	frame.getContentPane().add("North", this);
506 	frame.pack();
507 	frame.setVisible(true);
508 	frame.repaint();
509 
510 	frame.toFront();
511 	frame.requestFocus();
512 
513 	okButton.setAsDefaultButton();
514 	pnameText.requestFocus();
515 	Debug.message("CLNT:pmAccess:Show()");
516 
517     }
518 }
519