KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > repl > InteractionsEditorKit


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project:
4  * http://sourceforge.net/projects/drjava/ or http://www.drjava.org/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2003 JavaPLT group at Rice University (javaplt@rice.edu)
9  * All rights reserved.
10  *
11  * Developed by: Java Programming Languages Team
12  * Rice University
13  * http://www.cs.rice.edu/~javaplt/
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal with the Software without restriction, including without
18  * limitation the rights to use, copy, modify, merge, publish, distribute,
19  * sublicense, and/or sell copies of the Software, and to permit persons to
20  * whom the Software is furnished to do so, subject to the following
21  * conditions:
22  *
23  * - Redistributions of source code must retain the above copyright
24  * notice, this list of conditions and the following disclaimers.
25  * - Redistributions in binary form must reproduce the above copyright
26  * notice, this list of conditions and the following disclaimers in the
27  * documentation and/or other materials provided with the distribution.
28  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the
29  * names of its contributors may be used to endorse or promote products
30  * derived from this Software without specific prior written permission.
31  * - Products derived from this software may not be called "DrJava" nor
32  * use the term "DrJava" as part of their names without prior written
33  * permission from the JavaPLT group. For permission, write to
34  * javaplt@rice.edu.
35  *
36  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39  * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
40  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42  * OTHER DEALINGS WITH THE SOFTWARE.
43  *
44  END_COPYRIGHT_BLOCK*/

45
46 package edu.rice.cs.drjava.model.repl;
47
48 import edu.rice.cs.drjava.model.definitions.ColoringGlyphPainter;
49 import javax.swing.text.*;
50 import java.awt.*;
51
52
53 /**
54  * This is an editor kit for editing Java source files. It functions as the controller in the MVC arrangement.
55  * It implements a factory for new documents, and it also has a factory for Views (the things that render the document).
56  * @version $Id: InteractionsEditorKit.java 4043 2006-11-22 23:04:59Z rcartwright $
57  */

58 public class InteractionsEditorKit extends StyledEditorKit {
59   
60   /**
61    * Creates a new editor kit
62    */

63   public InteractionsEditorKit() {
64   }
65   
66   
67   private static ViewFactory _factory = new ViewFactory() {
68     
69     public View create(Element elem) {
70       String JavaDoc kind = elem.getName();
71       
72       if (kind != null) {
73         if (kind.equals(AbstractDocument.ContentElementName)) {
74           return _createColoringView(elem);
75         } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
76           return new ParagraphView(elem);
77         } else if (kind.equals(AbstractDocument.SectionElementName)) {
78           return new BoxView(elem, View.Y_AXIS);
79         } else if (kind.equals(StyleConstants.ComponentElementName)) {
80           return new ComponentView(elem);
81         } else if (kind.equals(StyleConstants.IconElementName)) {
82           return new IconView(elem);
83         }
84       }
85       
86       // default to text display
87
return _createColoringView(elem);
88     }
89     
90   };
91   
92   /** Get the MIME content type of the document.
93    * @return "text/java"
94    */

95   public String JavaDoc getContentType() { return "text/java"; }
96   
97   /** We want to use our ColoringView to render text, so here we return a factory that creates ColoringViews. */
98   public final ViewFactory getViewFactory() { return _factory; }
99   
100   public InteractionsDJDocument createDefaultDocument() {
101     return new InteractionsDJDocument();
102   }
103   
104   /** We only need to re-implement the painter for the GlyphView to modify its behavior. The GlyphView delegates its
105     * paint method to the painter. It also allows the painter to obtain the document to which the element
106     * belongs.
107     * @param elem The Element to pass to the GlyphView
108     * @return A GlyphView with modified behavior
109     */

110   private static GlyphView _createColoringView(Element elem) {
111     final GlyphView view = new GlyphView(elem);
112     view.setGlyphPainter(new ColoringGlyphPainter(new Runnable JavaDoc() {
113       public void run() {
114         if (view.getContainer() != null) view.getContainer().repaint();
115       }
116     }));
117     return view;
118   }
119 }
120
121
122
Popular Tags