Package com.ibm.richtext.styledtext

Examples of com.ibm.richtext.styledtext.StyledText


    public void test() {

        AttributeMap bold = new AttributeMap(TextAttribute.WEIGHT,
                                             TextAttribute.WEIGHT_BOLD);
        MText text = new StyledText("Hello there!", AttributeMap.EMPTY_ATTRIBUTE_MAP);
        text.replace(2, 2, 'V', bold);

        MTextIterator iter = new MTextIterator(text, FONT_MAPPER, 0, text.length());
        compareIterToText(iter, text);

        text.replace(6, 8, new StyledText("ALL_BOLD", bold), 0, 8);
        iter = new MTextIterator(text, FONT_MAPPER, 1, text.length()-3);
        compareIterToText(iter, text);

        iter = new MTextIterator(text, FONT_MAPPER, 0, text.length());
        compareIterToText(iter, text);
    }
View Full Code Here


            Object document = bundle.getObject(docName+".sample");
            MConstText text;
           
            if (document instanceof String) {
                text = new StyledText((String)document,
                                      AttributeMap.EMPTY_ATTRIBUTE_MAP);
            }
            else {
                URL url = (URL) document;
                ObjectInputStream in = new ObjectInputStream(url.openStream());
View Full Code Here

        else {
            String str = loadString(new File(args[0]));
            if (str == null) {
                throw new Error("Couldn't load String from file " + args[0]);
            }
            MConstText text = new StyledText(str, AttributeMap.EMPTY_ATTRIBUTE_MAP);
            FileUtils.saveMText(new File(args[1]), text);
        }
    }
View Full Code Here

    public void test() {

        AttributeMap bold = new AttributeMap(TextAttribute.WEIGHT,
                                             TextAttribute.WEIGHT_BOLD);
        MConstText text1 = new StyledText("Test contents. 1234\nHow about it?",
                                              AttributeMap.EMPTY_ATTRIBUTE_MAP);
        MConstText text2 = new StyledText("Another test string.", bold);

        _testWithText(text1);
        _testWithText(text2);
        _testWithText(new StyledText());

        StyledText big1 = new StyledText();
        for (int i=0; i < 50; i++) {
            big1.append(text1);
        }

        _testWithText(big1);
        StyledText big2 = new StyledText(text1);
        for (int i=0; i < 80; i++) {
            big2.append(text2);
        }

        _testWithText(big2);
    }
