@ -1,37 +1,76 @@
package com.fr.design.javascript ;
package com.fr.design.javascript ;
import com.fr.design.constants.KeyWords ;
import com.fr.design.DesignerEnvManager ;
import com.fr.design.DesignerEnvManager ;
import com.fr.design.gui.autocomplete.* ;
import com.fr.design.constants.KeyWords ;
import com.fr.design.dialog.BasicPane ;
import com.fr.design.gui.autocomplete.AutoCompletion ;
import com.fr.design.gui.autocomplete.BasicCompletion ;
import com.fr.design.gui.autocomplete.CompletionProvider ;
import com.fr.design.gui.autocomplete.DefaultCompletionProvider ;
import com.fr.design.gui.autocomplete.ShorthandCompletion ;
import com.fr.design.gui.icontainer.UIScrollPane ;
import com.fr.design.gui.icontainer.UIScrollPane ;
import com.fr.design.gui.ilable.UILabel ;
import com.fr.design.gui.ilable.UILabel ;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.RSyntaxTextArea ;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.RSyntaxTextArea ;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.SyntaxConstants ;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.SyntaxConstants ;
import com.fr.design.javascript.beautify.JavaScriptFormatHelper ;
import com.fr.design.layout.FRGUIPaneFactory ;
import com.fr.design.layout.FRGUIPaneFactory ;
import com.fr.design.dialog.BasicPane ;
import com.fr.design.utils.gui.GUICoreUtils ;
import com.fr.general.IOUtils ;
import com.fr.general.Inter ;
import javax.swing.* ;
import javax.swing.* ;
import java.awt.* ;
import java.awt.* ;
import java.awt.event.MouseAdapter ;
import java.awt.event.MouseEvent ;
import java.util.ArrayList ;
import java.util.ArrayList ;
public class JSContentPane extends BasicPane {
public class JSContentPane extends BasicPane {
private RSyntaxTextArea contentTextArea ;
private RSyntaxTextArea contentTextArea ;
private UILabel funNameLabel ;
private UILabel funNameLabel ;
private int titleWidth = 180 ;
private int titleWidth = 180 ;
public JSContentPane ( String [ ] args ) {
public JSContentPane ( String [ ] args ) {
this . setLayout ( FRGUIPaneFactory . createBorderLayout ( ) ) ;
this . setLayout ( FRGUIPaneFactory . createBorderLayout ( ) ) ;
funNameLabel = new UILabel ( ) ;
funNameLabel = new UILabel ( ) ;
this . setFunctionTitle ( args ) ;
this . setFunctionTitle ( args ) ;
this . add ( funNameLabel , BorderLayout . NORTH ) ;
UILabel label = new UILabel ( Inter . getLocText ( "FR-Designer_Format_JavaScript" ) , IOUtils . readIcon ( "com/fr/design/images/edit/format.png" ) , SwingConstants . LEFT ) ;
contentTextArea = new RSyntaxTextArea ( ) ;
label . setCursor ( new Cursor ( Cursor . HAND_CURSOR ) ) ;
contentTextArea . setCloseCurlyBraces ( true ) ;
label . setToolTipText ( Inter . getLocText ( "FR-Designer_Format_JavaScript" ) ) ;
contentTextArea . setLineWrap ( true ) ;
label . addMouseListener ( new MouseAdapter ( ) {
contentTextArea . setSyntaxEditingStyle ( SyntaxConstants . SYNTAX_STYLE_JAVASCRIPT ) ;
@Override
contentTextArea . setCodeFoldingEnabled ( true ) ;
public void mouseReleased ( MouseEvent e ) {
contentTextArea . setAntiAliasingEnabled ( true ) ;
new SwingWorker < String , Void > ( ) {
@Override
protected String doInBackground ( ) throws Exception {
return JavaScriptFormatHelper . beautify ( contentTextArea . getText ( ) ) ;
}
@Override
protected void done ( ) {
try {
String text = get ( ) ;
contentTextArea . setText ( text ) ;
} catch ( Exception ignore ) {
}
}
} . execute ( ) ;
}
} ) ;
this . add ( GUICoreUtils . createBorderLayoutPane (
funNameLabel , BorderLayout . WEST ,
label , BorderLayout . EAST
) , BorderLayout . NORTH ) ;
contentTextArea = new RSyntaxTextArea ( ) ;
contentTextArea . setCloseCurlyBraces ( true ) ;
contentTextArea . setLineWrap ( true ) ;
contentTextArea . setSyntaxEditingStyle ( SyntaxConstants . SYNTAX_STYLE_JAVASCRIPT ) ;
contentTextArea . setCodeFoldingEnabled ( true ) ;
contentTextArea . setAntiAliasingEnabled ( true ) ;
CompletionProvider provider = createCompletionProvider ( ) ;
CompletionProvider provider = createCompletionProvider ( ) ;
@ -41,93 +80,93 @@ public class JSContentPane extends BasicPane {
ac . setTriggerKey ( convert2KeyStroke ( shortCuts ) ) ;
ac . setTriggerKey ( convert2KeyStroke ( shortCuts ) ) ;
ac . install ( contentTextArea ) ;
ac . install ( contentTextArea ) ;
UIScrollPane sp = new UIScrollPane ( contentTextArea ) ;
UIScrollPane sp = new UIScrollPane ( contentTextArea ) ;
this . add ( sp , BorderLayout . CENTER ) ;
this . add ( sp , BorderLayout . CENTER ) ;
UILabel funNameLabel2 = new UILabel ( ) ;
UILabel funNameLabel2 = new UILabel ( ) ;
funNameLabel2 . setText ( "}" ) ;
funNameLabel2 . setText ( "}" ) ;
this . add ( funNameLabel2 , BorderLayout . SOUTH ) ;
this . add ( funNameLabel2 , BorderLayout . SOUTH ) ;
}
}
private KeyStroke convert2KeyStroke ( String ks ) {
private KeyStroke convert2KeyStroke ( String ks ) {
return KeyStroke . getKeyStroke ( ks . replace ( "+" , "pressed" ) ) ;
return KeyStroke . getKeyStroke ( ks . replace ( "+" , "pressed" ) ) ;
}
}
@Override
@Override
protected String title4PopupWindow ( ) {
protected String title4PopupWindow ( ) {
return "JS" ;
return "JS" ;
}
}
public void populate ( String js ) {
public void populate ( String js ) {
this . contentTextArea . setText ( js ) ;
this . contentTextArea . setText ( js ) ;
}
}
public String update ( ) {
public String update ( ) {
return this . contentTextArea . getText ( ) ;
return this . contentTextArea . getText ( ) ;
}
}
public void setFunctionTitle ( String [ ] args ) {
public void setFunctionTitle ( String [ ] args ) {
funNameLabel . setText ( createFunctionTitle ( args ) ) ;
funNameLabel . setText ( createFunctionTitle ( args ) ) ;
}
}
public void setFunctionTitle ( String [ ] args , String [ ] defaultArgs ) {
public void setFunctionTitle ( String [ ] args , String [ ] defaultArgs ) {
String [ ] titles ;
String [ ] titles ;
if ( defaultArgs = = null ) {
if ( defaultArgs = = null ) {
titles = args ;
titles = args ;
} else if ( args = = null ) {
} else if ( args = = null ) {
titles = defaultArgs ;
titles = defaultArgs ;
} else {
} else {
ArrayList list = new ArrayList ( ) ;
ArrayList list = new ArrayList ( ) ;
for ( String s : defaultArgs ) {
for ( String s : defaultArgs ) {
list . add ( s ) ;
list . add ( s ) ;
}
}
for ( String s : args ) {
for ( String s : args ) {
list . add ( s ) ;
list . add ( s ) ;
}
}
titles = ( String [ ] ) list . toArray ( new String [ list . size ( ) ] ) ;
titles = ( String [ ] ) list . toArray ( new String [ list . size ( ) ] ) ;
}
}
setFunctionTitle ( titles ) ;
setFunctionTitle ( titles ) ;
}
}
/ * *
/ * *
* 用html , 方便换行
* 用html , 方便换行
*
*
* @param args
* @param args
* @return
* @return
* /
* /
private String createFunctionTitle ( String [ ] args ) {
private String createFunctionTitle ( String [ ] args ) {
StringBuffer sb = new StringBuffer ( ) ;
StringBuffer sb = new StringBuffer ( ) ;
sb . append ( "<html> <body> <div style='height:16px'>function(" ) ;
sb . append ( "<html> <body> <div style='height:16px'>function(" ) ;
int width = titleWidth ;
int width = titleWidth ;
FontMetrics cellFM = this . getFontMetrics ( this . getFont ( ) ) ;
FontMetrics cellFM = this . getFontMetrics ( this . getFont ( ) ) ;
int tempwidth = 0 ;
int tempwidth = 0 ;
if ( args ! = null ) {
if ( args ! = null ) {
for ( int i = 0 ; i < args . length ; i + + ) {
for ( int i = 0 ; i < args . length ; i + + ) {
if ( args [ i ] = = null ) {
if ( args [ i ] = = null ) {
continue ;
continue ;
}
}
if ( cellFM . stringWidth ( args [ i ] ) < width ) {
if ( cellFM . stringWidth ( args [ i ] ) < width ) {
tempwidth = tempwidth + cellFM . stringWidth ( args [ i ] ) ;
tempwidth = tempwidth + cellFM . stringWidth ( args [ i ] ) ;
if ( tempwidth < width ) {
if ( tempwidth < width ) {
sb . append ( args [ i ] ) ;
sb . append ( args [ i ] ) ;
if ( i ! = args . length - 1 ) {
if ( i ! = args . length - 1 ) {
sb . append ( "," ) ;
sb . append ( "," ) ;
}
}
} else {
} else {
tempwidth = 0 ;
tempwidth = 0 ;
i = i - 1 ; // 后退一步
i = i - 1 ; // 后退一步
sb . append ( "</p><p>     " ) ;
sb . append ( "</p><p>     " ) ;
}
}
} else {
} else {
sb . append ( "</p><p>     " ) ;
sb . append ( "</p><p>     " ) ;
sb . append ( args [ i ] ) ;
sb . append ( args [ i ] ) ;
sb . append ( "</p>" ) ;
sb . append ( "</p>" ) ;
}
}
}
}
}
}
sb . append ( "){</div><body> </html>" ) ;
sb . append ( "){</div><body> </html>" ) ;
return sb . toString ( ) ;
return sb . toString ( ) ;
}
}
private CompletionProvider createCompletionProvider ( ) {
private CompletionProvider createCompletionProvider ( ) {