KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > wizard > CreateEARWizardPage


1
2 package com.bull.eclipse.jonas.wizard;
3
4 import java.io.File JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.HashMap JavaDoc;
8
9 import org.eclipse.core.resources.IProject;
10 import org.eclipse.jface.dialogs.MessageDialog;
11 import org.eclipse.jface.wizard.WizardPage;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.ModifyEvent;
14 import org.eclipse.swt.events.ModifyListener;
15 import org.eclipse.swt.events.SelectionAdapter;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.events.SelectionListener;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Group;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Text;
25
26
27
28
29 public class CreateEARWizardPage extends WizardPage
30                                         implements ModifyListener, SelectionListener
31 {
32     private Button addClientJarButton;
33     private Button addEjbButton;
34     private Button addRarButton;
35     private Button addWarButton;
36     private Text clientText;
37     private Text clientUriText;
38     private Text contentsText;
39     private Text ctxRootText;
40     private final String JavaDoc ctxRoot_kwd = "_ctx";
41     private Text descriptionText;
42     private Text displaynameText;
43     private ArrayList JavaDoc EarContents = new ArrayList JavaDoc();
44     private HashMap JavaDoc EarContentsMisc = new HashMap JavaDoc();
45     private Text earText;
46     private Text ejbText;
47     private Text ejbUriText;
48     private IProject proj;
49     private Text rarText;
50     private Text rarUriText;
51     private final String JavaDoc type_kwd = "_type";
52     private final String JavaDoc type_client = "client";
53     private final String JavaDoc type_ejb = "ejb";
54     private final String JavaDoc type_rar = "rar";
55     private final String JavaDoc type_war = "war";
56     private final String JavaDoc uri_kwd = "_uri";
57     private Text warText;
58     private Text webUriText;
59
60
61     public CreateEARWizardPage( String JavaDoc pagename )
62     {
63         super(pagename);
64     }
65
66     public void createControl( Composite parent )
67     {
68         Composite composite = new Composite(parent, SWT.NULL);
69         composite.setLayout(new GridLayout());
70         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
71
72         Composite earGroup = new Composite(composite,SWT.LEFT);
73         GridLayout layout = new GridLayout();
74         layout.numColumns = 2;
75         earGroup.setLayout(layout);
76         earGroup.setLayoutData( new GridData(GridData.FILL_HORIZONTAL) );
77
78         earText = addText(earGroup, "EAR file name");
79         displaynameText = addText(earGroup,"display name:");
80         descriptionText = addText(earGroup, "description:");
81         contentsText = addText( earGroup, "contents:" );
82         contentsText.setEditable( false );
83         earText.addModifyListener(this);
84
85         Group ejbGroup = createGroup(composite,"EJB");
86         ejbText = addText(ejbGroup,"JAR file name");
87         ejbText.addModifyListener(this);
88         ejbUriText = addText(ejbGroup,"ejb-uri:");
89         addEjbButton = addADDbutton(ejbGroup);
90         addEjbButton.addSelectionListener( new SelectionAdapter()
91             { public void widgetSelected( SelectionEvent e )
92                 { processAddEjbButton(); }
93             }
94         );
95
96         Group warGroup = createGroup(composite,"web app");
97         warText = addText(warGroup,"WAR file name");
98         warText.addModifyListener(this);
99         webUriText = addText(warGroup,"web-uri:");
100         ctxRootText = addText(warGroup,"context-root:");
101         addWarButton = addADDbutton(warGroup);
102         addWarButton.addSelectionListener( new SelectionAdapter()
103             { public void widgetSelected( SelectionEvent e )
104                 { processAddWarButton(); }
105             }
106         );
107
108         Group clientGroup = createGroup(composite,"client jar");
109         clientText = addText(clientGroup,"JAR file name");
110         clientText.addModifyListener(this);
111         clientUriText = addText(clientGroup,"client-uri:");
112         addClientJarButton = addADDbutton(clientGroup);
113         addClientJarButton.addSelectionListener( new SelectionAdapter()
114             { public void widgetSelected( SelectionEvent e )
115                 { processAddClientJarButton(); }
116             }
117         );
118
119         Group rarGroup = createGroup(composite,"connector");
120         rarText = addText(rarGroup,"RAR file name");
121         rarText.addModifyListener(this);
122         rarUriText = addText(rarGroup,"rar-uri:");
123         addRarButton = addADDbutton(rarGroup);
124         addRarButton.addSelectionListener( new SelectionAdapter()
125             { public void widgetSelected( SelectionEvent e )
126                 { processAddRarButton(); }
127             }
128         );
129
130         setMessage(null);
131         setControl(composite);
132         setPageComplete( validatePage() );
133     }
134
135     private Group createGroup( Composite parent, String JavaDoc title )
136     {
137         Group g = new Group( parent, SWT.NONE );
138         g.setText( title );
139         //g.setLayout( new RowLayout() );
140
g.setLayout( new GridLayout(2,false) );
141         g.setLayoutData( new GridData(GridData.FILL_HORIZONTAL) );
142         return g;
143     }
144
145     private Text addText( Composite parent, String JavaDoc lblTitle )
146     {
147         new Label( parent, SWT.RIGHT ).setText( lblTitle );
148         Text t = new Text( parent, SWT.BORDER );
149         GridData data = new GridData(GridData.FILL_HORIZONTAL);
150         t.setLayoutData(data);
151         return t;
152     }
153
154     private Button addADDbutton( Composite parent )
155     {
156         new Label(parent,SWT.NONE);
157         Button b = new Button(parent,SWT.NONE);
158         b.setText("Add");
159         b.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
160         return b;
161     }
162
163     private boolean fileExists( String JavaDoc s )
164     {
165         File JavaDoc f = proj.getFile(s).getLocation().toFile();
166         boolean b = f.exists();
167         if( !b ) {
168             MessageDialog.openInformation( null, "Error", "File does not exist in project: "+s );
169         }
170         return b;
171     }
172
173     private void processAddClientJarButton()
174     {
175         String JavaDoc s = clientText.getText().trim();
176         if( s.length()>0 && fileExists(s) ) {
177             addToContents(s,type_client,clientUriText);
178             clientText.setText("");
179             clientUriText.setText("");
180             setPageComplete( validatePage() );
181         }
182     }
183
184     private void processAddEjbButton()
185     {
186         String JavaDoc s = ejbText.getText().trim();
187         if( s.length()>0 && fileExists(s) ) {
188             addToContents(s,type_ejb,ejbUriText);
189             ejbText.setText("");
190             ejbUriText.setText("");
191             setPageComplete( validatePage() );
192         }
193     }
194
195     private void processAddRarButton()
196     {
197         String JavaDoc s = rarText.getText().trim();
198         if( s.length()>0 && fileExists(s) ) {
199             addToContents(s,type_rar,rarUriText);
200             rarText.setText("");
201             rarUriText.setText("");
202             setPageComplete( validatePage() );
203         }
204     }
205
206     private void processAddWarButton()
207     {
208         String JavaDoc s = warText.getText().trim();
209         if( s.length()>0 && fileExists(s) ) {
210             addToContents(s,type_war,webUriText);
211             EarContentsMisc.put( s + ctxRoot_kwd, ctxRootText.getText().trim() );
212             warText.setText("");
213             webUriText.setText("");
214             ctxRootText.setText("");
215             setPageComplete( validatePage() );
216         }
217     }
218
219     private void addToContents( String JavaDoc s2, String JavaDoc type, Text uri )
220     {
221         String JavaDoc s1 = contentsText.getText();
222         if( s1.length()>0 ) { s1 += ","; }
223         contentsText.setText( s1 + s2 );
224         EarContents.add( s2 );
225         EarContentsMisc.put( s2 + type_kwd, type );
226         String JavaDoc s3 = uri.getText().trim();
227         if( s3.length()==0 ) { s3 = basename(s2); }
228         EarContentsMisc.put( s2 + uri_kwd, s3 );
229     }
230
231     private String JavaDoc basename( String JavaDoc s )
232     {
233         int i;
234         String JavaDoc s2 = s;
235         if( (i=s2.lastIndexOf("/")) >= 0 ) { s2 = s2.substring(i+1); }
236         return s2;
237     }
238
239     public void modifyText( ModifyEvent e ) // ModifyListener implementation
240
{
241         setPageComplete( validatePage() );
242     }
243
244     public void setProj( IProject p )
245     {
246         proj = p;
247     }
248
249     private boolean validatePage()
250     {
251         return( earText != null
252                 && earText.getText().trim().length() > 0
253                 && !EarContents.isEmpty()
254                 && ejbText.getText().trim().length()==0
255                 && warText.getText().trim().length()==0
256                 && clientText.getText().trim().length()==0
257                 && rarText.getText().trim().length()==0
258             );
259     }
260
261     public String JavaDoc getAppXml()
262     {
263         return createAppXml();
264     }
265
266     public String JavaDoc getBldXml( String JavaDoc appxmlLoc )
267     {
268         return createBldXml( appxmlLoc );
269     }
270
271     private String JavaDoc createAppXml() // create 'application.xml' contents
272
{
273         Date JavaDoc now = new Date JavaDoc();
274         String JavaDoc appxml = "<?xml version='1.0' encoding='UTF-8'?>\n"
275             + "<!DOCTYPE application PUBLIC\n"
276             + "'-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN'\n"
277             + "'http://java.sun.com/dtd/application_1_3.dtd'>\n"
278             + "<!-- generated by JOPE " + now + " -->\n"
279             + "<application>\n"
280             + "<display-name>"+displaynameText.getText().trim()+"</display-name>\n"
281             + "<description>"+descriptionText.getText().trim()+"</description>\n";
282         for( int i=0; i<EarContents.size(); i++ ) {
283             String JavaDoc ar = (String JavaDoc)EarContents.get(i);
284             appxml += "<module>\n";
285             String JavaDoc type = (String JavaDoc)EarContentsMisc.get( ar + type_kwd );
286             String JavaDoc uri = (String JavaDoc)EarContentsMisc.get( ar + uri_kwd );
287             if( type.equals(type_war) ) {
288                 appxml += "<web>\n"
289                 + "<web-uri>" + uri + "</web-uri>\n"
290                 + "<context-root>" + EarContentsMisc.get(ar+ctxRoot_kwd) + "</context-root>\n"
291                 + "</web>";
292             }
293             else if( type.equals(type_ejb) ) { appxml += "<ejb>" + uri + "</ejb>"; }
294             else if( type.equals(type_client) ) { appxml += "<java>" + uri + "</java>"; }
295             else /* rar */ { appxml += "<connector>" + uri + "</connector>"; }
296             appxml += "</module>\n";
297         }
298         appxml += "</application>";
299         return appxml;
300     }
301
302     private String JavaDoc createBldXml( String JavaDoc appxml )
303     {
304         // create 'build.xml' containing 'ear' task
305
Date JavaDoc now = new Date JavaDoc();
306         String JavaDoc bldxml = "<project default='ear'>\n"
307             + "<!-- generated by JOPE " + now + " -->\n"
308             + "<target name='ear'>\n"
309             + "<ear destfile='"+ proj.getFile(earText.getText().trim()).getLocation() + "' "
310             + "appxml='" + appxml + "'>\n";
311         for( int i=0; i<EarContents.size(); i++ ) {
312             String JavaDoc ar = (String JavaDoc)EarContents.get(i);
313             String JavaDoc uri = (String JavaDoc)EarContentsMisc.get( ar + uri_kwd );
314             bldxml += "<zipfileset file='"
315             + proj.getFile(ar).getLocation()
316             + "' fullpath='" + uri + "'/>\n";
317         }
318         bldxml += "</ear>\n"
319             + "</target>\n"
320             + "</project>\n";
321         return bldxml;
322     }
323
324     public void widgetSelected( SelectionEvent e ) // SelectionListener
325
{
326         // This is useless because SelectionEvent.item IS NULL !!!
327
// Must use a per-Button SelectionListener.
328
}
329
330     public void widgetDefaultSelected( SelectionEvent e ) // SelectionListener
331
{
332         // this is not called for a Button
333
}
334
335 }
336
337
338
Popular Tags