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.Dimension ; 31 import java.awt.event.ActionEvent ; 32 import java.awt.event.ActionListener ; 33 import java.beans.FeatureDescriptor ; 34 import java.beans.PropertyEditorSupport ; 35 import java.util.ArrayList ; 36 import java.util.Collection ; 37 import java.util.HashMap ; 38 import java.util.List ; 39 import org.netbeans.modules.xml.schema.model.GlobalSimpleType; 40 import org.netbeans.modules.xml.schema.model.SchemaComponent; 41 import org.netbeans.modules.xml.schema.ui.nodes.schema.UnionNode; 42 import org.netbeans.modules.xml.xam.dom.NamedComponentReference; 43 import org.netbeans.modules.xml.xam.Named; 44 import org.openide.DialogDescriptor; 45 import org.openide.DialogDisplayer; 46 import org.openide.ErrorManager; 47 import org.openide.explorer.propertysheet.ExPropertyEditor; 48 import org.openide.explorer.propertysheet.PropertyEnv; 49 import org.openide.util.NbBundle; 50 51 55 public class MemberTypesEditor extends PropertyEditorSupport 56 implements ExPropertyEditor { 57 58 private MemberTypesPanel panel; 59 private DialogDescriptor descriptor; 60 private SchemaComponent component; 61 62 65 public MemberTypesEditor(SchemaComponent component) { 66 this.component = component; 67 } 68 69 public String getAsText() { 70 Object val = super.getValue(); 71 if (val == null){ 72 return null; 73 } 74 if(val instanceof Collection ) { 75 boolean first = true; 76 String str = ""; 77 for(Object elem: (Collection )val) { 78 if (elem instanceof NamedComponentReference){ 79 Object obj = ((NamedComponentReference)elem).get(); 80 if (obj instanceof Named){ 81 if(first) { 82 str = str.concat(((Named)obj).getName()); 83 first = false; 84 } else { 85 str = str.concat(" ").concat(((Named)obj).getName()); 86 } 87 } 88 } 89 } 90 return str; 91 } 92 return val.toString(); 94 } 95 96 @SuppressWarnings ("unchecked") 97 public Collection <NamedComponentReference<GlobalSimpleType>> getValue() { 98 return (Collection <NamedComponentReference<GlobalSimpleType>>) super.getValue(); 99 } 100 101 private Collection <NamedComponentReference<GlobalSimpleType>> getCurrentSelection( 102 List <GlobalSimpleType> newSelectionList) { 103 if(newSelectionList != null && !newSelectionList.isEmpty()) { 104 Collection <NamedComponentReference<GlobalSimpleType>> newSelectionRef = 105 new ArrayList <NamedComponentReference<GlobalSimpleType>>(); 106 HashMap <GlobalSimpleType,NamedComponentReference<GlobalSimpleType>> gstToRefMap = 107 new HashMap <GlobalSimpleType,NamedComponentReference<GlobalSimpleType>>(); 108 Collection <NamedComponentReference<GlobalSimpleType>> oldSelectionRef = getValue(); 109 if(oldSelectionRef!=null) { 110 for(NamedComponentReference<GlobalSimpleType> ref:oldSelectionRef) { 111 GlobalSimpleType gst = ref.get(); 112 gstToRefMap.put(gst,ref); 113 } 114 } 115 for (Object obj:newSelectionList) { 116 if (gstToRefMap.containsKey(obj)) { 117 newSelectionRef.add(gstToRefMap.get(obj)); 118 } else { 119 newSelectionRef.add(component.getModel().getFactory(). 120 createGlobalReference((GlobalSimpleType)obj, 121 GlobalSimpleType.class, component)); 122 } 123 } 124 return newSelectionRef; 125 } 126 return null; 127 } 128 129 public Component getCustomEditor() { 130 Collection <NamedComponentReference<GlobalSimpleType>> currentSelectionRef = getValue(); 131 Collection <GlobalSimpleType> currentSelection = null; 132 if(currentSelectionRef!=null) { 133 currentSelection = new ArrayList <GlobalSimpleType>(); 134 for(NamedComponentReference ref :currentSelectionRef) { 135 currentSelection.add(((GlobalSimpleType)ref.get())); 136 } 137 } 138 panel = new MemberTypesPanel(component.getModel(), currentSelection); 139 descriptor = new DialogDescriptor(panel, 140 NbBundle.getMessage(MemberTypesEditor.class, 141 "LBL_Custom_Property_Editor_Title", 142 new Object [] {NbBundle.getMessage(UnionNode.class, "LBL_UnionNode_TypeDisplayName") 143 , NbBundle.getMessage(UnionNode.class, "PROP_MemberTypes_DisplayName")}), 144 true, 145 new ActionListener () { 146 public void actionPerformed(ActionEvent evt) { 147 if (evt.getSource().equals(DialogDescriptor.OK_OPTION)) { 148 try { 149 setValue(getCurrentSelection(panel.getCurrentSelection())); 150 } catch (IllegalArgumentException iae) { 151 ErrorManager.getDefault().annotate(iae, ErrorManager.USER, 152 iae.getMessage(), iae.getLocalizedMessage(), 153 null, new java.util.Date ()); 154 throw iae; 155 } 156 } 157 } 158 } 159 ); 160 161 Dialog dlg = DialogDisplayer.getDefault().createDialog(descriptor); 162 dlg.setPreferredSize(new Dimension (400,300)); 163 return dlg; 164 } 165 166 167 public boolean supportsCustomEditor() { 168 return true; 169 } 170 171 public void attachEnv(PropertyEnv env ) { 172 FeatureDescriptor desc = env.getFeatureDescriptor(); 173 desc.setValue("canEditAsText", Boolean.FALSE); } 176 177 } 178 | Popular Tags |