KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rhtml > RhtmlKit


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.ruby.rhtml;
21
22 import java.awt.datatransfer.DataFlavor JavaDoc;
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.im.InputContext JavaDoc;
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.Reader JavaDoc;
31 import java.io.StringReader JavaDoc;
32 import java.io.StringWriter JavaDoc;
33 import javax.swing.Action JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JEditorPane JavaDoc;
36 import javax.swing.JPasswordField JavaDoc;
37 import javax.swing.TransferHandler JavaDoc;
38 import javax.swing.plaf.UIResource JavaDoc;
39 import javax.swing.text.BadLocationException JavaDoc;
40 import javax.swing.text.Caret JavaDoc;
41 import javax.swing.text.Document JavaDoc;
42 import javax.swing.text.EditorKit JavaDoc;
43 import javax.swing.text.Position JavaDoc;
44 import javax.swing.text.TextAction JavaDoc;
45 import javax.swing.text.JTextComponent JavaDoc;
46 import org.netbeans.api.lexer.Language;
47 import org.netbeans.editor.*;
48 import org.netbeans.editor.ext.*;
49 import org.netbeans.modules.editor.NbEditorDocument;
50 import org.netbeans.modules.editor.NbEditorKit;
51
52 /**
53  * Editor kit implementation for HTML content type
54  *
55  * @author Miloslav Metelka
56  * @version 1.00
57  */

58
59 public class RhtmlKit extends NbEditorKit implements org.openide.util.HelpCtx.Provider {
60     
61     public org.openide.util.HelpCtx getHelpCtx() {
62         return new org.openide.util.HelpCtx(RhtmlKit.class);
63     }
64     
65     static final long serialVersionUID =-1381945567613910297L;
66     
67     public static final String JavaDoc RHTML_MIME_TYPE = "application/x-httpd-eruby"; // NOI18N
68

69     public static final String JavaDoc shiftInsertBreakAction = "shift-insert-break"; // NOI18N
70

71     //comment folds
72
public static final String JavaDoc collapseAllCommentsAction = "collapse-all-comment-folds"; //NOI18N
73

74     public static final String JavaDoc expandAllCommentsAction = "expand-all-comment-folds"; //NOI18N
75

76     private static boolean setupReadersInitialized = false;
77     
78     //temporary - will be removed when lexer is stabilized
79
// private static final boolean J2EE_LEXER_COLORING = Boolean.getBoolean("j2ee_lexer_coloring"); //NOI18N
80
private static final boolean J2EE_LEXER_COLORING = true;
81     
82     public RhtmlKit(){
83 // if (!setupReadersInitialized){
84
// NbReaderProvider.setupReaders();
85
// setupReadersInitialized = true;
86
// }
87
}
88     
89     public String JavaDoc getContentType() {
90         return RHTML_MIME_TYPE;
91     }
92     
93     public CompletionJavaDoc createCompletionJavaDoc(ExtEditorUI extEditorUI) {
94         return null;
95     }
96     
97     protected void initDocument(BaseDocument doc) {
98         //add layers here
99
}
100     
101     /** Create new instance of syntax coloring scanner
102      * @param doc document to operate on. It can be null in the cases the syntax
103      * creation is not related to the particular document
104      */

105 // public Syntax createSyntax(Document doc) {
106
// return new HTMLSyntax();
107
// }
108
//
109
// /** Create syntax support */
110
// public SyntaxSupport createSyntaxSupport(BaseDocument doc) {
111
// return new HTMLSyntaxSupport(doc);
112
// }
113

114     /** old code completion is disabled. */
115     public Completion createCompletion(ExtEditorUI extEditorUI) {
116         return null;
117     }
118     
119 // /** used to create completion instance from completion provider
120
// * @deprecated
121
// */
122
// public Completion createCompletionForProvider(ExtEditorUI extEditorUI) {
123
// return new HTMLCompletion(extEditorUI);
124
// }
125

126     /*
127      * @Override()
128      */

129     public Document JavaDoc createDefaultDocument() {
130         Document JavaDoc doc = new RhtmlEditorDocument(this.getClass());
131         Object JavaDoc mimeType = doc.getProperty("mimeType"); //NOI18N
132
if (mimeType == null){
133             doc.putProperty("mimeType", getContentType()); //NOI18N
134
}
135         doc.putProperty(Language.class, RhtmlTokenId.language());
136         return doc;
137     }
138     
139 // public Formatter createFormatter() {
140
// //return new LineWrapFormatter(this.getClass());
141
// return new HTMLFormatter(this.getClass());
142
// }
143

144     /** Called after the kit is installed into JEditorPane */
145     public void install(javax.swing.JEditorPane JavaDoc c) {
146         super.install(c);
147         c.setTransferHandler(new HTMLTransferHandler());
148     }
149     
150     protected Action JavaDoc[] createActions() {
151         Action JavaDoc[] HTMLActions = new Action JavaDoc[] {
152             new HTMLDefaultKeyTypedAction(),
153                     new HTMLDeleteCharAction(deletePrevCharAction, false),
154                     new HTMLDeleteCharAction(deleteNextCharAction, true),
155                     new HTMLShiftBreakAction(),
156                     // replace MatchBraceAction with HtmlEditor own
157
new MatchBraceAction(ExtKit.matchBraceAction, false),
158                     new MatchBraceAction(ExtKit.selectionMatchBraceAction, true),
159                     //new HTMLGenerateFoldPopupAction(),
160
//new CollapseAllCommentsFolds(),
161
//new ExpandAllCommentsFolds()
162
};
163         return TextAction.augmentList(super.createActions(), HTMLActions);
164     }
165     
166     public static class HTMLDefaultKeyTypedAction extends ExtDefaultKeyTypedAction {
167         
168         protected void insertString(BaseDocument doc, int dotPos,
169                 Caret JavaDoc caret, String JavaDoc str,
170                 boolean overwrite) throws BadLocationException JavaDoc {
171             super.insertString(doc, dotPos, caret, str, overwrite);
172             //HTMLAutoCompletion.charInserted(doc, dotPos, caret, str.charAt(0));
173
}
174         
175     }
176     
177     public static class HTMLDeleteCharAction extends DeleteCharAction {
178         
179         public HTMLDeleteCharAction(String JavaDoc name, boolean nextChar) {
180             super(name, nextChar);
181         }
182         
183         protected void charBackspaced(BaseDocument doc, int dotPos, Caret JavaDoc caret, char ch) throws BadLocationException JavaDoc {
184             super.charBackspaced(doc, dotPos, caret, ch);
185             //HTMLAutoCompletion.charDeleted(doc, dotPos, caret, ch);
186
}
187     }
188     
189     
190     public static class HTMLShiftBreakAction extends BaseAction {
191         
192         static final long serialVersionUID =4004043376345356061L;
193         
194         public HTMLShiftBreakAction() {
195             super( shiftInsertBreakAction, ABBREV_RESET
196                     | MAGIC_POSITION_RESET | UNDO_MERGE_RESET);
197         }
198         
199         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
200             if (target != null) {
201                 Completion completion = ExtUtilities.getCompletion(target);
202                 if (completion != null && completion.isPaneVisible()) {
203                     if (completion.substituteText( true )) {
204                         // completion.setPaneVisible(false);
205
} else {
206                         completion.refresh(false);
207                     }
208                 }
209             }
210         }
211         
212     }
213     
214     /** This is implementation for MatchBraceAction for HTML file.
215      */

