Skip to content

Commit

Permalink
Limit number of dashboards shown in to toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
buchen committed May 27, 2018
1 parent a24dd03 commit ce70f0d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ public abstract class AbstractDropDown implements IMenuListener

public AbstractDropDown(ToolBar toolBar, String label)
{
this(toolBar, label, null, SWT.DROP_DOWN);
this(toolBar, label, null, SWT.DROP_DOWN, toolBar.getItemCount());
}

public AbstractDropDown(ToolBar toolBar, String label, Image image, int style)
{
this(toolBar, label, image, style, toolBar.getItemCount());
}

public AbstractDropDown(ToolBar toolBar, String label, Image image, int style, int index)
{
this.toolBar = toolBar;

dropdown = new ToolItem(toolBar, style);
dropdown = new ToolItem(toolBar, style, index);

if (image != null)
{
Expand Down Expand Up @@ -59,7 +64,13 @@ public void widgetSelected(SelectionEvent e)
public static final AbstractDropDown create(ToolBar toolBar, String label, Image image, int style,
IMenuListener listener)
{
return new AbstractDropDown(toolBar, label, image, style)
return create(toolBar, label, image, style, toolBar.getItemCount(), listener);
}

public static final AbstractDropDown create(ToolBar toolBar, String label, Image image, int style, int index,
IMenuListener listener)
{
return new AbstractDropDown(toolBar, label, image, style, index)
{
@Override
public void menuAboutToShow(IMenuManager manager)
Expand All @@ -72,7 +83,7 @@ public void menuAboutToShow(IMenuManager manager)
public static final AbstractDropDown create(ToolBar toolBar, String label, Image image, int style,
BiConsumer<AbstractDropDown, IMenuManager> listener)
{
return new AbstractDropDown(toolBar, label, image, style)
return new AbstractDropDown(toolBar, label, image, style, toolBar.getItemCount())
{
@Override
public void menuAboutToShow(IMenuManager manager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,45 +233,75 @@ protected void addButtons(ToolBar toolBar)

private void createDashboardToolItems()
{
int[] index = new int[] { 0 };
int index = 0;

getClient().getDashboards().forEach(board -> {
ToolItem item = new ToolItem(toolBar, SWT.DROP_DOWN, index[0]++);
item.setImage(board.equals(dashboard) ? Images.DASHBOARD_SELECTED.image() : Images.DASHBOARD.image());
item.setText(board.getName());
item.setData(board);
boolean includesSelected = false;

item.addSelectionListener(SelectionListener.widgetSelectedAdapter(event -> {
if (event.detail == SWT.ARROW)
{
Rectangle rect = item.getBounds();
Point pt = item.getParent().toDisplay(new Point(rect.x, rect.y));
Dashboard[] dashboards = getClient().getDashboards().toArray(Dashboard[]::new);
for (Dashboard board : dashboards)
{
if (index >= 4) // # of dashboards shown by default
break;

MenuManager manager = new MenuManager("#PopupMenu"); //$NON-NLS-1$
includesSelected = includesSelected || board.equals(dashboard);

if (!board.equals(dashboard))
{
manager.add(new SimpleAction(Messages.MenuShow, a -> selectDashboard(board)));
manager.add(new Separator());
}
createToolItem(index++, board);
}

manager.add(new SimpleAction(Messages.ConfigurationDuplicate, a -> createNewDashboard(board)));
manager.add(new SimpleAction(Messages.ConfigurationRename, a -> renameDashboard(board)));
manager.add(new SimpleAction(Messages.ConfigurationDelete, a -> deleteDashboard(board)));
if (!includesSelected && dashboard != null)
createToolItem(index++, dashboard);

Menu menu = manager.createContextMenu(toolBar);
menu.setLocation(pt.x, pt.y + rect.height);
menu.setVisible(true);
if (index < dashboards.length)
{
AbstractDropDown dropdown = AbstractDropDown.create(toolBar, Messages.MenuConfigureDashboards,
Images.SAVE.image(), SWT.NONE, index, manager -> getClient().getDashboards().forEach(d -> {
Action action = new SimpleAction(d.getName(), a -> selectDashboard(d));
action.setChecked(d.equals(dashboard));
manager.add(action);
}));

// attach one dashboard to the tool item so that the tool item is
// removed before re-creation
dropdown.getToolItem().setData(dashboards[0]);
}
}

item.addDisposeListener(e -> menu.dispose());
}
else
private void createToolItem(int index, Dashboard board)
{
ToolItem item = new ToolItem(toolBar, SWT.DROP_DOWN, index);
item.setImage(board.equals(dashboard) ? Images.DASHBOARD_SELECTED.image() : Images.DASHBOARD.image());
item.setText(board.getName());
item.setData(board);

item.addSelectionListener(SelectionListener.widgetSelectedAdapter(event -> {
if (event.detail == SWT.ARROW)
{
Rectangle rect = item.getBounds();
Point pt = item.getParent().toDisplay(new Point(rect.x, rect.y));

MenuManager manager = new MenuManager("#PopupMenu"); //$NON-NLS-1$

if (!board.equals(dashboard))
{
selectDashboard(board);
item.setImage(Images.DASHBOARD_SELECTED.image());
manager.add(new SimpleAction(Messages.MenuShow, a -> selectDashboard(board)));
manager.add(new Separator());
}
}));
});

manager.add(new SimpleAction(Messages.ConfigurationDuplicate, a -> createNewDashboard(board)));
manager.add(new SimpleAction(Messages.ConfigurationRename, a -> renameDashboard(board)));
manager.add(new SimpleAction(Messages.ConfigurationDelete, a -> deleteDashboard(board)));

Menu menu = manager.createContextMenu(toolBar);
menu.setLocation(pt.x, pt.y + rect.height);
menu.setVisible(true);

item.addDisposeListener(e -> menu.dispose());
}
else
{
selectDashboard(board);
}
}));
}

private void recreateDashboardToolItems()
Expand Down Expand Up @@ -488,12 +518,7 @@ private void selectDashboard(Dashboard board)
this.dashboard = board;
updateTitle(board.getName());

for (ToolItem item : toolBar.getItems())
{
if (item.getData() instanceof Dashboard)
item.setImage(board.equals(item.getData()) ? Images.DASHBOARD_SELECTED.image()
: Images.DASHBOARD.image());
}
recreateDashboardToolItems();

for (Control column : container.getChildren())
column.dispose();
Expand Down Expand Up @@ -523,8 +548,6 @@ private void createNewDashboard(Dashboard template)
getClient().addDashboard(newDashboard);
markDirty();

recreateDashboardToolItems();

selectDashboard(newDashboard);
}

Expand Down Expand Up @@ -554,7 +577,6 @@ private void deleteDashboard(Dashboard board)
Dashboard newDashboard = createDefaultDashboard();
getClient().addDashboard(newDashboard);
markDirty();
recreateDashboardToolItems();
return newDashboard;
}));
}
Expand Down

0 comments on commit ce70f0d

Please sign in to comment.