1 19 20 package org.netbeans.modules.java.codegen; 21 22 import java.util.*; 23 24 import javax.swing.text.Position ; 25 26 import org.openide.src.*; 27 import org.openide.text.CloneableEditorSupport; 28 import org.openide.text.PositionBounds; 29 import org.openide.text.PositionRef; 30 31 import org.netbeans.modules.java.bridge.Binding; 32 33 39 class Clazz extends Member implements Binding.Class, TextBinding.Container { 40 43 ContainerSupport container; 44 45 public Clazz(ClassElement el, SourceText s) { 46 super(el, s); 47 } 48 49 public TextBinding.Container getContainer() { 50 return this; 51 } 52 53 public boolean isEmpty() { 54 return container == null || container.isEmpty(); 55 } 56 57 public void updateChildren(Collection c) { 58 if (container == null && c.isEmpty()) 59 return; 60 initializeContainer(); 61 container.updateChildren(c); 62 } 63 64 68 public void changeClassType(boolean properClass) throws SourceException { 69 if (!source.isGeneratorEnabled()) 70 return; 71 72 ClassElement el = cloneClass(); 73 el.setClassOrInterface(properClass); 74 regenerateHeader(el); 75 } 76 77 79 public void changeInterfaces(Identifier[] replaceWith) throws SourceException { 80 if (!source.isGeneratorEnabled()) 81 return; 82 83 ClassElement el = cloneClass(); 84 el.setInterfaces(replaceWith); 85 regenerateHeader(el); 86 } 87 88 91 public void changeSuperclass(Identifier id) throws SourceException { 92 if (!source.isGeneratorEnabled()) 93 return; 94 95 ClassElement el = cloneClass(); 96 el.setSuperclass(id); 97 regenerateHeader(el); 98 } 99 100 103 protected Element cloneElement() { 104 ClassElement x = new ClassElement(); 105 ClassElement my = (ClassElement)getElement(); 106 try { 107 x.setName(my.getName()); 108 x.setModifiers(my.getModifiers()); 109 x.setSuperclass(my.getSuperclass()); 110 x.setInterfaces(my.getInterfaces()); 111 x.setClassOrInterface(my.isClassOrInterface()); 112 if (!my.getJavaDoc().isEmpty()) { 113 x.getJavaDoc().setRawText(my.getJavaDoc().getRawText()); 114 } 115 } catch (SourceException ex) { 116 } 118 return x; 119 } 120 121 private ClassElement cloneClass() { 122 return (ClassElement)cloneElement(); 123 } 124 125 protected int classifyProperty(String name) { 126 return CLASS_HEADER; 127 } 128 129 public ElementBinding findBindingAt(int pos) { 130 ElementBinding b = super.findBindingAt(pos); 131 if (b == this) { 132 ElementBinding b2 = container.findBindingAt(pos); 133 if (b2 != null) 134 return b2; 135 } 136 return b; 137 } 138 139 140 141 public boolean canInsertAfter(Binding b) { 142 if (container == null) { 143 return source.canWriteInside(bodyBounds); 144 } 145 return container.canInsertAfter(b); 146 } 147 148 150 public void reorder(Map fromToMap) throws SourceException { 151 container.reorder(fromToMap); 152 } 153 154 156 public void replace(Binding oldBinding, Binding newBinding) throws SourceException { 157 container.replace(oldBinding, newBinding); 158 } 159 160 public void changeMembers(MultiPropertyChangeEvent evt) throws SourceException { 161 if (container == null) { 162 int etype = evt.getEventType(); 163 if (etype == evt.TYPE_ADD || etype == evt.TYPE_COMPOUND) 164 initializeContainer(); 165 else 166 return; 167 } 168 container.changeMembers(evt); 169 } 170 171 177 public void insert(Binding toInitialize,Binding previous) throws SourceException { 178 initializeContainer(); 179 container.insert(toInitialize, previous); 180 } 181 182 protected void initializeContainer() { 183 if (container != null) 184 return; 185 container = new ContainerSupport(this.source, this); 186 } 187 188 public void regenerateWhole(Element el) { 189 } 190 191 PositionBounds findContainerBounds(TextBinding.Container cont) { 192 return bodyBounds; 193 } 194 195 public void create(PositionBounds bounds) throws SourceException { 196 ClassElement c = cloneClass(); 197 ClassElement orig = (ClassElement)getElement(); 198 199 wholeBounds = bounds; 201 super.regenerateWhole(c, true); 202 203 PositionRef r = bodyBounds.getBegin(); 205 boolean empty = true; 206 ElementBinding prevBinding= null; 207 208 for (int kind = 0; kind < 5; kind++) { 209 Element[] models; 210 211 switch (kind) { 212 case 0: 213 models = orig.getFields(); 214 break; 215 case 1: 216 models = orig.getInitializers(); 217 break; 218 case 2: 219 models = orig.getConstructors(); 220 break; 221 case 3: 222 models = orig.getMethods(); 223 break; 224 case 4: 225 models = orig.getClasses(); 226 break; 227 default: 228 models = null; 229 } 230 if (empty && models.length > 0) { 231 initializeContainer(); 232 empty = false; 233 } 234 235 for (int i = 0; i < models.length; i++) { 236 ElementBinding b = source.findBinding(models[i]); 237 container.insertChild(b, prevBinding, null); 238 prevBinding = b; 239 } 240 } 241 } 242 } 243 | Popular Tags |