1 21 22 package org.armedbear.j; 23 24 import java.awt.Color ; 25 import java.awt.Dimension ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import javax.swing.BorderFactory ; 29 import javax.swing.JToolBar ; 30 import org.xml.sax.Attributes ; 31 import org.xml.sax.ContentHandler ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 import org.xml.sax.XMLReader ; 35 import org.xml.sax.helpers.DefaultHandler ; 36 import org.xml.sax.helpers.XMLReaderFactory ; 37 38 public class ToolBar extends JToolBar implements ActionListener , 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 text, String iconFile, String methodName) 63 { 64 return addButton(text, iconFile, methodName, true); 65 } 66 67 public ToolBarButton addButton(String text, String iconFile, String 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 return preferences.getBooleanProperty(Property.TOOL_BAR_SHOW_TEXT, 113 Editor.lookAndFeel == null); 114 } 115 116 private static final boolean iconsEnabled() 117 { 118 return preferences.getBooleanProperty(Property.TOOL_BAR_SHOW_ICONS); 120 } 121 122 public static final boolean isRolloverEnabled() 123 { 124 return preferences.getBooleanProperty(Property.TOOL_BAR_IS_ROLLOVER, 126 Editor.lookAndFeel == null); 127 } 128 129 public void actionPerformed(ActionEvent 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 xmlReader = Utilities.getDefaultXMLReader(); 143 if (xmlReader == null) 144 return null; 145 try { 146 ToolBar toolBar = new ToolBar(frame); 147 Handler handler = new Handler (toolBar); 148 xmlReader.setContentHandler(handler); 149 InputSource inputSource = new InputSource (file.getInputStream()); 150 xmlReader.parse(inputSource); 151 return toolBar; 152 } 153 catch (Exception e) { 154 Log.error(e); 155 return null; 156 } 157 } 158 159 private static class Handler extends DefaultHandler implements ContentHandler 160 { 161 private final ToolBar toolBar; 162 163 public Handler(ToolBar toolBar) 164 { 165 this.toolBar = toolBar; 166 } 167 168 public void startElement(String uri, String localName, String qName, 169 Attributes attributes) throws SAXException 170 { 171 if (localName.equals("button") || qName.equals("button")) { 172 String label = attributes.getValue("", "label"); 173 String icon = attributes.getValue("", "icon"); 174 String 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 |