KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > ToolBar


1 /*
2  * ToolBar.java
3  *
4  * Copyright (C) 2000-2004 Peter Graves
5  * $Id: ToolBar.java,v 1.7 2004/05/25 01:19:11 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.Color JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import javax.swing.BorderFactory JavaDoc;
29 import javax.swing.JToolBar JavaDoc;
30 import org.xml.sax.Attributes JavaDoc;
31 import org.xml.sax.ContentHandler JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34 import org.xml.sax.XMLReader JavaDoc;
35 import org.xml.sax.helpers.DefaultHandler JavaDoc;
36 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
37
38 public class ToolBar extends JToolBar JavaDoc implements ActionListener JavaDoc, ToolBarConstants
39 {
40     private static final int STYLE_DEFAULT = 0;
41     private static final int STYLE_TEXT_ONLY = 1;
42     private static final int STYLE_ICON_ONLY = 2;
43
44     private static final Preferences preferences = Editor.preferences();
45
46     protected Frame frame;
47     protected int style = STYLE_DEFAULT;
48
49     public ToolBar(Frame frame)
50     {
51         this(frame, STYLE_DEFAULT);
52     }
53
54     public ToolBar(Frame frame, int style)
55     {
56         this.frame = frame;
57         this.style = style;
58         setFloatable(false);
59         setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.gray));
60     }
61
62     public ToolBarButton addButton(String JavaDoc text, String JavaDoc iconFile, String JavaDoc methodName)
63     {
64         return addButton(text, iconFile, methodName, true);
65     }
66
67     public ToolBarButton addButton(String JavaDoc text, String JavaDoc iconFile, String JavaDoc methodName,
68                                    boolean enabled)
69     {
70         ToolBarButton button = new ToolBarButton(frame, methodName, this);
71         switch (style) {
72             case STYLE_DEFAULT:
73                 if (textEnabled())
74                     button.setText(text);
75                 else
76                     button.setToolTipText(text);
77                 if (iconsEnabled())
78                     button.setIconFromFile(iconFile);
79                 button.setHorizontalTextPosition(ToolBarButton.CENTER);
80                 button.setVerticalTextPosition(ToolBarButton.BOTTOM);
81                 break;
82             case STYLE_ICON_ONLY:
83                 button.setIconFromFile(iconFile);
84                 button.setToolTipText(text);
85                 break;
86             case STYLE_TEXT_ONLY:
87                 button.setText(text);
88                 break;
89         }
90         button.setRolloverEnabled(isRolloverEnabled());
91         button.setEnabled(enabled);
92         add(button);
93         return button;
94     }
95
96     public ToolBarButton maybeAddInboxButton()
97     {
98         if (Editor.isMailEnabled())
99             if (preferences.getStringProperty(Property.INBOX) != null)
100                 return addButton("Inbox", ICON_MAIL_INBOX, "inbox");
101         return null;
102     }
103
104     public static final boolean isToolBarEnabled()
105     {
106         return textEnabled() || iconsEnabled();
107     }
108
109     private static final boolean textEnabled()
110     {
111         // Defaults to true for j's default look and feel.
112
return preferences.getBooleanProperty(Property.TOOL_BAR_SHOW_TEXT,
113             Editor.lookAndFeel == null);
114     }
115
116     private static final boolean iconsEnabled()
117     {
118         // Defaults to true in all cases.
119
return preferences.getBooleanProperty(Property.TOOL_BAR_SHOW_ICONS);
120     }
121
122     public static final boolean isRolloverEnabled()
123     {
124         // Defaults to true for j's default look and feel.
125
return preferences.getBooleanProperty(Property.TOOL_BAR_IS_ROLLOVER,
126             Editor.lookAndFeel == null);
127     }
128
129     public void actionPerformed(ActionEvent JavaDoc e)
130     {
131         final Editor editor = frame.getCurrentEditor();
132         editor.setFocusToDisplay();
133         editor.getDispatcher().actionPerformed(e);
134     }
135
136     public static ToolBar createToolBar(Frame frame, File file)
137     {
138         if (file == null)
139             return null;
140         if (!file.isFile())
141             return null;
142         XMLReader JavaDoc xmlReader = Utilities.getDefaultXMLReader();
143         if (xmlReader == null)
144             return null;
145         try {
146             ToolBar toolBar = new ToolBar(frame);
147             Handler JavaDoc handler = new Handler JavaDoc(toolBar);
148             xmlReader.setContentHandler(handler);
149             InputSource JavaDoc inputSource = new InputSource JavaDoc(file.getInputStream());
150             xmlReader.parse(inputSource);
151             return toolBar;
152         }
153         catch (Exception JavaDoc e) {
154             Log.error(e);
155             return null;
156         }
157     }
158
159     private static class Handler extends DefaultHandler JavaDoc implements ContentHandler JavaDoc
160     {
161         private final ToolBar toolBar;
162
163         public Handler(ToolBar toolBar)
164         {
165             this.toolBar = toolBar;
166         }
167
168         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
169             Attributes JavaDoc attributes) throws SAXException JavaDoc
170         {
171             if (localName.equals("button") || qName.equals("button")) {
172                 String JavaDoc label = attributes.getValue("", "label");
173                 String JavaDoc icon = attributes.getValue("", "icon");
174                 String JavaDoc command = attributes.getValue("", "command");
175                 toolBar.addButton(label, icon, command);
176             } else if (localName.equals("separator") || qName.equals("separator"))
177                 toolBar.addSeparator();
178         }
179     }
180 }
181
Popular Tags