You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@netbeans.apache.org by Marco Rossi <ma...@markreds.it> on 2020/07/27 15:27:24 UTC
Show label under icon in toolbar buttons
Hello,
In my RCP application I would like to get a toolbar that shows the action label under the icon of each button (similar to Mac OS applications).
I’ve found this post https://blogs.oracle.com/geertjan/custom-tabbed-toolbar-with-corporate-image-and-central-registry-integration <https://blogs.oracle.com/geertjan/custom-tabbed-toolbar-with-corporate-image-and-central-registry-integration> that I’ve adapted to my needs and it works except for that actions with a Presenter.Toolbar that is not a button (for example the QuickSearch and other custom actions similar to https://platform.netbeans.org/tutorials/70/nbm-google.html <https://platform.netbeans.org/tutorials/70/nbm-google.html>). In that cases I’m unable to show the components in the toolbar.
Maybe is there another way to achieve this?
Here is my code:
public abstract class ToolbarComponentProvider {
public abstract JComponent createToolbar();
public static ToolbarComponentProvider getDefault() {
ToolbarComponentProvider provider = Lookup.getDefault().lookup(ToolbarComponentProvider.class);
if (provider == null) {
provider = new DefaultToolbarComponentProvider();
}
return provider;
}
private static class DefaultToolbarComponentProvider extends ToolbarComponentProvider {
private Icon loadIconBaseWithExtension(String base, String extension, int size) {
String resourceID = base + size + extension;
Image image = ImageUtilities.loadImage(resourceID);
return (image != null) ? new ImageIcon(image) : null;
}
private JButton createButton(Action action) {
JButton button = new JButton(action);
button.setFocusable(false);
button.setBorderPainted(false);
button.setMargin(new java.awt.Insets(0, 2, 0, 2));
button.setText(Actions.cutAmpersand( (String) action.getValue(Action.NAME)));
String iconBase = (String) action.getValue("iconBase");
if (iconBase != null) {
int lastDot = iconBase.lastIndexOf('.');
int lastSlash = iconBase.lastIndexOf('/');
if ((lastSlash > lastDot) || (lastDot == -1)) { // no .extension
button.setIcon(loadIconBaseWithExtension(iconBase, "", 24));
} else {
String base = iconBase.substring(0, lastDot);
String ext = iconBase.substring(lastDot);
button.setIcon(loadIconBaseWithExtension(base, ext, 24));
}
button.setVerticalTextPosition(SwingConstants.BOTTOM);
button.setHorizontalTextPosition(SwingConstants.CENTER);
}
return button;
}
@Override
public JComponent createToolbar() {
// ToolbarPool.getDefault().waitFinished();
// return ToolbarPool.getDefault();
JPanel container = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
FileObject fo = FileUtil.getConfigFile("Toolbars");
for (FileObject parent : FileUtil.getOrder(Arrays.asList(fo.getChildren()), true)) {
if (parent.isFolder()) {
int count = 0;
JToolBar toolbar = new JToolBar(parent.getName());
for (FileObject child : FileUtil.getOrder(Arrays.asList(parent.getChildren()), true)) {
try {
DataObject dataObj = DataObject.find(child);
Object instanceObj;
InstanceCookie ic = dataObj.getLookup().lookup(InstanceCookie.class);
try {
instanceObj = ic.instanceCreate();
} catch (IOException | ClassNotFoundException ex) {
instanceObj = null;
}
if (instanceObj instanceof Action) {
Action action = (Action) instanceObj;
if (instanceObj instanceof Presenter.Toolbar) {
Component component = ( (Presenter.Toolbar) instanceObj).getToolbarPresenter();
if (component instanceof JPanel) {
// adding the component to toolbar but it never show
toolbar.add(component);
count++;
continue;
}
}
toolbar.add(createButton(action));
count++;
}
} catch (DataObjectNotFoundException ex) {
Logger.getLogger(ToolbarComponentProvider.class.getName()).severe(ex.getMessage());
}
}
if (count > 0) {
toolbar.add(new JSeparator(SwingConstants.VERTICAL));
toolbar.setFloatable(Boolean.TRUE.equals(parent.getAttribute("draggable")));
container.add(parent.getName(), toolbar);
}
}
}
return container;
}
}
}
Mark Reds