001: /*
002: * DefaultToolBarManager.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.util.Collections;
025: import java.util.HashMap;
026: import java.util.Vector;
027: import org.underworldlabs.Constants;
028: import org.underworldlabs.swing.RolloverButton;
029: import org.underworldlabs.swing.actions.ActionBuilder;
030:
031: /* ----------------------------------------------------------
032: * CVS NOTE: Changes to the CVS repository prior to the
033: * release of version 3.0.0beta1 has meant a
034: * resetting of CVS revision numbers.
035: * ----------------------------------------------------------
036: */
037:
038: /**
039: * Tool bar manager class for managing and creating
040: * tool bars and associated components.
041: *
042: * @author Takis Diakoumis
043: * @version $Revision: 1.6 $
044: * @date $Date: 2006/06/25 13:18:43 $
045: */
046: public class DefaultToolBarManager {
047:
048: /** All tool bars added */
049: private HashMap toolBars;
050:
051: /** The tool bar base panel */
052: private ToolBarBase toolBarBase;
053:
054: /** The button comparator */
055: private ButtonComparator buttonComparator;
056:
057: /**
058: * Creates a new instance of DefaultToolBarManager.
059: * The toolsConfigPath variable may be null and is usually
060: * a user defined/modified file system path differing from the
061: * defaults specified as a package resource XML path.
062: *
063: * @param toolsConfigPath - the user XML conf file path
064: * @param defaultToolsResourcePath - the default XML conf resource file
065: */
066: public DefaultToolBarManager(String toolsConfigPath,
067: String defaultToolsResourcePath) {
068: ToolBarProperties.init(toolsConfigPath,
069: defaultToolsResourcePath);
070:
071: // initialise the tool bar cache
072: toolBars = new HashMap();
073:
074: // initialise the tools base panel
075: toolBarBase = new ToolBarBase(ToolBarProperties
076: .getNextToolbarRow());
077:
078: // button comparator for sorting
079: buttonComparator = new ButtonComparator();
080: }
081:
082: /**
083: * Returns the tool bar base panel.
084: */
085: public ToolBarBase getToolBarBasePanel() {
086: return toolBarBase;
087: }
088:
089: /**
090: * Resets the state of the base tool bar panel.
091: */
092: protected void reset() {
093: toolBarBase.removeAll();
094: toolBarBase.setRows(ToolBarProperties.getNextToolbarRow());
095: }
096:
097: /**
098: * Repaints and revalidates the tool bar base following a change to
099: * its structure.
100: */
101: protected void fireToolbarsChanged() {
102: toolBarBase.repaint();
103: toolBarBase.revalidate();
104: }
105:
106: /**
107: * Builds (or rebuilds) the tool bar with the specified name
108: * and adds it to the tool bar base panel and local cache.
109: *
110: * @param name - the name of the tool bar as it appears in the
111: * XML tool bar conf file
112: * @param rebuild - whether this is a rebuild of an existing tool bar
113: */
114: protected void buildToolBar(String name, boolean rebuild) {
115: ToolBarWrapper eqtb = ToolBarProperties.getToolBar(name);
116:
117: if (!eqtb.isVisible() || !eqtb.hasButtons()) {
118: return;
119: }
120:
121: ToolBar toolBar = null;
122: if (rebuild) {
123: toolBar = (ToolBar) toolBars.get(name);
124:
125: if (toolBar != null) {
126: toolBar.removeAllButtons();
127: toolBar.invalidate();
128: } else {
129: toolBar = new ToolBar(toolBarBase, name);
130: toolBars.put(name, toolBar);
131: }
132:
133: } else {
134: toolBar = new ToolBar(toolBarBase, name);
135: toolBars.put(name, toolBar);
136: }
137:
138: Vector buttons = eqtb.getButtonsVector();
139: Collections.sort(buttons, buttonComparator);
140:
141: int count = 0;
142: RolloverButton button;
143:
144: for (int i = 0, k = buttons.size(); i < k; i++) {
145: ToolBarButton tb = (ToolBarButton) buttons.get(i);
146: if (!tb.isVisible()) {
147: continue;
148: }
149:
150: if (tb.isSeparator()) {
151: toolBar.addSeparator();
152: } else {
153: button = new RolloverButton(ActionBuilder.get(tb
154: .getActionId()), tb.getName());
155: button.setText(Constants.EMPTY);
156: toolBar.addButton(button);
157: }
158:
159: }
160:
161: toolBar.buildToolBar();
162: toolBarBase.addToolBar(toolBar, eqtb.getConstraints());
163:
164: }
165:
166: }
|