Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5046878

replaceRange() in TextArea gives shifted getSelectionStart/End

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2
    • client-libs
    • x86
    • windows_xp



      Name: gm110360 Date: 05/13/2004


      FULL PRODUCT VERSION :
      Java Plugin 1.4.2_03

      ADDITIONAL OS VERSION INFORMATION :
      ver Windows XP Professional 2002 Service Pack 1

      A DESCRIPTION OF THE PROBLEM :
      In an AWT TextArea, after you entered a few newlines, the newlines seem to be counted as positions, causing the replaceRange() function to fail.

      The function does something though, but it uses the wrong getSelectionStart() and getSelectionEnd(). These are shifted as many positions as what seems to be the number of returns entered.

      My textArea applet works fine in earlier plugins, so I think it is a bug.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
         public void vervang(String bgn, String nd) {
          undo_bgn = bgn;
          undo_nd = nd;
          werkTekst = tekst.getSelectedText();
          rplcTekst = bgn + werkTekst + nd;
          tekstBegin = tekst.getSelectionStart();
          tekstEind = tekst.getSelectionEnd();
          tekst.replaceRange(rplcTekst, tekstBegin, tekstEind);
          tekst.requestFocus();
          done = true;

        }

      The above function takes to arguments that are to be put before and after a selected text in the textarea. The selected text will be replaced by the sum of arg1, selectedText, arg2.

      The function vervang(arg1, arg2) is called from javascript:
      vervang("starttext","endtext");



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      You should just be able to replace a selected text with any other text.
      ACTUAL -
      The new text is pasted in the text you were editing, somewhere past the originally selected text, overwriting a piece of text and leaving the originally selected text as is.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.applet.*;
      import java.awt.*;
      import java.awt.TextComponent.*;
      import java.applet.AppletContext.*;


      /**
       * <p>Title: CMS Teksteditor</p>
       * <p>Description: Teksteditor voor in CMS</p>
       * <p>Copyright: Copyright (c) 2003</p>
       * <p>Company: Space Web Internet Team</p>
       * @author Bart McLeod
       * @version 2.00
       */

      public class tekstEditor2 extends Applet {
        private boolean isStandalone = true;
      //onderstaande tekst wijzigen voor elke licentie
        private String licenceTekst = new String("VBW Onderwijs & Ontwikkeling");
        private String copyTekst = new String("CMS teksteditor * Copyright Space Web Internet Team 2003 * www.spaceweb.nl * licentie voor ");
        private String licentie;
        public TextArea tekst = new TextArea(null, 1, 1, 1);
        public String nieuweTekst;
        String testtekst = "Hier kunt u iets typen";
        public int tekstBegin;
        public int tekstEind;
        public int iP; //insertionPoint
        public String werkTekst;
        public String rplcTekst;
        public String undo_bgn;
        public String undo_nd;
        public boolean done = false;
        public String huidigeTekst;
        public Font mijnFont = new Font("Arial", Font.PLAIN, 12);
        private Font copyFont = new Font("Arial", Font.PLAIN, 9);
        BorderLayout L = new BorderLayout();
      //Canvas ca = new Canvas ();
        Font nieuwFont = null;
        public Color achtergrond = new Color(125, 202, 222);
        Label licentieTekst = new Label();


        //Get a parameter value
        public String getParameter(String key, String def) {
          return isStandalone ? System.getProperty(key, def) :
            (getParameter(key) != null ? getParameter(key) : def);
        }

        //Construct the applet
        public tekstEditor2() {
        }
        //Initialize the applet
        public void init() {
          try {
            jbInit();
          }
          catch(Exception e) {
            e.printStackTrace();
          }
        }
        //Component initialization
        private void jbInit() throws Exception {
          tekst.isEditable();
          licentie = copyTekst + licenceTekst;
          setLayout(L);
          tekst.setFont(mijnFont);
          tekst.setText("");
          this.setBackground(achtergrond);
          this.setVisible(true);



          licentieTekst.setFont(copyFont);
          licentieTekst.setText(licentie);
          this.add(tekst, BorderLayout.CENTER);
          this.add(licentieTekst, BorderLayout.NORTH);

        }

        //Get Applet information
        public String getAppletInfo() {
          return "Teksteditor in html pagina";
        }

        //Get parameter info
        public String[][] getParameterInfo() {
          return null;
        }

        public void bal(int gr) {
          nieuwFont = new Font("Arial", Font.PLAIN, gr);
          tekst.setFont(nieuwFont);
          tekst.requestFocus();
        }


        public void vervang(String bgn, String nd) {
          undo_bgn = bgn;
          undo_nd = nd;
          werkTekst = tekst.getSelectedText();
          rplcTekst = bgn + werkTekst + nd;
          tekstBegin = tekst.getSelectionStart();
          tekstEind = tekst.getSelectionEnd();
          tekst.replaceRange(rplcTekst, tekstBegin, tekstEind);
          tekst.requestFocus();
          done = true;

        }

        public void undo() {
          if (done == true) {
            tekstEind = tekstEind + undo_nd.length() + undo_bgn.length();
            tekst.replaceRange(werkTekst, tekstBegin, tekstEind);
            tekst.requestFocus();
            done = false;
            //visualTekst();
          }
          else {

          }
        }

      }

      ---------- END SOURCE ----------
      (Incident Review ID: 233259)
      ======================================================================

            kdmsunw Kdm Kdm (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: