Programmazione:Java/Eclipse RCP/Utilizzo della CoolBar
Da WikiSitech.
Vai alla navigazioneVai alla ricercaIndice
CoolBarManager
Questa è una classe di esempio per la gestione della CoolBar.
NB: per essere il più sintetico possibile questo esempio non segue le best practice ^^
Definizione classe
public class CoolBarManager {
FormToolkit ft;
public CoolBarManager() {
ft = new FormToolkit(PlatformUI.getWorkbench().getDisplay());
}
public void create(Composite parent) {
// Expanded function after....
}
protected void setCoolItemSize(final CoolItem coolItem) {
// Expanded function after....
}
protected String shortenText(String textValue, ToolItem item) {
// Expanded function after....
}
protected int getMaxWidth(Image image) {
return image.getBounds().width * 5;
}
}
Funzione di creazione della CoolBar
public void create(Composite parent) {
// Create Coolbar composite container
Composite bar = ft.createComposite(parent);
bar.setLayout(new FormLayout());
// Create CoolBar control
CoolBar coolbar = new CoolBar(bar, SWT.FLAT);
fd = new FormData();
fd.top = new FormAttachment(0, 0);
fd.left = new FormAttachment(0, 0);
fd.right = new FormAttachment(100, 0);
coolbar.setLayoutData(fd);
// Create ToolBar control
ToolBar tb = new ToolBar(coolbar, SWT.HORIZONTAL | SWT.RIGHT);
tb.setFont(JFaceResources.getFont("org.eclipse.ui.smallFont")); // set small eclipse font as toolbar font
// Create CoolItem control
CoolItem ci = new CoolItem(coolbar, SWT.DROP_DOWN);
ci.setControl(tb);
ci.addSelectionListener(new CoolItemSelectionListener());
parent.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
setCoolItemSize(ci);
}
});
setCoolItemSize(ci);
}
Funzione di calcolo della dimensione del CoolItem
protected void setCoolItemSize(final CoolItem coolItem) {
// there is no coolItem when the bar is on the left
int rowHeight = 0;
ToolItem[] toolItems = tb.getItems();
for (int i = 0; i < toolItems.length; i++) {
rowHeight = Math.max(rowHeight, toolItems[i].getBounds().height);
}
Rectangle area = coolbar.getClientArea();
int rows = rowHeight <= 0 ? 1 : (int) Math.max(1, Math.floor(area.height / rowHeight));
if (rows == 1 || (tb.getStyle() & SWT.WRAP) == 0) {
Point p = tb.computeSize(SWT.DEFAULT, SWT.DEFAULT);
coolItem.setSize(coolItem.computeSize(p.x, p.y));
return;
}
Point offset = coolItem.computeSize(0, 0);
Point wrappedSize = tb.computeSize(area.width - offset.x, SWT.DEFAULT);
int h = rows * rowHeight;
int w = wrappedSize.y <= h ? wrappedSize.x : wrappedSize.x + 1;
coolItem.setSize(coolItem.computeSize(w, h));
}
Funzione di troncamento delle descrizioni troppo lunghe
protected String shortenText(String textValue, ToolItem item) {
if (textValue == null || item == null || item.isDisposed()) {
return null;
}
String returnText = textValue;
String originalText = textValue;
GC gc = new GC(item.getParent());
int maxWidth = getMaxWidth(item.getImage());
if (gc.textExtent(textValue).x >= maxWidth) {
for (int i = textValue.length(); i > 0; i--) {
String test = textValue.substring(0, i);
test = test + ellipsis;
if (gc.textExtent(test).x < maxWidth) {
returnText = test;
break;
}
}
if (item.getToolTipText() == null) {
item.setToolTipText(originalText);
}
}
gc.dispose();
return returnText;
}
class CoolItemSelectionListener extends SelectionAdapter {
private Menu menu = null;
public void widgetSelected(SelectionEvent event) {
/**
* A selection event will be fired when the cool item is selected by
* its gripper or if the drop down arrow (or 'chevron') is selected.
* Examine the event detail to determine where the widget was
* selected.
*/
if (event.detail == SWT.ARROW) {
/*
* If the popup menu is already up (i.e. user pressed arrow
* twice), then dispose it.
*/
if (menu != null) {
menu.dispose();
menu = null;
return;
}
/*
* Get the cool item and convert its bounds to display
* coordinates.
*/
CoolItem coolItem = (CoolItem) event.widget;
Rectangle itemBounds = coolItem.getBounds();
itemBounds.width = event.x - itemBounds.x;
Point pt = coolbar.toDisplay(new Point(itemBounds.x, itemBounds.y));
itemBounds.x = pt.x;
itemBounds.y = pt.y;
/* Get the toolbar from the cool item. */
ToolBar toolBar = (ToolBar) coolItem.getControl();
ToolItem[] tools = toolBar.getItems();
int toolCount = tools.length;
/*
* Convert the bounds of each tool item to display coordinates,
* and determine which ones are past the bounds of the cool
* item.
*/
int i = 0;
while (i < toolCount) {
Rectangle toolBounds = tools[i].getBounds();
pt = toolBar.toDisplay(new Point(toolBounds.x, toolBounds.y));
toolBounds.x = pt.x;
toolBounds.y = pt.y;
Rectangle intersection = itemBounds.intersection(toolBounds);
if (!intersection.equals(toolBounds)) {
break;
}
i++;
}
/*
* Create a pop-up menu with items for each of the hidden
* buttons.
*/
menu = new Menu(coolbar);
for (int j = i; j < toolCount; j++) {
ToolItem tool = tools[j];
MenuItem menuItem = new MenuItem(menu, SWT.NONE);
menuItem.setImage(tool.getImage());
String text = tool.getToolTipText();
if (text != null) {
menuItem.setText(text);
}
menuItem.setData(tool.getData());
menuItem.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
processButton(event);
}
});
}
/*
* Display the pop-up menu at the lower left corner of the arrow
* button. Dispose the menu when the user is done with it.
*/
pt = coolbar.toDisplay(new Point(event.x, event.y));
menu.setLocation(pt.x, pt.y);
menu.setVisible(true);
while (menu != null && !menu.isDisposed() && menu.isVisible()) {
if (!PlatformUI.getWorkbench().getDisplay().readAndDispatch()) {
PlatformUI.getWorkbench().getDisplay().sleep();
}
}
if (menu != null) {
menu.dispose();
menu = null;
}
}
}
}