@ -25,12 +25,19 @@ import com.fr.design.layout.FRGUIPaneFactory;
import com.fr.design.mainframe.DesignerContext ;
import com.fr.general.IOUtils ;
import com.fr.js.JavaScriptImpl ;
import com.fr.stable.StringUtils ;
import com.fr.stable.collections.combination.Pair ;
import javax.swing.JLabel ;
import javax.swing.JPanel ;
import javax.swing.KeyStroke ;
import javax.swing.SwingConstants ;
import javax.swing.SwingWorker ;
import javax.swing.border.EmptyBorder ;
import javax.swing.event.DocumentEvent ;
import javax.swing.event.DocumentListener ;
import java.awt.BorderLayout ;
import java.awt.Color ;
import java.awt.Cursor ;
import java.awt.Dimension ;
import java.awt.FontMetrics ;
@ -54,6 +61,13 @@ public class JSContentPane extends BasicPane {
private JSImplPopulateAction jsImplPopulateAction ;
private boolean modal ;
BasicDialog advancedEditorDialog ;
private JLabel warningLabel ;
//用来标记当前显示状态
private boolean showWarning = false ;
private static final Color WARING_LABEL_BACKGROUND = Color . decode ( "#FFFBE6" ) ;
private static final Color WARING_LABEL_FOREGROUND = new Color ( 0 , 0 , 0 , 216 ) ;
private JPanel endBracketsPanel ;
public JSContentPane ( ) { }
public JSContentPane ( String [ ] args ) {
@ -73,14 +87,91 @@ public class JSContentPane extends BasicPane {
UILabel funNameLabel2 = new UILabel ( ) ;
funNameLabel2 . setText ( "}" ) ;
this . add ( funNameLabel2 , BorderLayout . SOUTH ) ;
if ( isNeedContentWarning ( ) ) {
endBracketsPanel = FRGUIPaneFactory . createBorderLayout_S_Pane ( ) ;
endBracketsPanel . add ( funNameLabel2 , BorderLayout . NORTH ) ;
warningLabel = initWarningLabel ( ) ;
this . add ( endBracketsPanel , BorderLayout . SOUTH ) ;
} else {
this . add ( funNameLabel2 , BorderLayout . SOUTH ) ;
}
}
protected JLabel initWarningLabel ( ) {
JLabel warningLabel = new JLabel ( StringUtils . EMPTY ) ;
warningLabel . setOpaque ( true ) ;
warningLabel . setAlignmentX ( LEFT_ALIGNMENT ) ;
// 设置左右 5px 的间距
warningLabel . setBorder ( new EmptyBorder ( 0 , 5 , 0 , 5 ) ) ;
warningLabel . setPreferredSize ( new Dimension ( 200 , 20 ) ) ;
warningLabel . setBackground ( WARING_LABEL_BACKGROUND ) ;
warningLabel . setForeground ( WARING_LABEL_FOREGROUND ) ;
addContentListener4Warning ( ) ;
return warningLabel ;
}
private void addContentListener4Warning ( ) {
contentTextArea . getDocument ( ) . addDocumentListener ( new DocumentListener ( ) {
@Override
public void insertUpdate ( DocumentEvent e ) {
checkContent ( contentTextArea ) ;
}
@Override
public void removeUpdate ( DocumentEvent e ) {
checkContent ( contentTextArea ) ;
}
@Override
public void changedUpdate ( DocumentEvent e ) {
checkContent ( contentTextArea ) ;
}
} ) ;
}
private void checkContent ( RSyntaxTextArea contentTextArea ) {
String content = contentTextArea . getText ( ) . trim ( ) ;
Pair < Boolean , String > pair = checkContent ( content ) ;
boolean matches = Boolean . TRUE . equals ( pair . getFirst ( ) ) ;
String tip = pair . getSecond ( ) ! = null ? pair . getSecond ( ) : StringUtils . EMPTY ;
// 更新提示
updateWarningTip ( tip ) ;
if ( matches = = showWarning ) {
return ;
}
if ( matches ) {
addWarningLabel ( ) ;
showWarning = true ;
} else {
removeWarningLabel ( ) ;
showWarning = false ;
}
}
protected void updateWarningTip ( String tip ) {
if ( ! StringUtils . equals ( warningLabel . getText ( ) , tip ) ) {
warningLabel . setText ( tip ) ;
}
}
protected void removeWarningLabel ( ) {
endBracketsPanel . remove ( warningLabel ) ;
endBracketsPanel . revalidate ( ) ;
}
protected void addWarningLabel ( ) {
endBracketsPanel . add ( warningLabel , BorderLayout . SOUTH ) ;
endBracketsPanel . revalidate ( ) ;
}
public JSContentPane ( String [ ] args , boolean modal ) {
this ( args ) ;
this . modal = modal ;
}
public void setJsImplUpdateAction ( JSImplUpdateAction jsImplUpdateAction ) {
this . jsImplUpdateAction = jsImplUpdateAction ;
}
@ -93,7 +184,6 @@ public class JSContentPane extends BasicPane {
this . javaScript = javaScript ;
}
private void addNewPaneLabel ( ) {
UILabel advancedEditorLabel = new UILabel ( Toolkit . i18nText ( "Fine-Design_Advanced_Editor" ) , IconUtils . readIcon ( "com/fr/design/images/edit/advancedEditor.svg" ) , SwingConstants . LEFT ) ;
advancedEditorLabel . setCursor ( new Cursor ( Cursor . HAND_CURSOR ) ) ;
@ -328,4 +418,20 @@ public class JSContentPane extends BasicPane {
protected boolean needAdvancedEditor ( ) {
return true ;
}
/ * *
* 是否支持内容提示
* @return 是否支持
* /
protected boolean isNeedContentWarning ( ) {
return false ;
}
/ * *
* 自定义匹配js内容 , 并返回对应的提示文本
* @return < 是否匹配 , 提示文本 >
* /
protected Pair < Boolean , String > checkContent ( String content ) {
return new Pair < > ( false , StringUtils . EMPTY ) ;
}
}