Last active
August 29, 2015 14:14
-
-
Save fipro78/665a7512de0f15623d1d to your computer and use it in GitHub Desktop.
NatTable example with custom scrollbars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
import javax.annotation.PostConstruct; | |
import org.eclipse.jface.layout.GridDataFactory; | |
import org.eclipse.jface.layout.GridLayoutFactory; | |
import org.eclipse.nebula.widgets.nattable.NatTable; | |
import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor; | |
import org.eclipse.nebula.widgets.nattable.data.IDataProvider; | |
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider; | |
import org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor; | |
import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider; | |
import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultGridLayer; | |
import org.eclipse.nebula.widgets.nattable.style.theme.DarkNatTableThemeConfiguration; | |
import org.eclipse.nebula.widgets.nattable.util.GUIHelper; | |
import org.eclipse.nebula.widgets.nattable.viewport.IScroller; | |
import org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer; | |
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.layout.GridData; | |
import org.eclipse.swt.widgets.Composite; | |
import org.eclipse.swt.widgets.Event; | |
import org.eclipse.swt.widgets.Listener; | |
import com.codeaffine.eclipse.swt.widget.scrollbar.FlatScrollBar; | |
import com.vogella.nebula.nattable.model.Person; | |
import com.vogella.nebula.nattable.model.PersonService; | |
public class NatTableCustomScrollbarExamplePart { | |
@PostConstruct | |
public void postConstruct(Composite parent) { | |
// property names of the Person class | |
String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday" }; | |
// mapping from property to label, needed for column header labels | |
Map<String, String> propertyToLabelMap = new HashMap<String, String>(); | |
propertyToLabelMap.put("firstName", "Firstname"); | |
propertyToLabelMap.put("lastName", "Lastname"); | |
propertyToLabelMap.put("gender", "Gender"); | |
propertyToLabelMap.put("married", "Married"); | |
propertyToLabelMap.put("birthday", "Birthday"); | |
// Person model and PersonService can be found at | |
// GitHub Gist http://goo.gl/ATFJ6c | |
IColumnPropertyAccessor<Person> columnPropertyAccessor = | |
new ReflectiveColumnPropertyAccessor<Person>(propertyNames); | |
IDataProvider bodyDataProvider = | |
new ListDataProvider<Person>( | |
PersonService.getPersons(20), | |
columnPropertyAccessor); | |
DefaultGridLayer gridLayer = new DefaultGridLayer( | |
bodyDataProvider, | |
new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap)); | |
ViewportLayer viewportLayer = gridLayer.getBodyLayer().getViewportLayer(); | |
// NatTable and scrollbar container | |
Composite container = new Composite(parent, SWT.NONE); | |
GridLayoutFactory.swtDefaults().numColumns(2).margins(0, 0).spacing(0, 0).applyTo(container); | |
// NatTable as main control | |
NatTable natTable = new NatTable(container, gridLayer); | |
GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable); | |
// vertical scrollbar wrapped in another composite for layout | |
Composite verticalComposite = new Composite(container, SWT.NONE); | |
GridLayoutFactory.swtDefaults().margins(0, 0).spacing(0, 0).applyTo(verticalComposite); | |
GridData verticalData = GridDataFactory.swtDefaults().hint(14, SWT.DEFAULT).align(SWT.BEGINNING, SWT.FILL).grab(false, true).create(); | |
verticalComposite.setLayoutData(verticalData); | |
FlatScrollBar vertical = new FlatScrollBar(verticalComposite, SWT.VERTICAL); | |
GridDataFactory.fillDefaults().grab(true, true).applyTo(vertical); | |
// horizontal scrollbar wrapped in another composite for layout | |
Composite horizontalComposite = new Composite(container, SWT.NONE); | |
GridLayoutFactory.swtDefaults().margins(0, 0).spacing(0, 0).applyTo(horizontalComposite); | |
GridData horizontalData = GridDataFactory.swtDefaults().hint(SWT.DEFAULT, 14).align(SWT.FILL, SWT.BEGINNING).grab(true, false).create(); | |
horizontalComposite.setLayoutData(horizontalData); | |
FlatScrollBar horizontal = new FlatScrollBar(horizontalComposite, SWT.HORIZONTAL); | |
GridDataFactory.fillDefaults().grab(true, true).applyTo(horizontal); | |
// create the vertical scroller | |
FlatScrollBarScroller verticalScroller = new FlatScrollBarScroller(vertical); | |
// register the hide/show listener | |
verticalScroller.addListener(SWT.Hide, new Listener() { | |
@Override | |
public void handleEvent(Event event) { | |
GridDataFactory.createFrom(verticalData).exclude(true).applyTo(verticalComposite); | |
GridDataFactory.createFrom(horizontalData).span(2, 1).applyTo(horizontalComposite); | |
} | |
}); | |
verticalScroller.addListener(SWT.Show, new Listener() { | |
@Override | |
public void handleEvent(Event event) { | |
verticalComposite.setLayoutData(verticalData); | |
horizontalComposite.setLayoutData(horizontalData); | |
} | |
}); | |
// create the horizontal scroller | |
FlatScrollBarScroller horizontalScroller = new FlatScrollBarScroller(horizontal); | |
// register the hide/show listener | |
horizontalScroller.addListener(SWT.Hide, new Listener() { | |
@Override | |
public void handleEvent(Event event) { | |
GridDataFactory.createFrom(verticalData).span(1, 2).applyTo(verticalComposite); | |
GridDataFactory.createFrom(horizontalData).exclude(true).applyTo(horizontalComposite); | |
} | |
}); | |
horizontalScroller.addListener(SWT.Show, new Listener() { | |
@Override | |
public void handleEvent(Event event) { | |
verticalComposite.setLayoutData(verticalData); | |
horizontalComposite.setLayoutData(horizontalData); | |
} | |
}); | |
// set the custom IScroller to the ViewportLayer | |
viewportLayer.setVerticalScroller(verticalScroller); | |
viewportLayer.setHorizontalScroller(horizontalScroller); | |
// set a dark background to the wrapper container | |
container.setBackground(GUIHelper.COLOR_BLACK); | |
// set a dark styling to the scrollbars | |
vertical.setBackground(GUIHelper.COLOR_BLACK); | |
vertical.setPageIncrementColor(GUIHelper.COLOR_BLACK); | |
vertical.setThumbColor(GUIHelper.COLOR_DARK_GRAY); | |
horizontal.setBackground(GUIHelper.COLOR_BLACK); | |
horizontal.setPageIncrementColor(GUIHelper.COLOR_BLACK); | |
horizontal.setThumbColor(GUIHelper.COLOR_DARK_GRAY); | |
// set a dark styling to NatTable | |
natTable.setBackground(GUIHelper.COLOR_BLACK); | |
natTable.setTheme(new DarkNatTableThemeConfiguration()); | |
} | |
class FlatScrollBarScroller implements IScroller<FlatScrollBar> { | |
private FlatScrollBar scrollBar; | |
public FlatScrollBarScroller(FlatScrollBar scrollBar) { | |
this.scrollBar = scrollBar; | |
} | |
@Override | |
public FlatScrollBar getUnderlying() { | |
return scrollBar; | |
} | |
@Override | |
public boolean isDisposed() { | |
return scrollBar.isDisposed(); | |
} | |
@Override | |
public void addListener(int eventType, Listener listener) { | |
scrollBar.addListener(eventType, listener); | |
} | |
@Override | |
public void removeListener(int eventType, Listener listener) { | |
scrollBar.removeListener(eventType, listener); | |
} | |
@Override | |
public int getSelection() { | |
return scrollBar.getSelection(); | |
} | |
@Override | |
public void setSelection(int value) { | |
scrollBar.setSelection(value); | |
} | |
@Override | |
public int getMaximum() { | |
return scrollBar.getMaximum(); | |
} | |
@Override | |
public void setMaximum(int value) { | |
scrollBar.setMaximum(value); | |
} | |
@Override | |
public int getPageIncrement() { | |
return scrollBar.getPageIncrement(); | |
} | |
@Override | |
public void setPageIncrement(int value) { | |
scrollBar.setPageIncrement(value); | |
} | |
@Override | |
public int getThumb() { | |
return scrollBar.getThumb(); | |
} | |
@Override | |
public void setThumb(int value) { | |
scrollBar.setThumb(value); | |
} | |
@Override | |
public int getIncrement() { | |
return scrollBar.getIncrement(); | |
} | |
@Override | |
public void setIncrement(int value) { | |
scrollBar.setIncrement(value); | |
} | |
@Override | |
public boolean getEnabled() { | |
return scrollBar.getEnabled(); | |
} | |
@Override | |
public void setEnabled(boolean b) { | |
scrollBar.setEnabled(b); | |
} | |
@Override | |
public boolean getVisible() { | |
return scrollBar.getVisible(); | |
} | |
@Override | |
public void setVisible(boolean b) { | |
scrollBar.setVisible(b); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment