1 /* 2 * $Id$ 3 * 4 * Copyright (c) 2006, 2009, Oracle and/or its affiliates. All rights reserved. 5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 * 7 * This code is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 only, as 9 * published by the Free Software Foundation. Oracle designates this 10 * particular file as subject to the "Classpath" exception as provided 11 * by Oracle in the LICENSE file that accompanied this code. 12 * 13 * This code is distributed in the hope that it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 * version 2 for more details (a copy is included in the LICENSE file that 17 * accompanied this code). 18 * 19 * You should have received a copy of the GNU General Public License version 20 * 2 along with this work; if not, write to the Free Software Foundation, 21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 24 * or visit www.oracle.com if you need additional information or have any 25 * questions. 26 */ 27 package com.sun.javatest.exec; 28 29 import com.sun.javatest.tool.Preferences; 30 import java.awt.Color; 31 import java.awt.Component; 32 import java.awt.Graphics; 33 import java.awt.Insets; 34 import java.util.HashMap; 35 import java.util.Map; 36 import java.util.ResourceBundle; 37 import javax.swing.JComponent; 38 import javax.swing.JToolBar; 39 import javax.swing.border.BevelBorder; 40 41 /** 42 * The custom toolbar. 43 */ 44 public class JavaTestToolBar extends JToolBar { 45 46 /** 47 * Creates using specified ResourceBundle a new tool bar with 48 * the specified menu controlled state. 49 * @param bundle a resource bundle used to obtain the resources for the toolbar. 50 * @param resourceID String ID should be unique within the instance of the test suite. 51 * The ID is never visible to the user. The ID should be a short, 52 * alphanumeric (Latin) string. It is automatically used to retrieve the toolbar 53 * name and description from the resource bundle. 54 * Can't be null and empty string. 55 * @param menuControlled - true to make View/Toolbars/menu for the toolbar. 56 **/ 57 public JavaTestToolBar(ResourceBundle bundle, String resourceID, boolean menuControlled) { 58 super(); 59 theBundle = bundle; 60 id = resourceID; 61 inMenu = menuControlled; 62 setUpStyle(); 63 } 64 65 /** 66 * Creates using specified ResourceBundle a new menu controlled tool bar. 67 * @param bundle a resource bundle used to obtain the resources for the toolbar. 68 * @param resourceID String ID should be unique within the instance of the test suite. 69 * The ID is never visible to the user. The ID should be a short, 70 * alphanumeric (Latin) string. It is automatically used to retrieve the toolbar 71 * name and description from the resource bundle. 72 * Can't be null and empty string. 73 **/ 74 public JavaTestToolBar(ResourceBundle bundle, String resourceID) { 75 this(bundle, resourceID, true); 76 } 77 78 /** 79 * Get the identification string for this toolbar. 80 * @return the string ID for the tool bar. 81 **/ 82 public String getId() { 83 return id; 84 } 85 86 87 /** 88 * Get the long description of this toolbar's purpose. 89 * May be multiple sentences if desired. This is automatically retrieved from 90 * the supplied resource bundle by combining it with the toolbar ID (getId()), 91 * e.g. it will try to retrieve getId().tb.desc from the resource bundle. 92 * @return the long description for the tool bar. 93 **/ 94 public String getDescription() { 95 return theBundle.getString(getId() + ".tb.desc" ); 96 } 97 98 /** 99 * Get the short name of this toolbar. Would be used in places such as a toolbar 100 * selector drop-down for the user, so it should be kept to one or two words. 101 * This is automatically retrieved from the supplied resource bundle by 102 * combining it with the toolbar ID (getId()), e.g. it will try to retrieve 103 * getId().tb.name from the resource bundle. 104 * @return the short for the tool bar. 105 **/ 106 public String getName() { 107 return theBundle.getString(getId() + ".tb.name" ); 108 } 109 110 /** 111 * Determines whether this tool bar should be controlled from view menu. 112 * @return true if this tool bar is menu controlled. 113 **/ 114 public boolean isMenuControlled() { 115 return inMenu; 116 } 117 118 /** 119 * Shows or hides this component depending on the value of parameter visible. 120 * visible - true to make the component visible; false to make it invisible 121 **/ 122 public void setVisible(boolean visible) { 123 super.setVisible(visible); 124 saveVisibleState(visible); 125 } 126 127 void setVisibleNoStateAffect(boolean visible) { 128 super.setVisible(visible); 129 } 130 131 /** 132 * Reads toolbar visible state from user preferences 133 **/ 134 boolean readVisibleState() { 135 Object o = state.get(USER_TOOLBAR_PREF + getId()); 136 if (o instanceof String) { 137 return "true".equals(o); 138 } 139 return true; 140 } 141 142 /** 143 * Stores toolbar visible state as user preferences 144 **/ 145 void saveVisibleState(boolean visible) { 146 state.put(USER_TOOLBAR_PREF + getId(), Boolean.toString(visible)); 147 } 148 149 void save(Map map) { 150 map.putAll(state); 151 } 152 153 /** 154 * Accepts Map with parameters from ExecTool 155 **/ 156 void load(Map map) { 157 for (Object okey : map.keySet()) { 158 String key = (String)okey; 159 String tbKey = USER_TOOLBAR_PREF + getId(); 160 if (key.contains(tbKey)) { 161 state.put(okey, map.get(okey)); 162 } 163 } 164 165 Preferences prefs = Preferences.access(); 166 String visPref = prefs.getPreference(ExecTool.TOOLBAR_PREF); 167 boolean generalVisibleState = "true".equals(visPref); 168 setVisibleNoStateAffect(generalVisibleState && readVisibleState()); 169 } 170 171 /** 172 * Sets common style for a toolbar 173 **/ 174 private void setUpStyle() { 175 setFloatable(false); 176 setRollover(true); 177 setBorder(new ToolBarBorder()); 178 } 179 180 /** 181 * Sets common style for a toolbar with left border 182 **/ 183 class ToolBarBorder extends BevelBorder { 184 185 public ToolBarBorder() { 186 super(BevelBorder.RAISED); 187 } 188 189 public Insets getBorderInsets(Component c) { 190 return new Insets(2, 2, 2, lIn ); 191 } 192 193 public Insets getBorderInsets(Component c, Insets insets) { 194 insets.right = lIn; 195 insets.top = insets.left = insets.bottom = 2; 196 return insets; 197 } 198 199 protected void paintRaisedBevel(Component c, Graphics g, int x, int y, 200 int width, int height) { 201 int gap = 4; 202 int hlen=12; 203 204 if (c instanceof JComponent) { 205 JComponent jc = (JComponent) c; 206 Boolean paint = (Boolean)jc.getClientProperty(ToolBarPanel.PB_PROP_NAME); 207 if (paint != null && paint.booleanValue()) { 208 Color oldColor = g.getColor(); 209 int h = height; 210 int w = width; 211 212 if (h > hlen*2) { 213 int mid = y + (h/2); 214 y = mid - hlen; 215 h = hlen*2; 216 } 217 218 g.setColor(getShadowInnerColor(c)); 219 g.drawLine(x, y+gap, x, y+h-gap-1); 220 221 g.setColor(getHighlightInnerColor(c)); 222 g.drawLine(x+1, y+gap, x+1, y+h-gap-1); 223 } 224 } 225 } 226 227 private int lIn = 5; 228 229 } 230 231 final static String USER_TOOLBAR_PREF = "JavaTestToolBar.toolbar_"; 232 private boolean inMenu = false; 233 private ResourceBundle theBundle; 234 private String id; 235 private Map state = new HashMap(); 236 }