/*******************************************************************************
* Copyright (c) 2006-2007 Nicolas Richeton.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors :
* Nicolas Richeton (nicolas.richeton@gmail.com) - initial implementation
*******************************************************************************/
package org.eclipse.nebula.snippets.gallery;
import org.eclipse.nebula.widgets.gallery.DefaultGalleryGroupRenderer;
import org.eclipse.nebula.widgets.gallery.DefaultGalleryItemRenderer;
import org.eclipse.nebula.widgets.gallery.Gallery;
import org.eclipse.nebula.widgets.gallery.GalleryItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* This widget displays a simple gallery with some content.<br/> Scrolling is
* vertical.<br/><br/>
*
* <p>
* NOTE: THIS WIDGET AND ITS API ARE STILL UNDER DEVELOPMENT. THIS IS A
* PRE-RELEASE ALPHA VERSION. USERS SHOULD EXPECT API CHANGES IN FUTURE
* VERSIONS.
* </p>
*
* @author Nicolas Richeton (nicolas.richeton@gmail.com)
*/
public class SnippetRemove {
public static void main(String[] args) {
Display display = new Display();
Image itemImage = new Image(display, Program.findProgram("jpg") //$NON-NLS-1$
.getImageData());
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Gallery gallery = new Gallery(shell, SWT.V_SCROLL | SWT.MULTI);
// Renderers
DefaultGalleryGroupRenderer gr = new DefaultGalleryGroupRenderer();
gr.setMinMargin(2);
gr.setItemHeight(56);
gr.setItemWidth(72);
gr.setAutoMargin(true);
gallery.setGroupRenderer(gr);
DefaultGalleryItemRenderer ir = new DefaultGalleryItemRenderer();
gallery.setItemRenderer(ir);
for (int g = 0; g < 2; g++) {
GalleryItem group = new GalleryItem(gallery, SWT.NONE);
group.setText("Group " + g); //$NON-NLS-1$
group.setExpanded(true);
for (int i = 0; i < 50; i++) {
GalleryItem item = new GalleryItem(group, SWT.NONE);
if (itemImage != null) {
item.setImage(itemImage);
}
item.setText("Item " + i); //$NON-NLS-1$
}
}
// Double click remove the first selected item.
gallery.addMouseListener(new MouseListener() {
public void mouseDoubleClick(MouseEvent e) {
GalleryItem[] selection = gallery.getSelection();
if (selection == null)
return;
GalleryItem item = selection[0];
GalleryItem parent = item.getParentItem();
if (parent != null) {
parent.remove(parent.indexOf(item));
} else {
gallery.remove(gallery.indexOf(item));
}
}
public void mouseDown(MouseEvent e) {
// Do nothing
}
public void mouseUp(MouseEvent e) {
// Do nothing
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
if (itemImage != null)
itemImage.dispose();
display.dispose();
}
}