216     public static class MatchBraceAction extends ExtKit.MatchBraceAction {
217         
218         private boolean select; // whether the text between matched blocks should be selected
219

220         public MatchBraceAction(String JavaDoc name, boolean select) {
221             super(name, select);
222             this.select = select;
223         }
224         
225         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
226             if (target != null) {
227                 try {
228                     Caret JavaDoc caret = target.getCaret();
229                     BaseDocument doc = Utilities.getDocument(target);
230                     int dotPos = caret.getDot();
231                     ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
232                     
233                     //TODO: check whether we are in HTML text
234
if (dotPos > 0) {
235                         int[] matchBlk = sup.findMatchingBlock(dotPos - 1, false);
236                         if (matchBlk != null) {
237                             if (select) {
238                                 caret.moveDot(matchBlk[1]);
239                             } else {
240                                 caret.setDot(matchBlk[1]);
241                             }
242                         }
243                         
244                     } else{ // If this is not token from HTML Syntax -> call the original action from editor.
245
super.actionPerformed(evt, target);
246                     }
247                     
248                 } catch (BadLocationException JavaDoc e) {
249                     target.getToolkit().beep();
250                 }
251             }
252         }
253     }
254     
255 // public static class HTMLGenerateFoldPopupAction extends GenerateFoldPopupAction {
256
//
257
// protected void addAdditionalItems(JTextComponent target, JMenu menu){
258
// addAction(target, menu, collapseAllCommentsAction);
259
// addAction(target, menu, expandAllCommentsAction);
260
// }
261
// }
262
//
263
// public static class ExpandAllCommentsFolds extends BaseAction{
264
// public ExpandAllCommentsFolds(){
265
// super(expandAllCommentsAction);
266
// putValue(SHORT_DESCRIPTION, NbBundle.getBundle(HTMLKit.class).getString("expand-all-comment-folds"));
267
// putValue(BaseAction.POPUP_MENU_TEXT, NbBundle.getBundle(HTMLKit.class).getString("popup-expand-all-comment-folds"));
268
// }
269
//
270
// public void actionPerformed(ActionEvent evt, JTextComponent target) {
271
// FoldHierarchy hierarchy = FoldHierarchy.get(target);
272
// // Hierarchy locking done in the utility method
273
// FoldUtilities.expand(hierarchy, HTMLFoldTypes.COMMENT);
274
// }
275
// }
276
//
277
// public static class CollapseAllCommentsFolds extends BaseAction{
278
// public CollapseAllCommentsFolds(){
279
// super(collapseAllCommentsAction);
280
// putValue(SHORT_DESCRIPTION, NbBundle.getBundle(HTMLKit.class).getString("collapse-all-comment-folds")); //NOI18N
281
// putValue(BaseAction.POPUP_MENU_TEXT, NbBundle.getBundle(HTMLKit.class).getString("popup-collapse-all-comment-folds")); //NOI18N
282
// }
283
//
284
// public void actionPerformed(ActionEvent evt, JTextComponent target) {
285
// FoldHierarchy hierarchy = FoldHierarchy.get(target);
286
// // Hierarchy locking done in the utility method
287
// FoldUtilities.collapse(hierarchy, HTMLFoldTypes.COMMENT);
288
// }
289
// }
290
//
291

