KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > ActiveEditorDropDefaultProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.palette;
21 import javax.swing.text.BadLocationException JavaDoc;
22 import javax.swing.text.Caret JavaDoc;
23 import javax.swing.text.Document JavaDoc;
24 import javax.swing.text.JTextComponent JavaDoc;
25 import org.openide.text.ActiveEditorDrop;
26 import org.openide.util.lookup.InstanceContent;
27
28 /**
29  *
30  * @author Libor Kotouc
31  */

32 class ActiveEditorDropDefaultProvider implements InstanceContent.Convertor<String JavaDoc,ActiveEditorDrop> {
33
34     private static ActiveEditorDropDefaultProvider instance = new ActiveEditorDropDefaultProvider();
35
36     /** Creates a new instance of ActiveEditorDropDefaultProvider */
37     private ActiveEditorDropDefaultProvider() {
38     }
39     
40     static ActiveEditorDropDefaultProvider getInstance() {
41         return instance;
42     }
43     
44     public Class JavaDoc<? extends ActiveEditorDrop> type(String JavaDoc obj) {
45         //able to convert String instances only
46
return ActiveEditorDrop.class;
47     }
48
49     public String JavaDoc id(String JavaDoc obj) {
50         return obj;
51     }
52
53     public String JavaDoc displayName(String JavaDoc obj) {
54         return obj;
55     }
56
57     public ActiveEditorDrop convert(String JavaDoc obj) {
58         return getActiveEditorDrop(obj);
59     }
60     
61     private ActiveEditorDrop getActiveEditorDrop(String JavaDoc body) {
62
63         return new ActiveEditorDropDefault(body);
64     }
65     
66     private static class ActiveEditorDropDefault implements ActiveEditorDrop {
67
68         String JavaDoc body;
69
70         public ActiveEditorDropDefault(String JavaDoc body) {
71             this.body = body;
72         }
73
74         public boolean handleTransfer(JTextComponent JavaDoc targetComponent) {
75
76             if (targetComponent == null)
77                 return false;
78
79             try {
80                 Document JavaDoc doc = targetComponent.getDocument();
81                 Caret JavaDoc 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                 //replace selected text by the inserted one
87
int start = caret.getDot();
88                 doc.insertString(start, body, null);
89             }
90             catch (BadLocationException JavaDoc ble) {
91                 return false;
92             }
93
94             return true;
95         }
96     }
97     
98 }
99
Popular Tags