Package trackerModule.sim.map

Source Code of trackerModule.sim.map.MapTextPane

package trackerModule.sim.map;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import trackerModule.core.rulestructure.IMessageObject;
import trackerModule.core.rulestructure.RDB;
/**
* The Class MapTextPane.
*/
public class MapTextPane extends JTextPane {
  
  /** The Constant serialVersionUID. */
  private static final long serialVersionUID = -3224263850401029266L;
 
  /** The document. */
  DefaultStyledDocument doc;
 
  /** The attributes. */
  SimpleAttributeSet attrs = new SimpleAttributeSet();
   
    /** The offs. */
    int offs = 0;
   
    /** The list string. */
    ArrayList<String> listString;
   
    /** The parent dialog. */
    public MapFrame parentDlg;
   
  /**
   * Instantiates a new map text pane.
   *
   * @param p the map frame
   */
  public MapTextPane(MapFrame p) {
    super();
    parentDlg = p;
      
    // Make one of each kind of document.
    doc = new DefaultStyledDocument();
        setDocument(doc)
       
        class KeyStrokeListener implements KeyListener
            public void keyPressed(KeyEvent e){ 
                switch(e.getKeyCode()){ 
                     case KeyEvent.VK_ENTER:  
                     break
                
             } 
             public void keyTyped(KeyEvent e){} 
             public void keyReleased(KeyEvent e){
             } 
         } 
 
         KeyStrokeListener keyboardListener = new KeyStrokeListener()
         addKeyListener(keyboardListener)
         
         setDocument(doc);
        
         listString = new ArrayList<String>();
  }

  /**
   * Insert.
   *
   * @param str the script
   */
  public void insert(String str){
        StyleConstants.setForeground( attrs, Color.BLACK );
        if( str.charAt(0) != ' ' ){
          str = " " + str;
        }
         
        String str2 = str.replaceAll(";", ";\n");
       
        try {
      doc.insertString(offs, str2, attrs);
      offs += str2.length();
    } catch (BadLocationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
     
  /**
   * Insert text.
   *
   * @param text the text
   * @param caretOffset the caret offset
   */
  public void insertText(String text, int caretOffset) {
        String selText = getSelectedText();
        if( selText != null && selText.length() > 0 ) {
            int start = getSelectionStart();
            try {
                getDocument().remove(start, getSelectionEnd() - start);
                getDocument().insertString(start, text, null);
                setCaretPosition(start + caretOffset);
            }
            catch (BadLocationException ex) {
            }
        }
        else {
            try {
                int pos = getCaretPosition();
                getDocument().insertString(pos, text, null);
                setCaretPosition(pos + caretOffset);
            }
            catch (BadLocationException ex) {
            }
        }
        requestFocus();
  }
 
    /**
     * Insert text out.
     *
     * @param str the script
     */
    public void insertTextOut(String str){
      if( listString.size() == 0 )
        listString.add(0, str);
      else
        listString.set(0, str);
      printText();
    }
   
    /**
     * Insert text out.
     *
     * @param str the script
     * @param p the line index
     */
    public void insertTextOut(String str, int p){
      if( listString.size() <= p )
        listString.add(str);
      else
        listString.set(p, str);
      printText();
    }
        
    /**
     * Clear.
     */
    public void clear(){
      listString.clear();
      printText();
    }
   
    /**
     * Prints the text.
     */
    public void printText(){
      String strResult = "";
      for( String str: listString ){
        strResult += str + "\n";
      }
         
      setText(strResult);     
        requestFocus();
    }
 
    /**
     * The main method.
     *
     * @param args the arguments
     */
    public static void main(String[] args) {
      JFrame f = new JFrame("Test");
      f.getContentPane().setLayout(new BorderLayout());
      MapTextPane jtp = new MapTextPane(null);
      f.getContentPane().add(new JScrollPane(jtp), BorderLayout.CENTER);
      f.setSize(400, 400);
      f.show();
    }
}
TOP

Related Classes of trackerModule.sim.map.MapTextPane

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.