001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * ActiveEditorDropCodeClipProvider.java
043: *
044: * Created on July 27, 2006, 10:16 AM
045: *
046: * Creates a customized activeEditorDrop type for items like codeclips
047: *
048: * @author Joelle Lam <joelle.lam@sun.com>
049: * @version %I%, %G%
050: */
051:
052: package org.netbeans.modules.visualweb.palette.codeclips;
053:
054: import java.util.ArrayList;
055: import java.util.MissingResourceException;
056: import java.util.ResourceBundle;
057: import javax.swing.text.BadLocationException;
058: import javax.swing.text.Caret;
059: import javax.swing.text.Document;
060: import javax.swing.text.JTextComponent;
061: import org.openide.text.ActiveEditorDrop;
062: import org.openide.util.NbBundle;
063: import org.openide.util.lookup.InstanceContent;
064:
065: /**
066: * <p>Provides an ActiveEditorDropCodeClip of type ActiveEditorDrop.</p>
067: *
068: * @author Original Author: Libor Kotouc
069: * @author Modifier: Joelle Lam <joelle.lam@sun.com>
070: */
071: class ActiveEditorDropCodeClipProvider implements
072: InstanceContent.Convertor {
073:
074: private static ActiveEditorDropCodeClipProvider instance = new ActiveEditorDropCodeClipProvider();
075:
076: /**
077: * Creates a new instance of ActiveEditorDropCodeClipProvider
078: */
079: private ActiveEditorDropCodeClipProvider() {
080: }
081:
082: static ActiveEditorDropCodeClipProvider getInstance() {
083: return instance;
084: }
085:
086: /* This method must be modified in order to change the object assigned to the
087: * converter. If you do not modify this you will get an isAssignableFrom
088: * exception.
089: */
090: public Class type(Object obj) {
091: if (obj instanceof ArrayList)
092: return ActiveEditorDrop.class;
093:
094: return null;
095: }
096:
097: public String id(Object obj) {
098: return (String) ((ArrayList) obj).get(1);
099: }
100:
101: public String displayName(Object obj) {
102: return id(obj);
103: }
104:
105: /*
106: * This is converting the object to a string but the object was
107: * received as if it were the object name.
108: * Although it is not actually the object, this provider just casts the object to a
109: * string
110: **/
111: public Object convert(Object obj) {
112:
113: ArrayList codeclipArray = (ArrayList) obj;
114:
115: String bundleName = (String) codeclipArray.get(0);
116: String body = (String) codeclipArray.get(1);
117: String displayNameKey = (String) codeclipArray.get(2);
118:
119: Object drop = getActiveEditorDrop(bundleName, displayNameKey,
120: body);
121:
122: return drop;
123: }
124:
125: private ActiveEditorDrop getActiveEditorDrop(String bundleName,
126: String displayNameKey, String body) {
127: ResourceBundle ccbundle;
128: String title;
129: // I need to do this because I need the title name.
130: if (bundleName != null) {
131:
132: ccbundle = NbBundle.getBundle(bundleName);
133: try {
134: title = ccbundle.getString(displayNameKey);
135: } catch (MissingResourceException mre) {
136: title = displayNameKey;
137: }
138: body = CodeClipUtilities.fillFromBundle(body, bundleName);
139: } else {
140: title = displayNameKey;
141: }
142:
143: ActiveEditorDropCodeClip drop = new ActiveEditorDropCodeClip(
144: title, body);
145: return drop;
146: }
147:
148: private static class ActiveEditorDropCodeClip implements
149: ActiveEditorDrop {
150:
151: String body;
152: String title;
153:
154: public ActiveEditorDropCodeClip(String title, String body) {
155: this .body = body;
156: this .title = title;
157: }
158:
159: public boolean handleTransfer(JTextComponent targetComponent) {
160: if (targetComponent == null)
161: return false;
162:
163: String finalBody = CodeClipUtilities.parseClipForParams(
164: title, body);
165:
166: try {
167: Document doc = targetComponent.getDocument();
168: Caret caret = targetComponent.getCaret();
169: int p0 = Math.min(caret.getDot(), caret.getMark());
170: int p1 = Math.max(caret.getDot(), caret.getMark());
171: doc.remove(p0, p1 - p0);
172:
173: //replace selected text by the inserted one
174: int start = caret.getDot();
175: doc.insertString(start, finalBody, null);
176: } catch (BadLocationException ble) {
177: return false;
178: }
179:
180: return true;
181: }
182: }
183:
184: }
|