1 19 20 package org.netbeans.lib.editor.codetemplates; 21 22 import java.awt.Color ; 23 import javax.swing.text.Document ; 24 import javax.swing.text.JTextComponent ; 25 import javax.swing.text.Position ; 26 import org.netbeans.editor.Coloring; 27 import org.netbeans.editor.DrawContext; 28 import org.netbeans.editor.DrawLayer; 29 import org.netbeans.editor.MarkFactory; 30 import org.netbeans.lib.editor.util.swing.PositionRegion; 31 32 38 final class CodeTemplateDrawLayer extends DrawLayer.AbstractLayer { 39 40 public static final String NAME = "code-template-draw-layer"; 42 public static final int VISIBILITY = 5000; 43 44 private static final Coloring COLORING = new Coloring(null, null, new Color (138, 191, 236)); 45 46 private static int instanceCounter; 47 48 private CodeTemplateParameterImpl paramImpl; 49 50 private CodeTemplateInsertHandler handler; 51 52 private int regionIndex; 53 54 private Position regionStartPosition; 55 56 private Position regionEndPosition; 57 58 private boolean colorBackground; 59 60 private Coloring coloring; 61 62 private boolean textFramePropertyAssigned; 63 64 CodeTemplateDrawLayer(CodeTemplateParameterImpl paramImpl) { 65 super(NAME + instanceCounter++); this.paramImpl = paramImpl; 67 68 handler = paramImpl.getHandler(); 69 } 70 71 public void init(DrawContext ctx) { 72 coloring = null; 73 JTextComponent c = ctx.getEditorUI().getComponent(); 74 regionStartPosition = null; 75 if (c != null) { 76 if (handler.getActiveMasterImpl() == paramImpl) { 77 colorBackground = true; 78 int startOffset = ctx.getStartOffset(); 79 regionIndex = 0; 80 SyncDocumentRegion syncRegion = paramImpl.getRegion(); 81 int regionCount = syncRegion.getRegionCount(); 82 while (regionIndex < regionCount) { 83 PositionRegion region = syncRegion.getSortedRegion(regionIndex); 84 Position startPos = region.getStartPosition(); 85 if (startOffset <= startPos.getOffset()) { 86 regionStartPosition = startPos; 87 regionEndPosition = region.getEndPosition(); 88 setNextActivityChangeOffset(regionStartPosition.getOffset()); 89 break; 90 } 91 regionIndex++; 92 } 93 } else { 94 colorBackground = false; 95 } 96 } 97 } 98 99 public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { 100 if (regionStartPosition != null) { 101 int regionStartOffset = regionStartPosition.getOffset(); 102 int regionEndOffset = regionEndPosition.getOffset(); 103 int fragmentOffset = ctx.getFragmentOffset(); 104 SyncDocumentRegion syncRegion = paramImpl.getRegion(); 105 if (fragmentOffset == regionStartOffset && regionStartOffset != regionEndOffset) { 106 if (regionStartOffset == syncRegion.getFirstRegionStartOffset()) { 109 JTextComponent c = ctx.getEditorUI().getComponent(); 110 c.putClientProperty(DrawLayer.TEXT_FRAME_START_POSITION_COMPONENT_PROPERTY, 111 regionStartPosition); 112 c.putClientProperty(DrawLayer.TEXT_FRAME_END_POSITION_COMPONENT_PROPERTY, 113 regionEndPosition); 114 textFramePropertyAssigned = true; 115 } 116 coloring = colorBackground ? COLORING : null; 117 setNextActivityChangeOffset(regionEndOffset); 118 119 } else if (fragmentOffset == regionEndOffset) { 120 coloring = null; 121 regionIndex++; 122 if (regionIndex < syncRegion.getRegionCount()) { 123 PositionRegion region = syncRegion.getSortedRegion(regionIndex); 124 regionStartPosition = region.getStartPosition(); 125 regionEndPosition = region.getEndPosition(); 126 setNextActivityChangeOffset(regionStartPosition.getOffset()); 127 } else { 128 regionStartPosition = null; 129 regionEndPosition = null; 130 } 131 JTextComponent c = ctx.getEditorUI().getComponent(); 132 resetTextFrameProperties(c); 133 } 134 return true; 135 136 } else { 137 return false; 138 } 139 } 140 141 void resetTextFrameProperties(JTextComponent c) { 142 if (textFramePropertyAssigned) { 143 c.putClientProperty(DrawLayer.TEXT_FRAME_START_POSITION_COMPONENT_PROPERTY, null); 144 c.putClientProperty(DrawLayer.TEXT_FRAME_END_POSITION_COMPONENT_PROPERTY, null); 145 textFramePropertyAssigned = false; 146 } 147 } 148 149 public void updateContext(DrawContext ctx) { 150 if (coloring != null) { 151 coloring.apply(ctx); 152 } 153 } 154 155 } 156 | Popular Tags |