1 19 20 package org.netbeans.modules.palette; 21 import javax.swing.text.BadLocationException ; 22 import javax.swing.text.Caret ; 23 import javax.swing.text.Document ; 24 import javax.swing.text.JTextComponent ; 25 import org.openide.text.ActiveEditorDrop; 26 import org.openide.util.lookup.InstanceContent; 27 28 32 class ActiveEditorDropDefaultProvider implements InstanceContent.Convertor<String ,ActiveEditorDrop> { 33 34 private static ActiveEditorDropDefaultProvider instance = new ActiveEditorDropDefaultProvider(); 35 36 37 private ActiveEditorDropDefaultProvider() { 38 } 39 40 static ActiveEditorDropDefaultProvider getInstance() { 41 return instance; 42 } 43 44 public Class <? extends ActiveEditorDrop> type(String obj) { 45 return ActiveEditorDrop.class; 47 } 48 49 public String id(String obj) { 50 return obj; 51 } 52 53 public String displayName(String obj) { 54 return obj; 55 } 56 57 public ActiveEditorDrop convert(String obj) { 58 return getActiveEditorDrop(obj); 59 } 60 61 private ActiveEditorDrop getActiveEditorDrop(String body) { 62 63 return new ActiveEditorDropDefault(body); 64 } 65 66 private static class ActiveEditorDropDefault implements ActiveEditorDrop { 67 68 String body; 69 70 public ActiveEditorDropDefault(String body) { 71 this.body = body; 72 } 73 74 public boolean handleTransfer(JTextComponent targetComponent) { 75 76 if (targetComponent == null) 77 return false; 78 79 try { 80 Document doc = targetComponent.getDocument(); 81 Caret caret = targetComponent.getCaret(); 82 int p0 = Math.min(caret.getDot(), caret.getMark()); 83 int p1 = Math.max(caret.getDot(), caret.getMark()); 84 doc.remove(p0, p1 - p0); 85 86 int start = caret.getDot(); 88 doc.insertString(start, body, null); 89 } 90 catch (BadLocationException ble) { 91 return false; 92 } 93 94 return true; 95 } 96 } 97 98 } 99 | Popular Tags |