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 java.awt.GridBagConstraints; 13 import java.awt.GridBagLayout; 14 import java.awt.Insets; 15 import java.util.ArrayList; 16 import java.util.Iterator; 17 import java.util.List; 18 import java.util.Observable; 19 import java.util.Observer; 20 21 import javax.swing.JComponent; 22 import javax.swing.JOptionPane; 23 import javax.swing.JPanel; 24 import javax.swing.text.Document; 25 26 /*** 27 * Implements several methods commonly used by panels that have an 28 * underlying MappedModel. 29 * 30 * @author Fabian Ritzmann 31 */ 32 public class RegistryMappedPanel extends JPanel 33 implements Observer, MappedPanel { 34 35 private boolean mappingIsChanging = false; 36 private final List textFields = new ArrayList(); 37 private final String errorMessage; 38 39 40 /*** 41 * Sets initial values. Registers the panel as an observer with the 42 * underlying model. 43 * 44 * @param model The underlying MappedModel 45 * @param updateError Prepended to exception text if model update 46 * triggered an exception 47 */ 48 protected RegistryMappedPanel(RegistryMappedModel model, 49 String updateError) { 50 this.errorMessage = updateError; 51 model.addObserver(this); 52 } 53 54 55 /*** 56 * Returns a reference to this object. 57 * 58 * @return RegistryMappedPanel A reference to this object 59 */ 60 public final RegistryMappedPanel getRegistryMappedPanel() { 61 return this; 62 } 63 64 65 /*** 66 * Iterates through the text fields associated with this panel, 67 * clears the field and sets the new text to be displayed. Displays 68 * an error dialog if an exception occurs. 69 * 70 * For this to work it is necessary that all text fields implement 71 * the TextField interface and are added with addTextField. 72 * 73 * @see java.util.Observer#update(Observable, Object) 74 * 75 * @param o ignored 76 * @param arg ignored 77 */ 78 public void update(Observable o, Object arg) { 79 TextField field = null; 80 Document doc = null; 81 Iterator i = this.textFields.iterator(); 82 while (i.hasNext()) { 83 field = (TextField) i.next(); 84 try { 85 doc = field.getTextField().getDocument(); 86 doc.remove(0, doc.getLength()); 87 String text = field.getText(); 88 if (text != null) { 89 doc.insertString(0, text, null); 90 } 91 } catch (Exception e) { 92 JOptionPane.showMessageDialog(this, 93 this.errorMessage + e.getMessage(), "Error", 94 JOptionPane.ERROR_MESSAGE); 95 } 96 } 97 } 98 99 100 /*** 101 * Sets a flag that the mapping is changing. 102 * 103 * @see com.sun.xml.registry.client.browser.swing.ChangeableType#setChangingType(boolean) 104 */ 105 public void setMappingIsChanging(boolean changing) { 106 this.mappingIsChanging = changing; 107 } 108 109 110 /*** 111 * Returns the value of a flag. 112 * 113 * @see com.sun.xml.registry.client.browser.swing.ChangeableType#isChangingType() 114 */ 115 public boolean mappingIsChanging() { 116 return this.mappingIsChanging; 117 } 118 119 120 /*** 121 * Adds the text field to an internal list. All text fields in the list 122 * are updated when the underlying model reports a change. 123 */ 124 public void addTextField(TextField field) { 125 add(field.getTextField()); 126 this.textFields.add(field); 127 } 128 129 130 131 /*** 132 * Utility method that sets constraints on a layout. 133 * 134 * @param component Component that is affected 135 * @param c Container for constraints 136 * @param gbl Container for layout 137 * @param gridx value to set on constraints 138 * @param gridy value to set on constraints 139 * @param gridwidth value to set on constrains 140 * @param weightx value to set on constraints 141 * @param fill value to set on constrains 142 * @param anchor value to set on constraints 143 */ 144 public static void setConstraints(JComponent component, 145 GridBagConstraints c, 146 GridBagLayout gbl, 147 int gridx, int gridy, 148 int gridwidth, 149 double weightx, 150 int fill, int anchor) { 151 c.gridx = gridx; 152 c.gridy = gridy; 153 c.gridwidth = gridwidth; 154 c.gridheight = 1; 155 c.weightx = weightx; 156 c.weighty = 0.0; 157 c.fill = fill; 158 c.anchor = anchor; 159 c.insets = new Insets(4, 4, 0, 4); 160 gbl.setConstraints(component, c); 161 } 162 163 }

This page was automatically generated by Maven