📄 toolpanel.java
字号:
// **********************************************************************// // <copyright>// // BBN Technologies// 10 Moulton Street// Cambridge, MA 02138// (617) 873-8000// // Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/gui/ToolPanel.java,v $// $RCSfile: ToolPanel.java,v $// $Revision: 1.8.2.4 $// $Date: 2005/05/24 18:38:26 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.gui;import javax.swing.*;import java.awt.event.*;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.Properties;import java.util.List;import java.awt.*;import java.beans.*;import java.beans.beancontext.*;import com.bbn.openmap.PropertyConsumer;import com.bbn.openmap.util.Debug;import com.bbn.openmap.util.PropUtils;/** * Represents the toolbar containing tools to apply to the map. Tools * can be added in sequential order, and retrieved using the tool's * keyword. NOTE: Every time a string is passed into a method of this * class, the interned version of it is used as a key. * <P> * * When the ToolPanel is part of the BeanContext, it looks for Tools * that have also been added to the BeanContext. If there is more than * one ToolPanel in a BeanContext at a time, both will show the same * Tool faces. The 'components' property can be used to control which * tools can be added to a specific instance of a ToolPanel. That * property should contain a space separated list of prefixes used for * Tools, which in turn should be set in the Tools as their keys. * * @see Tool * @author john gash */public class ToolPanel extends JToolBar implements BeanContextChild, BeanContextMembershipListener, MapPanelChild, PropertyConsumer, ComponentListener { /** The set of tools contained on the toolbar. */ protected Hashtable items = new Hashtable(); /** * A flag to note whether the ToolPanel inserts spaces between * tools. */ protected boolean autoSpace = false; /** * BeanContextChildSupport object provides helper functions for * BeanContextChild interface. */ private BeanContextChildSupport beanContextChildSupport = new BeanContextChildSupport(this); /** * The property prefix used for this ToolPanel. */ protected String propertyPrefix = null; /** * A list of components to use for filtering tools found in the * MapHandler to add to this ToolPanel. */ public final static String ComponentsProperty = "components"; /** * A list of components to use for filtering out tools found in * the MapHandler. Components added to this list will NOT be added * to this ToolPanel. */ public final static String AvoidComponentsProperty = "avoid"; /** * A filter list of components to look for and add. */ protected List componentList = null; /** * A filter list of components to avoid. */ protected List avoidList = null; protected GridBagLayout gridbag = new GridBagLayout(); protected GridBagConstraints c = new GridBagConstraints(); /** * Holder that expands in the GridBagLayout, keeping things pushed * to the left side of the toolpanel. */ protected JLabel filler = null; /** * Constructor */ public ToolPanel() { setLayout(gridbag); setFloatable(false); setVisible(false); } /** * Add an item to the tool bar. * * @param key The key associated with the item. * @param item The Tool to add. */ public void add(String key, Tool item) { add(key, item, -1); } /** * A little array used to track what indexes are already used, to * prevent the GridBagLayout from placing things on top of each * other. */ protected boolean[] usedIndexes; public int MAX_INDEXES = 101; /** * Provides the next available component index for placement, * starting at 0. */ protected int getNextAvailableIndex() { return getNextAvailableIndex(0); } /** * Provides the next available component index for placement, * given a starting index. */ protected int getNextAvailableIndex(int startAt) { if (usedIndexes == null) { usedIndexes = new boolean[MAX_INDEXES]; } if (startAt < 0) startAt = 0; if (startAt >= MAX_INDEXES) startAt = MAX_INDEXES - 1; int i = startAt; // Find the first unused for (; i < MAX_INDEXES && usedIndexes[i] == true; i++) { } usedIndexes[i] = true; return i; } /** * Add an item to the tool bar. * * @param key The key associated with the item. * @param item The Tool to add. * @param index The position index for placement of the tool. -1 * puts it at the end, and if the position is greater than * the size, it is placed at the end. This class does not * remember where items were asked to be placed, so later * additions may mess up intended order. */ public void add(String key, Tool item, int index) { Container face = item.getFace(); if (face != null) { face.addComponentListener(this); items.put(key.intern(), item); if (autoSpace) { index *= 2; } c.gridy = 0; c.weightx = 0; c.anchor = GridBagConstraints.WEST; c.gridx = getNextAvailableIndex(index); gridbag.setConstraints(face, c); add(face); if (filler == null) { c.gridx = getNextAvailableIndex(MAX_INDEXES); c.anchor = GridBagConstraints.EAST; c.weightx = 1; filler = new JLabel(""); gridbag.setConstraints(filler, c); add(filler); } if (autoSpace) { JLabel l = new JLabel(" "); gridbag.setConstraints(l, c); add(l); } } setVisibility(); } /** * Add an item to the tool bar. Assumes that the key will be * picked out of the Tool. * * @param item The Tool to add. */ public void add(Tool item) { add(item, -1); } /** * Add an item to the tool bar. Assumes that the key will be * picked out of the Tool. * * @param item The Tool to add. * @param index The position to add the Tool. -1 will add it to * the end. */ public void add(Tool item, int index) { try { add(item.getKey(), item, index); } catch (NullPointerException npe) { if (item != null) { Debug.error("ToolPanel.add(): no name for " + item.getClass().getName()); npe.printStackTrace(); } else { Debug.error("ToolPanel.add(): no name for null tool."); } } } /** * Get an item from the tool bar. * * @param key The key associated with the item. * @return The tool associated with the key, null if not found. */ public Tool get(String key) { return (Tool) items.get(key.intern()); } /** Remove a tool with the right key */ public void remove(String key) { Tool tool = (Tool) items.remove(key.intern()); if (tool != null) { remove(tool.getFace()); tool.getFace().removeComponentListener(this); } } /** Add a space between tools. */ protected void addSpace() { add(new JLabel(" ")); } /** Set whether spaces are placed between tools. */ public void setAutoSpace(boolean set) { autoSpace = set; } /** * BorderLayout.NORTH by default for this class. */ protected String preferredLocation = java.awt.BorderLayout.NORTH; /** * MapPanelChild method. */ public void setPreferredLocation(String value) { preferredLocation = value; } /** MapPanelChild method. */ public String getPreferredLocation() { return preferredLocation; } /** Find out whether spaces are being placed between tools. */ public boolean isAutoSpace() { return autoSpace; } /** * Set the list of strings used by the ToolPanel to figure out * which Tools should be added (in the findAndInit()) method and * where they should go. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -