@ -1,7 +1,9 @@
package com.fr.design.actions.replace.utils ;
package com.fr.design.actions.replace.utils ;
import com.fr.design.actions.replace.info.Info ;
import com.fr.design.actions.replace.ui.ITReplaceMainDialog ;
import com.fr.design.actions.replace.ui.ITReplaceMainDialog ;
import com.fr.design.i18n.Toolkit ;
import com.fr.stable.StableUtils ;
import com.fr.stable.StableUtils ;
import com.fr.stable.StringUtils ;
import com.fr.stable.StringUtils ;
import com.fr.stable.collections.combination.Pair ;
import com.fr.stable.collections.combination.Pair ;
@ -97,6 +99,22 @@ public class ShowValueUtils {
}
}
}
}
/ * *
* 获取高亮后用于展示的搜索后内容
*
* @param str 原内容
* @param searchStr 搜索内容
* @param replaceStr 替换内容
* @return 处理完的文本
* /
public static String getReplaceString ( String str , String searchStr , String replaceStr ) {
if ( StringUtils . isEmpty ( searchStr ) ) {
return str ;
} else {
return updateReplaceHighlight ( str , searchStr , replaceStr ) ;
}
}
/ * *
/ * *
* 替换策略
* 替换策略
*
*
@ -187,6 +205,40 @@ public class ShowValueUtils {
}
}
}
}
/ * *
* 更新替换后的高亮
*
* @param str
* @param searchStr
* @param replaceStr
* @return
* /
public static String updateReplaceHighlight ( String str , String searchStr , String replaceStr ) {
if ( ITReplaceMainDialog . isMatched ( ) ) {
String result = str ;
Pattern pattern = containPattern ;
Matcher matcher = pattern . matcher ( str ) ;
int size = FONT_HEAD . length ( ) + FONT_TAIL . length ( ) ;
int different = getDifferent ( searchStr . length ( ) , replaceStr . length ( ) ) ;
int index = 0 ;
StringBuilder builder = new StringBuilder ( str ) ;
while ( matcher . find ( ) ) {
builder . replace ( matcher . start ( ) + index , matcher . end ( ) + index , FONT_HEAD + changeHtmlStr ( replaceStr ) + FONT_TAIL ) ;
index + = getDifferent ( size , different ) ;
index + = getIncreaseCount ( replaceStr ) ;
}
return changeOriginHtmlStr ( HEAD + builder . toString ( ) + TAIL ) ;
} else {
String ans = HEAD + replaceAll ( str , searchStr , FONT_HEAD + changeHtmlStr ( replaceStr ) + FONT_TAIL ) + TAIL ;
return changeOriginHtmlStr ( ans ) ;
}
}
private static int getDifferent ( int searchLength , int replaceLength ) {
return searchLength - replaceLength ;
}
/ * *
/ * *
* 用于处理原字符串中的尖括号
* 用于处理原字符串中的尖括号
* /
* /
@ -315,4 +367,38 @@ public class ShowValueUtils {
public static String joinStr4Position ( String . . . strings ) {
public static String joinStr4Position ( String . . . strings ) {
return StableUtils . join ( strings , JOIN_GAP_STRING ) ;
return StableUtils . join ( strings , JOIN_GAP_STRING ) ;
}
}
/ * *
* 更新替换后内容的高亮
*
* @param info 存储信息的数据结构
* @param str 原内容
* @param searchStr 搜索内容
* @param replacedStr 替换内容
* /
public static void updateAfterReplaceStr ( Info info , String str , String searchStr , String replacedStr ) {
info . getContent ( ) . setAfterReplaceStr ( ShowValueUtils . getReplaceString ( str , searchStr , replacedStr ) ) ;
}
/ * *
* 获取搜索或替换后的提示
*
* @param findCount
* @param replaceCount
* @param failedCount
* @return
* /
public static String getResultTip ( int findCount , int replaceCount , int failedCount ) {
StringBuilder str = new StringBuilder ( ) ;
str . append ( "<html>" ) . append ( Toolkit . i18nText ( "Fine-Design_Replace_Search_Finish" ) ) . append ( "<font color = 'rgb(61,153,249)'>" ) . append ( findCount ) . append ( "</font>" ) . append ( Toolkit . i18nText ( "Fine-Design_Replace_Result" ) ) ;
if ( replaceCount ! = 0 ) {
str . append ( Toolkit . i18nText ( "Fine-Design_Replace_Also_Finish" ) ) . append ( "<font color = 'rgb(61,153,249)'>" ) . append ( replaceCount ) . append ( "</font>" ) . append ( Toolkit . i18nText ( "Fine-Design_Replace_Result_Count" ) ) ;
if ( failedCount ! = 0 ) {
str . append ( Toolkit . i18nText ( "Fine-Design_Replace_Have" ) ) . append ( "<font color = 'rgb(236,124,125)'>" ) . append ( replaceCount ) . append ( "</font>" ) . append ( Toolkit . i18nText ( "Fine-Design_Replace_Can_Not_Replace" ) ) ;
}
}
return str . toString ( ) ;
}
}
}