TextEditor.java 11.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.xfa.text;

import com.adobe.xfa.gfx.GFXEnv;
import com.adobe.xfa.text.TextAttr;
import com.adobe.xfa.text.TextDisplay;
import com.adobe.xfa.text.TextPosn;
import com.adobe.xfa.text.TextPosnBase;
import com.adobe.xfa.text.TextRange;
import com.adobe.xfa.text.TextStream;
import com.adobe.xfa.ut.CoordPair;
import com.adobe.xfa.ut.IntegerHolder;
import com.adobe.xfa.ut.Rect;

public class TextEditor {
    public static final boolean LOGICAL_DEFAULT = true;
    private TextRange moRange = new TextRange();
    private GFXEnv mpoGfxEnv;
    private boolean mbUpdateDisplay;
    private Rect moPrevExtent;

    public TextEditor() {
        this.mbUpdateDisplay = true;
    }

    public TextEditor(TextEditor oSource) {
        this.moRange.copyFrom(oSource.moRange);
        this.mpoGfxEnv = oSource.mpoGfxEnv;
        this.mbUpdateDisplay = oSource.mbUpdateDisplay;
    }

    public TextEditor(GFXEnv poGfxEnv) {
        this.mpoGfxEnv = poGfxEnv;
        this.mbUpdateDisplay = true;
    }

    public TextEditor(TextStream poStream, GFXEnv poGfxEnv, boolean bSelectAll) {
        this.mpoGfxEnv = poGfxEnv;
        this.mbUpdateDisplay = true;
        this.associate(poStream, bSelectAll);
    }

    public TextEditor(TextRange oRange, GFXEnv poGfxEnv) {
        this.mpoGfxEnv = poGfxEnv;
        this.mbUpdateDisplay = true;
        this.associate(oRange);
    }

    public TextEditor(TextPosnBase oPosn, GFXEnv poGfxEnv) {
        this.mpoGfxEnv = poGfxEnv;
        this.mbUpdateDisplay = true;
        this.associate(oPosn);
    }

    public void associate(TextStream poStream, boolean bSelectAll) {
        TextRange oNewRange = new TextRange();
        if (bSelectAll) {
            oNewRange.associate(poStream);
        } else {
            oNewRange.associate(poStream, Integer.MAX_VALUE, Integer.MAX_VALUE);
        }
        this.moveComplete(oNewRange);
    }

    public void associate(TextRange oRange) {
        TextRange oNewRange = new TextRange(oRange);
        this.moveComplete(oNewRange);
    }

    public void associate(TextPosnBase oPosn) {
        TextRange oNewRange = new TextRange(oPosn.stream(), oPosn.index(), oPosn.index());
        this.moveComplete(oNewRange);
    }

    public void associate(int nIndexStart, int nIndexEnd) {
        TextRange oNewRange = new TextRange(this.moRange.stream(), nIndexStart, nIndexEnd);
        this.moveComplete(oNewRange);
    }

    public void userAssociate(TextStream poStream, int nIndexStart, int nIndexEnd) {
        TextRange oNewRange = new TextRange(poStream, TextEditor.userIndexToStreamIndex(poStream, nIndexStart), TextEditor.userIndexToStreamIndex(poStream, nIndexEnd));
        this.moveComplete(oNewRange);
    }

    public void getUserPosition(IntegerHolder nIndexStart, IntegerHolder nIndexEnd) {
        nIndexStart.value = TextEditor.streamIndexToUserIndex(this.moRange.stream(), this.moRange.start().index());
        nIndexEnd.value = TextEditor.streamIndexToUserIndex(this.moRange.stream(), this.moRange.end().index());
    }

    public TextRange range() {
        return this.moRange;
    }

    public void range(TextRange oNewRange) {
        this.associate(oNewRange);
    }

    public GFXEnv gfxEnv() {
        return this.mpoGfxEnv;
    }

    public void gfxEnv(GFXEnv poNewGfxEnv) {
        this.mpoGfxEnv = poNewGfxEnv;
    }

    public boolean updateDisplay() {
        return this.mbUpdateDisplay;
    }

    public void updateDisplay(boolean bNewUpdateDisplay) {
        this.mbUpdateDisplay = bNewUpdateDisplay;
    }

