001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.repl;
038:
039: import edu.rice.cs.drjava.model.definitions.ColoringGlyphPainter;
040: import javax.swing.text.*;
041: import java.awt.*;
042:
043: /**
044: * This is an editor kit for editing Java source files. It functions as the controller in the MVC arrangement.
045: * It implements a factory for new documents, and it also has a factory for Views (the things that render the document).
046: * @version $Id: InteractionsEditorKit.java 4255 2007-08-28 19:17:37Z mgricken $
047: */
048: public class InteractionsEditorKit extends StyledEditorKit {
049:
050: /**
051: * Creates a new editor kit
052: */
053: public InteractionsEditorKit() {
054: }
055:
056: private static ViewFactory _factory = new ViewFactory() {
057:
058: public View create(Element elem) {
059: String kind = elem.getName();
060:
061: if (kind != null) {
062: if (kind.equals(AbstractDocument.ContentElementName)) {
063: return _createColoringView(elem);
064: } else if (kind
065: .equals(AbstractDocument.ParagraphElementName)) {
066: return new ParagraphView(elem);
067: } else if (kind
068: .equals(AbstractDocument.SectionElementName)) {
069: return new BoxView(elem, View.Y_AXIS);
070: } else if (kind
071: .equals(StyleConstants.ComponentElementName)) {
072: return new ComponentView(elem);
073: } else if (kind.equals(StyleConstants.IconElementName)) {
074: return new IconView(elem);
075: }
076: }
077:
078: // default to text display
079: return _createColoringView(elem);
080: }
081:
082: };
083:
084: /** Get the MIME content type of the document.
085: * @return "text/java"
086: */
087: public String getContentType() {
088: return "text/java";
089: }
090:
091: /** We want to use our ColoringView to render text, so here we return a factory that creates ColoringViews. */
092: public final ViewFactory getViewFactory() {
093: return _factory;
094: }
095:
096: public InteractionsDJDocument createDefaultDocument() {
097: return new InteractionsDJDocument();
098: }
099:
100: /** We only need to re-implement the painter for the GlyphView to modify its behavior. The GlyphView delegates its
101: * paint method to the painter. It also allows the painter to obtain the document to which the element
102: * belongs.
103: * @param elem The Element to pass to the GlyphView
104: * @return A GlyphView with modified behavior
105: */
106: private static GlyphView _createColoringView(Element elem) {
107: final GlyphView view = new GlyphView(elem);
108: view.setGlyphPainter(new ColoringGlyphPainter(new Runnable() {
109: public void run() {
110: if (view.getContainer() != null)
111: view.getContainer().repaint();
112: }
113: }));
114: return view;
115: }
116: }
|