11package net .sf .j2s .ui .launching ;
22
3+ import java .io .File ;
4+ import java .io .FileInputStream ;
5+ import java .io .FileNotFoundException ;
6+ import java .io .IOException ;
7+ import java .util .Properties ;
38import net .sf .j2s .ui .Java2ScriptUIPlugin ;
49import net .sf .j2s .ui .preferences .PreferenceConstants ;
10+ import net .sf .j2s .ui .property .FileUtil ;
11+ import net .sf .j2s .ui .resources .ExternalResources ;
12+ import org .eclipse .core .resources .ResourcesPlugin ;
513import org .eclipse .core .runtime .CoreException ;
614import org .eclipse .debug .core .ILaunchConfiguration ;
715import org .eclipse .debug .core .ILaunchConfigurationWorkingCopy ;
816import org .eclipse .debug .ui .AbstractLaunchConfigurationTab ;
17+ import org .eclipse .jdt .core .IJavaModel ;
18+ import org .eclipse .jdt .core .IJavaProject ;
19+ import org .eclipse .jdt .core .JavaCore ;
20+ import org .eclipse .jdt .core .JavaModelException ;
921import org .eclipse .jdt .internal .debug .ui .JavaDebugImages ;
22+ import org .eclipse .jdt .launching .IJavaLaunchConfigurationConstants ;
1023import org .eclipse .jface .preference .IPreferenceStore ;
1124import org .eclipse .swt .SWT ;
25+ import org .eclipse .swt .events .FocusAdapter ;
26+ import org .eclipse .swt .events .FocusEvent ;
27+ import org .eclipse .swt .events .FocusListener ;
28+ import org .eclipse .swt .events .ModifyEvent ;
29+ import org .eclipse .swt .events .ModifyListener ;
1230import org .eclipse .swt .events .SelectionAdapter ;
1331import org .eclipse .swt .events .SelectionEvent ;
1432import org .eclipse .swt .graphics .Font ;
1836import org .eclipse .swt .widgets .Button ;
1937import org .eclipse .swt .widgets .Composite ;
2038import org .eclipse .swt .widgets .Group ;
39+ import org .eclipse .swt .widgets .Label ;
40+ import org .eclipse .swt .widgets .Text ;
2141
2242public class J2SConsoleOptionsTab extends AbstractLaunchConfigurationTab {
2343
24- private Button btnInner ;
44+ Button btnInner ;
2545
26- private Button btnCompatiable ;
46+ Button btnCompatiable ;
2747
28- private Button btnCompatiableRawJS ;// whether import or include mozilla.addon.js
48+ Button btnCompatiableRawJS ;// whether import or include mozilla.addon.js
2949
30- private Button btnFastView ;
50+ Button btnFastView ;
3151
32- private Button btnMaximize ;
52+ Button btnMaximize ;
53+
54+ Button btnUseGlobalURL ;
55+
56+ Button btnExternal ;
57+
58+ Label lblJ2SLib ;
59+
60+ Text txtJ2SLib ;
61+
62+ Label lblBin ;
63+
64+ Text txtBin ;
3365
3466public J2SConsoleOptionsTab () {
3567super ();
@@ -50,13 +82,13 @@ public void createControl(Composite parent) {
5082group .setFont (font );
5183layout =new GridLayout ();
5284group .setLayout (layout );
53- group .setLayoutData (new GridData (GridData .FILL_BOTH ));
85+ group .setLayoutData (new GridData (GridData .FILL_HORIZONTAL ));
5486
55- String controlName ="Console Misc Options" ;
87+ String controlName ="Java2Script Console Misc Options" ;
5688group .setText (controlName );
5789
58- btnInner =new Button (group ,SWT .CHECK );
59- btnInner .setText ("View Java2Script application in J2Sconsole " );
90+ btnInner =new Button (group ,SWT .RADIO );
91+ btnInner .setText ("Inner J2SConsole " );
6092btnInner .addSelectionListener (new SelectionAdapter () {
6193public void widgetSelected (SelectionEvent e ) {
6294updateLaunchConfigurationDialog ();
@@ -87,16 +119,26 @@ public void widgetSelected(SelectionEvent e) {
87119}
88120});
89121
90- btnCompatiable =new Button (group ,SWT .CHECK );
91- btnCompatiable .setText ("Generate codes with Mozilla Addon compatiabilities" );
122+ btnExternal =new Button (group ,SWT .RADIO );
123+ btnExternal .setText ("Registered external browsers, like Firefox, IE" );
124+ btnExternal .addSelectionListener (new SelectionAdapter () {
125+ public void widgetSelected (SelectionEvent e ) {
126+ updateLaunchConfigurationDialog ();
127+ btnFastView .setEnabled (btnInner .getSelection ());
128+ btnMaximize .setEnabled (btnInner .getSelection ());
129+ }
130+ });
131+
132+ btnCompatiable =new Button (comp ,SWT .CHECK );
133+ btnCompatiable .setText ("Generate codes with Mozilla Add-on supports" );
92134btnCompatiable .addSelectionListener (new SelectionAdapter () {
93135public void widgetSelected (SelectionEvent e ) {
94136updateLaunchConfigurationDialog ();
95137btnCompatiableRawJS .setEnabled (btnCompatiable .getSelection ());
96138}
97139});
98140
99- btnCompatiableRawJS =new Button (group ,SWT .CHECK );
141+ btnCompatiableRawJS =new Button (comp ,SWT .CHECK );
100142GridData gdjs =new GridData ();
101143gdjs .horizontalIndent =32 ;
102144btnCompatiableRawJS .setLayoutData (gdjs );
@@ -106,6 +148,56 @@ public void widgetSelected(SelectionEvent e) {
106148updateLaunchConfigurationDialog ();
107149}
108150});
151+
152+ btnUseGlobalURL =new Button (comp ,SWT .CHECK );
153+ btnUseGlobalURL .setText ("Use global *.js URL" );
154+ btnUseGlobalURL .addSelectionListener (new SelectionAdapter () {
155+ public void widgetSelected (SelectionEvent e ) {
156+ updateLaunchConfigurationDialog ();
157+ boolean selection =btnUseGlobalURL .getSelection ();
158+ lblJ2SLib .setEnabled (selection );
159+ lblBin .setEnabled (selection );
160+ txtJ2SLib .setEnabled (selection );
161+ txtBin .setEnabled (selection );
162+ }
163+ });
164+
165+ Composite txtComp =new Composite (comp ,SWT .NONE );
166+ GridData gdtxt =new GridData ();
167+ gdtxt .horizontalIndent =32 ;
168+ txtComp .setLayoutData (gdtxt );
169+ txtComp .setLayout (new GridLayout (2 ,false ));
170+
171+ lblJ2SLib =new Label (txtComp ,SWT .NONE );
172+ lblJ2SLib .setText ("J2SLib base URL:" );
173+
174+ txtJ2SLib =new Text (txtComp ,SWT .BORDER );
175+ GridData gdtxt1 =new GridData ();
176+ gdtxt1 .widthHint =240 ;
177+ txtJ2SLib .setLayoutData (gdtxt1 );
178+ FocusListener focusListener =new FocusAdapter () {
179+ public void focusGained (FocusEvent e ) {
180+ // select txt
181+ ((Text )e .widget ).selectAll ();
182+ }
183+ };
184+ ModifyListener modifyListener =new ModifyListener () {
185+ public void modifyText (ModifyEvent e ) {
186+ updateLaunchConfigurationDialog ();
187+ }
188+ };
189+ txtJ2SLib .addFocusListener (focusListener );
190+ txtJ2SLib .addModifyListener (modifyListener );
191+
192+ lblBin =new Label (txtComp ,SWT .NONE );
193+ lblBin .setText ("Binary folder URL:" );
194+
195+ txtBin =new Text (txtComp ,SWT .BORDER );
196+ GridData gdtxt2 =new GridData ();
197+ gdtxt2 .widthHint =240 ;
198+ txtBin .setLayoutData (gdtxt2 );
199+ txtBin .addFocusListener (focusListener );
200+ txtBin .addModifyListener (modifyListener );
109201}
110202
111203public void setDefaults (ILaunchConfigurationWorkingCopy configuration ) {
@@ -129,6 +221,16 @@ public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
129221
130222configuration .setAttribute (
131223IJ2SLauchingConfiguration .J2S_MOZILLA_ADDON_COMPATIABLE_RAW_JS ,true );
224+
225+ configuration .setAttribute (
226+ IJ2SLauchingConfiguration .USE_GLOBAL_ALAA_URL ,false );
227+
228+ configuration .setAttribute (
229+ IJ2SLauchingConfiguration .GLOBAL_J2SLIB_URL ,
230+ "http://archive.java2script.org/" +getCurrentReleaseAlias (configuration ) +"/" );
231+
232+ configuration .setAttribute (
233+ IJ2SLauchingConfiguration .GLOBAL_BINARY_URL ,getCurrentBinPath (configuration ));
132234}
133235
134236public void initializeFrom (ILaunchConfiguration configuration ) {
@@ -148,6 +250,7 @@ public void initializeFrom(ILaunchConfiguration configuration) {
148250boolean external =configuration .getAttribute (
149251IJ2SLauchingConfiguration .VIEW_IN_INNER_J2S_CONSOLE ,preferred );
150252btnInner .setSelection (external );
253+ btnExternal .setSelection (!external );
151254btnFastView .setEnabled (external );
152255btnMaximize .setEnabled (external );
153256
@@ -158,11 +261,95 @@ public void initializeFrom(ILaunchConfiguration configuration) {
158261btnCompatiable .setSelection (compatiable );
159262btnCompatiableRawJS .setEnabled (compatiable );
160263
264+ boolean useGlobal =configuration .getAttribute (
265+ IJ2SLauchingConfiguration .USE_GLOBAL_ALAA_URL ,false );
266+ btnUseGlobalURL .setSelection (useGlobal );
267+ lblJ2SLib .setEnabled (useGlobal );
268+ lblBin .setEnabled (useGlobal );
269+ txtJ2SLib .setEnabled (useGlobal );
270+ txtBin .setEnabled (useGlobal );
271+
272+ txtJ2SLib .setText (configuration .getAttribute (
273+ IJ2SLauchingConfiguration .GLOBAL_J2SLIB_URL ,
274+ "http://archive.java2script.org/" +getCurrentReleaseAlias (configuration ) +"/" ));
275+
276+ txtBin .setText (configuration .getAttribute (
277+ IJ2SLauchingConfiguration .GLOBAL_BINARY_URL ,getCurrentBinPath (configuration )));
278+
161279}catch (CoreException e ) {
162280e .printStackTrace ();
163281}
164282}
165283
284+ private String getCurrentBinPath (ILaunchConfiguration configuration ) {
285+ IJavaModel javaModel =JavaCore .create (ResourcesPlugin .getWorkspace ().getRoot ());
286+ try {
287+ String projectName =configuration .getAttribute (IJavaLaunchConfigurationConstants .ATTR_PROJECT_NAME , (String )null );
288+ if ((projectName ==null ) || (projectName .trim ().length () <1 )) {
289+ return null ;
290+ }
291+ IJavaProject javaProject =javaModel .getJavaProject (projectName );
292+ if ((javaProject ==null ) || !javaProject .exists ()) {
293+ return null ;
294+ }
295+ String path =javaProject .getOutputLocation ().toString ();
296+ int idx =path .indexOf ('/' ,2 );
297+ String relativePath ="" ;
298+ if (idx != -1 ) {
299+ relativePath =path .substring (idx +1 );
300+ }
301+ return relativePath ;
302+ }catch (JavaModelException e ) {
303+ e .printStackTrace ();
304+ }catch (CoreException e ) {
305+ e .printStackTrace ();
306+ }
307+ return "bin/" ;
308+ }
309+ private String getCurrentJ2SLibPath (File workingDir ) {
310+ String [][]allResources =ExternalResources .getAllResources ();
311+ String j2sLibPath =null ;
312+ if (allResources !=null &&allResources .length !=0 &&allResources [0 ].length !=0 ) {
313+ if ((allResources [0 ][0 ]).startsWith ("|" )) {
314+ allResources [0 ][0 ] =FileUtil .toRelativePath (allResources [0 ][0 ].substring (1 ),
315+ workingDir .getAbsolutePath ());;
316+ }
317+ j2sLibPath =allResources [0 ][0 ].substring (0 ,allResources [0 ][0 ].lastIndexOf ("/" ) +1 );
318+ }else {
319+ j2sLibPath ="../net.sf.j2s.lib/j2slib/" ;
320+ }
321+ return j2sLibPath ;
322+ }
323+ private String getCurrentReleaseAlias (ILaunchConfiguration configuration ) {
324+ File workingDir =null ;
325+ try {
326+ workingDir =J2SLaunchingUtil .getWorkingDirectory (configuration );
327+ String j2sLibPath =getCurrentJ2SLibPath (workingDir );
328+
329+ File j2slibFolder =new File (workingDir .getAbsolutePath (),j2sLibPath );
330+ File j2sRelease =new File (j2slibFolder ,".release" );
331+ Properties release =new Properties ();
332+ String alias ="1.0.0" ;
333+ String version ="20070304" ;
334+ release .put ("alias" ,alias );
335+ release .put ("version" ,version );
336+ if (j2sRelease .exists ()) {
337+ try {
338+ release .load (new FileInputStream (j2sRelease ));
339+ }catch (FileNotFoundException e ) {
340+ e .printStackTrace ();
341+ }catch (IOException e ) {
342+ e .printStackTrace ();
343+ }
344+ alias =release .getProperty ("alias" );
345+ version =release .getProperty ("version" );
346+ }
347+ return alias ;
348+ }catch (CoreException e ) {
349+ e .printStackTrace ();
350+ return "1.0.0" ;
351+ }
352+ }
166353public void performApply (ILaunchConfigurationWorkingCopy configuration ) {
167354configuration .setAttribute (IJ2SLauchingConfiguration .FAST_VIEW_J2S_CONSOLE ,
168355btnFastView .getSelection ());
@@ -174,6 +361,12 @@ public void performApply(ILaunchConfigurationWorkingCopy configuration) {
174361IJ2SLauchingConfiguration .J2S_MOZILLA_ADDON_COMPATIABLE ,btnCompatiable .getSelection ());
175362configuration .setAttribute (
176363IJ2SLauchingConfiguration .J2S_MOZILLA_ADDON_COMPATIABLE_RAW_JS ,btnCompatiableRawJS .getSelection ());
364+ configuration .setAttribute (
365+ IJ2SLauchingConfiguration .USE_GLOBAL_ALAA_URL ,btnUseGlobalURL .getSelection ());
366+ configuration .setAttribute (
367+ IJ2SLauchingConfiguration .GLOBAL_J2SLIB_URL ,txtJ2SLib .getText ());
368+ configuration .setAttribute (
369+ IJ2SLauchingConfiguration .GLOBAL_BINARY_URL ,txtBin .getText ());
177370}
178371
179372public String getName () {