nl.cloudfarming.client.geoviewer.jxmap.layerlist.LayerListFoldedButton Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geoviewer-jxmap Show documentation
Show all versions of geoviewer-jxmap Show documentation
AgroSense geoviewer JXMap implementation. Contains a map/geoviewer TopComponent based on the JXMap classes from swingx.
The newest version!
/**
* Copyright (C) 2008-2012 AgroSense Foundation.
*
* AgroSense is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
* this software, see the FLOSS License Exception
* .
*
* AgroSense is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AgroSense. If not, see .
*/
package nl.cloudfarming.client.geoviewer.jxmap.layerlist;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.BeanInfo;
import java.beans.PropertyVetoException;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingConstants;
import nl.cloudfarming.client.geoviewer.Geographical;
import nl.cloudfarming.client.geoviewer.event.GeoEvent;
import org.openide.explorer.ExplorerManager;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.Utilities;
/**
*
* @author Frantisek Post
*/
public class LayerListFoldedButton extends JButton {
public static final int BUTTON_WIDTH = 48;
public static final Dimension BUTTON_SIZE = new Dimension(BUTTON_WIDTH, BUTTON_WIDTH);
public static final Insets BUTTON_MARGIN = new Insets(1, 2, 1, 2);
private Node node;
private ExplorerManager explorerManager;
public LayerListFoldedButton(Node node, ExplorerManager explorerManager) {
super();
this.node = node;
this.explorerManager = explorerManager;
init();
}
private void init() {
setBorderPainted(true);
setIconTextGap(0);
setVerticalTextPosition(SwingConstants.BOTTOM);
setHorizontalTextPosition(SwingConstants.CENTER);
setFocusable(false);
setMargin(BUTTON_MARGIN);
setMinimumSize(BUTTON_SIZE);
setMaximumSize(BUTTON_SIZE);
setRolloverEnabled(true);
Font font = getFont();
setFont(font.deriveFont(font.getSize2D() - 1f)); //smaller
if (node != null) {
setText(node.getDisplayName());
setToolTipText(node.getDisplayName());
setIcon(new ImageIcon(node.getIcon(BeanInfo.ICON_COLOR_32x32)));
}
updateUI();
addMouseListener(new MouseAdapter() {
private JPopupMenu popup;
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
doPopup();
}
}
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
doPopup();
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (!isPopupShowing() && selectNode()) {
// trigger goto action
Geographical geographical = node.getLookup().lookup(Geographical.class);
if (geographical != null){
GeoEvent.getProducer().triggerEvent(GeoEvent.REQUEST_FOCUS, geographical);
}
}
}
private boolean selectNode() {
try {
explorerManager.setSelectedNodes(new Node[]{node});
} catch (PropertyVetoException ex) {
Exceptions.printStackTrace(ex);
return false;
}
return true;
}
private boolean isPopupShowing() {
return popup != null && popup.isShowing();
}
private void doPopup() {
selectNode();
Action[] actions = node.getActions(true);
popup = Utilities.actionsToPopup(actions, LayerListFoldedButton.this);
popup = addTitleToPopup(popup, node.getDisplayName());
popup.show(LayerListFoldedButton.this, getWidth(), 0);
}
});
}
@Override
public void updateUI() {
setUI(LayerListFoldedButtonUI.createUI(this));
}
private JPopupMenu addTitleToPopup(JPopupMenu menu, String title) {
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem titleItem = new JMenuItem(title);
Font font = titleItem.getFont();
titleItem.setFont(font.deriveFont(Font.BOLD).deriveFont(font.getSize2D() + 1.5f)); //bold and bigger
titleItem.setRolloverEnabled(false);
titleItem.setFocusable(false);
popupMenu.add(titleItem);
popupMenu.add(new JPopupMenu.Separator());
for (Component c : menu.getComponents()) {
popupMenu.add(c);
}
return popupMenu;
}
}