View Javadoc
1 /*** 2 * $Header: /cvsroot/ebxmlrr/jaxr/src/com/sun/xml/registry/client/browser/graph/ReferencePanel.java,v 1.4 2002/11/20 23:52:34 farrukh_najmi Exp $ 3 * 4 * 5 */ 6 package com.sun.xml.registry.client.browser.graph; 7 8 import java.awt.*; 9 import java.awt.event.*; 10 import java.net.*; 11 import java.io.*; 12 import java.util.*; 13 import java.beans.*; 14 import javax.swing.*; 15 import javax.xml.registry.*; 16 import javax.xml.registry.infomodel.*; 17 18 import com.sun.xml.registry.client.browser.conf.bindings.*; 19 import com.sun.xml.registry.client.browser.conf.bindings.types.*; 20 //import org.oasis.ebxml.registry.bindings.rim.*; 21 22 import java.lang.reflect.*; 23 24 import com.sun.xml.registry.client.browser.*; 25 26 /*** 27 * A panel that serves as an editor for setting a reference link between two RegistryObjects. 28 * 29 * @author <a href="mailto:Farrukh.Najmi@Sun.COM">Farrukh S. Najmi</a> 30 */ 31 public class ReferencePanel extends JPanel { 32 33 private RegistryObject src = null; 34 private RegistryObject target = null; 35 private GridBagConstraints c = new GridBagConstraints(); 36 String[] refAttributes = {}; 37 JComboBox refAttributeCombo = null; 38 39 boolean isCollectionRef = false; 40 41 /*** 42 * Class Constructor. 43 */ 44 public ReferencePanel(RegistryObject src, RegistryObject target) { 45 46 this.src = src; 47 this.target = target; 48 49 GridBagLayout gbl = new GridBagLayout(); 50 setLayout(gbl); 51 52 JLabel refAttributeLabel = new JLabel("Reference Attribute:", SwingConstants.LEFT); 53 c.gridx = 0; 54 c.gridy = 3; 55 c.gridwidth = 1; 56 c.gridheight = 1; 57 c.weightx = 0.0; 58 c.weighty = 0.0; 59 c.fill = GridBagConstraints.NONE; 60 c.anchor = GridBagConstraints.WEST; 61 c.insets = new Insets(4, 4, 0, 4); 62 gbl.setConstraints(refAttributeLabel, c); 63 add(refAttributeLabel); 64 65 refAttributeCombo = new JComboBox(getReferenceAttributes()); 66 refAttributeCombo.addActionListener(new ActionListener() { 67 68 public void actionPerformed(ActionEvent e) { 69 } 70 71 } 72 ); 73 74 refAttributeCombo.setEditable(true); 75 c.gridx = 0; 76 c.gridy = 4; 77 c.gridwidth = 1; 78 c.gridheight = 1; 79 c.weightx = 0.5; 80 c.weighty = 0.0; 81 c.fill = GridBagConstraints.HORIZONTAL; 82 c.anchor = GridBagConstraints.WEST; 83 c.insets = new Insets(4, 4, 0, 4); 84 gbl.setConstraints(refAttributeCombo, c); 85 add(refAttributeCombo); 86 } 87 88 public void setReferenceAttributeOnSourceObject() throws JAXRException { 89 String referenceAttribute = (String) refAttributeCombo.getSelectedItem(); 90 91 //Now use Refelection API to add target to src 92 try { 93 Class srcClass = src.getClass(); 94 Class targetClass = target.getClass(); 95 Class registryObjectClass = null; 96 97 98 String targetInterfaceName = targetClass.getName(); 99 targetInterfaceName = targetInterfaceName.substring(targetInterfaceName.lastIndexOf(".")+1); 100 if (targetInterfaceName.endsWith("Impl")) { 101 //Remove Impl suffix for JAXR provider Impl classes 102 targetInterfaceName = targetInterfaceName.substring(0, targetInterfaceName.length() -4); 103 } 104 105 targetInterfaceName = "javax.xml.registry.infomodel." + targetInterfaceName; 106 107 ClassLoader classLoader = srcClass.getClassLoader(); 108 try { 109 targetClass = classLoader.loadClass(targetInterfaceName); 110 registryObjectClass = classLoader.loadClass("javax.xml.registry.infomodel.RegistryObject"); 111 } 112 catch (ClassNotFoundException e) { 113 throw new JAXRException("No JAXR interface found by name " + targetInterfaceName); 114 } 115 116 String suffix = referenceAttribute.substring(0, 1).toUpperCase() + referenceAttribute.substring(1, referenceAttribute.length()); 117 Method method = null; 118 Class[] paramTypes = new Class[1]; 119 120 //See if there is a simple attribute of this name using type of targetObject 121 try { 122 paramTypes[0] = targetClass; 123 method = srcClass.getMethod("set" + suffix, paramTypes); 124 125 Object[] params = new Object[1]; 126 params[0] = target; 127 method.invoke(src, params); 128 isCollectionRef = false; 129 130 return; 131 } 132 catch (NoSuchMethodException e) { 133 } 134 catch (IllegalAccessException e) { 135 } 136 137 //See if there is a simple attribute of this name using base type RegistryObject 138 try { 139 paramTypes[0] = registryObjectClass; 140 method = srcClass.getMethod("set" + suffix, paramTypes); 141 142 Object[] params = new Object[1]; 143 params[0] = target; 144 method.invoke(src, params); 145 isCollectionRef = false; 146 147 return; 148 } 149 catch (NoSuchMethodException e) { 150 } 151 catch (IllegalAccessException e) { 152 } 153 154 //See if there is a addCXXX method for suffix of XXX ending in "s" for plural 155 if (suffix.endsWith("s")) { 156 suffix = suffix.substring(0, suffix.length()-1); 157 } 158 try { 159 paramTypes[0] = targetClass; 160 method = srcClass.getMethod("add" + suffix, paramTypes); 161 162 Object[] params = new Object[1]; 163 params[0] = target; 164 method.invoke(src, params); 165 isCollectionRef = true; 166 167 return; 168 } 169 catch (NoSuchMethodException e) { 170 } 171 catch (IllegalAccessException e) { 172 } 173 174 //See if there is a addCXXX method for suffix of XXX ending in "es" for plural 175 if (suffix.endsWith("e")) { 176 suffix = suffix.substring(0, suffix.length()-1); 177 } 178 try { 179 paramTypes[0] = targetClass; 180 method = srcClass.getMethod("add" + suffix, paramTypes); 181 182 Object[] params = new Object[1]; 183 params[0] = target; 184 method.invoke(src, params); 185 isCollectionRef = true; 186 187 return; 188 } 189 catch (NoSuchMethodException e) { 190 } 191 catch (IllegalAccessException e) { 192 } 193 194 throw new JAXRException("No method found for reference attribute " + referenceAttribute + " for src object of type " + srcClass.getName()); 195 196 } 197 catch (IllegalArgumentException e) { 198 throw new JAXRException(e); 199 } 200 catch (InvocationTargetException e) { 201 throw new JAXRException(e.getCause()); 202 } 203 catch (ExceptionInInitializerError e) { 204 throw new JAXRException(e); 205 } 206 } 207 208 public String getReferenceAttribute() { 209 String refAttribute = (String)refAttributeCombo.getSelectedItem(); 210 return refAttribute; 211 } 212 213 public String[] getReferenceAttributes() { 214 return refAttributes; 215 } 216 217 public void setReferenceAttributes(String[] refAttributes) { 218 this.refAttributes = refAttributes; 219 refAttributeCombo.setModel(new DefaultComboBoxModel(refAttributes)); 220 } 221 222 } 223 224 225 226

This page was automatically generated by Maven