View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 package com.sun.xml.registry.client.browser.swing; 11 12 import javax.swing.JOptionPane; 13 import javax.swing.JPanel; 14 import javax.swing.event.DocumentEvent; 15 import javax.swing.event.DocumentListener; 16 import javax.swing.text.Document; 17 import javax.xml.registry.JAXRException; 18 19 /*** 20 * DocumentListener that factors out some commonly used code. This in an 21 * implementation of the Strategy design pattern. 22 * 23 * The class implements the document listener methods and invokes an 24 * abstract method setText. The setText method is intended to set the 25 * entered text in the underlying model. 26 * 27 * @author Fabian Ritzmann 28 */ 29 public abstract class RegistryDocumentListener implements DocumentListener { 30 31 protected final JPanel panel; 32 protected final String errorMessage; 33 34 35 /*** 36 * Constructor. 37 * 38 * @param p Panel to which this listener belongs 39 * @param error String that is prepended to error message if 40 * setText throws an exception 41 */ 42 public RegistryDocumentListener(JPanel p, String error) { 43 this.panel = p; 44 this.errorMessage = error; 45 } 46 47 48 /*** 49 * Invokes update. 50 * 51 * @see javax.swing.event.DocumentListener#insertUpdate(DocumentEvent) 52 */ 53 public void insertUpdate(DocumentEvent e) { 54 update(e); 55 } 56 57 58 /*** 59 * Invokes update. 60 * 61 * @see javax.swing.event.DocumentListener#removeUpdate(DocumentEvent) 62 */ 63 public void removeUpdate(DocumentEvent e) { 64 update(e); 65 } 66 67 68 /*** 69 * Invokes update. 70 * 71 * @see javax.swing.event.DocumentListener#changedUpdate(DocumentEvent) 72 */ 73 public void changedUpdate(DocumentEvent e) { 74 update(e); 75 } 76 77 78 /*** 79 * Gets the text from the document and calls setText. Displays a message 80 * with an error if setText throws an exception. 81 * 82 * @param ev The event with changed text 83 */ 84 protected void update(DocumentEvent ev) { 85 Document doc = ev.getDocument(); 86 int docLength = doc.getLength(); 87 try { 88 String text = doc.getText(0, docLength); 89 setText(text); 90 } catch (Exception ex) { 91 JOptionPane.showMessageDialog(this.panel, 92 this.errorMessage + ex.getMessage(), 93 "Error", JOptionPane.ERROR_MESSAGE); 94 } 95 } 96 97 98 /*** 99 * Should be implemented to set a new text string on the underlying 100 * model. 101 * 102 * @param text The text in a document field 103 * 104 * @throws JAXRException Should be thrown if an exception in the 105 * underlying model is triggered 106 */ 107 protected abstract void setText(String text) throws JAXRException; 108 109 }

This page was automatically generated by Maven