1 19 20 25 26 package org.netbeans.modules.xml.schema.ui.basic.editors; 27 28 import java.awt.Component ; 29 import java.awt.Dialog ; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 import java.beans.FeatureDescriptor ; 33 import java.beans.PropertyEditorSupport ; 34 import java.util.ArrayList ; 35 import java.util.Collection ; 36 import org.netbeans.modules.xml.schema.model.GlobalSimpleType; 37 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent; 38 import org.netbeans.modules.xml.schema.model.SchemaComponent; 39 import org.netbeans.modules.xml.xam.Named; 40 import org.netbeans.modules.xml.xam.dom.NamedComponentReference; 41 import org.openide.DialogDescriptor; 42 import org.openide.DialogDisplayer; 43 import org.openide.ErrorManager; 44 import org.openide.explorer.propertysheet.ExPropertyEditor; 45 import org.openide.explorer.propertysheet.PropertyEnv; 46 import org.openide.util.NbBundle; 47 48 52 public class GlobalReferenceEditor<T extends ReferenceableSchemaComponent> 53 extends PropertyEditorSupport 54 implements ExPropertyEditor { 55 56 private SchemaComponentSelectionPanel panel; 57 private DialogDescriptor descriptor; 58 private SchemaComponent component; 59 private Class <T> referenceType; 60 private String typeDisplayName; 61 private String propertyDisplayName; 62 63 64 67 public GlobalReferenceEditor(SchemaComponent component, 68 String typeDisplayName, 69 String propertyDisplayName, 70 Class <T> referenceType) { 71 this.typeDisplayName = typeDisplayName; 72 this.propertyDisplayName = propertyDisplayName; 73 this.referenceType = referenceType; 74 this.component = component; 75 } 76 77 public String getAsText() { 78 Object val = getValue(); 79 if (val == null){ 80 return null; 81 } 82 if (val instanceof NamedComponentReference && component.getModel() != null){ 83 Object obj = ((NamedComponentReference)val).get(); 84 if (obj == null){ 85 return null; 86 } 87 if (obj instanceof Named){ 88 return ((Named)obj).getName(); 89 } 90 } 91 return val.toString(); 93 } 94 95 @SuppressWarnings ("unchecked") 96 public Component getCustomEditor() { 97 NamedComponentReference<T> currentGlobalReference = (NamedComponentReference<T>) getValue(); 98 Collection <SchemaComponent> exclude = null; 99 if(referenceType.isInstance(component)) { 100 exclude = new ArrayList <SchemaComponent>(); 101 exclude.add(component); 102 } else { 103 SchemaComponent parent = component.getParent(); 104 while (parent!=null) { 105 if(referenceType.isInstance(parent)) { 106 exclude = new ArrayList <SchemaComponent>(); 107 exclude.add(parent); 108 break; 109 } 110 parent = parent.getParent(); 111 } 112 } 113 boolean primitives = GlobalSimpleType.class.isAssignableFrom(referenceType); 115 panel = new SchemaComponentSelectionPanel<T>(component.getModel(), 116 referenceType, currentGlobalReference==null?null: 117 currentGlobalReference.get(), exclude, primitives); 118 descriptor = new SchemaComponentSelDialogDesc(panel, 119 NbBundle.getMessage(GlobalReferenceEditor.class, 120 "LBL_Custom_Property_Editor_Title", 121 new Object [] {typeDisplayName, propertyDisplayName}), 122 true, 123 new ActionListener () { 124 public void actionPerformed(ActionEvent evt) { 125 if (evt.getSource().equals(DialogDescriptor.OK_OPTION)) { 126 try { 127 setValue(getCurrentSelection(panel.getCurrentSelection())); 128 } catch (IllegalArgumentException iae) { 129 ErrorManager.getDefault().annotate(iae, ErrorManager.USER, 130 iae.getMessage(), iae.getLocalizedMessage(), 131 null, new java.util.Date ()); 132 throw iae; 133 } 134 } 135 } 136 }); 137 138 Dialog dlg = DialogDisplayer.getDefault().createDialog(descriptor); 139 return dlg; 140 } 141 142 143 public boolean supportsCustomEditor() { 144 return true; 145 } 146 147 private NamedComponentReference getCurrentSelection(SchemaComponent sc){ 148 return sc==null?null: 149 sc.getModel().getFactory().createGlobalReference( 150 referenceType.cast(sc), 151 referenceType , component); 152 } 153 154 public void attachEnv(PropertyEnv env ) { 155 FeatureDescriptor desc = env.getFeatureDescriptor(); 156 desc.setValue("canEditAsText", Boolean.FALSE); } 159 160 } 161 | Popular Tags |