292     
293     
294     
295     /* !!!!!!!!!!!!!!!!!!!!!
296      *
297      * Inner classes bellow were taken from BasicTextUI and rewritten in the place marked
298      * with [REWRITE_PLACE]. This needs to be done to fix the issue #43309
299      *
300      * !!!!!!!!!!!!!!!!!!!!!
301      */

302     static class HTMLTransferHandler extends TransferHandler JavaDoc implements UIResource JavaDoc {
303         
304         private JTextComponent JavaDoc exportComp;
305         private boolean shouldRemove;
306         private int p0;
307         private int p1;
308         
309         /**
310          * Try to find a flavor that can be used to import a Transferable.
311          * The set of usable flavors are tried in the following order:
312          * <ol>
313          * <li>First, an attempt is made to find a flavor matching the content type
314          * of the EditorKit for the component.
315          * <li>Second, an attempt to find a text/plain flavor is made.
316          * <li>Third, an attempt to find a flavor representing a String reference
317          * in the same VM is made.
318          * <li>Lastly, DataFlavor.stringFlavor is searched for.
319          * </ol>
320          */

321         protected DataFlavor JavaDoc getImportFlavor(DataFlavor JavaDoc[] flavors, JTextComponent JavaDoc c) {
322             DataFlavor JavaDoc plainFlavor = null;
323             DataFlavor JavaDoc refFlavor = null;
324             DataFlavor JavaDoc stringFlavor = null;
325             
326             if (c instanceof JEditorPane JavaDoc) {
327                 for (int i = 0; i < flavors.length; i++) {
328                     String JavaDoc mime = flavors[i].getMimeType();
329                     if (mime.startsWith(((JEditorPane JavaDoc)c).getEditorKit().getContentType())) {
330                         //return flavors[i]; [REWRITE_PLACE]
331
} else if (plainFlavor == null && mime.startsWith("text/plain")) { //NOI18N
332
plainFlavor = flavors[i];
333                     } else if (refFlavor == null && mime.startsWith("application/x-java-jvm-local-objectref") //NOI18N
334
&& flavors[i].getRepresentationClass() == java.lang.String JavaDoc.class) {
335                         refFlavor = flavors[i];
336                     } else if (stringFlavor == null && flavors[i].equals(DataFlavor.stringFlavor)) {
337                         stringFlavor = flavors[i];
338                     }
339                 }
340                 if (plainFlavor != null) {
341                     return plainFlavor;
342                 } else if (refFlavor != null) {
343                     return refFlavor;
344                 } else if (stringFlavor != null) {
345                     return stringFlavor;
346                 }
347                 return null;
348             }
349             
350             
351             for (int i = 0; i < flavors.length; i++) {
352                 String JavaDoc mime = flavors[i].getMimeType();
353                 if (mime.startsWith("text/plain")) { //NOI18N
354
return flavors[i];
355                 } else if (refFlavor == null && mime.startsWith("application/x-java-jvm-local-objectref") //NOI18N
356
&& flavors[i].getRepresentationClass() == java.lang.String JavaDoc.class) {
357                     refFlavor = flavors[i];
358                 } else if (stringFlavor == null && flavors[i].equals(DataFlavor.stringFlavor)) {
359                     stringFlavor = flavors[i];
360                 }
361             }
362             if (refFlavor != null) {
363                 return refFlavor;
364             } else if (stringFlavor != null) {
365                 return stringFlavor;
366             }
367             return null;
368         }
369         
370         /**
371          * Import the given stream data into the text component.
372          */

373         protected void handleReaderImport(Reader JavaDoc in, JTextComponent JavaDoc c, boolean useRead)
374         throws BadLocationException JavaDoc, IOException JavaDoc {
375             if (useRead) {
376                 int startPosition = c.getSelectionStart();
377                 int endPosition = c.getSelectionEnd();
378                 int length = endPosition - startPosition;
379                 EditorKit JavaDoc kit = c.getUI().getEditorKit(c);
380                 Document JavaDoc doc = c.getDocument();
381                 if (length > 0) {
382                     doc.remove(startPosition, length);
383                 }
384                 kit.read(in, doc, startPosition);
385             } else {
386                 char[] buff = new char[1024];
387                 int nch;
388                 boolean lastWasCR = false;
389                 int last;
390                 StringBuffer JavaDoc sbuff = null;
391                 
392                 // Read in a block at a time, mapping \r\n to \n, as well as single
393
// \r to \n.
394
while ((nch = in.read(buff, 0, buff.length)) != -1) {
395                     if (sbuff == null) {
396                         sbuff = new StringBuffer JavaDoc(nch);
397                     }
398                     last = 0;
399                     for(int counter = 0; counter < nch; counter++) {
400                         switch(buff[counter]) {
401                             case '\r':
402                                 if (lastWasCR) {
403                                     if (counter == 0) {
404                                         sbuff.append('\n');
405                                     } else {
406                                         buff[counter - 1] = '\n';
407                                     }
408                                 } else {
409                                     lastWasCR = true;
410                                 }
411                                 break;
412                             case '\n':
413                                 if (lastWasCR) {
414                                     if (counter > (last + 1)) {
415                                         sbuff.append(buff, last, counter - last - 1);
416                                     }
417                                     // else nothing to do, can skip \r, next write will
418
// write \n
419
lastWasCR = false;
420                                     last = counter;
421                                 }
422                                 break;
423                             default:
424                                 if (lastWasCR) {
425                                     if (counter == 0) {
426                                         sbuff.append('\n');
427                                     } else {
428                                         buff[counter - 1] = '\n';
429                                     }
430                                     lastWasCR = false;
431                                 }
432                                 break;
433                         }
434                     }
435                     if (last < nch) {
436                         if (lastWasCR) {
437                             if (last < (nch - 1)) {
438                                 sbuff.append(buff, last, nch - last - 1);
439                             }
440                         } else {
441                             sbuff.append(buff, last, nch - last);
442                         }
443                     }
444                 }
445                 if (lastWasCR) {
446                     sbuff.append('\n');
447                 }
448                 c.replaceSelection(sbuff != null ? sbuff.toString() : ""); //NOI18N
449
}
450         }
451         
452         // --- TransferHandler methods ------------------------------------
453

454         /**
455          * This is the type of transfer actions supported by the source. Some models are
456          * not mutable, so a transfer operation of COPY only should
457          * be advertised in that case.
458          *
459          * @param c The component holding the data to be transfered. This
460          * argument is provided to enable sharing of TransferHandlers by
461          * multiple components.
462          * @return This is implemented to return NONE if the component is a JPasswordField
463          * since exporting data via user gestures is not allowed. If the text component is
464          * editable, COPY_OR_MOVE is returned, otherwise just COPY is allowed.
465          */

466         public int getSourceActions(JComponent JavaDoc c) {
467             int actions = NONE;
468             if (! (c instanceof JPasswordField JavaDoc)) {
469                 if (((JTextComponent JavaDoc)c).isEditable()) {
470                     actions = COPY_OR_MOVE;
471                 } else {
472                     actions = COPY;
473                 }
474             }
475             return actions;
476         }
477         
478         /**
479          * Create a Transferable to use as the source for a data transfer.
480          *
481          * @param comp The component holding the data to be transfered. This
482          * argument is provided to enable sharing of TransferHandlers by
483          * multiple components.
484          * @return The representation of the data to be transfered.
485          *
486          */

487         protected Transferable JavaDoc createTransferable(JComponent JavaDoc comp) {
488             exportComp = (JTextComponent JavaDoc)comp;
489             shouldRemove = true;
490             p0 = exportComp.getSelectionStart();
491             p1 = exportComp.getSelectionEnd();
492             return (p0 != p1) ? (new HTMLTransferable(exportComp, p0, p1)) : null;
493         }
494         
495         /**
496          * This method is called after data has been exported. This method should remove
497          * the data that was transfered if the action was MOVE.
498          *
499          * @param source The component that was the source of the data.
500          * @param data The data that was transferred or possibly null
501          * if the action is <code>NONE</code>.
502          * @param action The actual action that was performed.
503          */

504         protected void exportDone(JComponent JavaDoc source, Transferable JavaDoc data, int action) {
505             // only remove the text if shouldRemove has not been set to
506
// false by importData and only if the action is a move
507
if (shouldRemove && action == MOVE) {
508                 HTMLTransferable t = (HTMLTransferable)data;
509                 t.removeText();
510             }
511             
512             exportComp = null;
513         }
514         
515         /**
516          * This method causes a transfer to a component from a clipboard or a
517          * DND drop operation. The Transferable represents the data to be
518          * imported into the component.
519          *
520          * @param comp The component to receive the transfer. This
521          * argument is provided to enable sharing of TransferHandlers by
522          * multiple components.
523          * @param t The data to import
524          * @return true if the data was inserted into the component, false otherwise.
525          */

526         public boolean importData(JComponent JavaDoc comp, Transferable JavaDoc t) {
527             JTextComponent JavaDoc c = (JTextComponent JavaDoc)comp;
528             
529             // if we are importing to the same component that we exported from
530
// then don't actually do anything if the drop location is inside
531
// the drag location and set shouldRemove to false so that exportDone
532
// knows not to remove any data
533
if (c == exportComp && c.getCaretPosition() >= p0 && c.getCaretPosition() <= p1) {
534                 shouldRemove = false;
535                 return true;
536             }
537             
538             boolean imported = false;
539             DataFlavor JavaDoc importFlavor = getImportFlavor(t.getTransferDataFlavors(), c);
540             if (importFlavor != null) {
541                 try {
542                     boolean useRead = false;
543                     if (comp instanceof JEditorPane JavaDoc) {
544                         JEditorPane JavaDoc ep = (JEditorPane JavaDoc)comp;
545                         if (!ep.getContentType().startsWith("text/plain") && //NOI18N
546
importFlavor.getMimeType().startsWith(ep.getContentType())) {
547                             useRead = true;
548                         }
549                     }
550                     InputContext JavaDoc ic = c.getInputContext();
551                     if (ic != null) {
552                         ic.endComposition();
553                     }
554                     Reader JavaDoc r = importFlavor.getReaderForText(t);
555                     handleReaderImport(r, c, useRead);
556                     imported = true;
557                 } catch (UnsupportedFlavorException JavaDoc ufe) {
558                     //just ignore
559
} catch (BadLocationException JavaDoc ble) {
560                     //just ignore
561
} catch (IOException JavaDoc ioe) {
562                     //just ignore
563
}
564             }
565             return imported;
566         }
567         
568         /**
569          * This method indicates if a component would accept an import of the given
570          * set of data flavors prior to actually attempting to import it.
571          *
572          * @param comp The component to receive the transfer. This
573          * argument is provided to enable sharing of TransferHandlers by
574          * multiple components.
575          * @param flavors The data formats available
576          * @return true if the data can be inserted into the component, false otherwise.
577          */

578         public boolean canImport(JComponent JavaDoc comp, DataFlavor JavaDoc[] flavors) {
579             JTextComponent JavaDoc c = (JTextComponent JavaDoc)comp;
580             if (!(c.isEditable() && c.isEnabled())) {
581                 return false;
582             }
583             return (getImportFlavor(flavors, c) != null);
584         }
585         
586         /**
587          * A possible implementation of the Transferable interface
588          * for text components. For a JEditorPane with a rich set
589          * of EditorKit implementations, conversions could be made
590          * giving a wider set of formats. This is implemented to
591          * offer up only the active content type and text/plain
592          * (if that is not the active format) since that can be
593          * extracted from other formats.
594          */

595         static class HTMLTransferable extends BasicTransferable {
596             
597             HTMLTransferable(JTextComponent JavaDoc c, int start, int end) {
598                 super(null, null);
599                 
600                 this.c = c;
601                 
602                 Document JavaDoc doc = c.getDocument();
603                 
604                 try {
605                     p0 = doc.createPosition(start);
606                     p1 = doc.createPosition(end);
607                     
608                     plainData = c.getSelectedText();
609                     
610                     if (c instanceof JEditorPane JavaDoc) {
611                         JEditorPane JavaDoc ep = (JEditorPane JavaDoc)c;
612                         
613                         mimeType = ep.getContentType();
614                         
615                         if (mimeType.startsWith("text/plain")) { //NOI18N
616
return;
617                         }
618                         
619                         StringWriter JavaDoc sw = new StringWriter JavaDoc(p1.getOffset() - p0.getOffset());
620                         ep.getEditorKit().write(sw, doc, p0.getOffset(), p1.getOffset() - p0.getOffset());
621                         
622                         if (mimeType.startsWith("text/html")) { //NOI18N
623
htmlData = sw.toString();
624                         } else {
625                             richText = sw.toString();
626                         }
627                     }
628                 } catch (BadLocationException JavaDoc ble) {
629                 } catch (IOException JavaDoc ioe) {
630                 }
631             }
632             
633             void removeText() {
634                 if ((p0 != null) && (p1 != null) && (p0.getOffset() != p1.getOffset())) {
635                     try {
636                         Document JavaDoc doc = c.getDocument();
637                         doc.remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
638                     } catch (BadLocationException JavaDoc e) {
639                     }
640                 }
641             }
642             
643             // ---- EditorKit other than plain or HTML text -----------------------
644

645             /**
646              * If the EditorKit is not for text/plain or text/html, that format
647              * is supported through the "richer flavors" part of BasicTransferable.
648              */

649             protected DataFlavor JavaDoc[] getRicherFlavors() {
650                 if (richText == null) {
651                     return null;
652                 }
653                 
654                 try {
655                     DataFlavor JavaDoc[] flavors = new DataFlavor JavaDoc[3];
656                     flavors[0] = new DataFlavor JavaDoc(mimeType + ";class=java.lang.String"); //NOI18N
657
flavors[1] = new DataFlavor JavaDoc(mimeType + ";class=java.io.Reader"); //NOI18N
658
flavors[2] = new DataFlavor JavaDoc(mimeType + ";class=java.io.InputStream;charset=unicode"); //NOI18N
659
return flavors;
660                 } catch (ClassNotFoundException JavaDoc cle) {
661                     // fall through to unsupported (should not happen)
662
}
663                 
664                 return null;
665             }
666             
667             /**
668              * The only richer format supported is the file list flavor
669              */

670             protected Object JavaDoc getRicherData(DataFlavor JavaDoc flavor) throws UnsupportedFlavorException JavaDoc {
671                 if (richText == null) {
672                     return null;
673                 }
674                 
675                 if (String JavaDoc.class.equals(flavor.getRepresentationClass())) {
676                     return richText;
677                 } else if (Reader JavaDoc.class.equals(flavor.getRepresentationClass())) {
678                     return new StringReader JavaDoc(richText);
679                 } else if (InputStream JavaDoc.class.equals(flavor.getRepresentationClass())) {
680                     return new ByteArrayInputStream JavaDoc(richText.getBytes());
681                 }
682                 throw new UnsupportedFlavorException JavaDoc(flavor);
683             }
684             
685             Position JavaDoc p0;
686             Position JavaDoc p1;
687             String JavaDoc mimeType;
688             String JavaDoc richText;
689             JTextComponent JavaDoc c;
690         }
691         
692     }
693     
694     private static class BasicTransferable implements Transferable JavaDoc, UIResource JavaDoc {
695         
696         protected String JavaDoc plainData;
697         protected String JavaDoc htmlData;
698         
699         private static DataFlavor JavaDoc[] htmlFlavors;
700         private static DataFlavor JavaDoc[] stringFlavors;
701         private static DataFlavor JavaDoc[] plainFlavors;
702         
703         static {
704             try {
705                 htmlFlavors = new DataFlavor JavaDoc[3];
706                 htmlFlavors[0] = new DataFlavor JavaDoc("text/html;class=java.lang.String"); //NOI18N
707
htmlFlavors[1] = new DataFlavor JavaDoc("text/html;class=java.io.Reader"); //NOI18N
708
htmlFlavors[2] = new DataFlavor JavaDoc("text/html;charset=unicode;class=java.io.InputStream"); //NOI18N
709

710                 plainFlavors = new DataFlavor JavaDoc[3];
711                 plainFlavors[0] = new DataFlavor JavaDoc("text/plain;class=java.lang.String"); //NOI18N
712
plainFlavors[1] = new DataFlavor JavaDoc("text/plain;class=java.io.Reader"); //NOI18N
713
plainFlavors[2] = new DataFlavor JavaDoc("text/plain;charset=unicode;class=java.io.InputStream"); //NOI18N
714

715                 stringFlavors = new DataFlavor JavaDoc[2];
716                 stringFlavors[0] = new DataFlavor JavaDoc(DataFlavor.javaJVMLocalObjectMimeType+";class=java.lang.String"); //NOI18N
717
stringFlavors[1] = DataFlavor.stringFlavor;
718                 
719             } catch (ClassNotFoundException JavaDoc cle) {
720                 System.err.println("error initializing javax.swing.plaf.basic.BasicTranserable"); ////NOI18N
721
}
722         }
723         
724         public BasicTransferable(String JavaDoc plainData, String JavaDoc htmlData) {
725             this.plainData = plainData;
726             this.htmlData = htmlData;
727         }
728         
729         
730         /**
731          * Returns an array of DataFlavor objects indicating the flavors the data
732          * can be provided in. The array should be ordered according to preference
733          * for providing the data (from most richly descriptive to least descriptive).
734          * @return an array of data flavors in which this data can be transferred
735          */

736         public DataFlavor JavaDoc[] getTransferDataFlavors() {
737             DataFlavor JavaDoc[] richerFlavors = getRicherFlavors();
738             int nRicher = (richerFlavors != null) ? richerFlavors.length : 0;
739             int nHTML = (isHTMLSupported()) ? htmlFlavors.length : 0;
740             int nPlain = (isPlainSupported()) ? plainFlavors.length: 0;
741             int nString = (isPlainSupported()) ? stringFlavors.length : 0;
742             int nFlavors = nRicher + nHTML + nPlain + nString;
743             DataFlavor JavaDoc[] flavors = new DataFlavor JavaDoc[nFlavors];
744             
745             // fill in the array
746
int nDone = 0;
747             if (nRicher > 0) {
748                 System.arraycopy(richerFlavors, 0, flavors, nDone, nRicher);
749                 nDone += nRicher;
750             }
751             if (nHTML > 0) {
752                 System.arraycopy(htmlFlavors, 0, flavors, nDone, nHTML);
753                 nDone += nHTML;
754             }
755             if (nPlain > 0) {
756                 System.arraycopy(plainFlavors, 0, flavors, nDone, nPlain);
757                 nDone += nPlain;
758             }
759             if (nString > 0) {
760                 System.arraycopy(stringFlavors, 0, flavors, nDone, nString);
761                 nDone += nString;
762             }
763             return flavors;
764         }
765         
766         /**
767          * Returns whether or not the specified data flavor is supported for
768          * this object.
769          * @param flavor the requested flavor for the data
770          * @return boolean indicating whether or not the data flavor is supported
771          */

772         public boolean isDataFlavorSupported(DataFlavor JavaDoc flavor) {
773             DataFlavor JavaDoc[] flavors = getTransferDataFlavors();
774             for (int i = 0; i < flavors.length; i++) {
775                 if (flavors[i].equals(flavor)) {
776                     return true;
777                 }
778             }
779             return false;
780         }
781         
782         /**
783          * Returns an object which represents the data to be transferred. The class
784          * of the object returned is defined by the representation class of the flavor.
785          *
786          * @param flavor the requested flavor for the data
787          * @see DataFlavor#getRepresentationClass
788          * @exception IOException if the data is no longer available
789          * in the requested flavor.
790          * @exception UnsupportedFlavorException if the requested data flavor is
791          * not supported.
792          */

793         public Object JavaDoc getTransferData(DataFlavor JavaDoc flavor) throws UnsupportedFlavorException JavaDoc, IOException JavaDoc {
794             DataFlavor JavaDoc[] richerFlavors = getRicherFlavors();
795             if (isRicherFlavor(flavor)) {
796                 return getRicherData(flavor);
797             } else if (isHTMLFlavor(flavor)) {
798                 String JavaDoc data = getHTMLData();
799                 data = (data == null) ? "" : data; //NOI18N
800
if (String JavaDoc.class.equals(flavor.getRepresentationClass())) {
801                     return data;
802                 } else if (Reader JavaDoc.class.equals(flavor.getRepresentationClass())) {
803                     return new StringReader JavaDoc(data);
804                 } else if (InputStream JavaDoc.class.equals(flavor.getRepresentationClass())) {
805                     return new ByteArrayInputStream JavaDoc(data.getBytes());
806                 }
807                 // fall through to unsupported
808
} else if (isPlainFlavor(flavor)) {
809                 String JavaDoc data = getPlainData();
810                 data = (data == null) ? "" : data;
811                 if (String JavaDoc.class.equals(flavor.getRepresentationClass())) {
812                     return data;
813                 } else if (Reader JavaDoc.class.equals(flavor.getRepresentationClass())) {
814                     return new StringReader JavaDoc(data);
815                 } else if (InputStream JavaDoc.class.equals(flavor.getRepresentationClass())) {
816                     return new ByteArrayInputStream JavaDoc(data.getBytes());
817                 }
818                 // fall through to unsupported
819

820             } else if (isStringFlavor(flavor)) {
821                 String JavaDoc data = getPlainData();
822                 data = (data == null) ? "" : data; //NOI18N
823
return data;
824             }
825             throw new UnsupportedFlavorException JavaDoc(flavor);
826         }
827         
828         // --- richer subclass flavors ----------------------------------------------
829

830         protected boolean isRicherFlavor(DataFlavor JavaDoc flavor) {
831             DataFlavor JavaDoc[] richerFlavors = getRicherFlavors();
832             int nFlavors = (richerFlavors != null) ? richerFlavors.length : 0;
833             for (int i = 0; i < nFlavors; i++) {
834                 if (richerFlavors[i].equals(flavor)) {
835                     return true;
836                 }
837             }
838             return false;
839         }
840         
841         /**
842          * Some subclasses will have flavors that are more descriptive than HTML
843          * or plain text. If this method returns a non-null value, it will be
844          * placed at the start of the array of supported flavors.
845          */

846         protected DataFlavor JavaDoc[] getRicherFlavors() {
847             return null;
848         }
849         
850         protected Object JavaDoc getRicherData(DataFlavor JavaDoc flavor) throws UnsupportedFlavorException JavaDoc {
851             return null;
852         }
853         
854         // --- html flavors ----------------------------------------------------------
855

856         /**
857          * Returns whether or not the specified data flavor is an HTML flavor that
858          * is supported.
859          * @param flavor the requested flavor for the data
860          * @return boolean indicating whether or not the data flavor is supported
861          */

862         protected boolean isHTMLFlavor(DataFlavor JavaDoc flavor) {
863             DataFlavor JavaDoc[] flavors = htmlFlavors;
864             for (int i = 0; i < flavors.length; i++) {
865                 if (flavors[i].equals(flavor)) {
866                     return true;
867                 }
868             }
869             return false;
870         }
871         
872         /**
873          * Should the HTML flavors be offered? If so, the method
874          * getHTMLData should be implemented to provide something reasonable.
875          */

876         protected boolean isHTMLSupported() {
877             return htmlData != null;
878         }
879         
880         /**
881          * Fetch the data in a text/html format
882          */

883         protected String JavaDoc getHTMLData() {
884             return htmlData;
885         }
886         
887         // --- plain text flavors ----------------------------------------------------
888

889         /**
890          * Returns whether or not the specified data flavor is an plain flavor that
891          * is supported.
892          * @param flavor the requested flavor for the data
893          * @return boolean indicating whether or not the data flavor is supported
894          */

895         protected boolean isPlainFlavor(DataFlavor JavaDoc flavor) {
896             DataFlavor JavaDoc[] flavors = plainFlavors;
897             for (int i = 0; i < flavors.length; i++) {
898                 if (flavors[i].equals(flavor)) {
899                     return true;
900                 }
901             }
902             return false;
903         }
904         
905         /**
906          * Should the plain text flavors be offered? If so, the method
907          * getPlainData should be implemented to provide something reasonable.
908          */

909         protected boolean isPlainSupported() {
910             return plainData != null;
911         }
912         
913         /**
914          * Fetch the data in a text/plain format.
915          */

916         protected String JavaDoc getPlainData() {
917             return plainData;
918         }
919         
920         // --- string flavorss --------------------------------------------------------
921

922         /**
923          * Returns whether or not the specified data flavor is a String flavor that
924          * is supported.
925          * @param flavor the requested flavor for the data
926          * @return boolean indicating whether or not the data flavor is supported
927          */

928         protected boolean isStringFlavor(DataFlavor JavaDoc flavor) {
929             DataFlavor JavaDoc[] flavors = stringFlavors;
930             for (int i = 0; i < flavors.length; i++) {
931                 if (flavors[i].equals(flavor)) {
932                     return true;
933                 }
934             }
935             return false;
936         }
937         
938         
939     }
940     
941     // END of fix of issue #43309
942

943     public class RhtmlEditorDocument extends NbEditorDocument {
944         public RhtmlEditorDocument(Class JavaDoc kitClass) {
945             super(kitClass);
946         }
947         
948         public boolean addLayer(DrawLayer layer, int visibility) {
949             //filter out the syntax layer adding
950
if(!(layer instanceof DrawLayerFactory.SyntaxLayer)) {
951                 return super.addLayer(layer, visibility);
952             } else {
953                 return false;
954             }
955         }
956     }
957     
958     
959 }
960
961
Popular Tags