1 19 package org.netbeans.modules.xml.tax.beans; 20 21 import java.beans.*; 22 import java.awt.Component ; 23 24 import org.openide.NotifyDescriptor; 25 import org.openide.DialogDescriptor; 26 import org.openide.DialogDisplayer; 27 28 import org.netbeans.tax.*; 29 import javax.swing.JComponent ; 30 31 import org.netbeans.modules.xml.tax.util.TAXUtil; 32 33 38 public class Lib { 39 40 private static String CREATE_ATTRIBUTE_NAME = Util.THIS.getString ("TEXT_new_attribute_name"); 41 private static String CREATE_ATTRIBUTE_VALUE = Util.THIS.getString ("TEXT_new_attribute_value"); 42 43 44 45 50 public static Component getCustomizer (Object object) { 51 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: object = " + object); 53 if (object == null) 54 return null; 55 56 BeanInfo beanInfo; 57 try { 58 beanInfo = Introspector.getBeanInfo (object.getClass()); 59 } catch (IntrospectionException e) { 60 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: exception = " + e); 62 return null; 63 } 64 Class clazz = beanInfo.getBeanDescriptor().getCustomizerClass(); 65 if (clazz == null) { 66 return null; 67 } 68 69 Object o; 70 try { 71 o = clazz.newInstance (); 72 } catch (InstantiationException e) { 73 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: exception = " + e); 75 return null; 76 } catch (IllegalAccessException e) { 77 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: exception = " + e); 79 return null; 80 } 81 82 if (!!! (o instanceof Customizer) ) { 83 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: is NOT instanceof Customizer: " + o); 86 return null; 87 } 88 89 Customizer cust = ((java.beans.Customizer )o); 90 91 Component comp = null; 93 if (o instanceof Component ) { 94 comp = (Component )o; 95 } else { 96 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: is NOT instanceof Component: " + o); 99 return null; 100 } 101 102 cust.setObject (object); 103 104 return comp; 105 } 106 107 109 public static Component getCustomizer (Class classClass, Object property, String propertyName) { 110 BeanInfo beanInfo; 111 try { 112 beanInfo = Introspector.getBeanInfo (classClass); 113 } catch (IntrospectionException e) { 114 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: exception = " + e); 116 return null; 117 } 118 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: beaninfo = " + beanInfo); 120 PropertyDescriptor[] propDescrs = beanInfo.getPropertyDescriptors(); 121 PropertyDescriptor propertyDescriptor = null; 122 for ( int i = 0; i < propDescrs.length; i++ ) { 123 if ( propertyName.equals (propDescrs[i].getName()) ) { 124 propertyDescriptor = propDescrs[i]; 125 break; 126 } 127 } 128 if ( propertyDescriptor == null ) { 129 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: have NOT property: " + propertyName); 131 return null; 132 } 133 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: propertyDescriptor: " + propertyDescriptor); 135 Class clazz = propertyDescriptor.getPropertyEditorClass(); 136 137 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: propertyEditorClass: " + clazz); 139 if (clazz == null) { 140 return null; 141 } 142 Object peo; 143 try { 144 peo = clazz.newInstance (); 145 } catch (InstantiationException e) { 146 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: exception = " + e); 148 return null; 149 } catch (IllegalAccessException e) { 150 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: exception = " + e); 152 return null; 153 } 154 155 if (!!! (peo instanceof PropertyEditor) ) { 156 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: is NOT instanceof PropertyEditor: " + peo); 159 return null; 160 } 161 162 PropertyEditor editor = ((PropertyEditor)peo); 163 editor.setValue (property); 164 Component comp = editor.getCustomEditor(); 165 if ( comp == null ) { 166 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: have NOT customizer: " + editor); 168 return null; 169 } 170 if (!!! (comp instanceof Customizer) ) { 171 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Lib::getCustomizer: is NOT instanceof Customizer: " + comp); 174 return null; 175 } 176 Customizer cust = ((Customizer)comp); 177 178 180 return comp; 181 } 182 183 185 public static boolean confirmAction (String message) { 186 NotifyDescriptor nd = new NotifyDescriptor.Confirmation (message, NotifyDescriptor.YES_NO_OPTION); 187 188 Object option = DialogDisplayer.getDefault().notify (nd); 189 190 return ( option == NotifyDescriptor.YES_OPTION ); 191 } 192 193 194 197 public static TreeAttribute createAttributeDialog () { 198 return createAttributeDialog(false); 199 } 200 201 204 public static TreeAttribute createAttributeDialog (boolean mode) { 205 try { 206 TreeAttribute attr = new TreeAttribute (CREATE_ATTRIBUTE_NAME, CREATE_ATTRIBUTE_VALUE); 207 Component customizer = getCustomizer (attr); 208 209 if ( customizer == null ) { 210 return null; 211 } 212 213 return (TreeAttribute)customNode (attr, customizer, Util.THIS.getString ("TITLE_new_attribute"), mode); 214 } catch (TreeException exc) { 215 TAXUtil.notifyTreeException (exc); 216 return null; 217 } 218 } 219 220 221 223 private static TreeNode customNode (TreeNode treeNode, Component panel, String title, boolean mode) { 224 DialogDescriptor dd = new DialogDescriptor 225 (panel, title, true, DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, 226 DialogDescriptor.BOTTOM_ALIGN, null, null); 227 228 if (panel instanceof JComponent ) { 230 if (mode) { 232 ((JComponent )panel).putClientProperty("xml-edit-mode", "new"); } else { 234 ((JComponent )panel).putClientProperty("xml-edit-mode", "edit"); } 236 } 237 238 DialogDisplayer.getDefault ().createDialog (dd).setVisible(true); 239 240 if (dd.getValue() != DialogDescriptor.OK_OPTION) { 241 return null; 242 } 243 return treeNode; 244 } 245 246 } 247 | Popular Tags |