|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.java.bridge; import java.beans.*; import java.util.*; import java.lang.ref.WeakReference; import org.openide.nodes.Node; import org.openide.src.*; import javax.jmi.reflect.InvalidObjectException; import javax.jmi.reflect.RefBaseObject; import org.netbeans.api.mdr.events.*; import org.netbeans.jmi.javamodel.Field; import org.netbeans.jmi.javamodel.IsOfType; import org.netbeans.jmi.javamodel.TypeReference; import org.netbeans.jmi.javamodel.ArrayReference; import org.netbeans.jmi.javamodel.EnumConstant; final class FieldElementImpl extends MemberElementImpl implements FieldElement.Impl { private static final long serialVersionUID = 6799964214013985830L; private ElementImpl.ElementListener fieldListener; FieldElementImpl(DefaultLangModel model, Field fieldElement) { super(model, fieldElement); javadoc = new JavaDocImpl.Field(null, this); } public void connectListener () { FieldListener fieldListener = new FieldListener (this); fieldListener.connect (); } protected void createFromModel(Element el) throws SourceException { super.createFromModel(el); FieldElement f = (FieldElement)el; setType(f.getType()); setInitValue(f.getInitValue()); setJavaDocText(f.getJavaDoc().getRawText(), true); } /** Type of the variable. * @return the type */ public Type getType() { repository.beginTrans(false); try { setClassPath(); if (javaElement instanceof EnumConstant) { return descrToType(((Field) javaElement).getType()); } return typeReferenceToType (((Field) javaElement).getTypeName ()); } catch (InvalidObjectException e) { if (fieldListener != null) return ((FieldListener)fieldListener).type; else return Type.VOID; } finally { repository.endTrans(false); } } public JavaDoc.Field getJavaDoc() { updateJavadoc(); return (JavaDoc.Field)javadoc; } public Object readResolve() { return null; } /** Setter for type of the variable. * @param type the variable type */ public void setType(Type type) throws SourceException { checkWritable(); checkDocument(); boolean failed = true; repository.beginTrans (true); try { setClassPath(); Type oldType = getType (); PropertyChangeEvent evt; type = resolveType(type); if (compareSourceTypes(oldType, type)) { failed = false; checkIsValid (); return; } evt = new PropertyChangeEvent(getElement(), PROP_TYPE, oldType, type); checkVetoablePropertyChange(evt); ((Field) javaElement).setTypeName (typeToTypeReference (type)); failed = false; } catch (InvalidObjectException e) { throwIsInvalid (); } finally { repository.endTrans (failed); } } /** Getter for the initial value. * @return initial value for the variable or empty string if it is not initialized */ public String getInitValue() { repository.beginTrans(false); try { setClassPath(); return ((Field)javaElement).getInitialValueText(); } catch (InvalidObjectException e) { return null; } finally { repository.endTrans(false); } } /** Setter for the initial value. * @param value initial value for the variable */ public void setInitValue(String newInitValue) throws SourceException { checkWritable(); checkDocument(); String oldInitValue=getInitValue(); boolean failed = true; repository.beginTrans (true); try { PropertyChangeEvent evt; setClassPath(); if ((newInitValue == oldInitValue) || ((newInitValue != null) && newInitValue.equals(oldInitValue))) { checkIsValid (); return; } evt = new PropertyChangeEvent(getElement(), PROP_INIT_VALUE, oldInitValue, newInitValue); // no constraings on the Initializer... only check vetoable listeners. checkVetoablePropertyChange(evt); ((Field)javaElement).setInitialValueText(newInitValue); failed = false; } catch (InvalidObjectException e) { throwIsInvalid (); } finally { repository.endTrans (failed); } } public void fireTypeChange (Type oldType, Type newType) { oldType = resolveType (oldType); newType = resolveType (newType); if (compareSourceTypes(oldType, newType)) return; PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_TYPE, oldType, newType); fireOwnPropertyChange(evt); FieldElement oldField = (FieldElement) cloneSelf (); try { FieldElement.Impl impl = (FieldElement.Impl) oldField.getCookie (Element.Impl.class); impl.setType (oldType); } catch (SourceException e) { e.printStackTrace (); } notifyConnectionChange (oldField); } public void fireInitValueChange (String oldVal, String newVal) { PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_INIT_VALUE, oldVal, newVal); fireOwnPropertyChange(evt); } // Utility methods. ////////////////////////////////////////////////////////////////////////////////// protected Element createWrapperElement() { return null; } public String toString() { return "FieldElementImpl[" + getName().getSourceName() + "]"; // NOI18N } protected Element cloneSelf() { return (Element)((FieldElement)getElement()).clone(); } // .......................................................................... static class FieldListener extends MemberElementImpl.MemberElementListener implements MDRPreChangeListener { // [PENDING] this code duplicates MethodListener private Type type; private Map eventsMap = new HashMap(); private Set typeRefs = new HashSet(); FieldListener (FieldElementImpl impl) { super (impl); } public void connect () { if (REGISTER_LISTENER) { super.connect (); TypeReference typeRef = ((Field) javaElement).getTypeName(); if (typeRef != null) { type = ((FieldElementImpl) getImpl ()).typeReferenceToType (typeRef); while (typeRef != null) { ((MDRChangeSource) typeRef).addListener(this); typeRefs.add(typeRef); typeRef = typeRef.getParent(); } } else { type = null; } } } public void remove () { super.remove (); for (Iterator iter = typeRefs.iterator(); iter.hasNext();) { try { ((MDRChangeSource) iter.next()).removeListener(this); } catch (InvalidObjectException e) { } } typeRefs.clear(); } public void doChange(MDRChangeEvent event) { super.doChange (event); if (event instanceof AttributeEvent) { AttributeEvent attrEvent = (AttributeEvent) event; String attrName = attrEvent.getAttributeName(); Object source = event.getSource(); if (attrName.equals("typeName") || ((source instanceof TypeReference) && // NOI18N (attrName.equals("name") || attrName.equals("dimCount") || attrName.equals("typeArguments") || attrName.equals("parent")))) { // NOI18N EventInfo info = (EventInfo) eventsMap.get(event); Type oldType = type; type = ((FieldElementImpl) getImpl ()).stringToType (info.newName); processEventInfo(info); ((FieldElementImpl) impl).fireTypeChange (oldType, type); } else if (attrName.equals("initialValueText")) { // NOI18N ((FieldElementImpl) impl).fireInitValueChange ((String)attrEvent.getOldElement(), (String)attrEvent.getNewElement()); } } } private void processEventInfo(EventInfo info) { for (Iterator iter = info.removed.iterator(); iter.hasNext();) { Object obj = iter.next(); ((MDRChangeSource) obj).removeListener(this); typeRefs.remove(obj); } for (Iterator iter = info.added.iterator(); iter.hasNext();) { Object obj = iter.next(); ((MDRChangeSource) obj).addListener(this); typeRefs.add(obj); } } private TypeReference getRoot(TypeReference typeRef) { Object comp = typeRef.refImmediateComposite(); while (comp instanceof TypeReference) { typeRef = (TypeReference)comp; comp = typeRef.refImmediateComposite(); } return typeRef; } private void typeRefToName(StringBuffer buf, TypeReference typeRef) { TypeReference parent = typeRef.getParent(); if (parent != null) { typeRefToName(buf, parent); buf.append('.'); } buf.append(typeRef.getName()); } private String typeRefToName(TypeReference typeRef) { if (typeRef == null) return null; int dimCount = 0; if (typeRef instanceof ArrayReference) { dimCount = ((ArrayReference) typeRef).getDimCount(); typeRef = typeRef.getParent(); } StringBuffer buf = new StringBuffer(); typeRefToName(buf, typeRef); if (buf.length() > 0) { while (dimCount > 0) { buf.append("[]"); // NOI18N dimCount--; } } return buf.toString(); } private void collectTypeRefs(List list, TypeReference typeRef) { while (typeRef != null) { list.add(typeRef); typeRef = typeRef.getParent(); } } public void changeCancelled(MDRChangeEvent e) { eventsMap.remove(e); } public void plannedChange(MDRChangeEvent event) { RefBaseObject source = (RefBaseObject) event.getSource (); if (event instanceof AttributeEvent) { AttributeEvent attrEvent = (AttributeEvent) event; String attrName = attrEvent.getAttributeName(); EventInfo info = new EventInfo(); if (attrName.equals ("typeName")) { // NOI18N TypeReference prev = (TypeReference) attrEvent.getOldElement (); TypeReference curr = (TypeReference) attrEvent.getNewElement (); if (event.isOfType (AttributeEvent.EVENT_ATTRIBUTE_SET)) { collectTypeRefs(info.removed, prev); collectTypeRefs(info.added, curr); info.newName = typeRefToName(curr); } else if (event.isOfType (AttributeEvent.EVENT_ATTRIBUTE_REMOVE)) { collectTypeRefs(info.removed, prev); info.newName = null; } else { // EVENT_ATTRIBUTE_ADD collectTypeRefs(info.added, curr); info.newName = typeRefToName(curr); } eventsMap.put(event, info); } else if ((source instanceof TypeReference) && (attrName.equals("name"))) { // NOI18N info.newName = typeRefToName(getRoot((TypeReference) source)); eventsMap.put(event, info); } else if (attrName.equals("parent")) { // NOI18N TypeReference prev = (TypeReference) attrEvent.getOldElement (); TypeReference curr = (TypeReference) attrEvent.getNewElement (); if (prev != null) collectTypeRefs(info.removed, prev); if (curr != null) collectTypeRefs(info.added, curr); info.newName = typeRefToName(getRoot((TypeReference) source)); eventsMap.put(event, info); } else if (attrName.equals("dimCount") || attrName.equals("typeArguments")) { // NOI18N info.newName = typeRefToName(getRoot((TypeReference) source)); eventsMap.put(event, info); } } } class EventInfo { List removed = new LinkedList(); List added = new LinkedList(); String newName; } } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.