1 19 20 package org.netbeans.modules.j2ee.persistence.editor.completion; 21 22 import org.netbeans.api.db.explorer.ConnectionManager; 23 import org.netbeans.api.db.explorer.DatabaseConnection; 24 import org.netbeans.api.editor.completion.Completion; 25 import org.netbeans.editor.BaseDocument; 26 import org.netbeans.editor.ext.CompletionQuery; 27 import org.netbeans.spi.editor.completion.*; 28 29 import java.awt.Color ; 30 import java.awt.Component ; 31 import java.awt.Font ; 32 import java.awt.Graphics ; 33 import java.awt.event.KeyEvent ; 34 import javax.swing.text.BadLocationException ; 35 import javax.swing.text.JTextComponent ; 36 37 41 42 public abstract class NNResultItem implements CompletionQuery.ResultItem, CompletionItem { 43 44 protected int substituteOffset = -1; 45 46 public abstract String getItemText(); 47 48 public int getSubstituteOffset() { 49 return substituteOffset; 50 } 51 52 public boolean substituteCommonText(JTextComponent c, int offset, int len, int subLen) { 53 return false; 57 } 58 59 public boolean substituteText(JTextComponent c, int offset, int len, boolean shift) { 60 BaseDocument doc = (BaseDocument)c.getDocument(); 61 String text = getItemText(); 62 63 if (text != null) { 64 if (toAdd != null && !toAdd.equals("\n")) text += toAdd; 66 doc.atomicLock(); 68 try { 69 String textToReplace = doc.getText(offset, len); 70 if (text.equals(textToReplace)) return false; 71 72 if(!text.startsWith("\"")) { 74 text = quoteText(text); 75 } 76 77 char ch = doc.getText(offset + len, 1).charAt(0); 79 if(ch == '"') { 80 len++; 82 } 83 84 doc.remove(offset, len); 85 doc.insertString(offset, text, null); 86 } catch (BadLocationException e) { 87 e.printStackTrace(); 89 } finally { 90 doc.atomicUnlock(); 91 } 92 return true; 93 94 } else { 95 return false; 96 } 97 } 98 99 public Component getPaintComponent(javax.swing.JList list, boolean isSelected, boolean cellHasFocus) { 100 Component ret = getPaintComponent(isSelected); 101 if (ret==null) return null; 102 if (isSelected) { 103 ret.setBackground(list.getSelectionBackground()); 104 ret.setForeground(list.getSelectionForeground()); 105 } else { 106 ret.setBackground(list.getBackground()); 107 ret.setForeground(list.getForeground()); 108 } 109 ret.getAccessibleContext().setAccessibleName(getItemText()); 110 ret.getAccessibleContext().setAccessibleDescription(getItemText()); 111 return ret; 112 } 113 114 public abstract Component getPaintComponent(boolean isSelected); 115 116 public int getPreferredWidth(Graphics g, Font defaultFont) { 117 Component renderComponent = getPaintComponent(false); 118 return renderComponent.getPreferredSize().width; 119 } 120 121 public void render(Graphics g, Font defaultFont, Color defaultColor, 122 Color backgroundColor, int width, int height, boolean selected) { 123 Component renderComponent = getPaintComponent(selected); 124 renderComponent.setFont(defaultFont); 125 renderComponent.setForeground(defaultColor); 126 renderComponent.setBackground(backgroundColor); 127 renderComponent.setBounds(0, 0, width, height); 128 ((NNPaintComponent)renderComponent).paintComponent(g); 129 } 130 131 public String toString() { 132 return getItemText(); 133 } 134 135 137 public static final String COMPLETION_SUBSTITUTE_TEXT= "completion-substitute-text"; 139 static String toAdd; 140 141 public void processKeyEvent(KeyEvent evt) { 142 if (evt.getID() == KeyEvent.KEY_TYPED) { 143 Completion completion = Completion.get(); 144 switch (evt.getKeyChar()) { 145 case ' ': 146 if (evt.getModifiers() == 0) { 147 completion.hideCompletion(); 148 completion.hideDocumentation(); 149 } 150 break; 151 case ';': 152 case ',': 153 case '(': 154 completion.hideCompletion(); 155 completion.hideDocumentation(); 156 case '.': 157 if (defaultAction((JTextComponent )evt.getSource(), Character.toString(evt.getKeyChar()))) { 158 evt.consume(); 159 break; 160 } 161 } 162 } 163 } 164 165 protected String quoteText(String s) { 166 return "\"" + s + "\""; 167 } 168 169 public CharSequence getSortText() { 170 return getItemText(); 171 } 172 173 public CharSequence getInsertPrefix() { 174 return getItemText(); 175 } 176 177 public CompletionTask createDocumentationTask() { 178 return null; 179 } 180 181 public CompletionTask createToolTipTask() { 182 return null; 183 } 184 185 public boolean instantSubstitution(JTextComponent c) { 186 Completion completion = Completion.get(); 187 completion.hideCompletion(); 188 completion.hideDocumentation(); 189 defaultAction(c); 190 return true; 191 } 192 193 public void defaultAction(JTextComponent component) { 194 Completion completion = Completion.get(); 195 completion.hideCompletion(); 196 completion.hideDocumentation(); 197 defaultAction(component, ""); 198 } 199 200 private boolean defaultAction(JTextComponent component, String addText) { 201 int substOffset = substituteOffset; 202 if (substOffset == -1) 203 substOffset = component.getCaret().getDot(); 204 NNResultItem.toAdd = addText; 205 return substituteText(component, substOffset, component.getCaret().getDot() - substOffset, false); 206 } 207 208 private abstract static class DBElementItem extends NNResultItem { 209 210 private String name; 211 private boolean quote; 212 213 protected static NNPaintComponent.DBElementPaintComponent paintComponent = null; 214 215 public DBElementItem(String name, boolean quote, int substituteOffset) { 217 this.name = name; 218 this.quote = quote; 219 this.substituteOffset = substituteOffset; 220 } 221 222 public DBElementItem(String name, boolean quote) { 223 this(name, quote, -1); 224 } 225 226 protected String getName() { 227 return name; 228 } 229 230 protected boolean getQuoted() { 231 return quote; 232 } 233 234 public String getItemText() { 235 if (quote) { 236 return quoteText(name); 237 } else { 238 return name; 239 } 240 } 241 242 public Component getPaintComponent(boolean isSelected) { 243 if (paintComponent == null) { 244 paintComponent = new NNPaintComponent.DBElementPaintComponent(); 245 } 246 paintComponent.setString(getTypeName() + ": " + name); paintComponent.setSelected(isSelected); 248 return paintComponent; 249 } 250 251 public Object getAssociatedObject() { 252 return this; 253 } 254 255 258 public abstract String getTypeName(); 259 } 260 261 public static final class PersistenceUnitElementItem extends DBElementItem { 262 263 protected static NNPaintComponent.PersistenceUnitElementPaintComponent paintComponent = null; 264 265 public PersistenceUnitElementItem(String name, boolean quote, int substituteOffset) { 266 super(name, quote, substituteOffset); 267 } 268 269 public String getTypeName() { 270 return "Persistence Unit"; 271 } 272 273 public int getSortPriority() { 274 return 100; 275 } 276 277 public Component getPaintComponent(boolean isSelected) { 278 if (paintComponent == null) { 279 paintComponent = new NNPaintComponent.PersistenceUnitElementPaintComponent(); 280 } 281 paintComponent.setContent(getName()); 282 paintComponent.setSelected(isSelected); 283 return paintComponent; 284 } 285 286 } 287 288 public static final class EntityPropertyElementItem extends DBElementItem { 289 290 protected static NNPaintComponent.EntityPropertyElementPaintComponent paintComponent = null; 291 292 public EntityPropertyElementItem(String name, boolean quote, int substituteOffset) { 293 super(name, quote, substituteOffset); 294 } 295 296 public String getTypeName() { 297 return "Persistence Unit"; 298 } 299 300 public int getSortPriority() { 301 return 100; 302 } 303 304 public Component getPaintComponent(boolean isSelected) { 305 if (paintComponent == null) { 306 paintComponent = new NNPaintComponent.EntityPropertyElementPaintComponent(); 307 } 308 paintComponent.setContent(getName()); 309 paintComponent.setSelected(isSelected); 310 return paintComponent; 311 } 312 313 } 314 315 public static final class CatalogElementItem extends DBElementItem { 316 317 public CatalogElementItem(String name, boolean quote, int substituteOffset) { 318 super(name, quote, substituteOffset); 319 } 320 321 public String getTypeName() { 322 return "Catalog"; 323 } 324 325 public int getSortPriority() { 326 return 100; 327 } 328 } 329 330 public static final class SchemaElementItem extends DBElementItem { 331 332 public SchemaElementItem(String name, boolean quote, int substituteOffset) { 333 super(name, quote, substituteOffset); 334 } 335 336 public String getTypeName() { 337 return "Schema"; 338 } 339 340 public int getSortPriority() { 341 return 100; 342 } 343 } 344 345 public static final class TableElementItem extends DBElementItem { 346 347 protected static NNPaintComponent.TableElementPaintComponent paintComponent = null; 348 349 public TableElementItem(String name, boolean quote, int substituteOffset) { 350 super(name, quote, substituteOffset); 351 } 352 353 public String getTypeName() { 354 return "Table"; 355 } 356 357 public int getSortPriority() { 358 return 100; 359 } 360 361 public Component getPaintComponent(boolean isSelected) { 362 if (paintComponent == null) { 363 paintComponent = new NNPaintComponent.TableElementPaintComponent(); 364 } 365 paintComponent.setContent(getName()); 366 paintComponent.setSelected(isSelected); 367 return paintComponent; 368 } 369 370 public Object getAssociatedObject() { 371 return this; 372 } 373 } 374 375 public static final class ColumnElementItem extends DBElementItem { 376 377 private String tableName; 378 379 protected static NNPaintComponent.ColumnElementPaintComponent paintComponent = null; 380 381 public ColumnElementItem(String columnName, String tableName, boolean quote, int substituteOffset) { 382 super(columnName, quote, substituteOffset); 383 this.tableName = tableName; 384 } 385 386 public String getTypeName() { 387 return "Column"; 388 } 389 390 public int getSortPriority() { 391 return 100; 392 } 393 394 public String getItemText() { 395 return "\"" + getName() + "\""; } 397 398 public Component getPaintComponent(boolean isSelected) { 399 if (paintComponent == null) { 400 paintComponent = new NNPaintComponent.ColumnElementPaintComponent(); 401 } 402 paintComponent.setContent(getName(), tableName); 403 paintComponent.setSelected(isSelected); 404 return paintComponent; 405 } 406 407 public Object getAssociatedObject() { 408 return this; 409 } 410 411 } 412 413 public static final class NoConnectionElementItem extends NNResultItem { 414 415 private static NNPaintComponent.NoConnectionItemPaintComponent paintComponent = null; 416 private DatabaseConnection dbconn; 417 418 public NoConnectionElementItem(DatabaseConnection dbconn) { 419 this.dbconn = dbconn; 420 } 421 422 public int getSortPriority() { 423 return 1; 424 } 425 426 public String getItemText() { 427 return ""; 428 } 429 430 public boolean substituteText(JTextComponent c, int offset, int len, boolean shift) { 431 ConnectionManager.getDefault().showConnectionDialog(dbconn); 432 return false; 433 } 434 435 public Component getPaintComponent(boolean isSelected) { 436 if (paintComponent == null) { 437 paintComponent = new NNPaintComponent.NoConnectionItemPaintComponent(); 438 } 439 paintComponent.setSelected(isSelected); 440 return paintComponent; 441 } 442 443 public Object getAssociatedObject() { 444 return this; 445 } 446 447 } 448 449 } 450 | Popular Tags |