1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package com.oracle.javafx.scenebuilder.kit.fxom.glue; 33 34 /** 35 * 36 * 37 */ 38 public class GlueCharacters extends GlueAuxiliary { 39 40 public enum Type { 41 TEXT, 42 COMMENT 43 } 44 45 private final Type type; 46 private String data; 47 48 public GlueCharacters(GlueDocument document, Type type, String data) { 49 super(document); 50 this.type = type; 51 this.data = data; 52 } 53 54 public Type getType() { 55 return type; 56 } 57 58 public String getData() { 59 return data; 60 } 61 62 public void setData(String data) { 63 this.data = data; 64 } 65 66 public void adjustIndentBy(int delta) { 67 /* 68 * data 69 * 70 * 'xxxxxxx\nbbbbbbxxxxxxx\nbbbbbbbbxxxxxxxxxx\nbbbbbxxxxx....' 71 * 72 * b : white space 73 * x : any other char 74 * 75 * Indenting means: 76 * - when delta > 0, inserting 'delta' spaces after each '\n' char 77 * - when delta < 0, removing 'delta' spaces after each '\n' char *when possible* 78 */ 79 80 final StringBuilder newValue = new StringBuilder(); 81 82 if (delta > 0) { 83 for (int i = 0, length = data.length(); i < length; i++) { 84 final char ch = data.charAt(i); 85 newValue.append(ch); 86 if (ch == '\n') { 87 for (int n = 0; n < delta; n++) { 88 newValue.append(' '); 89 } 90 } 91 } 92 } else { 93 for (int i = 0, length = data.length(); i < length; i++) { 94 final char ch = data.charAt(i); 95 newValue.append(ch); 96 if (ch == '\n') { 97 while ((i+1 < length) 98 && (data.charAt(i+1) == ' ') 99 && (delta < 0)) { 100 i++; 101 delta++; 102 } 103 } 104 } 105 } 106 107 data = newValue.toString(); 108 } 109 110 public int guessIndent() { 111 int result; 112 113 /* 114 * If data match the following pattern 115 * 116 * 'xxxxxxx\nbbbbbbxxxxxxx....' 117 * 118 * b : white space 119 * x : any other char 120 * 121 * then returns number of b characters else returns -1. 122 */ 123 124 int i = 0; 125 final int count = data.length(); 126 while ((i < count) && data.charAt(i) != '\n') { 127 i++; 128 } 129 130 if (i < count) { 131 i++; 132 result = 0; 133 while ((i < count) && data.charAt(i) == ' ') { 134 result++; 135 i++; 136 } 137 } else { 138 result = -1; 139 } 140 141 return result; 142 } 143 }