Added
Link Here
|
1 |
package org.eclipse.jst.jsf.facesconfig.ui.snippets; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.util.ArrayList; |
5 |
import java.util.List; |
6 |
|
7 |
import org.eclipse.core.resources.IFile; |
8 |
import org.eclipse.core.resources.IProject; |
9 |
import org.eclipse.core.resources.ResourcesPlugin; |
10 |
import org.eclipse.core.runtime.IPath; |
11 |
import org.eclipse.core.runtime.IStatus; |
12 |
import org.eclipse.emf.common.util.EList; |
13 |
import org.eclipse.emf.common.util.URI; |
14 |
import org.eclipse.emf.ecore.resource.Resource; |
15 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
16 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
17 |
import org.eclipse.jface.viewers.ISelection; |
18 |
import org.eclipse.jface.viewers.IStructuredSelection; |
19 |
import org.eclipse.jst.jsf.facesconfig.emf.FacesConfigType; |
20 |
import org.eclipse.jst.jsf.facesconfig.emf.NavigationRuleType; |
21 |
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.PageflowEditor; |
22 |
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.editpart.PageflowNodeEditPart; |
23 |
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowPackage; |
24 |
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.synchronization.FC2PFTransformer; |
25 |
import org.eclipse.wst.common.componentcore.ComponentCore; |
26 |
import org.eclipse.wst.common.snippets.internal.AbstractSnippetProvider; |
27 |
import org.eclipse.wst.common.snippets.internal.model.SnippetManager; |
28 |
import org.eclipse.wst.common.snippets.ui.ISnippetInsertion; |
29 |
|
30 |
/** |
31 |
* @author I024096 |
32 |
* |
33 |
*/ |
34 |
public class FacesXmlProvider extends AbstractSnippetProvider { |
35 |
|
36 |
|
37 |
|
38 |
public String getId() { |
39 |
return FacesXmlProvider.class.getCanonicalName(); |
40 |
} |
41 |
|
42 |
public ISnippetInsertion getSnippetInstertion() { |
43 |
return new NavigationSnippetInsertion(); |
44 |
} |
45 |
|
46 |
public boolean isActionEnabled(ISelection selection) { |
47 |
return false; |
48 |
} |
49 |
|
50 |
public IStatus saveAdditionalContent(IPath path) { |
51 |
ISelection selection = fEditorPart.getEditorSite().getSelectionProvider().getSelection(); |
52 |
|
53 |
IPath path_for_model = ((PageflowEditor)fEditorPart).getPageflowManager().getPath(); |
54 |
IFile containerForLocation = ResourcesPlugin.getWorkspace().getRoot().getFile(path_for_model); |
55 |
IProject project = containerForLocation.getProject(); |
56 |
|
57 |
FC2PFTransformer modelsTransform = ((PageflowEditor)fEditorPart).getModelsTransform(); |
58 |
FacesConfigType facesConfig = modelsTransform.getFacesConfig(); |
59 |
Object[] selectedJSF = ((IStructuredSelection)selection).toArray(); |
60 |
List<NavigationRuleType> result = new ArrayList<NavigationRuleType>(); |
61 |
|
62 |
for (Object object : selectedJSF) { |
63 |
PageflowNodeEditPart node = (PageflowNodeEditPart) object; |
64 |
result.addAll(getNavigation(facesConfig, (String) node.getPageflowElement().getFCElements().get(PageflowPackage.PF_PAGE__PATH))); |
65 |
} |
66 |
|
67 |
copyJsp(result, project, path); |
68 |
|
69 |
ResourceSet rset = new ResourceSetImpl(); |
70 |
Resource resource = rset.createResource(URI.createFileURI(path.append("faces-config-snippet.xml").toOSString())); //$NON-NLS-1$ |
71 |
resource.getContents().addAll(result); |
72 |
|
73 |
try { |
74 |
resource.save(null); |
75 |
} catch (IOException e) { |
76 |
e.printStackTrace(); |
77 |
} |
78 |
|
79 |
return null; |
80 |
} |
81 |
|
82 |
private void copyJsp(List<NavigationRuleType> navRules, IProject project, IPath savePath) { |
83 |
IPath webContentPath = getWebContentPath(project); |
84 |
List<IPath> filesToStore = new ArrayList<IPath>(); |
85 |
for (NavigationRuleType navigationRuleType : navRules) { |
86 |
String jspPath = navigationRuleType.getFromViewId().getTextContent(); |
87 |
IPath jsp = webContentPath.append(jspPath); |
88 |
filesToStore.add(jsp); |
89 |
} |
90 |
SnippetManager.getInstance().moveFilesInStorageFolder(savePath, filesToStore, project); |
91 |
} |
92 |
|
93 |
private List<NavigationRuleType> getNavigation(FacesConfigType facesConfig, String name) { |
94 |
EList navigationRule = facesConfig.getNavigationRule(); |
95 |
List<NavigationRuleType> result = new ArrayList<NavigationRuleType>(); |
96 |
for (Object object : navigationRule) { |
97 |
NavigationRuleType nrt = (NavigationRuleType) object; |
98 |
String textContent = nrt.getFromViewId().getTextContent(); |
99 |
if (name.equals(textContent)){ |
100 |
result.add(nrt); |
101 |
} |
102 |
} |
103 |
return result; |
104 |
} |
105 |
|
106 |
/** |
107 |
* @param project |
108 |
* @return Web Content path as saved in component properties. |
109 |
*/ |
110 |
public static IPath getWebContentPath(IProject project) { |
111 |
if (project != null) { |
112 |
return ComponentCore.createComponent(project).getRootFolder().getUnderlyingFolder().getProjectRelativePath(); |
113 |
} |
114 |
return null; |
115 |
} |
116 |
|
117 |
} |