    public boolean moveChar(boolean bForward, boolean bSelect, boolean bLogical) {
        TextPosnBase oPosn = new TextPosnBase(this.moRange.loose());
        boolean bMoved = bForward ? oPosn.nextUserPosn(!bLogical) : oPosn.prevUserPosn(!bLogical);
        this.moveComplete(oPosn, bSelect);
        return bMoved;
    }

    public boolean moveChar(boolean bForward, boolean bSelect) {
        return this.moveChar(bForward, bSelect, true);
    }

    public boolean moveWord(boolean bForward, boolean bSelect, boolean bLogical, int eWordMode) {
        TextPosnBase oPosn = new TextPosnBase(this.moRange.loose());
        boolean bMoved = bForward ? oPosn.nextWord(!bLogical, eWordMode) : oPosn.prevWord(!bLogical, eWordMode);
        this.moveComplete(oPosn, bSelect);
        return bMoved;
    }

    public boolean moveWord(boolean bForward, boolean bSelect, int eWordMode) {
        return this.moveWord(bForward, bSelect, true, eWordMode);
    }

    public boolean moveLine(boolean bForward, boolean bSelect) {
        TextPosnBase oPosn = new TextPosnBase(this.moRange.loose());
        boolean bMoved = false;
        if (bForward) {
            bMoved = oPosn.down();
            if (!bMoved && bSelect) {
                bMoved = oPosn.end();
            }
        } else {
            bMoved = oPosn.up();
            if (!bMoved && bSelect) {
                bMoved = oPosn.start();
            }
        }
        this.moveComplete(oPosn, bSelect);
        return bMoved;
    }

    public boolean movePara(boolean bForward, boolean bSelect) {
        TextPosnBase oPosn = new TextPosnBase(this.moRange.loose());
        boolean bMoved = bForward ? oPosn.nextPara() : oPosn.prevPara();
        this.moveComplete(oPosn, bSelect);
        return bMoved;
    }

    public boolean moveAll(boolean bAbsolute, boolean bForward, boolean bSelect, boolean bLogical) {
        TextPosnBase oPosn = new TextPosnBase(this.moRange.loose());
        boolean bMoved = false;
        if (bAbsolute) {
            if (bForward) {
                oPosn.last(!bLogical);
            } else {
                oPosn.first(!bLogical);
            }
            bMoved = true;
        } else {
            bMoved = bForward ? oPosn.end(!bLogical) : oPosn.start(!bLogical);
        }
        this.moveComplete(oPosn, bSelect);
        return bMoved;
    }

    public boolean moveAll(boolean bAbsolute, boolean bForward, boolean bSelect) {
        return this.moveAll(bAbsolute, bForward, bSelect, true);
    }

    public void grabWord(int eWordMode) {
        TextRange oNewRange = new TextRange(this.moRange);
        oNewRange.grabWord();
        this.moveComplete(oNewRange);
    }

    public void grabWord() {
        this.grabWord(0);
    }

    public void grabLine() {
        TextRange oNewRange = new TextRange(this.moRange);
        oNewRange.grabLine();
        this.moveComplete(oNewRange);
    }

    public void grabPara() {
        TextRange oNewRange = new TextRange(this.moRange);
        oNewRange.grabPara();
        this.moveComplete(oNewRange);
    }

    public void grabAll() {
        TextRange oNewRange = new TextRange(this.moRange.stream());
        this.moveComplete(oNewRange);
    }

    public int selectCount() {
        return this.moRange.countText();
    }

    public String selectText() {
        return this.moRange.text();
    }

    public void selectText(TextStream oSelect) {
        this.moRange.text(oSelect);
    }

    public TextAttr attributeGet() {
        return this.moRange.attribute();
    }

    public void attributeSet(TextAttr oNewAttr) {
        this.editStart();
        this.moRange.attribute(oNewAttr);
        this.editComplete(true);
    }

    public void replace(char cInsert, boolean bKeepRange) {
        this.editStart();
        this.moRange.replace(cInsert);
        this.editComplete(bKeepRange);
    }

    public void replace(String sInsert, boolean bKeepRange) {
        this.editStart();
        this.moRange.replace(sInsert);
        this.editComplete(bKeepRange);
    }

    public void replace(TextStream oInsert, boolean bKeepRange) {
        this.editStart();
        this.moRange.replace(oInsert);
        this.editComplete(bKeepRange);
    }

