001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.ui.components;
042:
043: import org.netbeans.lib.profiler.ui.UIUtils;
044: import java.awt.*;
045: import java.net.URL;
046: import javax.swing.*;
047: import javax.swing.border.AbstractBorder;
048: import javax.swing.border.CompoundBorder;
049: import javax.swing.plaf.UIResource;
050: import javax.swing.plaf.basic.BasicToolBarUI;
051:
052: /**
053: * A special version of ToolBar that has transparent background and rollover button style.
054: * Can be used e.g. on on a white background form-like UI.
055: *
056: * @author Ian Formanek
057: */
058: public class FlatToolBar extends JToolBar {
059: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
060:
061: public static class FlatMarginBorder extends AbstractBorder {
062: //~ Methods --------------------------------------------------------------------------------------------------------------
063:
064: public Insets getBorderInsets(Component c) {
065: return getBorderInsets(c, new Insets(0, 0, 0, 0));
066: }
067:
068: public Insets getBorderInsets(Component c, Insets insets) {
069: Insets margin = null;
070:
071: if (c instanceof AbstractButton) {
072: AbstractButton b = (AbstractButton) c;
073: margin = b.getMargin();
074: }
075:
076: insets.top = (margin != null) ? margin.top : 0;
077: insets.left = (margin != null) ? margin.left : 0;
078: insets.bottom = (margin != null) ? margin.bottom : 0;
079: insets.right = (margin != null) ? margin.right : 0;
080:
081: return insets;
082: }
083: }
084:
085: /**
086: * Special thin border for rollover toolbar buttons.
087: */
088: public static class FlatRolloverButtonBorder extends AbstractBorder {
089: //~ Instance fields ------------------------------------------------------------------------------------------------------
090:
091: private Color normal;
092: private Color pressed;
093: private Color roll;
094: private boolean borderPainted = false;
095:
096: //~ Constructors ---------------------------------------------------------------------------------------------------------
097:
098: public FlatRolloverButtonBorder(Color pressed, Color roll) {
099: super ();
100: this .pressed = pressed;
101: this .roll = roll;
102: this .borderPainted = false;
103: }
104:
105: public FlatRolloverButtonBorder(Color pressed, Color roll,
106: Color normal) {
107: super ();
108: this .pressed = pressed;
109: this .roll = roll;
110: this .normal = normal;
111: this .borderPainted = true;
112: }
113:
114: //~ Methods --------------------------------------------------------------------------------------------------------------
115:
116: public Insets getBorderInsets(Component c) {
117: return getBorderInsets(c, new Insets(0, 0, 0, 0));
118: }
119:
120: public Insets getBorderInsets(Component c, Insets insets) {
121: // leave room for default visual
122: insets.top = 2;
123: insets.left = insets.bottom = insets.right = 3;
124:
125: return insets;
126: }
127:
128: public void paintBorder(Component c, Graphics g, int x, int y,
129: int w, int h) {
130: AbstractButton b = (AbstractButton) c;
131: ButtonModel model = b.getModel();
132:
133: if ((model.isRollover() && !(model.isPressed() && !model
134: .isArmed()))
135: || model.isSelected()) {
136: Color oldColor = g.getColor();
137: g.translate(x, y);
138:
139: if ((model.isPressed() && model.isArmed())
140: || model.isSelected()) {
141: // Draw the pressd button
142: g.setColor(pressed);
143: g.drawRect(0, 0, w - 1, h - 1);
144: } else {
145: // Draw a rollover button
146: g.setColor(roll);
147: g.drawRect(0, 0, w - 1, h - 1);
148: }
149:
150: g.translate(-x, -y);
151: g.setColor(oldColor);
152: } else if (borderPainted) {
153: Color oldColor = g.getColor();
154: g.translate(x, y);
155: g.setColor(normal);
156: g.drawRect(0, 0, w - 1, h - 1);
157: g.translate(-x, -y);
158: g.setColor(oldColor);
159: }
160: }
161: }
162:
163: private static class MyToolBarUI extends BasicToolBarUI {
164: //~ Instance fields ------------------------------------------------------------------------------------------------------
165:
166: private AbstractBorder myRolloverBorder;
167:
168: //~ Constructors ---------------------------------------------------------------------------------------------------------
169:
170: public MyToolBarUI() {
171: myRolloverBorder = new CompoundBorder(
172: new FlatRolloverButtonBorder(Color.GRAY,
173: Color.LIGHT_GRAY), new FlatMarginBorder());
174: }
175:
176: //~ Methods --------------------------------------------------------------------------------------------------------------
177:
178: protected void setBorderToRollover(Component c) {
179: if (c instanceof AbstractButton) {
180: AbstractButton b = (AbstractButton) c;
181:
182: if (b.getBorder() instanceof UIResource) {
183: b.setBorder(myRolloverBorder);
184: }
185:
186: b.setRolloverEnabled(true);
187: }
188: }
189: }
190:
191: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
192:
193: public static final int BUTTON_STYLE_HORIZONTAL = 1;
194: public static final int BUTTON_STYLE_VERICAL = 2;
195:
196: //~ Instance fields ----------------------------------------------------------------------------------------------------------
197:
198: int buttonStyle = BUTTON_STYLE_HORIZONTAL;
199:
200: //~ Constructors -------------------------------------------------------------------------------------------------------------
201:
202: /**
203: * Creates a horizontal toolbar with horizontal button style (i.e. icon left to text)
204: */
205: public FlatToolBar() {
206: this (HORIZONTAL, BUTTON_STYLE_HORIZONTAL);
207: }
208:
209: /**
210: * Creates a toolbar with specified orientation and horizontal button style (i.e. icon left to text)
211: *
212: * @see JToolBar.HORIZONTAL
213: * @see JToolBar.VERTICAL
214: * @param orientation
215: */
216: public FlatToolBar(int orientation) {
217: this (orientation, BUTTON_STYLE_HORIZONTAL);
218: }
219:
220: /**
221: * Creates a toolbar with specified orientation and button style
222: *
223: * @see JToolBar.HORIZONTAL
224: * @see JToolBar.VERTICAL
225: *
226: * @param orientation
227: * @param buttonStyle
228: */
229: public FlatToolBar(int orientation, int buttonStyle) {
230: super (orientation);
231: this .buttonStyle = buttonStyle;
232:
233: if (!UIUtils.isGTKLookAndFeel()) {
234: setUI(new MyToolBarUI());
235: }
236:
237: setFloatable(false);
238: setOpaque(false);
239: putClientProperty("JToolBar.isRollover", Boolean.TRUE); //NOI18N
240: }
241:
242: //~ Methods ------------------------------------------------------------------------------------------------------------------
243:
244: protected void addImpl(Component comp, Object constraints, int index) {
245: if (comp instanceof AbstractButton) {
246: AbstractButton ab = (AbstractButton) comp;
247: ab.setContentAreaFilled(false);
248: ab.setMargin(new Insets(3, 3, 3, 3));
249:
250: if (buttonStyle == BUTTON_STYLE_VERICAL) {
251: ab.setVerticalTextPosition(SwingConstants.BOTTOM);
252: ab.setHorizontalTextPosition(SwingConstants.CENTER);
253: }
254: }
255:
256: super .addImpl(comp, constraints, index);
257: }
258:
259: protected JButton createActionComponent(Action a) {
260: JButton b = super .createActionComponent(a);
261:
262: if (buttonStyle == BUTTON_STYLE_VERICAL) {
263: b.putClientProperty("hideActionText", Boolean.FALSE); //NOI18N
264:
265: String iconBase = (String) a.getValue("iconBase"); //NOI18N
266:
267: if (iconBase != null) {
268: try {
269: System.err.println("URL for: "
270: + insertBeforeSuffix(iconBase, "32")); //NOI18N
271:
272: URL url = a.getClass().getResource(
273: insertBeforeSuffix(iconBase, "32")); //NOI18N
274: System.err.println("is: " + url); //NOI18N
275: b.setIcon(new ImageIcon(url));
276: } catch (Exception e) {
277: e.printStackTrace(System.err);
278: }
279: }
280: }
281:
282: return b;
283: }
284:
285: static String insertBeforeSuffix(String path, String toInsert) {
286: String withoutSuffix = path;
287: String suffix = ""; // NOI18N
288:
289: if (path.lastIndexOf('.') >= 0) { //NOI18N
290: withoutSuffix = path.substring(0, path.lastIndexOf('.')); //NOI18N
291: suffix = path.substring(path.lastIndexOf('.'), path
292: .length()); //NOI18N
293: }
294:
295: return withoutSuffix + toInsert + suffix;
296: }
297: }
|