View Full Code Here

     * Text must be editable to pass this test.
     */
    public void _testModifications(MConstText insertionText,
                                   boolean restoreOldText) {

        MConstText oldText = new StyledText(fTextPanel.getText());
        final int insLength = insertionText.length();

        fListener.allowAll();
        fListener.expectEvent(TextPanelEvent.TEXT_CHANGED);
        fListener.expectEvent(TextPanelEvent.SELECTION_RANGE_CHANGED);
        fTextPanel.append(insertionText);
        fListener.assertNotExpectingEvents();
       
        if (fTextPanel.getSelectionStart() != oldText.length() + insLength) {
            reportError("Append didn't result in correct selection");
        }

        fListener.expectEvent(TextPanelEvent.TEXT_CHANGED);
        fListener.expectEvent(TextPanelEvent.SELECTION_RANGE_CHANGED);
        fTextPanel.insert(insertionText, 0);
        fListener.assertNotExpectingEvents();
        fListener.allowAll();
       
        if (fTextPanel.getSelectionStart() != insLength) {
            reportError("Insert didn't result in correct selection");
        }

        fTextPanel.replaceRange(insertionText, insLength, insLength+oldText.length());
        if (fTextPanel.getSelectionStart() != insLength*2) {
            reportError("Replace didn't result in correct selection");
        }
        if (fTextPanel.getSelectionEnd() != insLength*2) {
            reportError("Replace didn't result in correct selection");
        }
        if (fTextPanel.getTextLength() != insLength*3) {
            reportError("textLength is incorrect");
        }

        if (restoreOldText) {
            fTextPanel.setText(oldText);
            if (fTextPanel.getSelectionStart() != oldText.length()) {
                reportError("setText didn't result in correct selection");
            }
            if (fTextPanel.getTextLength() != oldText.length()) {
                reportError("length incorrect after setText");
            }
        }
       
        fListener.allowAll();
View Full Code Here

        //catch(Error e) {
        //    logMessage("iterationCount="+iterationCount+";  expexting="+exp);
        //    throw e;
        //}

        MConstText insText = new StyledText("7",
                                AttributeMap.EMPTY_ATTRIBUTE_MAP);

        final int origLength = fTextPanel.getTextLength();
        int start = origLength / 3;
       
        fListener.allowEvent(TextPanelEvent.SELECTION_RANGE_CHANGED);
        fListener.allowEvent(TextPanelEvent.SELECTION_STYLES_CHANGED);

        for (int i=start; i < BIG_COMMAND_LOG_SIZE+start; i++) {
            fListener.expectEvent(TextPanelEvent.UNDO_STATE_CHANGED);
            fListener.expectEvent(TextPanelEvent.TEXT_CHANGED);
            if (fTextPanel.getSelectionStart() != fTextPanel.getSelectionEnd()) {
                fListener.expectEvent(TextPanelEvent.SELECTION_EMPTY_CHANGED);
            }
            fTextPanel.insert(insText, i);
            fListener.assertNotExpectingEvents();
        }

        fListener.allowEvent(TextPanelEvent.SELECTION_EMPTY_CHANGED);
       
        for (int i=0; i < BIG_COMMAND_LOG_SIZE-1; i++) {
            fListener.expectEvent(TextPanelEvent.UNDO_STATE_CHANGED);
            fListener.expectEvent(TextPanelEvent.TEXT_CHANGED);
            fTextPanel.undo();
            fListener.assertNotExpectingEvents();
        }
        if (!fTextPanel.canUndo()) {
            reportError("Command log is too small");
        }
       
        fListener.allowAll();
        fTextPanel.undo();
        if (fTextPanel.canUndo()) {
            reportError("Command log is too large");
        }

        if (fTextPanel.getTextLength() != origLength * insText.length()) {
            reportError("Text length was not restored");
        }

        for (int i=0; i < BIG_COMMAND_LOG_SIZE; i++) {
            fTextPanel.redo();
View Full Code Here

        AttributeMap boldAttrs = new AttributeMap(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
        AttributeMap italicAttrs = new AttributeMap(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
        AttributeMap emptyAttrs = AttributeMap.EMPTY_ATTRIBUTE_MAP;

        final String bold1Str_getString = "b";
        MConstText bold1Str = new StyledText(bold1Str_getString, boldAttrs);

        final String italic1Str_getString = "i";
        MConstText italic1Str = new StyledText(italic1Str_getString, italicAttrs);

        final String plain1Str_getString = "p";
        MConstText plain1Str = new StyledText(plain1Str_getString, emptyAttrs);

        StyledText temp = new StyledText();
        temp.append(bold1Str);
        temp.append(italic1Str);
        final String boldItalicStr_getString = bold1Str_getString.concat(italic1Str_getString);
        MConstText boldItalicStr = temp;

        temp = new StyledText();
        temp.append(bold1Str);
        temp.append(bold1Str);
        temp.append(bold1Str);
        final String bold3Str_getString = "bbb";
        MConstText bold3Str = temp;

        MText buf = new StyledText();
        String plainText = new String();
        //int testIteration=0; - now instance variables so errln can report it
        //int theCase=0;

        final int NUM_CASES = 14;
        boolean[] casesExecuted = new boolean[NUM_CASES];
        final int stopAt = -1;
        Random rand = new Random(RAND_SEED);

        final String ALWAYS_DIFFERENT = "\uFEFF";

            for (testIteration=0; testIteration < TEST_ITERATIONS; testIteration++) {

                theCase = randInt(rand, NUM_CASES);

                casesExecuted[theCase] = true;

                if (testIteration == stopAt) {
                    testIteration = stopAt;  // Convenient place to put breakpoint
                }

                int timeStamp = buf.getTimeStamp();
                String oldPlainText = plainText;
                if (oldPlainText == null) {
                    errln("oldPlainText is null!");
                }

                switch (theCase) {

                    case 0:
                        // create new string; replace chars at start with different style
                        buf = new StyledText();
                        buf.append(bold3Str);
                        buf.replace(0, 1, italic1Str, 0, italic1Str.length());
                        buf.replace(0, 0, italic1Str, 0, italic1Str.length());

                        plainText = bold3Str_getString.substring(1, bold3Str.length());
                        plainText = italic1Str_getString.concat(plainText);
                        plainText = italic1Str_getString.concat(plainText);
                        oldPlainText = null;
                        break;

                    case 1:
                        // delete the last character from the string
                        if (buf.length() == 0) {
                            buf.replace(0, 0, italic1Str, 0, italic1Str.length());
                            plainText = italic1Str_getString;
                            oldPlainText = ALWAYS_DIFFERENT;
                        }
                        buf.remove(buf.length()-1, buf.length());
                        plainText = plainText.substring(0, plainText.length()-1);
                        break;

                    case 2:
                        // replace some of the buffer with boldItalicStr
                        int rStart = randInt(rand, buf.length()+1);
                        int rStop = randInt(rand, rStart, buf.length()+1);
                        buf.replace(rStart, rStop, boldItalicStr);
                        {
                            String newString = (rStart>0)? plainText.substring(0, rStart) : new String();
                            newString = newString.concat(boldItalicStr_getString);
                            if (rStop < plainText.length())
                                newString = newString.concat(plainText.substring(rStop, plainText.length()));
                            oldPlainText = ALWAYS_DIFFERENT;
                            plainText = newString;
                        }
                        break;

                    case 3:
                        // repeatedly insert strings into the center of the buffer
                        {
                            int insPos = buf.length() / 2;
                            String prefix = plainText.substring(0, insPos);
                            String suffix = plainText.substring(insPos, plainText.length());
                            String middle = new String();
                            for (int ii=0; ii<4; ii++) {
                                MConstText which = (ii%2==0)? boldItalicStr : bold3Str;
                                String whichString = (ii%2==0)? boldItalicStr_getString : bold3Str_getString;
                                int tempPos = insPos+middle.length();
                                buf.insert(tempPos, which);
                                middle = middle.concat(whichString);
                            }
                            plainText = prefix.concat(middle).concat(suffix);
                            oldPlainText = ALWAYS_DIFFERENT;
                        }
                        break;

                    case 4:
                    // insert bold1Str at end
                        buf.append(bold1Str);
                        plainText = plainText.concat(bold1Str_getString);
                        break;

                    case 5:
                    // delete a character from the string
                        if (buf.length() > 0) {
                            int delPos = randInt(rand, buf.length()-1);
                            buf.remove(delPos, delPos+1);
                            plainText = plainText.substring(0, delPos).concat(plainText.substring(delPos+1));
                        }
                        else {
                            buf.replace(0, 0, plain1Str, 0, plain1Str.length());
                            plainText = plain1Str_getString;
                        }
                        break;

                    case 6:
                    // replace the contents of the buffer (except the first character) with itself
                        {
                            int start = buf.length() > 1? 1 : 0;
                            buf.replace(start, buf.length(), buf);
                            plainText = plainText.substring(0, start).concat(plainText);
                            if (buf.length() > 0) {
                                oldPlainText = ALWAYS_DIFFERENT;
                            }
                        }
                        break;

                    case 7:
                    // append the contents of the buffer to itself
                        {
                            MConstText content = buf;
                            buf.insert(buf.length(), content);
                            plainText = plainText.concat(plainText);
                        }
                        break;

                    case 8:
                    // replace the buffer with boldItalicStr+bold3Str
                        {
                            MText replacement = new StyledText();
                            replacement.append(boldItalicStr);
                            replacement.append(bold3Str);
                            buf.replace(0, buf.length(), replacement, 0, replacement.length());
                            plainText = boldItalicStr_getString.concat(bold3Str_getString);
                            oldPlainText = ALWAYS_DIFFERENT;
                        }
                        break;
View Full Code Here

        randomTest();
    }

    private void easyTests() {

        MText text = new StyledText("a\nb\nc\nd\n", PLAIN);
        text.modifyParagraphStyles(0, text.length(), A_MOD);
        verifyParagraphCount(text);

        MText src = new StyledText("XXX\nYYY", PLAIN);
        src.modifyParagraphStyles(0, src.length(), B_MOD);
        verifyParagraphCount(src);

        MText temp = text.extractWritable(0, text.length());
        temp.append(src);
        verifyParagraphCount(temp);
        for (int i=0; i < text.length(); i++) {
            if (!temp.paragraphStyleAt(i).equals(text.paragraphStyleAt(i))) {
                errln("Paragraph styles are wrong");
            }
        }
        for (int i=0; i < src.length(); i++) {
            if (!temp.paragraphStyleAt(i+text.length()).equals(src.paragraphStyleAt(i))) {
                errln("Paragraph styles are wrong");
            }
        }

        temp = text.extractWritable(0, text.length());
        temp.replace(0, 1, src, 0, src.length());
        verifyParagraphCount(temp);
        if (temp.paragraphLimit(0) != 4) {
            errln("Paragraph limit is wrong");
        }
        if (!temp.paragraphStyleAt(0).equals(B_STYLE)) {
            errln("First style is wrong");
        }
        if (!temp.paragraphStyleAt(4).equals(A_STYLE)) {
            errln("Style after insert is wrong");
        }

        // test append
        MConstText newSrc = src.extract(4, 7);
        MText initC = new StyledText("cccccc", PLAIN);
        initC.modifyParagraphStyles(0, initC.length(), C_MOD);
        initC.append(newSrc);
        // now initC should be one paragraph with style B
        if (initC.paragraphLimit(0) != initC.length()) {
            errln("Should only be one paragraph");
        }
        if (initC.paragraphStyleAt(0) != initC.paragraphStyleAt(initC.length())) {
            errln("Two different paragraph styles");
        }
        if (!initC.paragraphStyleAt(initC.length()/2).equals(B_STYLE)) {
            errln("Incorrect paragraph style");
        }
       
        text = new StyledText("aaa\n", PLAIN);
        text.modifyParagraphStyles(0, text.length(), A_MOD);
        text.modifyParagraphStyles(text.length(), text.length(), B_MOD);
        if (text.paragraphStyleAt(text.length()) != B_STYLE) {
            errln("0-length paragraph at end has incorrect style");
        }
View Full Code Here

        return start + (Math.abs(rand.nextInt())%(limit-start)) ;
    }

    private void randomTest() {

        MText noParagraph = new StyledText("zzzz", PLAIN);
        noParagraph.modifyParagraphStyles(0, noParagraph.length(), A_MOD);
        MText twoParagraphs = new StyledText("aaa\nbbb", PLAIN);
        twoParagraphs.modifyParagraphStyles(0, twoParagraphs.paragraphLimit(0), B_MOD);
        MText threeParagraphs = new StyledText("cc\ndd\nee", PLAIN);
        threeParagraphs.modifyParagraphStyles(0, 3, C_MOD);
        threeParagraphs.modifyParagraphStyles(3, 6, D_MOD);
        threeParagraphs.modifyParagraphStyles(6, 8, E_MOD);
        MText trailingP1 = new StyledText("hhhh\n", PLAIN);
        trailingP1.modifyParagraphStyles(0, trailingP1.paragraphLimit(0), C_MOD);
        MText trailingP2 = new StyledText("iii\n", PLAIN);
        trailingP2.modifyParagraphStyles(0, 0, D_MOD);
        trailingP2.modifyParagraphStyles(trailingP2.length(), trailingP2.length(), B_MOD);

        if (!trailingP2.paragraphStyleAt(trailingP2.length()-1).equals(D_STYLE)) {
            errln("Style incorrect in trailingP2");
        }
        if (!trailingP2.paragraphStyleAt(trailingP2.length()).equals(B_STYLE)) {
            errln("Ending style incorrect in trailingP2");
        }

        MConstText[] tests = { noParagraph, twoParagraphs,
                                    threeParagraphs, trailingP1, trailingP2 };

        Random random = new Random(RAND_SEED);

        int stopAt = 465;
        int i = 0;
        try {
            for (i=0; i < NUM_TESTS; i++) {

                int srcIndex = randInt(random, tests.length);
                int targetIndex = randInt(random, tests.length);
                MText target = new StyledText(tests[targetIndex]);
                MConstText src = tests[srcIndex];

                int srcStart = randInt(random, src.length());
                int srcLimit = randInt(random, srcStart, src.length());
                int start = randInt(random, target.length());
                int limit = randInt(random, start, target.length());

                if (i == stopAt) {
                    stopAt = i;
                }
View Full Code Here

    private String twelveLines = fiveLines + "\n" + fiveLines + "\nf\n";
    AttributeMap PLAIN = AttributeMap.EMPTY_ATTRIBUTE_MAP;

    public void test() {

        MConstText text = new StyledText(fiveLines, PLAIN);
        _testLineExceptions(makeFormatter(text, 100, true), 5);
        _testLineAccess(makeFormatter(text, 100, true), 5);

        text = new StyledText(twelveLines, PLAIN);
        _testLineExceptions(makeFormatter(text, 3, false), 12);
        _testLineAccess(makeFormatter(text, 100, true), 12);

        _testWithModification();
    }
View Full Code Here

TOP

Related Classes of com.ibm.richtext.styledtext.StyledText

Copyright © 2018 www.massapicom. 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.