Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

28: Display empty packages. #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
Expand All @@ -29,12 +28,18 @@
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

/**
* SWT Dialog element to select a package from the selected project.
Expand All @@ -44,12 +49,15 @@
public class PackageBrowserDialog extends Dialog
{

private Combo packages;
private Tree packages;
private Label error;

private IProject project;
private String path;
private IProject project;
private String path;

private String selectedPackage;
private String selectedPackage;

private static final int IMAGE_MARGIN = 2;

public PackageBrowserDialog(Shell parentShell)
{
Expand All @@ -64,17 +72,69 @@ protected Control createDialogArea(Composite parent)
layout.marginRight = 5;
layout.marginLeft = 10;
container.setLayout(layout);
packages = new Combo(container, SWT.READ_ONLY);
packages.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
packages = new Tree(container, SWT.VIRTUAL | SWT.BORDER);
packages.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
packages.addListener(SWT.MeasureItem, measureItemListener());
packages.addListener(SWT.PaintItem, paintItemListener());
packages.addListener(SWT.Selection, listener -> error.setText(""));
error = new Label(container, SWT.NONE);
error.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
error.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
fillPackageSelection();
return container;
}

@Override
protected void okPressed()
{
selectedPackage = packages.getItem(packages.getSelectionIndex());
super.okPressed();
TreeItem[] items = packages.getSelection();
if (items.length == 1) {
TreeItem item = items[0];
String text = item.getText();
if (item.getData() instanceof Image && text != null) {
selectedPackage = text;
super.okPressed();
}
}
error.setText("Please select a package!");
}

@Override
protected boolean isResizable()
{
return true;
}

/**
* @return {@link SWT#PaintItem} listener for the tree so that the default foreground can be
* augmented.
*/
private Listener paintItemListener()
{
return event -> {
TreeItem item = (TreeItem)event.item;
Image trailingImage = (Image)item.getData();
if (trailingImage != null) {
int itemHeight = packages.getItemHeight();
int imageHeight = trailingImage.getBounds().height;
int y = event.y + (itemHeight - imageHeight) / 2;
event.gc.drawImage(trailingImage, 1, y);
}
};
}

/**
* @return {@link SWT#MeasureItem} listener for the tree so that item sizes can be specified.
*/
private Listener measureItemListener()
{
return event -> {
TreeItem item = (TreeItem)event.item;
Image trailingImage = (Image)item.getData();
if (trailingImage != null) {
event.width += trailingImage.getBounds().width + IMAGE_MARGIN;
}
};
}

private void fillPackageSelection()
Expand All @@ -84,23 +144,46 @@ private void fillPackageSelection()
}
try {
IJavaProject javaProject = JavaCore.create(project);
IPackageFragmentRoot testDir = null;
testDir = getPackageRoot(javaProject.getPackageFragmentRoots(), path);
IPackageFragmentRoot testDir = getPackageRoot(javaProject.getPackageFragmentRoots(), path);
if (testDir == null) {
return;
}
IPackageFragment[] packs = getPackages(testDir.getChildren());
for (IPackageFragment pack : packs) {
String packageName = pack.getElementName();
if (packageName != null && !packageName.isEmpty()) {
packages.add(packageName);
if (packageName != null) {
TreeItem treePackage = new TreeItem(packages, 0);
treePackage.setText(packageName);
treePackage.setData(getPackageIcon(!pack.containsJavaResources()));
}
}
} catch (JavaModelException jme) {
Dialogs.error(jme);
}
}

/**
* Get the icon for the package tree.
*
* @param isEmpty Defines whether the package is empty or not.
* @return Different package icon for empty/non empty package.
*/
private Image getPackageIcon(boolean isEmpty)
{
String file = "/src/site/resources/images/";
if (isEmpty) {
file += "package_empty.png";
}
else {
file += "package.png";
}
Image image = new Image(Display.getDefault(), getClass().getResourceAsStream(file));
GC gc = new GC(image);
gc.drawImage(image, 16, 16);
gc.dispose();
return image;
}

/**
* Filters through an array of java elements (folders, packages, etc)
* and only returns packages, that contain classes.
Expand All @@ -113,30 +196,13 @@ private IPackageFragment[] getPackages(IJavaElement[] elements)
{
List<IPackageFragment> packageList = new ArrayList<>();
for (IJavaElement elem : elements) {
if (elem instanceof IPackageFragment && hasClasses((IPackageFragment)elem)) {
if (elem instanceof IPackageFragment && !((IPackageFragment)elem).hasSubpackages()) {
packageList.add((IPackageFragment)elem);
}
}
return packageList.toArray(new IPackageFragment[0]);
}

/**
* Looks for existing <code>.java</code> classes in the package.
*
* @param pack The java package to search.
* @return true when existing classes where found.
*/
private boolean hasClasses(IPackageFragment pack)
throws JavaModelException
{
for (IJavaElement elem : pack.getChildren()) {
if (elem instanceof ICompilationUnit) {
return true;
}
}
return false;
}

private IPackageFragmentRoot getPackageRoot(IPackageFragmentRoot[] roots, String path)
{
for (IPackageFragmentRoot root : roots) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.