Ticket #2495: 2495-reduced.patch
File 2495-reduced.patch, 29.9 KB (added by pap, 15 years ago) |
---|
-
modules/org.sophie2.main.func.text/src/main/java/org/sophie2/main/func/text/links/TextLinkProcessor.java
### Eclipse Workspace Patch 1.0 #P sophie
2 2 3 3 import java.util.HashMap; 4 4 5 5 6 import org.sophie2.base.commons.structures.ImmTreeMap; 6 7 import org.sophie2.base.commons.util.ImmColor; 7 8 import org.sophie2.base.commons.util.ImmMap; 8 9 import org.sophie2.base.model.text.HotAttr; 10 import org.sophie2.base.model.text.TextChange; 9 11 import org.sophie2.base.model.text.TextReplaceChange; 10 12 import org.sophie2.base.model.text.elements.CommonAttr; 11 13 import org.sophie2.base.model.text.model.ImmTextInterval; … … 130 132 131 133 public DefaultTextEffect applyChange(TextProcessorEffect prevEffect, ImmText oldSource, 132 134 ImmText oldResult, TextLinkOptions procOptions) { 133 // TextChange change = prevEffect.getChange(); 134 // ImmText sourceText = prevEffect.getText(); 135 // ImmText toProcess = sourceText.subText(change.getTargetDamage(sourceText)); 136 // 137 // TextReplaceChange effect = new TextReplaceChange( 138 // change.getSourceDamage(oldSource), process(toProcess, procOptions)); 139 // 140 // ImmText result = effect.applyTo(oldResult); 141 // assert result.getEnd() == sourceText.getEnd() : result.getEnd() + " : " + sourceText.getEnd(); 142 // 143 // return new DefaultTextEffect(effect, result); 144 145 ImmText res = process(prevEffect.getText(), procOptions); 146 return new DefaultTextEffect(ImmTextUtils.getChange(oldResult, res), res); 135 136 TextChange change = prevEffect.getChange(); 137 ImmText oldResult2 = prevEffect.getText(); 138 ImmTextInterval damagedInterval = change.getTargetDamage(oldSource); 139 ImmText damagedText = oldResult2.subText(damagedInterval); 140 damagedText = process(damagedText, procOptions); 141 ImmText res = ImmTextUtils.fastReplaceInterval(damagedInterval, oldResult2, damagedText); 142 if (res == null) { 143 ImmText prefixText = oldResult2.subText(new ImmTextInterval(oldResult2.getBegin(), damagedInterval.getBegin())); 144 ImmText suffixText = oldResult2.subText(new ImmTextInterval(damagedInterval.getEnd(), oldResult2.getEnd())); 145 res = prefixText.concat(damagedText); 146 res = res.concat(suffixText); 147 } 148 TextChange newChange = new TextReplaceChange(damagedInterval, damagedText); 149 return new DefaultTextEffect(newChange, res); 147 150 } 148 151 149 152 public ImmText process(ImmText sourceText, TextLinkOptions procOptions) { … … 161 164 HashMap<HotAttr<?>, Object> values = new HashMap<HotAttr<?>, Object>(); 162 165 ImmColor fgColor = procOptions.getColor(linkId); 163 166 if (fgColor != null) { 164 165 167 values.put(CommonAttr.FOREGROUND_COLOR, fgColor); 166 unitStyle = unitStyle.derive(values);167 168 168 // TODO performance could be better if the style is applied for a whole interval. 169 result = result.applyStyle(unitStyle, new ImmTextInterval(i, i+1)); 169 HotStyleDef newStyle = HotStyleDef.getEmpty().derive(values); 170 int endIndex = getEndForLinkId(sourceText, i); 171 result = result.applyStyle(newStyle, new ImmTextInterval(i, endIndex)); 172 i += endIndex; 170 173 } 171 174 } 172 175 } 173 176 174 177 return result; 175 178 } 179 private int getEndForLinkId(ImmText sourceText, int beginIndex) { 180 String linkId = sourceText.unitAt(beginIndex).getStyle().getValue(CommonAttr.LINK_ATTRIBUTE); 181 int endIndex = beginIndex; 182 for (; endIndex < sourceText.getEnd(); ++endIndex) { 183 if (! sourceText.unitAt(endIndex).getStyle().getValue(CommonAttr.LINK_ATTRIBUTE).equals(linkId)) { 184 break; 185 } 186 } 187 return endIndex; 188 } 176 189 177 190 public DefaultTextEffect setOptions(ImmText source, ImmText oldProcessed, 178 191 TextLinkOptions oldOptions, TextLinkOptions newOptions) { -
modules/org.sophie2.base.model.text/src/main/java/org/sophie2/base/model/text/model/TextUtils.java
97 97 * The text's prefix. 98 98 */ 99 99 public static ImmText getPrefix(ImmText text, int toIndex) { 100 if (text.getEnd() == 0 ) {100 if (text.getEnd() == 0 || text.getEnd() == toIndex) { 101 101 return text; 102 102 } 103 103 ImmTextInterval interval = … … 120 120 */ 121 121 public static ImmText getSuffix(ImmText text, int fromIndex) { 122 122 123 if (text.getEnd() == 0 && fromIndex == 0) {123 if (text.getEnd() == 0 || fromIndex == text.getBegin()) { 124 124 return text; 125 125 } 126 126 int begin = ImmTextUtils.advance(text, text.getBegin(), fromIndex); -
modules/org.sophie2.base.model.text/src/main/java/org/sophie2/base/model/text/model/ImmHotText.java
89 89 public ImmHotText replace(ImmTextInterval interval, ImmText newText) { 90 90 assert interval != null : "Interval should not be null"; 91 91 assert newText != null : "Text should not be null"; 92 92 93 93 if (interval.isEmpty() && newText.getEnd() == 0) { 94 94 return this; 95 95 } … … 114 114 newTextUnits = newTextUnits.remove(index); 115 115 } 116 116 117 int endRunIndex = getRunEnd(0); 117 HotStyleDef lastUnitStyle = null; 118 HotStyleDef lastUsedStyle = null; 118 119 HotStyleDef newStyle = newStyles; 119 120 if (newText.getEnd() >= 1) { 120 121 TextUnit unit = newText.unitAt(0); 121 122 HotStyleDef style = unit.getStyle(); 122 newStyle = newStyle.derive(style); 123 lastUnitStyle = style; 124 newStyle = newStyle.derive(style); 125 lastUsedStyle = newStyle; 123 126 } 124 127 125 128 for (int i = 0; i < newText.getEnd(); ++i, ++index) { 126 129 TextUnit unit = newText.unitAt(i); 127 if (i >= endRunIndex) { 128 HotStyleDef style = unit.getStyle(); 129 newStyle = newStyle.replaceDerive(style); 130 endRunIndex = getRunEnd(endRunIndex + 1); 130 HotStyleDef style = unit.getStyle(); 131 if (lastUnitStyle != null && 132 style.getHash().equals(lastUnitStyle.getHash())) { 133 newStyle = lastUsedStyle; 134 } else { 135 //I do not see the difference between using derive 136 //and replaceDerive(if sth. is wrong with the styles-use replaceDerive instead) 137 //--diana 05-08-10 138 newStyle = newStyle.derive(style); 139 lastUsedStyle = newStyle; 140 lastUnitStyle = style; 131 141 } 132 142 133 143 newTextUnits = … … 156 166 } 157 167 158 168 public ImmHotText applyStyle(HotStyleDef style, ImmTextInterval interval) { 159 169 160 170 if (interval.isEmpty()) { 161 171 return this; 162 172 } 163 173 164 174 ImmList<TextUnit> newImmTextUnits = this.textUnits; 175 HotStyleDef lastUnitStyle = this.textUnits.get(interval.getBegin()).getStyle(); 176 HotStyleDef lastUsedStyle = this.textUnits.get(interval.getBegin()).getStyle().derive(style); 165 177 178 166 179 // TODO what if the begin is after the end?? --kyli 167 180 for (int i = interval.getBegin(); i < interval.getEnd(); ++i) { 181 HotStyleDef unitStyle = this.textUnits.get(i).getStyle(); 182 HotStyleDef newStyle = lastUsedStyle; 183 if (! unitStyle.getHash().equals(lastUnitStyle.getHash())) { 184 newStyle = unitStyle.derive(style); 185 lastUnitStyle = unitStyle; 186 lastUsedStyle = newStyle; 187 } 168 188 TextUnit newTextUnit = new TextUnit(this.textUnits.get(i).getChar(), 169 this.textUnits.get(i).getStyle().derive(style));189 newStyle); 170 190 newImmTextUnits = newImmTextUnits.set(i, newTextUnit); 171 191 } 172 192 … … 241 261 ImmHotText otherHotText = (ImmHotText) otherText; 242 262 return new ImmHotText(this.textUnits.concat(otherHotText.textUnits)); 243 263 } 244 245 private int getRunEnd(int i) { 246 if (this.getEnd() <= i) { 247 return i; 264 265 @SuppressWarnings("unused") 266 private ImmHotText replaceUnits(ImmTextInterval interval, ImmHotText text) { 267 ImmList<TextUnit> newUnits = text.textUnits; 268 ImmList<TextUnit> newTextUnits = this.textUnits; 269 assert interval.getEnd() - interval.getBegin() == newUnits.size() : "Inconsistent replace!"; 270 for(int i = interval.getBegin(); i < interval.getEnd(); ++i) { 271 newTextUnits = newTextUnits.set(i, newUnits.get(i - interval.getBegin())); 248 272 } 249 int endIndex = i; 250 assert endIndex < this.getEnd() && endIndex >= this.getBegin(); 273 assert newTextUnits.size() == this.textUnits.size(); 274 return new ImmHotText(newTextUnits); 275 } 251 276 252 HotStyleDef styleDef = unitAt(endIndex).getStyle(); 253 while (endIndex < this.getEnd()) { 254 if (! unitAt(endIndex).getStyle().equals(styleDef)) { 255 break; 256 } 257 ++ endIndex; 258 } 259 return endIndex - 1 < getBegin() ? endIndex : endIndex - 1; 260 } 277 //Currently the HotStyleDef::equals method is not fast enough to use this function ;( 278 // private int getRunEnd(int i) { 279 // if (this.getEnd() <= i) { 280 // return i; 281 // } 282 // int endIndex = i; 283 // assert endIndex < this.getEnd() && endIndex >= this.getBegin(); 284 // 285 // HotStyleDef styleDef = unitAt(endIndex).getStyle(); 286 // while (endIndex < this.getEnd()) { 287 // if (! unitAt(endIndex).getStyle().equals(styleDef)) { 288 // break; 289 // } 290 // ++ endIndex; 291 // } 292 // return endIndex - 1 < getBegin() ? endIndex : endIndex - 1; 293 // } 261 294 } -
modules/org.sophie2.base.connectivity/.settings/org.eclipse.jdt.launching.prefs
1 #Thu Aug 12 11:26:08 EEST 2010 2 eclipse.preferences.version=1 3 org.eclipse.jdt.launching.PREF_STRICTLY_COMPATIBLE_JRE_NOT_AVAILABLE=warning -
modules/org.sophie2.base.model.text/src/main/java/org/sophie2/base/model/text/mvc/CaretProcessor.java
4 4 import java.util.Map; 5 5 6 6 import org.sophie2.base.model.text.HotAttr; 7 import org.sophie2.base.model.text.TextChange; 8 import org.sophie2.base.model.text.TextReplaceChange; 7 9 import org.sophie2.base.model.text.elements.LayoutAttr; 8 10 import org.sophie2.base.model.text.model.ImmText; 9 11 import org.sophie2.base.model.text.model.ImmTextInterval; … … 45 47 ImmText sourceText = prevEffect.getText(); 46 48 ImmText newText = getStyledText(sourceText, procOptions); 47 49 48 return new DefaultTextEffect(ImmTextUtils.getChange(oldResult, newText), newText); 50 ImmTextInterval damagedInterval = prevEffect.getChange().getSourceDamage(oldSource); 51 ImmText replaceText = newText.subText(damagedInterval); 52 TextChange change = new TextReplaceChange(damagedInterval, replaceText); 53 return new DefaultTextEffect(change, newText); 49 54 } 50 55 51 56 public Class<CaretOptions> getOptionsClass() { … … 59 64 public DefaultTextEffect setOptions(ImmText source, 60 65 ImmText oldProcessed, CaretOptions oldOptions, CaretOptions newOptions) { 61 66 67 62 68 ImmText newText = getStyledText(source, newOptions); 63 69 64 70 return new DefaultTextEffect(ImmTextUtils.getChange(oldProcessed, newText), newText); -
modules/org.sophie2.main.func.text/.settings/org.eclipse.core.resources.prefs
1 #Thu Aug 12 10:29:41 EEST 2010 2 eclipse.preferences.version=1 3 encoding/<project>=UTF-8 -
modules/org.sophie2.main.func.text/src/main/java/org/sophie2/main/func/text/rtf/ApplyRtfStylesUtility.java
20 20 import org.sophie2.base.model.text.elements.TextAlign; 21 21 import org.sophie2.base.model.text.model.ImmHotText; 22 22 import org.sophie2.base.model.text.model.ImmText; 23 import org.sophie2.base.model.text.model.ImmTextInterval;24 23 import org.sophie2.base.model.text.model.ImmTextUtils; 25 24 import org.sophie2.base.model.text.style.HotStyleDef; 26 25 import org.sophie2.main.app.commons.util.AttributedPair; … … 75 74 76 75 applyStyles(element, builder, entries); 77 76 78 79 ImmText newText = new ImmHotText(builder.toString(), null);77 String text = builder.toString(); 78 ImmText newText = ImmTextUtils.createEmptyText(); 80 79 int curPos = newText.getBegin(); 81 80 82 81 for (ElementEntry entry : entries) { 83 82 int length = entry.getLength(); 84 ImmTextInterval interval = new ImmTextInterval(curPos, ImmTextUtils.85 advance(newText, curPos, length));86 83 HotStyleDef def = HotStyleDef.getEmpty().derive(entry.getAttributes()); 87 newText = newText.applyStyle(def, interval); 88 curPos = ImmTextUtils.advance(newText, curPos, length); 84 ImmText styledText = new ImmHotText(text.substring(curPos, curPos + length) , def); 85 curPos += length; 86 newText = newText.concat(styledText); 89 87 } 90 88 91 89 return newText; -
modules/org.sophie2.main.func.text/src/main/java/org/sophie2/main/func/text/search/TextSearchProcessor.java
4 4 5 5 import org.sophie2.base.commons.util.ImmColor; 6 6 import org.sophie2.base.model.text.HotAttr; 7 import org.sophie2.base.model.text.TextChange; 7 8 import org.sophie2.base.model.text.TextReplaceChange; 8 9 import org.sophie2.base.model.text.elements.CommonAttr; 9 10 import org.sophie2.base.model.text.model.ImmTextInterval; … … 31 32 } 32 33 33 34 private static TextSearchProcessor instance = null; 34 35 35 36 /** 36 37 * Gets the only instance of this processor. 37 38 * … … 63 64 64 65 ImmText newResult = process(sourceText, procOptions); 65 66 66 return new DefaultTextEffect( 67 ImmTextUtils.getChange(oldResult, newResult), newResult); 67 ImmTextInterval damagedInterval = prevEffect.getChange().getSourceDamage(oldSource); 68 ImmText damagedText = newResult.subText(damagedInterval); 69 TextChange change = new TextReplaceChange(damagedInterval, damagedText); 70 71 return new DefaultTextEffect(change, newResult); 68 72 } 69 73 70 74 public Class<TextSearchOptions> getOptionsClass() { … … 73 77 74 78 public ImmText process(ImmText sourceText, TextSearchOptions procOptions) { 75 79 80 ImmTextInterval searchedInterval = procOptions.getInterval(); 81 if (searchedInterval.isEmpty()) { 82 return sourceText; 83 } 76 84 HashMap<HotAttr<?>, Object> styleValues = new HashMap<HotAttr<?>, Object>(); 77 85 styleValues.put(CommonAttr.BACKGROUND_COLOR, procOptions.getColor()); 78 86 79 87 HotStyleDef style = HotStyleDef.getEmpty().derive(styleValues); 80 if ( procOptions.getInterval().getBegin() >= sourceText.getBegin() &&81 procOptions.getInterval().getEnd() <= sourceText.getEnd()) {82 return sourceText.applyStyle(style, procOptions.getInterval());88 if (searchedInterval.getBegin() >= sourceText.getBegin() && 89 searchedInterval.getEnd() <= sourceText.getEnd()) { 90 return sourceText.applyStyle(style, searchedInterval); 83 91 } 84 92 return sourceText; 85 93 } … … 119 127 private final ImmTextInterval interval; 120 128 private final ImmColor highlightColor; 121 129 130 @Override 131 public boolean equals(Object o) { 132 if (o == null) { 133 return false; 134 } 135 if (! (o instanceof TextSearchOptions)) { 136 return false; 137 } 138 TextSearchOptions opts = (TextSearchOptions) o; 139 return this.highlightColor.equals(opts.getColor()) 140 && this.interval.equals(opts.getInterval()); 141 } 122 142 /** 123 143 * Default constructor. 124 144 * -
modules/org.sophie2.extra.func.spellcheck/src/main/java/org/sophie2/extra/func/spellcheck/UnderliningProcessor.java
4 4 import java.util.List; 5 5 6 6 import org.sophie2.base.model.text.HotAttr; 7 import org.sophie2.base.model.text.TextChange; 7 8 import org.sophie2.base.model.text.TextReplaceChange; 8 9 import org.sophie2.base.model.text.elements.LayoutAttr; 9 10 import org.sophie2.base.model.text.model.ImmText; 10 11 import org.sophie2.base.model.text.model.ImmTextInterval; 11 import org.sophie2.base.model.text.model.ImmTextUtils;12 12 import org.sophie2.base.model.text.mvc.DefaultTextEffect; 13 13 import org.sophie2.base.model.text.mvc.TextProcessor; 14 14 import org.sophie2.base.model.text.mvc.TextProcessorEffect; … … 49 49 ImmText sourceText = prevEffect.getText(); 50 50 ImmText newResult = process(sourceText, procOptions); 51 51 52 return new DefaultTextEffect( 53 ImmTextUtils.getChange(oldResult, newResult), newResult); 52 ImmTextInterval damagedInterval = prevEffect.getChange().getSourceDamage(oldSource); 53 ImmText damagedText = newResult.subText(damagedInterval); 54 TextChange change = new TextReplaceChange(damagedInterval, damagedText); 55 return new DefaultTextEffect(change, newResult); 54 56 } 55 57 56 58 public Class<UnderlineOptions> getOptionsClass() { … … 70 72 71 73 if (procOptions.getIntervals() != null) { 72 74 for (ImmTextInterval interval : procOptions.getIntervals()) { 73 if ( interval.getBegin() >= result.getBegin() &&75 if (!interval.isEmpty() && interval.getBegin() >= result.getBegin() && 74 76 interval.getEnd() <= result.getEnd ()){ 75 77 result = result.applyStyle(style, interval); 76 78 } … … 86 88 UnderlineOptions newOptions) { 87 89 88 90 ImmText result = process (source, newOptions); 89 91 90 92 //TODO: this is possibly slow and unneeded 91 93 ImmTextInterval oldDamagedInterval = new ImmTextInterval (oldProcessed.getBegin(), oldProcessed.getEnd()); 92 94 ImmTextInterval newDamagedInterval = new ImmTextInterval (result.getBegin(), result.getEnd()); -
modules/org.sophie2.base.model.text/src/main/java/org/sophie2/base/model/text/mvc/SelectionProcessor.java
44 44 45 45 public DefaultTextEffect applyChange(TextProcessorEffect prevEffect, 46 46 ImmText oldSource, ImmText oldResult, SelectionOptions procOptions) { 47 47 48 48 ImmText sourceText = prevEffect.getText(); 49 49 ImmText newText = processText(sourceText, procOptions); 50 50 51 51 assert newText.getEnd() == 52 52 sourceText.getEnd() : newText.getEnd() + " : " + sourceText.getEnd(); 53 ImmTextInterval damagedInterval = prevEffect.getChange().getSourceDamage(oldSource); 54 ImmText damagedText = newText.subText(damagedInterval); 53 55 54 return new DefaultTextEffect(ImmTextUtils.getChange(oldResult, newText), newText); 56 TextChange change = new TextReplaceChange(damagedInterval, damagedText); 57 return new DefaultTextEffect(change, newText); 55 58 } 56 59 57 60 public Class<SelectionOptions> getOptionsClass() { … … 75 78 Object value = procOptions.getValue(); 76 79 ImmTextInterval selectionInterval = procOptions.getSelection(); 77 80 81 if (selectionInterval.isEmpty()) { 82 return sourceText; 83 } 78 84 if (ImmTextUtils.isIndexInText(selectionInterval.getBegin(), sourceText) && 79 85 ImmTextUtils.isIndexInText(selectionInterval.getEnd(), sourceText)) { 80 86 … … 93 99 public DefaultTextEffect setOptions(ImmText source, 94 100 ImmText oldProcessed, SelectionOptions oldOptions, SelectionOptions newOptions) { 95 101 102 ImmText newText; 103 if (newOptions.equals(oldOptions)) { 104 newText = oldProcessed; 105 } else { 106 newText = processText(source, newOptions); 107 } 96 108 ImmTextInterval damagedInterval = 97 109 oldOptions.getSelection().unite(newOptions.getSelection()); 98 110 99 ImmText newText = processText(source, newOptions);100 101 111 TextChange change = 102 112 new TextReplaceChange(damagedInterval, newText.subText(damagedInterval)); 103 113 -
modules/org.sophie2.extra.func.spellcheck/src/test/java/org/sophie2/extra/func/spellcheck/SpellCheckDemoTest.java
327 327 String textStr = modelText.toString(); 328 328 329 329 List<String> misspelled = new LinkedList<String> (); 330 SpellCheckUtility.getMisspelledWords (misspelled, null, textStr);330 //SpellCheckUtility.getMisspelledWords (misspelled, null, textStr); 331 331 332 332 DefaultListModel model = (DefaultListModel) listMisspelled.getModel(); 333 333 -
modules/org.sophie2.base.connectivity/.settings/org.eclipse.jdt.core.prefs
1 # Wed Oct 21 16:55:23 EEST 20091 #Thu Aug 12 11:26:08 EEST 2010 2 2 eclipse.preferences.version=1 3 3 org.eclipse.jdt.core.builder.cleanOutputFolder=clean 4 4 org.eclipse.jdt.core.builder.duplicateResourceTask=warning … … 16 16 org.eclipse.jdt.core.codeComplete.localSuffixes= 17 17 org.eclipse.jdt.core.codeComplete.staticFieldPrefixes= 18 18 org.eclipse.jdt.core.codeComplete.staticFieldSuffixes= 19 org.eclipse.jdt.core.codeComplete.staticFinalFieldPrefixes= 20 org.eclipse.jdt.core.codeComplete.staticFinalFieldSuffixes= 19 21 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 20 22 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 21 23 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -
modules/org.sophie2.base.model.text/src/main/java/org/sophie2/base/model/text/model/ImmTextUtils.java
1 1 package org.sophie2.base.model.text.model; 2 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.Method; 3 5 import java.util.HashMap; 4 6 import java.util.Map; 5 7 … … 70 72 public static int advance(ImmText text, int index, int offset) { 71 73 if (!isIndexInText(index, text)) { 72 74 throw new IllegalArgumentException("The input index does not " + 73 "belong to the current text: " + index + " text: " + text + ":");75 "belong to the current text: " + index + " text: " + text + ":"); 74 76 } 75 77 76 78 int newIndex = index + offset; … … 96 98 * New concatenate text. 97 99 */ 98 100 public static ImmText concat(ImmText text1, ImmText text2) { 99 101 100 102 return text1.concat(text2); 101 // ImmText replacedText = text1.replace(new ImmTextInterval(text1.getEnd(),102 // text1.getEnd()), text2);103 // int endText1 = text1.getEnd();104 // for (int i = text2.getBegin(); i < text2.getEnd(); ++i) {105 // int pos = i + endText1;106 // replacedText = replacedText.applyStyle(107 // HotStyleDef.getDefault().derive(text2.unitAt(i).getStyle()),108 // new ImmTextInterval(pos, pos + 1));109 // }110 // return replacedText;103 // ImmText replacedText = text1.replace(new ImmTextInterval(text1.getEnd(), 104 // text1.getEnd()), text2); 105 // int endText1 = text1.getEnd(); 106 // for (int i = text2.getBegin(); i < text2.getEnd(); ++i) { 107 // int pos = i + endText1; 108 // replacedText = replacedText.applyStyle( 109 // HotStyleDef.getDefault().derive(text2.unitAt(i).getStyle()), 110 // new ImmTextInterval(pos, pos + 1)); 111 // } 112 // return replacedText; 111 113 } 112 114 113 115 /** … … 145 147 text.getStyleValue(attribute, new ImmTextInterval(beginPosition, beginPosition)))) { 146 148 beginPosition = advance(text, beginPosition, - 1); 147 149 } 148 150 149 151 if (!(beginPosition == text.getBegin() && value.equals( 150 152 text.getStyleValue(attribute, new ImmTextInterval(beginPosition, beginPosition))))) { 151 153 beginPosition = advance(text, beginPosition, 1); … … 195 197 return text.unitAt(position).getStyle().getValue(attribute); 196 198 } 197 199 198 200 199 201 /** 200 202 * Creates a change that modifies the source text to the result. 201 203 * … … 208 210 * change.applyTo(source).getStyledHash().equals(result.getStyledHash). 209 211 */ 210 212 public static TextChange getChange(ImmText source, ImmText result) { 211 213 212 214 // FIXME this implementation is dummy.. --kyli@2010-05-20 213 215 return new TextReplaceChange( 214 216 new ImmTextInterval(source.getBegin(), source.getEnd()), result); 215 217 } 218 /** 219 * Method for fast(faster than using the replace function of the text) replacing interval of a text with another text. 220 * The replacing text should be the same length as the interval. The styles of the new text are not derived - 221 * they are the same as using concat function. 222 * 223 * @param interval 224 * The interval to be replaced. 225 * @param oldText 226 * The text to be replaced. 227 * @param replacedText 228 * The replacing text. 229 * @return 230 * The new replaced text. 231 */ 232 public static ImmText fastReplaceInterval(ImmTextInterval interval, ImmText oldText, ImmText replacedText) { 233 if(! (oldText instanceof ImmHotText) || ! (replacedText instanceof ImmHotText)) { 234 return null; 235 } 236 ImmHotText oldHotText = (ImmHotText) oldText; 237 ImmHotText replacedHotText = (ImmHotText) replacedText; 238 try { 239 Method m = oldHotText.getClass().getDeclaredMethod("replaceUnits", ImmTextInterval.class, ImmHotText.class); 240 m.setAccessible(true); 241 try { 242 return (ImmText) m.invoke(oldHotText, interval, replacedHotText); 243 } catch (IllegalArgumentException e) { 244 return null; 245 } catch (IllegalAccessException e) { 246 return null; 247 } catch (InvocationTargetException e) { 248 return null; 249 } 250 } catch (SecurityException e) { 251 return null; 252 } catch (NoSuchMethodException e) { 253 return null; 254 } 255 } 216 256 } -
modules/org.sophie2.base.model.text/src/main/java/org/sophie2/base/model/text/style/HotStyleDef.java
4 4 import java.util.Iterator; 5 5 import java.util.Map; 6 6 7 import org.sophie2.base.commons.structures.ImmTreeList; 7 8 import org.sophie2.base.commons.structures.ImmTreeMap; 8 9 import org.sophie2.base.commons.util.Hash; 9 10 import org.sophie2.base.commons.util.Hashable; 10 11 import org.sophie2.base.commons.util.Hasher; 11 import org.sophie2.base.commons.util.AccessCreateCollectionUtil;12 12 import org.sophie2.base.commons.util.ImmList; 13 13 import org.sophie2.base.commons.util.ImmMap; 14 import org.sophie2.base.commons.util.ImmMap.ImmEntry;15 14 import org.sophie2.base.model.text.HotAttr; 16 15 import org.sophie2.base.model.text.elements.CommonAttr; 17 16 import org.sophie2.base.model.text.model.TextUnit; … … 37 36 private static final Map<ImmMap<HotAttr<?>, Object>, HotStyleDef> usedStyles = 38 37 new HashMap<ImmMap<HotAttr<?>,Object>, HotStyleDef>(); 39 38 40 39 private Hash hashCode = null; 40 41 41 private HotStyleDef(ImmMap<HotAttr<?>, Object> values) { 42 42 // Disable direct instantiation. 43 43 ImmMap<HotAttr<?>, Object> allValues = values; … … 80 80 * The style's attributes. 81 81 */ 82 82 public ImmList<HotAttr<?>> getAttributeKeys() { 83 ImmList<HotAttr<?>> keys = AccessCreateCollectionUtil.<HotAttr<?>>createDosImmList(); 84 for (ImmEntry<HotAttr<?>, Object> entry : this.attrValues.asList()) { 85 keys = keys.add(entry.getKey()); 86 } 87 return keys; 83 return ImmTreeList.create(this.attrValues.toMap().keySet()); 88 84 } 89 85 90 86 … … 242 238 } 243 239 244 240 public Hash getHash() { 245 246 Hasher hasher = new Hasher(); 247 // XXX values may not be hash-able.. 248 // think of a smarter realization of HotStyleDef!! --kyli 249 Iterator<ImmMap.ImmEntry<HotAttr<?>, Object>> it = this.attrValues.iterator(); 250 while (it.hasNext()) { 251 ImmMap.ImmEntry<HotAttr<?>, Object> entry = it.next(); 252 hasher.addHashed(entry.getKey().getHash()); 253 hasher.addObject(entry.getValue()); 241 if (this.hashCode == null) { 242 Hasher hasher = new Hasher(); 243 // XXX values may not be hash-able.. 244 // think of a smarter realization of HotStyleDef!! --kyli 245 Iterator<ImmMap.ImmEntry<HotAttr<?>, Object>> it = this.attrValues.iterator(); 246 while (it.hasNext()) { 247 ImmMap.ImmEntry<HotAttr<?>, Object> entry = it.next(); 248 hasher.addHashed(entry.getKey().getHash()); 249 hasher.addObject(entry.getValue()); 250 } 251 this.hashCode = hasher.toHash(); 254 252 } 255 return hasher.toHash();253 return this.hashCode; 256 254 } 257 255 } 256 No newline at end of file