1 19 20 package org.netbeans.modules.xml.text.completion; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Font ; 25 import java.awt.Graphics ; 26 import java.awt.event.KeyEvent ; 27 import java.io.IOException ; 28 29 import javax.swing.text.*; 30 import javax.swing.Icon ; 31 32 import org.netbeans.editor.*; 33 import org.netbeans.editor.Utilities; 35 import javax.swing.JLabel ; 36 import org.netbeans.api.editor.completion.Completion; 37 import org.netbeans.spi.editor.completion.CompletionItem; 38 39 import org.netbeans.editor.ext.CompletionQuery.ResultItem; 40 import org.netbeans.editor.ext.ExtFormatter; 41 import org.netbeans.spi.editor.completion.CompletionTask; 42 import org.netbeans.spi.editor.completion.support.AsyncCompletionTask; 43 44 50 class XMLResultItem implements ResultItem, CompletionItem { 51 52 private static final int XML_ITEMS_SORT_PRIORITY = 20; 53 public String displayText; 55 private String replacementText; 56 public javax.swing.Icon icon; 58 public Color foreground = Color.black; 59 public Color background = Color.white; 60 public Color selectionForeground = Color.black; 61 public Color selectionBackground = new Color (204, 204, 255); 62 private static JLabel rubberStamp = new JLabel (); 63 private XMLCompletionResultItemPaintComponent component; 64 private boolean shift = false; 65 66 static { 67 rubberStamp.setOpaque( true ); 68 } 69 70 74 public XMLResultItem(String replacementText){ 75 this(replacementText, null); 76 } 77 78 81 public XMLResultItem(String replacementText, String displayText) { 82 this.replacementText = replacementText; 83 this.displayText = displayText != null ? displayText : replacementText; 84 } 85 86 95 public XMLResultItem(String displayText, javax.swing.Icon icon, Color foreground, Color background, Color selectionForeground, Color selectionBackground) { 96 this.displayText = displayText; 97 this.icon = icon; 98 this.foreground = foreground; 99 this.background = background; 100 this.selectionForeground = selectionForeground; 101 this.selectionBackground = selectionBackground; 102 } 103 104 107 public String getReplacementText(int modifiers){ 108 return displayText; 109 } 110 111 112 protected Icon getIcon(){ 113 return icon; 114 } 115 116 123 boolean replaceText( JTextComponent component, String text, int offset, int len) { 124 BaseDocument doc = (BaseDocument)component.getDocument(); 125 doc.atomicLock(); 126 try { 127 String currentText = doc.getText(offset, (doc.getLength() - offset) < text.length() ? (doc.getLength() - offset) : text.length()) ; 128 if(!text.equals(currentText)) { 129 doc.remove( offset, len ); 130 doc.insertString( offset, text, null); 131 } else { 132 int newCaretPos = component.getCaret().getDot() + text.length() - len; 133 component.setCaretPosition(newCaretPos < doc.getLength() ? newCaretPos : doc.getLength()); 139 } 140 } catch( BadLocationException exc ) { 143 return false; } finally { 147 doc.atomicUnlock(); 148 } 149 return true; 150 } 151 152 public boolean substituteCommonText( JTextComponent c, int offset, int len, int subLen ) { 153 return replaceText( c, getReplacementText(0).substring( 0, subLen ), offset, len ); 154 } 155 156 159 public final boolean substituteText( JTextComponent c, int offset, int len, boolean shift ) { 160 int modifier = shift ? java.awt.event.InputEvent.SHIFT_MASK : 0; 161 return substituteText(c, offset, len, modifier); 162 } 163 164 public boolean substituteText( JTextComponent c, int offset, int len, int modifiers ){ 165 return replaceText(c, getReplacementText(modifiers), offset, len); 166 } 167 168 169 public java.awt.Component getPaintComponent( javax.swing.JList list, boolean isSelected, boolean cellHasFocus ) { 170 if (getIcon() != null) rubberStamp.setIcon(getIcon()); 172 173 rubberStamp.setText( displayText ); 174 if (isSelected) { 175 rubberStamp.setBackground(selectionBackground); 176 rubberStamp.setForeground(selectionForeground); 177 } else { 178 rubberStamp.setBackground(background); 179 rubberStamp.setForeground(foreground); 180 } 181 return rubberStamp; 182 } 183 184 public final String getItemText() { 185 return replacementText; 186 } 187 188 public String toString() { 189 return getItemText(); 190 } 191 192 Color getPaintColor() { return Color.BLUE; } 193 194 public CompletionTask createDocumentationTask() { 198 return null; } 201 202 public CompletionTask createToolTipTask() { 203 return null; 204 } 205 206 public void defaultAction(JTextComponent component) { 207 int substOffset = getSubstituteOffset(); 208 if (substOffset == -1) 209 substOffset = component.getCaretPosition(); 210 211 if(!shift) Completion.get().hideAll(); 212 substituteText(component, substOffset, component.getCaretPosition() - substOffset, shift); 213 } 214 215 static int substituteOffset = -1; 216 217 public int getSubstituteOffset() { 218 return substituteOffset; 219 } 220 221 public CharSequence getInsertPrefix() { 222 return getItemText(); 223 } 224 225 public Component getPaintComponent(boolean isSelected) { 226 XMLCompletionResultItemPaintComponent component = 227 new XMLCompletionResultItemPaintComponent.StringPaintComponent(getPaintColor()); 228 component.setSelected(isSelected); 229 component.setString(getItemText()); 230 return component; 231 } 232 233 public int getPreferredWidth(Graphics g, Font defaultFont) { 234 Component renderComponent = getPaintComponent(false); 235 return renderComponent.getPreferredSize().width; 236 } 237 238 public int getSortPriority() { 239 return XML_ITEMS_SORT_PRIORITY; 240 } 241 242 public CharSequence getSortText() { 243 return getItemText(); 244 } 245 246 public boolean instantSubstitution(JTextComponent component) { 247 defaultAction(component); 248 return true; 249 } 250 251 public void processKeyEvent(KeyEvent e) { 252 shift = (e.getKeyCode() == KeyEvent.VK_ENTER && e.getID() == KeyEvent.KEY_PRESSED && e.isShiftDown()); 253 } 254 255 public void render(Graphics g, Font defaultFont, 256 Color defaultColor, Color backgroundColor, 257 int width, int height, boolean selected) { 258 Component renderComponent = getPaintComponent(selected); 259 renderComponent.setFont(defaultFont); 260 renderComponent.setForeground(defaultColor); 261 renderComponent.setBackground(backgroundColor); 262 renderComponent.setBounds(0, 0, width, height); 263 ((XMLCompletionResultItemPaintComponent)renderComponent).paintComponent(g); 264 } 265 266 } 267 | Popular Tags |