1 19 20 package org.netbeans.modules.java.codegen; 21 22 import javax.swing.text.Position ; 23 import javax.swing.text.StyledDocument ; 24 25 import org.openide.src.*; 26 import org.openide.text.*; 27 28 import org.netbeans.modules.java.bridge.Binding; 29 30 35 class SourceB extends ElementBinding implements Binding.Source { 36 38 PositionBounds packageBounds; 39 40 42 ContainerSupport classContainer; 43 44 46 ContainerSupport importContainer; 47 48 public SourceB(SourceElement src, SourceText s) { 49 super(src, s); 50 classContainer = new ContainerSupport(s, this); 51 importContainer = new ContainerSupport(s, this); 52 importContainer.separateMembers = false; 53 } 54 55 protected Element cloneElement() { 56 return null; 57 } 58 59 public void changePackage(final Identifier id) throws SourceException { 60 if (!source.isGeneratorEnabled()) 61 return; 62 63 source.runAtomic(getElement(), new ExceptionRunnable() { 64 public void run() throws Exception { 65 PositionBounds bounds; 66 67 if (packageBounds == null) 68 bounds = findPackageBounds(); 69 else 70 bounds = packageBounds; 71 if (id == null) { 72 CodeGenerator.clearBounds(bounds, true); 73 packageBounds = null; 74 } else { 75 StringBuffer sb = new StringBuffer (); 76 77 sb.append("package "); sb.append(id.getSourceName()); 79 sb.append(";"); CodeGenerator.fillTextBounds(bounds, sb.toString()); 81 if (packageBounds == null) { 82 StyledDocument d = source.getDocument(); 84 d.insertString(bounds.getEnd().getOffset(), 85 "\n", null); } 87 packageBounds = bounds; 88 } 89 } 90 }); 91 } 92 93 public Binding.Container getImportsSection() { 94 return importContainer; 95 } 96 97 public Binding.Container getClassSection() { 98 return classContainer; 99 } 100 101 public ElementBinding findBindingAt(int pos) { 102 return classContainer.findBindingAt(pos); 103 } 104 105 108 public void remove() throws SourceException { 109 } 110 111 public void updateBounds(int kind, PositionBounds b) { 112 super.updateBounds(kind, b); 113 if (kind == BOUNDS_PACKAGE) { 115 packageBounds = b; 116 } 117 } 118 119 123 PositionBounds findContainerBounds(TextBinding.Container cont) { 124 int begin = -1, end = -1; 125 126 if (cont == importContainer) { 127 if (packageBounds != null) { 128 begin = packageBounds.getEnd().getOffset(); 129 } else if (importContainer == null || importContainer.isEmpty()) { 130 begin = 0; 132 } else { 133 begin = importContainer.getFirstPosition().getOffset(); 134 } 135 if (classContainer == null || classContainer.isEmpty()) { 136 end = -1; 137 } else { 138 end = classContainer.getFirstPosition().getOffset(); 139 } 140 } else if (cont == classContainer) { 141 if (importContainer == null || importContainer.isEmpty()) { 142 if (packageBounds == null) { 143 begin = 0; 144 } else { 145 begin = packageBounds.getEnd().getOffset(); 146 } 147 } else { 148 begin = importContainer.getLastPosition().getOffset(); 149 } 150 } else 151 throw new IllegalArgumentException (cont.toString()); 152 153 if (end == -1) { 154 StyledDocument doc = source.getEditorSupport().getDocument(); 155 if (doc == null) 156 end = begin; 157 else 158 end = doc.getLength(); 159 } 160 161 PositionBounds b = new PositionBounds( 162 source.createPos(begin, Position.Bias.Forward), 163 source.createPos(end, Position.Bias.Backward)); 164 return b; 165 } 166 167 private PositionBounds findPackageBounds() throws SourceException { 168 PositionBounds bounds = null; 169 170 if (importContainer != null && !importContainer.isEmpty()) 171 bounds = importContainer.getContainerBounds(); 172 173 if (bounds == null && !classContainer.isEmpty()) 174 bounds = classContainer.getContainerBounds(); 175 176 if (bounds != null) 177 return CodeGenerator.createNewLineBounds(bounds.getBegin(), 178 CodeGenerator.BOUNDS_PACKAGE); 179 180 return CodeGenerator.createNewLineBounds( 181 source.createPos(0, Position.Bias.Forward), 182 CodeGenerator.BOUNDS_PACKAGE); 183 } 184 185 public void updatePackageBounds(PositionBounds b) { 186 this.packageBounds = b; 187 } 188 } 189 | Popular Tags |