// Copyright 1996, Marimba Inc. All Rights Reserved.


// Confidential and Proprietary Information of Marimba, Inc.


// @(#)JavaWidget.java, 1.11, 12/15/96





package marimba.text.editor;





import marimba.util.*;


import java.awt.*;


import marimba.text.*;


import marimba.text.keymap.*;





/**


 * This class subclasses TextView and adds support for indenting for Java


 * code.  NOTE: This is very primitive, but useful none the less.


 *


 * @author	Jonathan Payne


 * @version 1.11, 12/15/96


 */


public class JavaWidget extends TextView {


    static final int NOWHERE_SPECIAL = 0;


    static final int UNKNOWN = 1;


    static final int IN_COMMENT = 2;


    static final int IN_STRING = 4;





    static int indentIncrement = 4;





    /** Where we are, in the context of Java code. */


    int where = UNKNOWN;





    public JavaWidget() {


	super(new JavaText());


	setFont(new Font("Courier", Font.PLAIN, 12));


    }





    public void getWhere(int pos) {


	where = UNKNOWN;


    }





    public void handleCommand(Event event, KeyboardCommand cmd) {


	JavaText text = (JavaText) this.text;


	if (cmd.getCode() == SELF_INSERT) {


	    switch (event.key) {


	      case '}': {


		  int caret = getCaret();





		  if (caret <= text.getIndent(caret)) {


		      int pos = text.getMatchingParen(caret, -1);


		      int col = text.getColumnFor(text.getIndent(pos));





		      caret = text.deleteWhiteSpace(caret);


		      setCaret(text.indentTo(caret, col));


		  }


		  insert('}');


		  break;


	      }





	      case '\t': {


		  int caret = getCaret();


		  int caretCol = text.getColumnFor(caret);





		  if (caret <= text.getIndent(caret)) {


		      int pos = text.getMatchingParen(caret, -1);


		      int col = text.getColumnFor(text.getIndent(pos));





		      caret = text.deleteWhiteSpace(caret);


		      if (caret == text.length() ||


			  text.byteAt(caret) != '}') {


			  col += indentIncrement;


		      }


		      if (caretCol >= col) {


			  col = (caretCol + indentIncrement) - (caretCol % indentIncrement);


		      }


		      setCaret(text.indentTo(caret, col));


		      break;


		  }


		  /* falls through */


	      }





	      default:


		super.handleCommand(event, cmd);


	    }


	} else {


	    super.handleCommand(event, cmd);


	}


    }	    


}


