1 19 20 package org.netbeans.modules.java.bridge; 21 22 import java.beans.PropertyChangeEvent ; 23 24 import org.openide.src.*; 25 26 import org.netbeans.api.mdr.events.*; 27 import org.netbeans.jmi.javamodel.JavaPackage; 28 29 public class ImportImpl extends ElementImpl implements ImportElement.Impl { 30 31 SourceElementImpl sourceImpl; 32 33 private ElementImpl.ElementListener importListener; 34 35 private static final long serialVersionUID = -3319955190266278185L; 36 37 ImportImpl(DefaultLangModel model, org.netbeans.jmi.javamodel.Import imp) { 38 super(model, imp); 39 } 40 41 public void connectListener () { 42 importListener = new ImportListener (this); 43 importListener.connect (); 44 } 45 46 protected void createFromModel(Element model) throws SourceException { 47 ImportElement i = (ImportElement)model; 48 setImport(i.getImport()); 49 } 50 51 public void setParent(ElementImpl impl) { 52 sourceImpl = (SourceElementImpl)impl; 53 } 54 55 public Import getImport() { 56 repository.beginTrans(false); 57 try { 58 if (javaElement.isValid()) { 59 setClassPath(); 60 org.netbeans.jmi.javamodel.Import importStatement = (org.netbeans.jmi.javamodel.Import) javaElement; 61 String impName = importStatement.getName (); 62 if (impName == null) impName = ""; 64 Identifier id = Identifier.create (impName); 65 return new Import (id, importStatement.getImportedNamespace() instanceof JavaPackage ? Import.PACKAGE : Import.CLASS); } else { 67 return null; 68 } 69 } finally { 70 repository.endTrans(false); 71 } 72 } 73 74 public SourceElementImpl findSource() { 75 return sourceImpl; 76 } 77 78 public void setImport(Import imp) throws SourceException { 79 checkWritable(false); 80 checkDocument(); 81 org.netbeans.jmi.javamodel.Import importStatement = (org.netbeans.jmi.javamodel.Import) javaElement; 82 boolean failed = true; 83 repository.beginTrans (true); 84 try { 85 if (javaElement.isValid()) { 86 setClassPath(); 87 boolean isNamed = !importStatement.getName ().endsWith(".*"); String impName = importStatement.getName (); 89 if ((isNamed == imp.isClass ()) && 90 (imp.getIdentifier().getSourceName().equals(impName))) { 91 failed = false; 92 return; 93 } 94 Import old = new Import (Identifier.create (impName), 95 isNamed ? Import.CLASS : Import.PACKAGE); 96 PropertyChangeEvent evt = new PropertyChangeEvent ( 97 getElement(), "import", old, imp); checkVetoablePropertyChange(evt); 99 100 failed = false; 102 } else { 103 failed = false; 104 throwIsInvalid (); 105 } 106 } finally { 107 repository.endTrans (failed); 108 } 109 } 110 111 protected Element cloneSelf() { 112 ImportElement.MemoryImpl impl = new ImportElement.MemoryImpl(); 113 try { 114 impl.setImport(getImport()); 115 } catch (SourceException e) { 116 } 117 return new ImportElement(impl, null); 118 } 119 120 public void fireImportChange (Import oldValue, Import newValue) { 121 if (oldValue.equals (newValue)) 122 return; 123 PropertyChangeEvent evt = new PropertyChangeEvent ( 124 getElement(), "import", oldValue, newValue); fireOwnPropertyChange(evt); 126 127 ImportElement old = (ImportElement) cloneSelf (); 128 try { 129 old.setImport (oldValue); 130 } catch (SourceException e) { 131 e.printStackTrace (); 132 } 133 notifyConnectionChange (old); 134 } 135 136 public Object readResolve() { 137 return null; 138 } 139 140 public Object writeReplace() { 141 return null; 142 } 143 144 protected boolean parentValid() { 145 return sourceImpl != null && sourceImpl.isValid(); 146 } 147 148 protected void checkWritable(boolean unsafeOp) throws SourceException { 149 if (sourceImpl != null) { 150 sourceImpl.checkWritable(unsafeOp); 151 } 152 } 153 154 156 static class ImportListener extends ElementImpl.ElementListener { 157 158 Import value; 159 160 ImportListener (ImportImpl impl) { 161 super (impl); 162 value = impl.getImport (); 163 } 164 165 public void doChange (MDRChangeEvent event) { 166 super.doChange (event); 167 if ((source == javaElement) && (event instanceof AttributeEvent)) { 168 AttributeEvent attrEvent = (AttributeEvent) event; 169 String attrName = attrEvent.getAttributeName (); 170 if (attrName.equals ("name")) { String name = (String ) attrEvent.getNewElement (); 172 if (name == null) { 173 name = ""; 174 } 175 boolean isClass = true; 176 if (name.endsWith(".*")) { isClass = false; 178 name = name.substring (0, name.length() - 2); 179 } 180 Import old = value; 181 value = new Import (Identifier.create (name), isClass); 182 ((ImportImpl) impl).fireImportChange (old, value); 183 } 184 } 185 } 186 } 187 188 } 189 | Popular Tags |