001: /*
002: * ToolBar.java
003: *
004: * Copyright (C) 2000-2004 Peter Graves
005: * $Id: ToolBar.java,v 1.7 2004/05/25 01:19:11 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.Color;
025: import java.awt.Dimension;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import javax.swing.BorderFactory;
029: import javax.swing.JToolBar;
030: import org.xml.sax.Attributes;
031: import org.xml.sax.ContentHandler;
032: import org.xml.sax.InputSource;
033: import org.xml.sax.SAXException;
034: import org.xml.sax.XMLReader;
035: import org.xml.sax.helpers.DefaultHandler;
036: import org.xml.sax.helpers.XMLReaderFactory;
037:
038: public class ToolBar extends JToolBar implements ActionListener,
039: ToolBarConstants {
040: private static final int STYLE_DEFAULT = 0;
041: private static final int STYLE_TEXT_ONLY = 1;
042: private static final int STYLE_ICON_ONLY = 2;
043:
044: private static final Preferences preferences = Editor.preferences();
045:
046: protected Frame frame;
047: protected int style = STYLE_DEFAULT;
048:
049: public ToolBar(Frame frame) {
050: this (frame, STYLE_DEFAULT);
051: }
052:
053: public ToolBar(Frame frame, int style) {
054: this .frame = frame;
055: this .style = style;
056: setFloatable(false);
057: setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0,
058: Color.gray));
059: }
060:
061: public ToolBarButton addButton(String text, String iconFile,
062: String methodName) {
063: return addButton(text, iconFile, methodName, true);
064: }
065:
066: public ToolBarButton addButton(String text, String iconFile,
067: String methodName, boolean enabled) {
068: ToolBarButton button = new ToolBarButton(frame, methodName,
069: this );
070: switch (style) {
071: case STYLE_DEFAULT:
072: if (textEnabled())
073: button.setText(text);
074: else
075: button.setToolTipText(text);
076: if (iconsEnabled())
077: button.setIconFromFile(iconFile);
078: button.setHorizontalTextPosition(ToolBarButton.CENTER);
079: button.setVerticalTextPosition(ToolBarButton.BOTTOM);
080: break;
081: case STYLE_ICON_ONLY:
082: button.setIconFromFile(iconFile);
083: button.setToolTipText(text);
084: break;
085: case STYLE_TEXT_ONLY:
086: button.setText(text);
087: break;
088: }
089: button.setRolloverEnabled(isRolloverEnabled());
090: button.setEnabled(enabled);
091: add(button);
092: return button;
093: }
094:
095: public ToolBarButton maybeAddInboxButton() {
096: if (Editor.isMailEnabled())
097: if (preferences.getStringProperty(Property.INBOX) != null)
098: return addButton("Inbox", ICON_MAIL_INBOX, "inbox");
099: return null;
100: }
101:
102: public static final boolean isToolBarEnabled() {
103: return textEnabled() || iconsEnabled();
104: }
105:
106: private static final boolean textEnabled() {
107: // Defaults to true for j's default look and feel.
108: return preferences
109: .getBooleanProperty(Property.TOOL_BAR_SHOW_TEXT,
110: Editor.lookAndFeel == null);
111: }
112:
113: private static final boolean iconsEnabled() {
114: // Defaults to true in all cases.
115: return preferences
116: .getBooleanProperty(Property.TOOL_BAR_SHOW_ICONS);
117: }
118:
119: public static final boolean isRolloverEnabled() {
120: // Defaults to true for j's default look and feel.
121: return preferences.getBooleanProperty(
122: Property.TOOL_BAR_IS_ROLLOVER,
123: Editor.lookAndFeel == null);
124: }
125:
126: public void actionPerformed(ActionEvent e) {
127: final Editor editor = frame.getCurrentEditor();
128: editor.setFocusToDisplay();
129: editor.getDispatcher().actionPerformed(e);
130: }
131:
132: public static ToolBar createToolBar(Frame frame, File file) {
133: if (file == null)
134: return null;
135: if (!file.isFile())
136: return null;
137: XMLReader xmlReader = Utilities.getDefaultXMLReader();
138: if (xmlReader == null)
139: return null;
140: try {
141: ToolBar toolBar = new ToolBar(frame);
142: Handler handler = new Handler(toolBar);
143: xmlReader.setContentHandler(handler);
144: InputSource inputSource = new InputSource(file
145: .getInputStream());
146: xmlReader.parse(inputSource);
147: return toolBar;
148: } catch (Exception e) {
149: Log.error(e);
150: return null;
151: }
152: }
153:
154: private static class Handler extends DefaultHandler implements
155: ContentHandler {
156: private final ToolBar toolBar;
157:
158: public Handler(ToolBar toolBar) {
159: this .toolBar = toolBar;
160: }
161:
162: public void startElement(String uri, String localName,
163: String qName, Attributes attributes)
164: throws SAXException {
165: if (localName.equals("button") || qName.equals("button")) {
166: String label = attributes.getValue("", "label");
167: String icon = attributes.getValue("", "icon");
168: String command = attributes.getValue("", "command");
169: toolBar.addButton(label, icon, command);
170: } else if (localName.equals("separator")
171: || qName.equals("separator"))
172: toolBar.addSeparator();
173: }
174: }
175: }
|