dcsimg
How to avoid the excess dots in TreeViewer while loading more than one image?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sundaramoorthy_subramanian
Posted On:   Wednesday, April 11, 2007 10:48 AM

When i tried to load more than one images in TreeViewer by using subclass of LabelProvider class (through getImage(Object element)) of eclipse framework, i am getting the number of dots increased in front of the images based on the size of the images.I doesn't want the number of dots to get increased. scenarios: Tree viewer with 1 image - ... (3 dots) Tree viewer with 2 images- ......(6 dots) Tree viewer with 3 images- .........(9 dots) can anybody help me to solve this problem? source code.. import java.io.File; import java.util.Arrays; import org.eclipse.jface.resource.CompositeImageDescriptor; import org.eclipse.jface.resource.   More>>


			
When i tried to load more than one images in TreeViewer by using subclass of LabelProvider class (through getImage(Object element)) of eclipse framework, i am getting the number of dots increased in front of the images based on the size of the images.I doesn't want the number of dots to get increased.

scenarios:

Tree viewer with 1 image - ... (3 dots)
Tree viewer with 2 images- ......(6 dots)
Tree viewer with 3 images- .........(9 dots)

can anybody help me to solve this problem?

source code..

			
import java.io.File;
import java.util.Arrays;

import org.eclipse.jface.resource.CompositeImageDescriptor;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;


/**
* This view simply mirrors the current selection in the workbench window.
* It works for both, element and text selection.
*/
public class TreeViewSample extends ViewPart {



public void createPartControl(Composite parent) {
TreeViewer tv = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
tv.setContentProvider(new TreeContentProvider());
tv.setLabelProvider(new TreeLabelProvider());
tv.setInput("root");

}


public void dispose() {
super.dispose();
}



class TreeLabelProvider extends LabelProvider {
@Override
public Image getImage(Object element) {

ImageDescriptor imgDesc = PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_ELEMENT);
// Image img = new Merger(new ImageDescriptor[] { imgDesc, imgDesc, imgDesc }).createImage();
// Image img = new Merger(new ImageDescriptor[] { imgDesc, imgDesc }).createImage();
Image img = new Merger(new ImageDescriptor[] { imgDesc }).createImage();
return img;

}

@Override
public String getText(Object element) {
String text = ((File) element).getName();

if (text.length() == 0) {
text = ((File) element).getPath();
}
return text;
}



@Override
public boolean isLabelProperty(Object element, String property) {
return true;
}



}
class TreeContentProvider implements ITreeContentProvider {

public Object[] getChildren(Object arg0) {
return ((File) arg0).listFiles();
}

public Object getParent(Object arg0) {
// Return this file's parent file
return ((File) arg0).getParentFile();
}

public boolean hasChildren(Object arg0) {
// Get the children
Object[] obj = getChildren(arg0);

// Return whether the parent has children
return obj == null ? false : obj.length > 0;
}

/**
* Gets the root element(s) of the tree
*
* @param arg0
* the input data
* @return Object[]
*/
public Object[] getElements(Object arg0) {
// These are the root elements of the tree
// We don't care what arg0 is, because we just want all
// the root nodes in the file system
return File.listRoots();
}

/**
* Disposes any created resources
*/
public void dispose() {
// Nothing to dispose
}

/**
* Called when the input changes
*
* @param arg0
* the viewer
* @param arg1
* the old input
* @param arg2
* the new input
*/
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// Nothing to change
}
}
class Merger extends CompositeImageDescriptor {
private ImageDescriptor[] pieces;

private Point size;

public Merger(ImageDescriptor[] pieces) {
super();
this.pieces = pieces;
}

@Override
protected void drawCompositeImage(int width, int height) {
getSize(); // make sure it is loaded

int x = 0;
for (ImageDescriptor d : pieces) {
ImageData data = d.getImageData();
drawImage(data, x, (size.y - data.height));
x += data.width;
}
}

@Override
protected synchronized Point getSize() {
if (size == null) {
int width = 0;
int height = 0;
for (ImageDescriptor d : pieces) {
ImageData data = d.getImageData();
width = width + data.width;
height = Math.max(height, data.height);
}
this.size = new Point(width, height);
}
return size;
}

@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + Arrays.hashCode(pieces);
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Merger other = (Merger) obj;
if (!Arrays.equals(pieces, other.pieces))
return false;
return true;
}
}
@Override
public void setFocus() {
// TODO Auto-generated method stub

}

}


   <<Less
About | Sitemap | Contact