001: /*
002: * ToolBarBase.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.toolbar;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.Dimension;
027: import java.awt.Graphics;
028: import java.awt.Point;
029:
030: import java.beans.PropertyChangeEvent;
031: import java.beans.PropertyChangeListener;
032:
033: import javax.swing.JLayeredPane;
034: import javax.swing.SwingUtilities;
035: import javax.swing.UIManager;
036:
037: /* ----------------------------------------------------------
038: * CVS NOTE: Changes to the CVS repository prior to the
039: * release of version 3.0.0beta1 has meant a
040: * resetting of CVS revision numbers.
041: * ----------------------------------------------------------
042: */
043:
044: /**
045: *
046: * @author Takis Diakoumis
047: * @version $Revision: 1.4 $
048: * @date $Date: 2006/05/14 06:56:07 $
049: */
050: public class ToolBarBase extends JLayeredPane implements
051: PropertyChangeListener {
052:
053: private static Color highlight;
054: private ToolBarLayout toolBarLayout;
055:
056: public ToolBarBase(int initialRowSize) {
057: super ();
058: highlight = UIManager.getColor("ToolBar.highlight");
059: toolBarLayout = new ToolBarLayout(initialRowSize);
060: setLayout(toolBarLayout);
061: }
062:
063: public void addToolBar(ToolBar toolBar, int row, int position) {
064: add(toolBar, new ToolBarConstraints(row, position));
065: toolBar.addPropertyChangeListener(this );
066: }
067:
068: public void addToolBar(ToolBar toolBar, int row, int position,
069: int minimumWidth) {
070: add(toolBar,
071: new ToolBarConstraints(row, position, minimumWidth));
072: toolBar.addPropertyChangeListener(this );
073: }
074:
075: public void addToolBar(ToolBar toolBar, ToolBarConstraints tbc) {
076: add(toolBar, tbc);
077: toolBar.addPropertyChangeListener(this );
078: }
079:
080: public void addToolBar(ToolBar toolBar, int row, int position,
081: int minimumWidth, int preferredWidth) {
082: add(toolBar, new ToolBarConstraints(row, position,
083: minimumWidth, preferredWidth));
084: toolBar.addPropertyChangeListener(this );
085: }
086:
087: public void setRows(int rows) {
088: toolBarLayout.setRows(rows);
089: }
090:
091: public void toolBarMoved(ToolBar toolBar) {
092: Point point = toolBar.getLocation();
093: toolBarMoved(toolBar, point.x, point.y);
094: }
095:
096: public boolean rowAdded(ToolBar toolBar) {
097: boolean added = toolBarLayout.maybeAddRow(toolBar);
098:
099: if (added) {
100: repaint();
101: revalidate();
102: }
103:
104: return added;
105: }
106:
107: public void toolBarResized(ToolBar toolBar, int locX) {
108: toolBarLayout.componentResized(toolBar, locX);
109: }
110:
111: public void toolBarMoved(ToolBar toolBar, int locX, int locY) {
112: toolBarLayout.componentMoved(toolBar, locX, locY);
113: repaint();
114: revalidate();
115: }
116:
117: public void removeAll() {
118: toolBarLayout.removeComponents();
119: super .removeAll();
120: }
121:
122: public void propertyChange(PropertyChangeEvent e) {
123: String propertyName = e.getPropertyName();
124:
125: if (propertyName == ToolBar.TOOL_BAR_BEGIN_MOVING) {
126: ToolBar toolBar = null;
127: Component[] components = getComponents();
128:
129: for (int i = 0; i < components.length; i++) {
130: toolBar = (ToolBar) components[i];
131: toolBar.enableButtonsSelection(false);
132: }
133:
134: setLayer((ToolBar) e.getSource(), JLayeredPane.DRAG_LAYER
135: .intValue());
136: }
137:
138: else if (propertyName == ToolBar.TOOL_BAR_MOVING)
139: rowAdded((ToolBar) e.getSource());
140:
141: else if (propertyName == ToolBar.TOOL_BAR_MOVED) {
142: ToolBar toolBar = (ToolBar) e.getSource();
143: setLayer(toolBar, JLayeredPane.DEFAULT_LAYER.intValue());
144: toolBarMoved(toolBar);
145: }
146:
147: else if (propertyName == ToolBar.TOOL_BAR_RESIZING) {
148: ToolBar toolBar = (ToolBar) e.getSource();
149: toolBarResized(toolBar, Integer.parseInt(e.getNewValue()
150: .toString()));
151: validate();
152: }
153:
154: else if (propertyName == ToolBar.TOOL_BAR_DESELECTED) {
155:
156: SwingUtilities.invokeLater(new Runnable() {
157: public void run() {
158: ToolBar toolBar = null;
159: Component[] components = getComponents();
160:
161: for (int i = 0; i < components.length; i++) {
162: toolBar = (ToolBar) components[i];
163: toolBar.enableButtonsSelection(true);
164: ToolBarProperties.setToolBarConstraints(toolBar
165: .getName(), toolBarLayout
166: .getConstraint(toolBar));
167: }
168:
169: ToolBarProperties.saveTools();
170: }
171: });
172:
173: }
174:
175: }
176:
177: public void paintComponent(Graphics g) {
178: super .paintComponent(g);
179:
180: int height = getHeight();
181: int width = getWidth();
182:
183: g.setColor(highlight);
184: g.drawLine(0, height - 3, width, height - 3);
185: }
186:
187: }
|