1/* 2 3 Licensed to the Apache Software Foundation (ASF) under one or more 4 contributor license agreements. See the NOTICE file distributed with 5 this work for additional information regarding copyright ownership. 6 The ASF licenses this file to You under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with 8 the License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 18*/ 19 20package components; 21 22import java.util.Iterator; 23import java.util.List; 24import java.util.MissingResourceException; 25import java.util.ResourceBundle; 26 27import javax.swing.JButton; 28import javax.swing.JToolBar; 29 30import org.apache.batik.util.resources.ResourceFormatException; 31import org.apache.batik.util.resources.ResourceManager; 32 33/** 34 * This class represents a tool bar factory which builds 35 * tool bars from the content of a resource file. <br> 36 * 37 * The resource entries format is (for a tool bar named 'ToolBar'):<br> 38 * <pre> 39 * ToolBar = Item1 Item2 - Item3 ... 40 * See ButtonFactory.java for details about the items 41 * ... 42 * '-' represents a separator 43 * </pre> 44 * All entries are optional. 45 * 46 * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a> 47 * @version $Id: ToolBarFactory.java,v 1.1 2013/02/21 11:24:35 jschimpf Exp $ 48 */ 49public class ToolBarFactory extends ResourceManager { 50 // Constants 51 // 52 private static final String SEPARATOR = "-"; 53 54 /** 55 * The button factory 56 */ 57 private ButtonFactory buttonFactory; 58 59 /** 60 * Creates a new tool bar factory 61 * @param rb the resource bundle that contains the menu bar 62 * description. 63 * @param am the actions to add to menu items 64 */ 65 public ToolBarFactory(ResourceBundle rb, ActionMap am) { 66 super(rb); 67 buttonFactory = new ButtonFactory(rb, am); 68 } 69 70 /** 71 * Creates a tool bar 72 * @param name the name of the menu bar in the resource bundle 73 * @throws MissingResourceException if one of the keys that compose the 74 * tool bar is missing. 75 * It is not thrown if the action key is missing. 76 * @throws ResourceFormatException if a boolean is malformed 77 * @throws MissingListenerException if an item action is not found in the 78 * action map. 79 */ 80 public JToolBar createJToolBar(String name) 81 throws MissingResourceException, 82 ResourceFormatException, 83 MissingListenerException { 84 JToolBar result = new JToolBar(); 85 List buttons = getStringList(name); 86 Iterator it = buttons.iterator(); 87 88 while (it.hasNext()) { 89 String s = (String)it.next(); 90 if (s.equals(SEPARATOR)) { 91 result.add(new JToolbarSeparator()); 92 } else { 93 result.add(createJButton(s)); 94 } 95 } 96 return result; 97 } 98 99 /** 100 * Creates and returns a new swing button 101 * @param name the name of the button in the resource bundle 102 * @throws MissingResourceException if key is not the name of a button. 103 * It is not thrown if the mnemonic and the action keys are missing 104 * @throws ResourceFormatException if the mnemonic is not a single 105 * character 106 * @throws MissingListenerException if the button action is not found in 107 * the action map. 108 */ 109 public JButton createJButton(String name) 110 throws MissingResourceException, 111 ResourceFormatException, 112 MissingListenerException { 113 return buttonFactory.createJToolbarButton(name); 114 } 115} 116