1+ package net .sf .j2s .ui .launching ;
2+ import net .sf .j2s .ui .launching .template .J2SAppLauncherTemplateContributor ;
3+ import net .sf .j2s .ui .launching .template .J2STemplateManager ;
4+ import net .sf .j2s .ui .launching .template .TemplateInfo ;
5+
6+ import org .eclipse .core .runtime .CoreException ;
7+ import org .eclipse .debug .core .ILaunch ;
8+ import org .eclipse .debug .core .ILaunchConfiguration ;
9+ import org .eclipse .debug .core .ILaunchConfigurationWorkingCopy ;
10+ import org .eclipse .debug .ui .AbstractLaunchConfigurationTab ;
11+ import org .eclipse .jface .dialogs .Dialog ;
12+ import org .eclipse .jface .dialogs .InputDialog ;
13+ import org .eclipse .jface .dialogs .MessageDialog ;
14+ import org .eclipse .swt .SWT ;
15+ import org .eclipse .swt .events .ModifyEvent ;
16+ import org .eclipse .swt .events .ModifyListener ;
17+ import org .eclipse .swt .events .SelectionAdapter ;
18+ import org .eclipse .swt .events .SelectionEvent ;
19+ import org .eclipse .swt .graphics .Image ;
20+ import org .eclipse .swt .layout .GridData ;
21+ import org .eclipse .swt .layout .GridLayout ;
22+ import org .eclipse .swt .layout .RowData ;
23+ import org .eclipse .swt .layout .RowLayout ;
24+ import org .eclipse .swt .widgets .Button ;
25+ import org .eclipse .swt .widgets .Combo ;
26+ import org .eclipse .swt .widgets .Composite ;
27+ import org .eclipse .swt .widgets .Label ;
28+ import org .eclipse .swt .widgets .Text ;
29+ import org .eclipse .ui .ISharedImages ;
30+ import org .eclipse .ui .PlatformUI ;
31+
32+ /**
33+ * "template" Option tab for j2s application launcher configuration dialog
34+ *
35+ * TODO: detect user changes in code and ask him if want to save it.
36+ * TODO: set default template button.
37+ * @author sgurin
38+ *
39+ */
40+ public class J2STemplateOptionsTab extends AbstractLaunchConfigurationTab {
41+
42+ private J2sTemplateComposite comp ;
43+ private J2SAppLauncherTemplateContributor contributor ;
44+
45+ // protected boolean templateCodeDirty=false;
46+
47+ // **** implement the interface **** //
48+
49+ public J2STemplateOptionsTab (J2SAppLauncherTemplateContributor contributor ) {
50+ super ();
51+ this .contributor =contributor ;
52+ }
53+
54+ public void createControl (Composite parent ) {
55+ try {
56+ comp = new J2sTemplateComposite (this , parent , SWT .NULL );
57+ } catch (Exception e ) {
58+ MessageDialog .openError (parent .getShell (), "Error initializing templates" ,
59+ "An error occurs when trying to initialize templates : " +e .getMessage ());
60+ e .printStackTrace ();
61+ return ;
62+ }
63+ GridData gd = new GridData (GridData .FILL_BOTH );
64+ comp .setLayoutData (gd );
65+ setControl (comp );
66+ }
67+
68+ public void setDefaults (ILaunchConfigurationWorkingCopy configuration ) {
69+ configuration .setAttribute (IJ2SLauchingConfiguration .VELOCITY_CODE ,
70+ (String ) null );
71+ configuration .setAttribute (IJ2SLauchingConfiguration .OUTPUT_FILE_NAME ,
72+ (String ) null );
73+ }
74+
75+ public void initializeFrom (ILaunchConfiguration configuration ) {
76+ try {
77+ comp .getApplyTemplateCheck ().setSelection (configuration .getAttribute (
78+ IJ2SLauchingConfiguration .APPLY_TEMPLATE , true ));
79+ comp .updateTemaplteCheck ();
80+
81+ comp .getTemplateCodeText ().setText (configuration .getAttribute (
82+ IJ2SLauchingConfiguration .VELOCITY_CODE , "" ));
83+ comp .getOutputFileText ().setText (configuration .getAttribute (
84+ IJ2SLauchingConfiguration .OUTPUT_FILE_NAME , "" ));
85+ comp .selectTemplateByName (configuration .getAttribute (
86+ IJ2SLauchingConfiguration .TEMPLATE_NAME , "" ));
87+
88+ } catch (CoreException e ) {
89+ e .printStackTrace ();
90+ }
91+ }
92+
93+ public void performApply (ILaunchConfigurationWorkingCopy c ) {
94+
95+ // if(templateCodeDirty ) {
96+ // try {
97+ // templateCodeDirty=false;
98+ // comp.doSaveTemplateAs("Template code modified! \nIf you whish to save the changes, please enter the name\nof new template.");
99+ // } catch (Exception e) {
100+ // e.printStackTrace();
101+ // }
102+ // }
103+
104+ boolean applyTemplate = comp .getApplyTemplateCheck ().getSelection ();
105+ String templateCode = comp .getTemplateCodeText ().getText ();
106+ int selIdx = comp .getTemplateCombo ().getSelectionIndex ();
107+ String templateName =selIdx ==-1 ? "" : comp .getTemplateCombo ().getItems ()[selIdx ];
108+ String outputFileNameTemplate = comp .getOutputFileText ().getText ();
109+
110+ c .setAttribute (IJ2SLauchingConfiguration .APPLY_TEMPLATE , applyTemplate );
111+ c .setAttribute (IJ2SLauchingConfiguration .VELOCITY_CODE , templateCode );
112+ c .setAttribute (IJ2SLauchingConfiguration .OUTPUT_FILE_NAME , outputFileNameTemplate );
113+ c .setAttribute (IJ2SLauchingConfiguration .TEMPLATE_NAME , templateName );
114+ }
115+
116+ public String getName () {
117+ return "Template" ;
118+ }
119+ public Image getImage () {
120+ return getClasspathImage ();
121+ }
122+ public static Image getClasspathImage () {
123+ return PlatformUI .getWorkbench ().getSharedImages ().getImage (ISharedImages .IMG_OBJS_TASK_TSK );
124+ }
125+
126+
127+ /* (non-Javadoc)
128+ * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
129+ */
130+ public void activated (ILaunchConfigurationWorkingCopy workingCopy ) {}
131+
132+ /* (non-Javadoc)
133+ * @see org.eclipse.debug.ui.ILaunchConfigurationTab#deactivated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
134+ */
135+ public void deactivated (ILaunchConfigurationWorkingCopy workingCopy ) {}
136+
137+ // **** GUI **** //
138+
139+ /**
140+ * GUI class
141+ * @author sgurin
142+ */
143+ public class J2sTemplateComposite extends org .eclipse .swt .widgets .Composite {
144+
145+ private Label label1 ;
146+ private Button resetBtn ;
147+ private Button applyTemplateCheck ;
148+ private Text outputFileText ;
149+ private Label label3 ;
150+ private Button saveBtn ;
151+ private Composite composite1 ;
152+ private Text templateCodeText ;
153+ private Label label2 ;
154+ private Combo templateCombo ;
155+ private J2STemplateOptionsTab optionsTab ;
156+ private J2STemplateManager manager ;
157+ private String [] currentTemplateList ;
158+
159+ public J2sTemplateComposite (J2STemplateOptionsTab optionsTab ,
160+ Composite parent , int style ) throws Exception {
161+ super (parent , style );
162+ this .optionsTab = optionsTab ;
163+ manager = new J2STemplateManager (contributor );
164+ initGUI ();
165+ }
166+ public Button getApplyTemplateCheck () {
167+ return applyTemplateCheck ;
168+ }
169+ public Text getOutputFileText () {
170+ return outputFileText ;
171+ }
172+ public Text getTemplateCodeText () {
173+ return templateCodeText ;
174+ }
175+ public Combo getTemplateCombo () {
176+ return templateCombo ;
177+ }
178+ private void doSaveTemplateAs (String msg ) throws Exception {
179+ InputDialog dialog = new InputDialog (J2sTemplateComposite .this .getShell (),
180+ "Save As" , msg , templateCombo .getText (), null );
181+ if (dialog .open () == Dialog .OK ) {
182+ String tname = dialog .getValue ();
183+ if (contributor .getBuiltInTemplates ().contains (tname )) {
184+ MessageDialog .openError (J2sTemplateComposite .this .getShell (), "Error" ,
185+ "Error: a predefined template named \" " +tname +"\" already exists. \n " +
186+ "Predefined templates cannot be overwritten, please choose another name" );
187+ return ;
188+ }
189+ else if (J2SLaunchingUtil .arrayContains (manager .listTemplateNames (), tname )) {
190+ MessageDialog overwriteDialog = new MessageDialog (J2sTemplateComposite .this .getShell (),
191+ "Overwrite existing template" , null , "A template named \" " +tname +"\" already exists\n " +
192+ "Are you sure you want to overwrite it?" , MessageDialog .QUESTION , new String []{"Ok" , "Cancel" }, 0 );
193+ if (overwriteDialog .open () != 0 ) {
194+ return ;
195+ }
196+ }
197+ //do it
198+ manager .putTemplate (new TemplateInfo (tname , templateCodeText .getText (), outputFileText .getText ()));
199+ currentTemplateList = manager .listTemplateNames ();
200+ templateCombo .setItems (currentTemplateList );
201+ selectTemplateByName (tname );
202+ }
203+ }
204+
205+ private void initGUI () {
206+ try {
207+ GridLayout thisLayout = new GridLayout ();
208+ thisLayout .makeColumnsEqualWidth = true ;
209+ this .setLayout (thisLayout );
210+ {
211+ applyTemplateCheck = new Button (this , SWT .CHECK | SWT .LEFT );
212+ applyTemplateCheck .setText ("Apply template" );
213+ GridData button1LData = new GridData ();
214+ applyTemplateCheck .setLayoutData (button1LData );
215+ applyTemplateCheck .addSelectionListener (new SelectionAdapter () {
216+ public void widgetSelected (SelectionEvent e ) {
217+ optionsTab .updateLaunchConfigurationDialog ();
218+ updateTemaplteCheck ();
219+
220+ }
221+ });
222+ }
223+ {
224+ label1 = new Label (this , SWT .NONE );
225+ label1 .setText ("Template:" );
226+ GridData label1LData = new GridData ();
227+ label1 .setLayoutData (label1LData );
228+ }
229+ {
230+ composite1 = new Composite (this , SWT .NONE );
231+ RowLayout composite1Layout = new RowLayout (org .eclipse .swt .SWT .HORIZONTAL );
232+ composite1Layout .marginLeft = 0 ;
233+ composite1 .setLayout (composite1Layout );
234+ GridData composite1LData = new GridData ();
235+ composite1 .setLayoutData (composite1LData );
236+ {
237+ templateCombo = new Combo (composite1 , SWT .NONE );
238+ // templateCombo.setText("temlate1");
239+ currentTemplateList = manager .listTemplateNames ();
240+ templateCombo .setItems (currentTemplateList );
241+ RowData templateComboLData = new RowData ();
242+ templateCombo .setLayoutData (templateComboLData );
243+ templateCombo .addSelectionListener (new SelectionAdapter () {
244+ // @Override
245+ public void widgetSelected (SelectionEvent e ) {
246+ try {
247+ optionsTab .updateLaunchConfigurationDialog ();
248+ String tname = currentTemplateList [templateCombo .getSelectionIndex ()];
249+ TemplateInfo template = manager .getTemplate (tname );
250+ outputFileText .setText (template .getFileOutputName ());
251+ templateCodeText .setText (template .getTemplateCode ());
252+ }catch (Exception ex ) {
253+ ex .printStackTrace ();
254+ }
255+ }
256+ });
257+ }
258+ {
259+ saveBtn = new Button (composite1 , SWT .PUSH | SWT .CENTER );
260+ saveBtn .setText ("save as" );
261+ RowData saveBtnLData = new RowData ();
262+ saveBtn .setLayoutData (saveBtnLData );
263+ saveBtn .addSelectionListener (new SelectionAdapter () {
264+
265+ public void widgetSelected (SelectionEvent e ) {
266+ try {
267+ doSaveTemplateAs ("Please enter name for new template:" );
268+ } catch (Exception ex ) {
269+ MessageDialog .openError (J2sTemplateComposite .this .getShell (),
270+ "Error" , "Unespected error: " +ex .getMessage ());
271+ }
272+ }
273+ });
274+ }
275+ {
276+ resetBtn = new Button (composite1 , SWT .PUSH | SWT .CENTER );
277+ resetBtn .setText ("reset" );
278+ resetBtn .addSelectionListener (new SelectionAdapter () {
279+ // @Override
280+ public void widgetSelected (SelectionEvent e ) {
281+ try {
282+ MessageDialog overwriteDialog = new MessageDialog (J2sTemplateComposite .this .getShell (),
283+ "Reset J2S templates" , null , "You are about to reset all user defined J2s templates. \n " +
284+ "All templates but predefined J2S' will be deleted. \n Are you sure?" ,
285+ MessageDialog .QUESTION , new String []{"Ok" , "Cancel" }, 0 );
286+ if (overwriteDialog .open () == 0 ) {
287+ String [] names = manager .listTemplateNames ();
288+ for (int i = 0 ; i < names .length ; i ++)
289+ manager .removeTemplate (names [i ]);
290+ templateCombo .setItems (manager .listTemplateNames ());
291+ }
292+ }catch (Exception ex ) {
293+ MessageDialog .openError (J2sTemplateComposite .this .getShell (),
294+ "Error" , "Unespected error: " +ex .getMessage ());
295+ ex .printStackTrace ();
296+ }
297+ }
298+ });
299+ }
300+ }
301+ {
302+ label3 = new Label (this , SWT .NONE );
303+ label3 .setText ("Output File Name:" );
304+ GridData label3LData = new GridData ();
305+ label3 .setLayoutData (label3LData );
306+ }
307+ {
308+ outputFileText = new Text (this , SWT .NONE );
309+ GridData outputFileTextLData = new GridData ();
310+ outputFileTextLData .horizontalAlignment = GridData .FILL ;
311+ outputFileTextLData .grabExcessHorizontalSpace = true ;
312+ outputFileText .setLayoutData (outputFileTextLData );
313+ outputFileText .addModifyListener (new ModifyListener () {
314+ // @Override
315+ public void modifyText (ModifyEvent e ) {
316+ optionsTab .updateLaunchConfigurationDialog ();
317+ }
318+ });
319+ }
320+ {
321+ label2 = new Label (this , SWT .NONE );
322+ label2 .setText ("Velocity Code: " );
323+ GridData label2LData = new GridData ();
324+ label2 .setLayoutData (label2LData );
325+ }
326+ {
327+ templateCodeText = new Text (this , SWT .MULTI | SWT .WRAP | SWT .V_SCROLL | SWT .H_SCROLL );
328+ templateCodeText .setText ("text1" );
329+
330+ GridData text1LData = new GridData ();
331+ text1LData .verticalAlignment = GridData .FILL ;
332+ text1LData .horizontalAlignment = GridData .FILL ;
333+ text1LData .grabExcessHorizontalSpace = true ;
334+ text1LData .grabExcessVerticalSpace = true ;
335+ templateCodeText .setLayoutData (text1LData );
336+
337+ templateCodeText .addModifyListener (new ModifyListener () {
338+ // @Override
339+ public void modifyText (ModifyEvent e ) {
340+ optionsTab .updateLaunchConfigurationDialog ();
341+ // templateCodeDirty=true;
342+ }
343+ });
344+ }
345+ this .layout ();
346+ pack ();
347+ } catch (Exception e ) {
348+ e .printStackTrace ();
349+ }
350+ }
351+ protected void updateTemaplteCheck () {
352+ if (!applyTemplateCheck .getSelection ()) {
353+ templateCombo .setEnabled (false );
354+ templateCodeText .setEnabled (false );
355+ outputFileText .setEnabled (false );
356+ }
357+ else {
358+ templateCombo .setEnabled (true );
359+ templateCodeText .setEnabled (true );
360+ outputFileText .setEnabled (true );
361+ }
362+ }
363+ public void selectTemplateByName (String tname ) {
364+ String [] items = templateCombo .getItems ();
365+ for (int i = 0 ; i < items .length ; i ++) {
366+ if (items [i ].equals (tname )) {
367+ templateCombo .select (i );
368+ }
369+ }
370+ }
371+ }
372+ }
0 commit comments