/* +8~Tk_B
* Copyright (c) 2006, Cao Kai. All rights reserved. sjIFu.Q'm
* qR+AVt
* This software is distributable for personal studying. See the <UDL+zw
* terms of the license in the documentation provided with this software. *iP;~G{
*/ SDVX
@93X"=&=?
package com.caokai.jscf.swing; .4rH>3OZt
s02+T mD
import java.awt.*; js"1q4_\
import java.awt.event.*; Sz;4VIr~~
import javax.swing.*; ^8Hd 6(
import java.io.*; ~#3:+p
import java.net.*; g7w`'| H
l~t
import com.caokai.jscf.utilities.*; 2g%hPbTRk
import com.caokai.jscf.resources.*; ^HnRo}{.<
/** e VO7@GH.
* JavaFormatterGUI lVMz!_of
* hB.T:Z8
* @author <a href="mailto:gcyx-0222163.com">Rackery</a> y $[:99e
* @version $Revision: 1.6 $ nh[B$"{
*/ >("~rU*
jfx6f42@7
public class JavaFormatterGUI extends JFrame > dE^<1 p
{ !5 +\/@ Y
private Configurations config; Qmzb:<h!
W,JxyN9`
private JMenu fileMenu = new JMenu(), b>y^n
editMenu = new JMenu(), onf*EOC
setMenu = new JMenu(), c ;(Sr4
aboutMenu = new JMenu(), 3@JQn;
language = new JMenu(); PvZ^%~cPc
.-^VS(X
private JMenuItem openItem = new JMenuItem(), })./LXyk8
saveItem = new JMenuItem(), &{g$ M+g
copyItem = new JMenuItem(), -[68oIg$#
cutItem = new JMenuItem(), ,%& ndT3ZY
pasteItem = new JMenuItem(), {^`iT{ Q0
chinese = new JMenuItem(), 3fp4@
japanese = new JMenuItem(), [VL/Y3J:
english = new JMenuItem(), q [Az/rU
aboutItem = new JMenuItem(); *"%m){
gZIAJ 9t.
private JMenuBar bar; %sa ZHVM&
6qkHen
private JSCFArea target; c?Q qHf.
private ControlPanel control; %FFvyB$C
{PoP@/>G
private File currentFile = null; YN-%Wz?P
P>TcV8'2
private JFileChooser chooser = new JFileChooser(); gcb%"Z `
JPLe2LI^'
private final int PRDEFINED_CHINESE = 0; v}!r,k%8%
private final int PRDEFINED_JAPANESE = 2; 4T*FZKsEN
private final int PRDEFINED_ENGLISH = 1; e|YAK
>H2M#`w[
public JavaFormatterGUI() h)&zy)`V.
{ @-YM,
super("JavaFormatter"); ';0|8 i 1
initializeConfig("resources/build/jscf_cn.properties"); .|{gQ7_K j
setMenuItems(); zuI^Y:zk
registerActions(); 6z8|^*)
installMenus(); i @. Q+-
setJMenuBar(bar); $`?9 dF
f^X@"(`
target = JSCFArea.getSingleInstance(); [n31k&81}
target.setup(); -l)0IB.z
control = new ControlPanel(target); {wwk{nk+db
getContentPane().add(control,"South"); TFQsA Y
getContentPane().add(new JScrollPane(target),"Center"); Eq~w-6
setSize(600,680); XWFT_
centerFrame(); MOEHWDW=x
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); )V C[ 2xA
} uHE7)s_!
4}o?=<v/
public void initializeConfig(String file) ye ]S7 [
{ 'uZQrq*NF
try _f=y-e2GY
{ JYN\7Di+72
config = new Configurations(file); i<%O9
} :\qcP d#
catch(ConfigurationsException ce) Gx3Ec @
{ #Uw^P
ce.printStackTrace(); <br KT dY!
} L0<6Kn6
} Ck qj{4
?k:O g
public void installMenus() 0c]`d.r
{ OOwAS~
fileMenu.add(openItem); VHVA1NU}
fileMenu.add(saveItem); ~0"DJlyov)
+k[Oi ^JW
editMenu.add(copyItem); :*J.*0$
editMenu.add(cutItem); M Q@#
editMenu.add(pasteItem); &xgt=M >
Ui'G+ 7
language.add(chinese); ;fHDBLCG<
language.add(english); IoY;
language.add(japanese); zp1:9Xs
:kUVm'!o
aboutMenu.add(aboutItem); Oo/%4D4
setMenu.add(language); F.cQ!)wyZ
{0{
bar = new JMenuBar(); -r==d)F6
bar.add(fileMenu); qf=_klu
bar.add(editMenu); /y-ic&y
bar.add(setMenu); ]SXv2
bar.add(aboutMenu); |Dq,K/
} 9,9cm^0g
<-c|Fj2
public void setMenuItems() W+2m\a3
{ z+3]wSV
fileMenu.setText(config.getValue("file.label")); P9si>E3Zt
editMenu.setText(config.getValue("edit.label")); )LXf]sw_
setMenu.setText(config.getValue("set.label")); JFT(TI!Z2
aboutMenu.setText(config.getValue("about.label")); Oj _P!,
language.setText(config.getValue("language.label")); 4};OPB6cJ
%ks#fvn?
openItem.setText(config.getValue("open.label")); `1L-0eX{{
saveItem.setText(config.getValue("save.label")); !sG{o5l
copyItem.setText(config.getValue("copy.label")); ?f#Z"]5m
cutItem.setText(config.getValue("cut.label")); 9:"2:R C
pasteItem.setText(config.getValue("paste.label")); $+E+Hoe
4;EGU"l
aboutItem.setText(config.getValue("about.label")); 4"ns<I"
!T9p c
chinese.setText(config.getValue("chinese.label")); (+m:\?AgX
japanese.setText(config.getValue("japanese.label")); .ike})@i
english.setText(config.getValue("english.label")); v.j:]lr/
} R| ulV/FZ
,&83HQ
public void registerActions() vQk pN|<V
{ - p>HKP3
LanguageListener listener = new LanguageListener(); hQLXYD/F
chinese.addActionListener(listener); 'So-S=A
japanese.addActionListener(listener); 6z|ruOL
english.addActionListener(listener); QE4wjF$
s8)& ~
OpenFileListener openListener = new OpenFileListener(); >NC&oz'u
openItem.addActionListener(openListener); } usr?%
u'l#tW2o
SaveFileListener saveListener = new SaveFileListener(); O Y5i Tz
saveItem.addActionListener(saveListener); del`C|0S
Yv2hzzn
aboutItem.addActionListener(new ActionListener() fX H G
{ .S*|G$Ir
public void actionPerformed(ActionEvent ae) %,q%1H
{ z_@ex
AboutDialog ad = new AboutDialog(JavaFormatterGUI.this,config.getValue("register.name")); @5X_<Hi
ad.setVisible(true); uf #Cf
} %-!!m D?
}); :di;qw~$
b4~ D5y
copyItem.addActionListener(new ActionListener() Gii X*
{ &Zh|c&?
public void actionPerformed(ActionEvent ae) }mMi: d
{ 'lT{ :SU3
target.copy(); 1 baz1#sS(
} ld fCx?
}); 05GwQ|'K !
jP@= e%}t5
cutItem.addActionListener(new ActionListener() [%tazTWl~
{ <uiA5 9so
public void actionPerformed(ActionEvent ae) z }7wI .
{ ,<6 C>{$
target.cut(); )b &Gk7QJ+
} O>HfC0t
}); Odd9L6EN
CxwBs IQ
pasteItem.addActionListener(new ActionListener() )]'6]F>T
{ a!9}!
public void actionPerformed(ActionEvent ae) DR<FGk-GB
{ 2Zix@xGF$
target.paste(); @Q54%
} f%-6IrC
}); ?:C$g]'
} }jDX op(
(SD!TK
class LanguageListener implements ActionListener mbT;vG
{ {ae@)g
public void actionPerformed(ActionEvent ae) <!HnsF*lQ
{ s=OQV;8g~
Object obj = ae.getSource(); @;IDG a
JMenuItem item = null; C,fS"(c*}
`/=+'<}
if(obj instanceof JMenuItem) cZ,;^
{ (yCS:+;*=
item = (JMenuItem)obj; xc, hJ@
} '5/M]YV2
if(item == chinese) IV$;F^2g'
{ [H&x&qkq
changeLanguage(PRDEFINED_CHINESE); f7Y*Mp2R
} 7evYPXv3]
else if(item == english) 6v1{ 5k
{ ] gk-P
changeLanguage(PRDEFINED_ENGLISH); xDko'5D:
} YwW6/C!.S
else if(item == japanese) 24W3~+Fc
{ c4Gub.:w!
changeLanguage(PRDEFINED_JAPANESE); x)5oK>~
} 89Ct5u^|
} dTX.G
} 9^}ut5o#L
I;26,'
class OpenFileListener implements ActionListener #%[C
{ " W93RW}q_
public void actionPerformed(ActionEvent ae) 7VXrU1T,
{ Q -bq{k
File file = null; gHBq8>bFSp
FileReader fr = null; 9n1s?\
String content = ""; k#Rj" bb
int value = chooser.showOpenDialog(JavaFormatterGUI.this); 8esSKR-
if(value == JFileChooser.APPROVE_OPTION && u GJlm1I
chooser.getSelectedFile() != null) iob2BNY-
{ E8`3Zuo
file = chooser.getSelectedFile(); VZHelQ(
try wdbJgz1ubL
{ )(P j V
fr = new FileReader(file); a 8t[ :F
} WS410E
catch(FileNotFoundException fnfe) th-W|{)@0
{ PUjy1=9
JOptionPane.showMessageDialog(null,"没有找到文件!可能已经被删除!"); (SWA,
} \!|hEf<$
int readbyte; XJ )-)Y?7
try _2^4o u
{ &B jg(vPz
while( (readbyte = fr.read()) != -1) 'VNSv L:f
{ XghQP%zl
target.append(String.valueOf((char)readbyte)); DJ6K(Mg%I{
chooser.setCurrentDirectory(file.getParentFile()); /<sw4+
currentFile = file; 2n!v=1~S
} @p$ [It
fr.close(); J|=.zLw
} Qn 6@'
catch(IOException ioe){ } u'O%pmmp
} 04o H!.
} E1`PX8r
} |>: 0c-
]p"?B|T
class SaveFileListener implements ActionListener z+ dr&]
{ FTC?^[=tp
public void actionPerformed(ActionEvent ae) oYGPz7$@
{ kl~[Hnt
File file = null; Cp7G ?~n
PrintWriter fileOut; Iuyxl,'BH
int value = chooser.showSaveDialog(JavaFormatterGUI.this); 8 |@:ep!{
if(value == JFileChooser.APPROVE_OPTION && "0 >r=\G7
chooser.getSelectedFile() != null) 's]?]\N
{ M&CQ%%e
file = chooser.getSelectedFile(); f^<uD.F-E#
} k+d*qJm |
M#)q7 B
if(file.exists()){ E;Nd0R3
int response = JOptionPane.showConfirmDialog(null,"该文件已经存在! 是否替换当前文件 !","确认选择",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE); 5+@OGsK[&
if(response==JOptionPane.YES_OPTION){ `+<P XL2oP
file.delete(); 8KY+SsZRw
try{ 9*a0'3
fileOut = new PrintWriter(new FileWriter(file)); C__Ha^p;YO
fileOut.println(target.getText()); \^?~C52v3
fileOut.close(); o5>Mcqe!
} VwzGhw6
catch(IOException e2){ } x= Se?LYO
} Of#&fl1^
else "h:3$?{
{ ZI;Y2EEI#n
return; m.DeW&
} b*t).8
} i4I W+41W
bou\|db ]
else cshi!0
{ VXtH'k
try ?Qx M?~\ @
{ nPtU9/*V#
fileOut = new PrintWriter( new FileWriter(file)); ,d"X>F,
fileOut.println(target.getText()); u #GZ[/
fileOut.close(); 4Ej#9~<n
} +fdDr;W
catch(IOException e2){ } KDi("Xj'o
} 02 )-Bz:a
} ,|t t]
} A<a|0DZZ
,_?ggHVF3
class ControlPanel extends JPanel xVFl\
{ 1e3poV
JSCFArea target; 1D-CV o
JButton formatButton; 018},T3Q
JavaCodeFormatter formatter; LnMwxP`<
bdUKd8S
public ControlPanel(JSCFArea target) R+;umaDa-T
{ p"F^b!
this.target = target; opz6 -=
formatter = JavaCodeFormatter.getSingletonInstance(); o?wo'2XY/
formatButton = new JButton(); -} ,Kv]X
decorateButton(); 1=$|KrN
add(formatButton); .#/HRoc
} tW [ h%<
G5hCrv}V,
public void decorateButton() N=A,[Spt
{ :jQ`wICNS
formatButton.setIcon(new ImageIcon("resources/icons/encode.gif")); :_WsbmF_
formatButton.setRolloverIcon(new ImageIcon("resources/icons/rollover.gif")); ZB]"z%Tr
formatButton.setText(config.getValue("formatButton.label")); I5f]*G*5s
formatButton.setBorder(BorderFactory.createEmptyBorder()); wls)gse9Vg
formatButton.setHorizontalTextPosition(JButton.CENTER); 7zq{j)
formatButton.addActionListener(new ActionListener(){ .G?Px`d{
public void actionPerformed(ActionEvent ae){ ~rF|MV` :
try gCb3(+=c
{ \E{[r:X
formatter.format(target.getText()); nrKjEt>
} -:)+]|S
catch(FormatException fe) ]> v3Rb%
{ Z}A0%-$:K
fe.printStackTrace(); uhJ=bufSs>
} UBCAx:h
target.setText(formatter.getFormattedCode()); ^&zq B \
} pl' NYw
}); 'ggN2EZ^
vOB%!
PzU_Z~I
formatButton.addMouseListener(new MouseAdapter(){ m. _+
public void mousePressed(MouseEvent me){ N_z^"Q1q?8
formatButton.setIcon(new ImageIcon("resources/icons/encodes.gif")); &PUXY!u
} '{eNP{Lm
public void mouseReleased(MouseEvent me1){ igKkIGT
formatButton.setIcon(new ImageIcon("resources/icons/encode.gif")); b6JQb! jm
} [EXYbCH.:p
}); sm$>Fo
} )p>Y!1ejv
} TKqa\q`;`-
>!HG v gp
public void changeLanguage(int type) !f_posqJ~z
{ u7~| GF;
switch(type) ,B~D'E:r
{ Yj65#MT
case 0: initializeConfig("resources/build/jscf_cn.properties"); 6jH:mMJg
setMenuItems(); n#q`pgd<
control.formatButton.setText(config.getValue("formatButton.label")); 8vO$UY-
break; l iO1^U 9
case 1: initializeConfig("resources/build/jscf_en.properties"); *UW &B
setMenuItems(); [zJZSA:
control.formatButton.setText(config.getValue("formatButton.label")); Cf5VNFbb
break; B;uGt@ph3
case 2: initializeConfig("resources/build/jscf_jp.properties"); Ub.)-0[x
setMenuItems(); (AuB;YEmN
control.formatButton.setText(config.getValue("formatButton.label")); O?=jgp:b
break; SP9xIo
default:break; JwV O2B
} IE@b nU^
} ySb@:0!
ej+FjA%AX
public void centerFrame() `A|KLOD
{ n<M]su ND<
Toolkit toolkit = Toolkit.getDefaultToolkit(); $0 jDx
Dimension dim = toolkit.getScreenSize(); 6'M{+N
int screenWidth = dim.width, LK1>::cx
screenHeight = dim.height, 17&r)iw
width = getWidth(), K>R2D(d;
height = getHeight(); :nNy%GtQLQ
setLocation((screenWidth - width)/2,(screenHeight - height)/2); Qb(B(`
} w^axv U
}
/* +GIz 8Q4
* Copyright (c) 2006, Cao Kai. All rights reserved. Qiv^Lx|f
* M!KiIq
* This software is distributable for personal studying. See the u+>:[e9h
* terms of the license in the documentation provided with this software. Nj~1]`'y7.
*/ oS !P
E.3ggs
package com.caokai.jscf.utilities; Z4tsMp)8wh
fs"yy NZ
import java.io.*; ,6CXh
import java.util.*; M!0{ Jfgt!
import java.net.*; jlw$z.SB;
|A` bR C
/** @#)Z6ZsU
* Configuration (ZTy,?Q_
* dMeeJEPWE
* @author <a href="mailto:gcyx-0222163.com">Rackery</a> t2.>E,BI
* @version $Revision: 1.6 $ gBW
*/ D6Qk7
y^4]V-I=u
public class Configurations [i}<~d>
{ Cn nfQ
private Properties config = new Properties(); MSLjsU
private String fileName = ""; |*u]v \ a
y(~Xf<0"}b
public Configurations(){} rCz-Fxl
public Configurations(String fileName) throws ConfigurationsException j'GV?XUn?{
{ u&C}SDO
try 7 DoKP|
{ WJo i?5
FileInputStream fin = new FileInputStream(fileName); oq i>7m9E
config.load(fin); :S !OQ]l
fin.close(); +j\RFKQ|
} j{?'q X_
catch(IOException ioe) =k7&. =Qk0
{ qOv /#i_C
throw new ConfigurationsException("Can not find the file!"); #x+L1x=
} gc>s~;2[wr
this.fileName = fileName; ?8y3i
} m&r4HS a
?\Gvg
zZt^a6
public String getValue(String item) 164`eP>
{ `>#*>M=fS
return config.getProperty(item); -9igC*
} Z}XzuB
o}7l5aZZ
public String getValue(String item,String defaultValue) ZHg#j3<i
{ L~D?]kP`?
return config.getProperty(item,defaultValue); 0wTnv
} _lg;/&A4E;
;v$}|Us}\9
public void setValue(String item,String value) rLQ+})
{ o"c{&s.`
config.setProperty(item,value); 8n&J
} |sp2Kw%
<*nLh`
public void saveFile(String fileName,String description) throws ConfigurationsException Y_}*k?6[h
{ Env Z{&
try pBntAri
{ 5osP.IyV
FileOutputStream fout = new FileOutputStream(fileName); $UsqZ
config.store(fout,description); 3t [ t]5
fout.close(); w2,d+{
} z`{>9q
catch(IOException ioe) `H#+[6
{ ?eH4gysF
throw new ConfigurationsException("Can not save the file!"); fd2,8&/_5
} nEda~27
} `A~2~|$ 0
VIs):o;i*
public void saveFile(String fileName) throws ConfigurationsException $e2\t
{ -g/~]M'j
saveFile(fileName,""); & &n ^<4
} zaTK]
E*Ix*3 J
public void saveFile() throws ConfigurationsException gB*@A% l'
{ H#.`sE7Ul
if(fileName.length() == 0) l!1653m
{ A@6LY PUr
throw new ConfigurationsException("You need to give the file name!"); x`.d**qh
} JL-o,G 3k^
saveFile(fileName); x)@D6<B
} r`Cw$c_O
}