1 19 20 package org.netbeans.modules.web.core.syntax.completion; 21 22 import java.awt.Font ; 23 import java.awt.Graphics ; 24 import java.net.URL ; 25 import org.netbeans.api.editor.completion.Completion; 26 import org.netbeans.editor.BaseDocument; 27 import org.netbeans.editor.ext.CompletionQuery; 28 import org.netbeans.spi.editor.completion.*; 29 import org.netbeans.spi.editor.completion.support.AsyncCompletionTask; 30 31 import java.awt.Color ; 32 import java.awt.Component ; 33 import java.awt.event.KeyEvent ; 34 import javax.swing.text.BadLocationException ; 35 import javax.swing.text.JTextComponent ; 36 import javax.swing.*; 37 38 39 44 45 public abstract class ResultItem implements CompletionQuery.ResultItem, CompletionItem { 46 47 static String toAdd; 48 public static final String COMPLETION_SUBSTITUTE_TEXT= "completion-substitute-text"; 50 protected int selectionStartOffset = -1; 51 protected int selectionEndOffset = -1; 52 private int substituteOffset = -1; 54 protected boolean shift = false; 55 56 public int getSubstituteOffset() { 57 return substituteOffset; 58 } 59 60 public void setSubstituteOffset(int offset) { 61 this.substituteOffset = offset; 62 } 63 64 public abstract String getItemText(); 65 66 public abstract Component getPaintComponent(boolean isSelected); 67 68 public abstract int getSortPriority(); 69 70 public CharSequence getSortText() { 71 return getItemText(); 72 } 73 74 public void processKeyEvent(KeyEvent e) { 75 shift = (e.getKeyCode() == KeyEvent.VK_ENTER && e.getID() == KeyEvent.KEY_PRESSED && e.isShiftDown()); 76 } 77 78 public boolean substituteCommonText(JTextComponent c, int offset, int len, int subLen) { 79 return false; 83 } 84 85 public boolean substituteText(JTextComponent c, int offset, int len, boolean shift) { 87 BaseDocument doc = (BaseDocument)c.getDocument(); 88 String text = getItemText(); 89 90 if (text != null) { 91 if (toAdd != null && !toAdd.equals("\n")) text += toAdd; 93 doc.atomicLock(); 95 try { 96 String textToReplace = doc.getText(offset, len); 97 if (text.equals(textToReplace)) return false; 98 99 doc.remove(offset, len); 100 doc.insertString(offset, text, null); 101 if (selectionStartOffset >= 0) { 102 c.select(offset + selectionStartOffset, 103 offset + selectionEndOffset); 104 } 105 } catch (BadLocationException e) { 106 } finally { 108 doc.atomicUnlock(); 109 } 110 } 111 112 return true; 113 } 114 115 public Component getPaintComponent(javax.swing.JList list, boolean isSelected, boolean cellHasFocus) { 116 Component ret = getPaintComponent(isSelected); 117 if (ret==null) return null; 118 if (isSelected) { 119 ret.setBackground(list.getSelectionBackground()); 120 ret.setForeground(list.getSelectionForeground()); 121 } else { 122 ret.setBackground(list.getBackground()); 123 ret.setForeground(list.getForeground()); 124 } 125 ret.getAccessibleContext().setAccessibleName(getItemText()); 126 ret.getAccessibleContext().setAccessibleDescription(getItemText()); 127 return ret; 128 } 129 130 public int getPreferredWidth(Graphics g, Font defaultFont) { 131 Component renderComponent = getPaintComponent(false); 132 return renderComponent.getPreferredSize().width; 133 } 134 135 public void render(Graphics g, Font defaultFont, Color defaultColor, 136 Color backgroundColor, int width, int height, boolean selected) { 137 Component renderComponent = getPaintComponent(selected); 138 renderComponent.setFont(defaultFont); 139 renderComponent.setForeground(defaultColor); 140 renderComponent.setBackground(backgroundColor); 141 renderComponent.setBounds(0, 0, width, height); 142 ((ResultItemPaintComponent)renderComponent).paintComponent(g); 143 } 144 145 public boolean instantSubstitution(JTextComponent c) { 146 defaultAction(c); 147 return true; 148 } 149 150 public CompletionTask createDocumentationTask() { 151 return new AsyncCompletionTask(new JspCompletionProvider.DocQuery(this)); 152 } 153 154 public abstract URL getHelpURL(); 155 156 159 public abstract String getHelp(); 160 161 162 public abstract boolean hasHelp(); 163 164 public CompletionTask createToolTipTask() { 165 return null; 166 } 167 168 public int getImportance() { 169 return 0; 170 } 171 172 public void defaultAction(JTextComponent component) { 173 int substOffset = getSubstituteOffset(); 174 if (substOffset == -1) 175 substOffset = component.getCaret().getDot(); 176 177 if(!shift) Completion.get().hideAll(); 178 substituteText(component, substOffset, component.getCaret().getDot() - substOffset, shift); 179 } 180 181 } 182 | Popular Tags |