blob: 32cfc767370c4d23ddb9439242ad4e5ef7fa9068 [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqDropdownMenuAddToolbar
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqDropdownMenuAddToolbar
:description: Apache NetBeans wiki DevFaqDropdownMenuAddToolbar
:toc: left
:toc-title:
:syntax: true
=== How do I add a drop-down menu to a toolbar button?
To add a drop-down menu to a component in a toolbar, you can either extend `CallableSystemAction` and override
`public Component getToolbarPresenter()`,
or implement `javax.swing.Action` or any subclass thereof, and implement `link:http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/actions/Presenter.Toolbar.html[Presenter.Toolbar]`
which defines that method.
You might want to create a `JToggleButton`, and when the button is pressed, show a `JPopupMenu`. (Also try `org.openide.awt.DropDownButtonFactory`.)
Example:
[source,java]
----
public class PickDrawingLineAction extends CallableSystemAction {
private static JToggleButton toggleButton;
private static ButtonGroup buttonGroup;
private static JPopupMenu popup;
private MyMenuItemListener menuItemListener;
List handledCharts;
public void performAction() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
toggleButton.setSelected(true);
}
});
}
public String getName() {
return "Pick Drawing Line";
}
public HelpCtx getHelpCtx() {
return HelpCtx.DEFAULT_HELP;
}
protected boolean asynchronous() {
return false;
}
public Component getToolbarPresenter() {
Image iconImage = Utilities.loadImage(
"org/blogtrader/platform/core/netbeans/resources/drawingLine.png");
ImageIcon icon = new ImageIcon(iconImage);
toggleButton = new JToggleButton();
toggleButton.setIcon(icon);
toggleButton.setToolTipText("Pick Drawing Line");
popup = new JPopupMenu();
menuItemListener = new MyMenuItemListener();
handledCharts = PersistenceManager.getDefalut()
.getAllAvailableHandledChart();
buttonGroup = new ButtonGroup();
for (AbstractHandledChart handledChart : handledCharts) {
JRadioButtonMenuItem item =
new JRadioButtonMenuItem(handledChart.toString());
item.addActionListener(menuItemListener);
buttonGroup.add(item);
popup.add(item);
}
toggleButton.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int state = e.getStateChange();
if (state == ItemEvent.SELECTED) {
/** show popup menu on toggleButton at position: (0, height) */
popup.show(toggleButton, 0, toggleButton.getHeight());
}
}
});
popup.addPopupMenuListener(new PopupMenuListener() {
public void popupMenuCanceled(PopupMenuEvent e) {
toggleButton.setSelected(false);
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
toggleButton.setSelected(false);
}
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
}
});
return toggleButton;
}
private class MyMenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
JMenuItem item = (JMenuItem)ev.getSource();
String selectedStr = item.getText();
AnalysisChartTopComponent analysisTc =
AnalysisChartTopComponent.getSelected();
if (analysisTc == null) {
return;
}
AbstractChartViewContainer viewContainer =
analysisTc.getSelectedViewContainer();
AbstractChartView masterView = viewContainer.getMasterView();
if (!(masterView instanceof WithDrawingPart)) {
return;
}
DrawingPart drawingPart =
((WithDrawingPart)masterView).getCurrentDrawing();
if (drawingPart == null) {
JOptionPane.showMessageDialog(
WindowManager.getDefault().getMainWindow(),
"Please add a layer firstly to pick line type",
"Pick line type",
JOptionPane.OK_OPTION,
null);
return;
}
AbstractHandledChart selectedHandledChart = null;
for (AbstractHandledChart handledChart : handledCharts) {
if (handledChart.toString().equalsIgnoreCase(selectedStr)) {
selectedHandledChart = handledChart;
break;
}
}
if (selectedHandledChart == null) {
return;
}
AbstractHandledChart handledChart =
selectedHandledChart.createNewInstance();
handledChart.setPart(drawingPart);
drawingPart.setHandledChart(handledChart);
Series masterSeries = viewContainer.getMasterSeries();
DrawingDescriptor description =
viewContainer.getDescriptors().findDrawingDescriptor(
drawingPart.getLayerName(),
masterSeries.getUnit(),
masterSeries.getNUnits());
if (description != null) {
Node stockNode = analysisTc.getActivatedNodes()[0];
Node node =
stockNode.getChildren()
.findChild(DescriptorGroupNode.DRAWINGS)
.getChildren().findChild(description.getDisplayName());
if (node != null) {
ViewAction action =
(ViewAction)node.getLookup().lookup(ViewAction.class);
assert action != null :
"view action of this layer's node is null!";
action.view();
}
} else {
/** best effort, should not happen */
viewContainer.setCursorCrossVisible(false);
drawingPart.setActived(true);
SwitchHideShowDrawingLineAction.updateToolbar(viewContainer);
}
}
}
}
----
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqDropdownMenuAddToolbar[http://wiki.netbeans.org/DevFaqDropdownMenuAddToolbar] ,
that was last modified by NetBeans user Jtulach
on 2010-07-24T20:33:44Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.