KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > importer > ui > contribution > base > ModelDetailPage


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * ModelDetailPage.java,v 1.1 2005/05/12 17:10:24 marcelop Exp
16  */

17 package org.eclipse.emf.importer.ui.contribution.base;
18
19 import java.io.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.graphics.Point;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.layout.RowLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Event;
36 import org.eclipse.swt.widgets.FileDialog;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Text;
39 import org.eclipse.ui.actions.WorkspaceModifyOperation;
40 import org.eclipse.ui.dialogs.ResourceSelectionDialog;
41
42 import org.eclipse.emf.codegen.util.CodeGenUtil;
43 import org.eclipse.emf.common.util.URI;
44 import org.eclipse.emf.common.util.WrappedException;
45 import org.eclipse.emf.importer.ImporterPlugin;
46 import org.eclipse.emf.importer.ModelImporter;
47 import org.eclipse.emf.importer.util.ImporterUtil;
48
49
50 /**
51  * @since 2.1.0
52  */

53 public class ModelDetailPage extends ModelImporterPage
54 {
55   protected Text modelLocationText;
56   protected Button loadButton;
57   protected Text genModelNameText;
58   protected Button modelLocationBrowseFileSystemButton;
59   protected Button modelLocationBrowseWorkspaceButton;
60
61   protected String JavaDoc[] filterExtensions;
62
63   protected boolean showGenModel = false;
64   protected boolean usingInternalSetName = true;
65   
66
67   public ModelDetailPage(ModelImporter modelImporter, String JavaDoc pageName)
68   {
69     super(modelImporter, pageName);
70     showGenModel = getModelImporter().getGenModelFileName() == null;
71   }
72
73   public void dispose()
74   {
75     if (modelLocationText != null)
76     {
77       modelLocationText.removeListener(SWT.Modify, this);
78       modelLocationText = null;
79     }
80     if (loadButton != null)
81     {
82       loadButton.removeListener(SWT.Selection, this);
83       loadButton = null;
84     }
85     if (genModelNameText != null)
86     {
87       genModelNameText.removeListener(SWT.Modify, this);
88       genModelNameText = null;
89     }
90     if (modelLocationBrowseFileSystemButton != null)
91     {
92       modelLocationBrowseFileSystemButton.removeListener(SWT.Selection, this);
93       modelLocationBrowseFileSystemButton = null;
94     }
95     if (modelLocationBrowseWorkspaceButton != null)
96     {
97       modelLocationBrowseWorkspaceButton.removeListener(SWT.Selection, this);
98       modelLocationBrowseWorkspaceButton = null;
99     }
100
101     super.dispose();
102   }
103
104   public boolean showGenModel()
105   {
106     return showGenModel;
107   }
108
109   protected void pageActivated(boolean firstTime, int cause)
110   {
111     if (firstTime)
112     {
113       if (getModelImporter().getOriginalGenModelPath() != null)
114       {
115         getControl().getDisplay().asyncExec(new Runnable JavaDoc()
116           {
117             public void run()
118             {
119               handleOriginalModelFile();
120             }
121           });
122       }
123     }
124   }
125
126   protected void handleOriginalModelFile()
127   {
128     refreshModel();
129   }
130
131   public void createControl(Composite parent)
132   {
133     Composite composite = new Composite(parent, SWT.NONE);
134     composite.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL));
135
136     GridLayout layout = new GridLayout();
137     layout.verticalSpacing = 8;
138     composite.setLayout(layout);
139
140     createModelLocationControl(composite);
141     addControl(composite);
142     if (showGenModel())
143     {
144       createGenModelNameControl(composite);
145     }
146
147     adjustLoadButton();
148     setControl(composite);
149   }
150
151   protected void createModelLocationControl(Composite parent)
152   {
153     Composite composite = new Composite(parent, SWT.NONE);
154     composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
155     {
156       GridLayout layout = new GridLayout(2, false);
157       layout.marginLeft = -5;
158       layout.marginRight = -5;
159       composite.setLayout(layout);
160     }
161     
162     Label modelLocationLabel = new Label(composite, SWT.LEFT);
163     modelLocationLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
164     modelLocationLabel.setText(getModelLocationTextLabel());
165
166     Composite buttonComposite = new Composite(composite, SWT.NONE);
167     buttonComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END));
168     {
169       RowLayout layout = new RowLayout();
170       layout.justify = true;
171       layout.pack = true;
172       layout.spacing = 5;
173       layout.marginRight = 0;
174       buttonComposite.setLayout(layout);
175     }
176
177     modelLocationBrowseFileSystemButton = new Button(buttonComposite, SWT.PUSH);
178     modelLocationBrowseFileSystemButton.setText(getBrowseFileSystemButtonLabel());
179     modelLocationBrowseFileSystemButton.addListener(SWT.Selection, this);
180
181     modelLocationBrowseWorkspaceButton = new Button(buttonComposite, SWT.PUSH);
182     modelLocationBrowseWorkspaceButton.setText(getBrowseWorkspaceButtonLabel());
183     modelLocationBrowseWorkspaceButton.addListener(SWT.Selection, this);
184
185     Composite modelLocationComposite = new Composite(parent, SWT.NONE);
186     modelLocationComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
187     {
188       GridLayout layout = new GridLayout(2, false);
189       layout.marginTop = -5;
190       layout.marginLeft = -5;
191       layout.marginRight = -5;
192       modelLocationComposite.setLayout(layout);
193     }
194
195     modelLocationText = new Text(modelLocationComposite, SWT.SINGLE | SWT.BORDER);
196     modelLocationText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
197     if (getModelImporter().getModelLocation() != null)
198     {
199       modelLocationText.setText(getModelImporter().getModelLocation());
200     }
201     modelLocationText.addListener(SWT.Modify, this);
202     
203     createLoadButton(modelLocationComposite);
204   }
205   
206   protected void createLoadButton(Composite parent)
207   {
208     loadButton = new Button(parent, SWT.PUSH);
209     loadButton.setText(ImporterPlugin.INSTANCE.getString("_UI_Load_label"));
210     GridData data = new GridData(GridData.END);
211     data.widthHint = 50;
212     loadButton.setLayoutData(data);
213     loadButton.addListener(SWT.Selection, this);
214   }
215
216   protected void createGenModelNameControl(Composite parent)
217   {
218     Label genModelNameLabel = new Label(parent, SWT.LEFT);
219     genModelNameLabel.setText(ImporterPlugin.INSTANCE.getString("_UI_GeneratorModelName_label"));
220
221     genModelNameText = new Text(parent, SWT.SINGLE | SWT.BORDER);
222     genModelNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
223
224     genModelNameText.addListener(SWT.Modify, this);
225   }
226
227   protected void addControl(Composite parent)
228   {
229   }
230   
231   protected void doHandleEvent(Event event)
232   {
233     if (event.type == SWT.Modify && event.widget == modelLocationText)
234     {
235       setErrorMessage(null);
236       setMessage(null);
237       getModelImporter().setModelLocation(null);
238       getModelImporter().clearEPackagesCollections();
239       adjustLoadButton();
240     }
241     else if (event.type == SWT.Selection && event.widget == loadButton)
242     {
243       refreshModel();
244     }
245     else if (event.type == SWT.Modify && event.widget == genModelNameText)
246     {
247       usingInternalSetName = false;
248       getModelImporter().setGenModelFileName(genModelNameText.getText());
249       IStatus status = getModelImporter().checkGenModelFileName();
250       handleStatus(status);
251     }
252     else if (event.type == SWT.Selection && event.widget == modelLocationBrowseFileSystemButton)
253     {
254       browseFileSystem();
255     }
256     else if (event.type == SWT.Selection && event.widget == modelLocationBrowseWorkspaceButton)
257     {
258       browseWorkspace();
259     }
260     else
261     {
262       super.doHandleEvent(event);
263     }
264     getContainer().updateButtons();
265   }
266   
267   protected void adjustLoadButton()
268   {
269     if (loadButton != null)
270     {
271       String JavaDoc text = modelLocationText.getText();
272       loadButton.setEnabled(text != null && text.trim().length() > 0);
273     }
274   }
275
276   protected String JavaDoc getModelLocationTextLabel()
277   {
278     return ImporterPlugin.INSTANCE.getString("_UI_ModelLocation_label");
279   }
280
281   protected String JavaDoc getBrowseFileSystemButtonLabel()
282   {
283     return ImporterPlugin.INSTANCE.getString("_UI_BrowseFileSystemFile_label");
284   }
285
286   protected String JavaDoc getBrowseWorkspaceButtonLabel()
287   {
288     return ImporterPlugin.INSTANCE.getString("_UI_BrowseWorkspace_label");
289   }
290
291   protected String JavaDoc getSelectModelLabel()
292   {
293     return ImporterPlugin.INSTANCE.getString("_UI_SelectModel_label");
294   }
295
296   protected boolean supportMultipleModelLocation()
297   {
298     return true;
299   }
300
301   protected String JavaDoc[] getFilterExtensions()
302   {
303     if (filterExtensions == null)
304     {
305       List JavaDoc fileExtensions = getModelImporter().getFileExtensions();
306       if (fileExtensions.isEmpty())
307       {
308         filterExtensions = new String JavaDoc []{ "*.*" };
309       }
310       else if (fileExtensions.size() == 1)
311       {
312         filterExtensions = new String JavaDoc[]{"*." + (String JavaDoc)fileExtensions.get(0)};
313       }
314       else
315       {
316         StringBuffer JavaDoc allFilterExtensions = new StringBuffer JavaDoc();
317         String JavaDoc[] extensions = new String JavaDoc [fileExtensions.size() + 1];
318         for (int i = 1, lenght=extensions.length; i < lenght; i++)
319         {
320           extensions[i] = "*." + (String JavaDoc)fileExtensions.get(i-1);
321           allFilterExtensions.append(";").append(extensions[i]);
322         }
323         allFilterExtensions.deleteCharAt(0);
324         extensions[0] = allFilterExtensions.toString();
325         filterExtensions = extensions;
326       }
327     }
328     return filterExtensions;
329   }
330
331   protected boolean isValidWorkspaceResource(IResource resource)
332   {
333     if (resource.getType() == IResource.FILE && !CodeGenUtil.isInJavaOutput(resource))
334     {
335       String JavaDoc[] filterExtensions = getFilterExtensions();
336       if (filterExtensions.length > 0)
337       {
338         for (int i = 0; i < filterExtensions.length; i++)
339         {
340           if (filterExtensions[i].endsWith(".*") || filterExtensions[i].endsWith("." + resource.getFileExtension()))
341           {
342             return true;
343           }
344         }
345       }
346
347     }
348     return false;
349   }
350
351   protected void setModelLocationText(String JavaDoc location)
352   {
353     location = location.trim();
354     StringBuffer JavaDoc text = new StringBuffer JavaDoc(modelLocationText.getText());
355     if (!location.equals(text))
356     {
357       if (supportMultipleModelLocation())
358       {
359         Point textSelection = modelLocationText.getSelection();
360         text.delete(textSelection.x, textSelection.y);
361         location = text.append(" ").append(location).toString();
362       }
363       modelLocationText.setText(location.trim());
364     }
365   }
366
367   protected boolean browseFileSystem()
368   {
369     FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN | (supportMultipleModelLocation() ? SWT.MULTI : SWT.SINGLE));
370     fileDialog.setFilterExtensions(getFilterExtensions());
371
372     URI modelURI = getModelImporter().getFirstModelLocationURI(true);
373     if (modelURI != null)
374     {
375       fileDialog.setFileName(modelURI.toFileString());
376     }
377     
378     if (fileDialog.open() != null && fileDialog.getFileNames().length > 0)
379     {
380       String JavaDoc[] fileNames = fileDialog.getFileNames();
381       StringBuffer JavaDoc text = new StringBuffer JavaDoc();
382       for (int i = 0; i < fileNames.length; ++i)
383       {
384         String JavaDoc filePath = fileDialog.getFilterPath() + File.separator + fileNames[i];
385         text.append(URI.createFileURI(filePath).toString());
386         text.append(" ");
387       }
388       setModelLocationText(text.toString());
389       refreshModel();
390       return true;
391     }
392     return false;
393   }
394
395   protected boolean browseWorkspace()
396   {
397     ResourceSelectionDialog resourceSelectionDialog = new ResourceSelectionDialog(
398       getShell(),
399       ResourcesPlugin.getWorkspace().getRoot(),
400       getSelectModelLabel());
401
402     resourceSelectionDialog.open();
403     Object JavaDoc[] result = resourceSelectionDialog.getResult();
404     if (result != null)
405     {
406       StringBuffer JavaDoc text = new StringBuffer JavaDoc();
407       int length = supportMultipleModelLocation() ? result.length : 1;
408       for (int i = 0; i < length; ++i)
409       {
410         IResource resource = (IResource)result[i];
411         if (isValidWorkspaceResource(resource))
412         {
413           text.append(URI.createURI(URI.createPlatformResourceURI(resource.getFullPath().toString()).toString(), true));
414           text.append(" ");
415         }
416       }
417       setModelLocationText(text.toString());
418       refreshModel();
419       return true;
420     }
421     return false;
422   }
423
424   protected void refreshModel()
425   {
426     WorkspaceModifyOperation initializeOperation = new WorkspaceModifyOperation()
427       {
428         protected void execute(IProgressMonitor progressMonitor) throws CoreException
429         {
430           IStatus errorStatus = null;
431           setErrorMessage(null);
432           setMessage(null);
433           
434           try
435           {
436             refreshModel(progressMonitor);
437           }
438           catch (Exception JavaDoc exception)
439           {
440             ImporterPlugin.INSTANCE.log(exception);
441             errorStatus = ImporterUtil.createErrorStatus(exception, true);
442           }
443           finally
444           {
445             progressMonitor.done();
446           }
447           
448           if (errorStatus != null)
449           {
450             handleStatus(errorStatus, errorStatus.getMessage(), ImporterPlugin.INSTANCE.getString("_UI_LoadProblem_title"), ImporterPlugin.INSTANCE.getString("_UI_ProblemsEncounteredProcessing_message"));
451           }
452         }
453       };
454
455     getModelImporter().setModelLocation(modelLocationText.getText());
456
457     try
458     {
459       getContainer().run(false, false, initializeOperation);
460     }
461     catch (Exception JavaDoc exception)
462     {
463       ImporterPlugin.INSTANCE.log(exception);
464     }
465
466     if (isPageComplete())
467     {
468       setPageComplete(true);
469     }
470     else
471     {
472       setPageComplete(false);
473       modelLocationText.selectAll();
474       modelLocationText.setFocus();
475     }
476   }
477   
478   public boolean isPageComplete()
479   {
480     return super.isPageComplete()
481       && !getModelImporter().getEPackages().isEmpty()
482       && !getModelImporter().getModelLocationURIs().isEmpty();
483   }
484
485   protected void refreshModel(IProgressMonitor progressMonitor) throws Exception JavaDoc
486   {
487     IStatus status = null;
488     try
489     {
490       status = getModelImporter().computeEPackages(progressMonitor);
491       getModelImporter().adjustEPackages(progressMonitor);
492     }
493     catch (WrappedException wrappedException)
494     {
495       if (wrappedException.exception() instanceof FileNotFoundException JavaDoc)
496       {
497         setMessage(null);
498         setErrorMessage(ImporterPlugin.INSTANCE.getString("_UI_SpecifyAValidModel_message"));
499         return;
500       }
501       else
502       {
503         throw wrappedException.exception();
504       }
505     }
506     
507     internalSetGenModelFileName(getDefaultGenModelFileName());
508     IStatus nameStatus = getModelImporter().checkGenModelFileName();
509     if (!nameStatus.isOK())
510     {
511       if (status.isOK())
512       {
513         status = nameStatus;
514       }
515       else
516       {
517         status = ImporterUtil.mergeStatus(status, nameStatus);
518       }
519     }
520     
521     handleStatus(status);
522   }
523   
524   protected String JavaDoc getDefaultGenModelFileName()
525   {
526     return getModelImporter().computeDefaultGenModelFileName();
527   }
528   
529   protected void internalSetGenModelFileName(String JavaDoc name)
530   {
531     if (usingInternalSetName && showGenModel() && name != null)
532     {
533       getModelImporter().setGenModelFileName(name);
534       setHandlingEvent(false);
535       genModelNameText.setText(getModelImporter().getGenModelFileName());
536       setHandlingEvent(true);
537     }
538   }
539 }
540
Popular Tags