use of com.devonfw.cobigen.eclipse.wizard.common.model.stubs.IJavaElementStub in project cobigen by devonfw.
the class CheckStateListener method selectNewResources.
/**
* Sets all resources which will be created to be initially selected
*/
private void selectNewResources() {
CheckboxTreeViewer resourcesTree = this.page.getResourcesTree();
LinkedList<Object> worklist = Lists.newLinkedList(Arrays.asList(((SelectFileContentProvider) resourcesTree.getContentProvider()).getElements(resourcesTree.getInput())));
while (worklist.peek() != null) {
Object o = worklist.poll();
if (o instanceof IJavaElementStub || o instanceof IResourceStub || (o instanceof OffWorkspaceResourceTreeNode && !Files.exists(((OffWorkspaceResourceTreeNode) o).getAbsolutePath()))) {
resourcesTree.setChecked(o, true);
}
worklist.addAll(Arrays.asList(((SelectFileContentProvider) resourcesTree.getContentProvider()).getChildren(o)));
}
}
use of com.devonfw.cobigen.eclipse.wizard.common.model.stubs.IJavaElementStub in project cobigen by devonfw.
the class AbstractGenerateWizard method userConfirmed.
/**
* Checks whether files will be overwritten by the generation process and whether the user is aware of this behavior
* and confirms it
*
* @return true, if the user confirms the changes being made or no files will be overwritten<br>
* false, otherwise
*/
private boolean userConfirmed() {
LOG.info("Check for necessary user confirmation to be displayed.");
List<Object> diff = Lists.newArrayList(this.page1.getSelectedResources());
// Delete simulated resources
Iterator<Object> it = diff.iterator();
while (it.hasNext()) {
Object r = it.next();
if (r instanceof IResourceStub || r instanceof IJavaElementStub || (r instanceof OffWorkspaceResourceTreeNode && !Files.exists(((OffWorkspaceResourceTreeNode) r).getAbsolutePath()))) {
it.remove();
}
}
// Delete mergable files
it = diff.iterator();
while (it.hasNext()) {
Object resource = it.next();
String path = null;
if (resource instanceof IJavaElement) {
try {
path = ((IJavaElement) resource).getCorrespondingResource().getFullPath().toString();
} catch (JavaModelException e) {
LOG.error("An internal java model exception occured while retrieving the java elements '{}' corresponding resource.", ((IJavaElement) resource).getElementName(), e);
}
} else if (resource instanceof IResource) {
path = ((IResource) resource).getFullPath().toString();
} else if (resource instanceof OffWorkspaceResourceTreeNode) {
path = ((OffWorkspaceResourceTreeNode) resource).getAbsolutePathStr();
}
if (path != null && this.cobigenWrapper.isMergableFile(path, this.page1.getSelectedIncrements())) {
it.remove();
}
}
if (!diff.isEmpty()) {
LOG.info("Opening dialog for user confirmation... waiting for user interaction.");
MessageDialog dialog = new MessageDialog(getShell(), "Warning!", null, "You have selected resources that are already existent and will be overwritten when proceeding.\nDo you really want to replace the existing files by newly generated ones?", MessageDialog.WARNING, new String[] { "Yes", "No" }, 1);
int result = dialog.open();
LOG.info("Got user input. Continue processing...");
if (result == 1 || result == SWT.DEFAULT) {
LOG.info("Finish user confirmation checking: user indicates to not override existing files.");
return false;
}
}
LOG.info("Finish user confirmation checking.");
return true;
}
use of com.devonfw.cobigen.eclipse.wizard.common.model.stubs.IJavaElementStub in project cobigen by devonfw.
the class SelectFileContentProvider method stubNonExistentChildren.
/**
* Stubs all non existent Packages, which are selected to be generated and returns all stubbed packages
*
* @param parentElement parent {@link IJavaElement} to retrieve the children from
* @param considerPackages states whether packages should be considered when retrieving the children. This also
* includes recursively retrieving children of packages
* @return List of {@link IPackageFragment}s, which will be stubbed
* @throws JavaModelException if an internal exception occurs while accessing the eclipse jdt java model
*/
private List<Object> stubNonExistentChildren(Object parentElement, boolean considerPackages) throws JavaModelException {
// Default package handling
if (parentElement instanceof IPackageFragment && ((IPackageFragment) parentElement).isDefaultPackage()) {
// a default package cannot have packages as children
return Lists.newArrayList();
}
List<Object> stubbedChildren = new LinkedList<>();
String debugInfo = null;
if (parentElement instanceof IJavaElement) {
List<String> nonExistentChildren = getNonExistentChildren(((IJavaElement) parentElement).getPath());
for (String path : nonExistentChildren) {
// check inclusion and exclusion patterns to stub the correct elements
if (!JavaClasspathUtil.isCompiledSource((IJavaElement) parentElement, path)) {
stubNonExistentChildren(((IJavaElement) parentElement).getCorrespondingResource(), stubbedChildren);
continue;
}
IPath elementpath = new Path(path);
IJavaElementStub javaElementStub;
if (targetIsFile(elementpath, nonExistentChildren)) {
// If the file is not a direct child of the parent, we will skip it
IPath p = elementpath.removeFirstSegments(((IJavaElement) parentElement).getPath().segmentCount());
if (p.segmentCount() != 1) {
continue;
} else if (this._cachedProvidedResources.containsKey(path) && !stubbedChildren.contains(this._cachedProvidedResources.get(path))) {
// if already seen, just get it and skip creation
stubbedChildren.add(this._cachedProvidedResources.get(path));
continue;
}
// Create CompilationUnit Stub
javaElementStub = new ICompilationUnitStub();
javaElementStub.setElementType(IJavaElement.COMPILATION_UNIT);
debugInfo = "COMPILATION_UNIT";
javaElementStub.setElementName(elementpath.lastSegment());
javaElementStub.setPath(elementpath);
} else {
// it
if (!isDefinedInSourceFolder(path) || !considerPackages) {
continue;
} else if (this._cachedProvidedResources.containsKey(path) && !stubbedChildren.contains(this._cachedProvidedResources.get(path))) {
// if already seen, just get it and skip creation
stubbedChildren.add(this._cachedProvidedResources.get(path));
continue;
}
// Create PackageFragment Stub
javaElementStub = new IPackageFragmentStub();
javaElementStub.setElementType(IJavaElement.PACKAGE_FRAGMENT);
debugInfo = "PACKAGE_FRAGMENT";
((IPackageFragmentStub) javaElementStub).setNonJavaResources(new Object[0]);
javaElementStub.setPath(elementpath);
IJavaElement parent = determineJavaModelParent((IJavaElement) parentElement, javaElementStub);
IPath packagePath = elementpath.removeFirstSegments(parent.getPath().segmentCount());
packagePath.removeTrailingSeparator();
String elementName = packagePath.toString().replaceAll("/", ".");
javaElementStub.setElementName(elementName);
}
if (((IJavaElement) parentElement) instanceof IPackageFragment) {
javaElementStub.setParent(((IJavaElement) parentElement).getParent());
} else {
javaElementStub.setParent(determineJavaModelParent((IJavaElement) parentElement, javaElementStub));
}
IJavaElement[] javaChildren = new IJavaElement[0];
if (considerPackages) {
Object[] childrenArr = stubNonExistentChildren(javaElementStub, false).toArray();
javaChildren = Arrays.copyOf(childrenArr, childrenArr.length, IJavaElement[].class);
}
javaElementStub.setChildren(javaChildren);
if (!stubbedChildren.contains(javaElementStub)) {
stubbedChildren.add(javaElementStub);
this._cachedProvidedResources.put(javaElementStub.getPath().toString(), javaElementStub);
LOG.debug("Stub created for {} with element name '{}' and path '{}'", debugInfo, javaElementStub.getElementName(), javaElementStub.getPath().toString());
}
}
// forget any stubbed file #151/#137
if (parentElement instanceof IPackageFragmentRoot) {
List<IPackageFragment> allExistingPackageFragments = HierarchicalTreeOperator.retrievePackageChildren((IPackageFragmentRoot) parentElement, Lists.newArrayListWithCapacity(0));
for (IPackageFragment frag : allExistingPackageFragments) {
List<Object> recStubbedChildren = stubNonExistentChildren(frag, considerPackages);
for (Object c : recStubbedChildren) {
if (!stubbedChildren.contains(c)) {
stubbedChildren.add(c);
}
}
}
}
} else if (parentElement instanceof IResource) {
stubNonExistentChildren((IResource) parentElement, stubbedChildren);
}
return stubbedChildren;
}
Aggregations