    public void replacePara() {
        this.editStart();
        this.moRange.delete();
        TextPosnBase oPosn = new TextPosnBase(this.moRange.anchor());
        oPosn.insertPara();
        this.editComplete();
    }

    public void deleteChar(boolean bForward) {
        this.editStart();
        TextPosnBase oPosn = new TextPosnBase(this.moRange.loose());
        if (bForward) {
            oPosn.deleteAhead();
        } else {
            oPosn.deleteBack();
        }
        this.editComplete();
    }

    public void deleteWord(boolean bForward, int eWordMode) {
        this.grabWord();
        this.deleteSelect();
    }

    public void deleteWord(boolean bForward) {
        this.deleteWord(bForward, 0);
    }

    public void deleteLine(boolean bForward) {
        this.grabLine();
        this.deleteSelect();
    }

    public void deletePara(boolean bForward) {
        this.grabPara();
        this.deleteSelect();
    }

    public void deleteAll(boolean bForward) {
        this.grabAll();
        this.deleteSelect();
    }

    public void deleteSelect() {
        this.editStart();
        this.moRange.delete();
        this.editComplete();
    }

    public TextEditor cloneEditor() {
        return new TextEditor(this);
    }

    public void onTextChanged(boolean bExtentChanged) {
    }

    public CoordPair relToAbs(GFXEnv poGfxEnv, CoordPair oPoint) {
        return oPoint;
    }

    public CoordPair absToRel(GFXEnv poGfxEnv, CoordPair oPoint) {
        return oPoint;
    }

    public void showCaret() {
    }

    public void complete() {
        this.moRange.start(this.moRange.end());
    }

    private void moveComplete(TextPosnBase oPosn, boolean bSelect, TextStream poDescendantStream) {
        TextRange oNewRange = new TextRange(this.moRange);
        oNewRange.loose(oPosn.index());
        if (!bSelect) {
            TextPosnBase oStart = new TextPosnBase(oPosn);
            if (poDescendantStream != null && poDescendantStream != this.moRange.stream()) {
                oStart.prev();
            }
            oNewRange.anchor(oStart.index());
        }
        this.moveComplete(oNewRange);
    }

    private void moveComplete(TextPosnBase oPosn, boolean bSelect) {
        this.moveComplete(oPosn, bSelect, null);
    }

    private void moveComplete(TextRange oNewRange) {
        TextDisplay poDisplay;
        oNewRange.tighten();
        if (!this.mbUpdateDisplay || this.mpoGfxEnv == null || (poDisplay = this.display()) != null) {
            // empty if block
        }
        this.moRange = oNewRange;
        this.showCaret();
    }

    private void editStart() {
        this.moRange.tighten();
        Rect poExtent = this.extent();
        if (poExtent != null) {
            this.moPrevExtent = poExtent;
        }
    }

    private void editComplete(boolean bKeepRange) {
        if (!bKeepRange) {
            this.moRange.start(this.moRange.end());
        }
        boolean bExtentChange = false;
        Rect poExtent = this.extent();
        if (poExtent != null && poExtent != this.moPrevExtent) {
            bExtentChange = true;
        }
        this.onTextChanged(bExtentChange);
        this.showCaret();
    }

    private void editComplete() {
        this.editComplete(false);
    }

    private Rect extent() {
        return null;
    }

    private TextDisplay display() {
        TextStream poStream = this.moRange.stream();
        if (poStream == null) {
            return null;
        }
        return poStream.display();
    }

    private static int userIndexToStreamIndex(TextStream poStream, int nUserIndex) {
        TextPosnBase oPosn = new TextPosnBase(poStream);
        if (nUserIndex > poStream.posnCount()) {
            nUserIndex = poStream.posnCount();
        }
        while (nUserIndex-- > 0) {
            oPosn.nextUserPosn();
        }
        return oPosn.index();
    }

    private static int streamIndexToUserIndex(TextStream poStream, int nStreamIndex) {
        TextPosnBase oPosn = new TextPosnBase(poStream, nStreamIndex);
        int nUserIndex = 0;
        while (oPosn.prevUserPosnType() != 0) {
            ++nUserIndex;
        }
        return nUserIndex;
    }
}