001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Vadim L. Bogdanov
019: * @version $Revision$
020: */package javax.swing.plaf.metal;
021:
022: import java.awt.Component;
023: import java.awt.Graphics;
024: import java.awt.Insets;
025: import java.awt.Point;
026: import java.awt.event.ContainerListener;
027: import java.awt.event.MouseEvent;
028: import java.beans.PropertyChangeListener;
029:
030: import javax.swing.JComponent;
031: import javax.swing.JToolBar;
032: import javax.swing.border.Border;
033: import javax.swing.event.MouseInputListener;
034: import javax.swing.plaf.BorderUIResource;
035: import javax.swing.plaf.ComponentUI;
036: import javax.swing.plaf.basic.BasicToolBarUI;
037:
038: public class MetalToolBarUI extends BasicToolBarUI {
039: protected class MetalDockingListener extends DockingListener {
040: public MetalDockingListener(final JToolBar t) {
041: super (t);
042: }
043:
044: public void mousePressed(final MouseEvent e) {
045: if (!isOnBumps(e.getPoint())) {
046: return;
047: }
048:
049: setDragOffset(e.getPoint());
050: super .mousePressed(e);
051: }
052:
053: public void mouseDragged(final MouseEvent e) {
054: if (!isDragging && !isOnBumps(e.getPoint())) {
055: return;
056: }
057: super .mouseDragged(e);
058: }
059:
060: private boolean isOnBumps(final Point p) {
061: if (p.x < 0 || p.y < 0) {
062: return false;
063: }
064: Insets insets = toolBar.getInsets();
065: if (toolBar.getOrientation() == HORIZONTAL) {
066: return p.x < insets.left && p.y < toolBar.getHeight();
067: } else {
068: return p.x < toolBar.getWidth() && p.y < insets.top;
069: }
070: }
071: }
072:
073: protected class MetalContainerListener extends ToolBarContListener {
074: protected MetalContainerListener() {
075: }
076: }
077:
078: protected class MetalRolloverListener extends PropertyListener {
079: protected MetalRolloverListener() {
080: }
081: }
082:
083: protected ContainerListener contListener;
084: protected PropertyChangeListener rolloverListener;
085:
086: public static ComponentUI createUI(final JComponent c) {
087: return new MetalToolBarUI();
088: }
089:
090: public void installUI(final JComponent c) {
091: super .installUI(c);
092: }
093:
094: public void uninstallUI(final JComponent c) {
095: super .uninstallUI(c);
096: }
097:
098: public void update(final Graphics g, final JComponent c) {
099: super .update(g, c);
100: }
101:
102: protected void installListeners() {
103: super .installListeners();
104:
105: contListener = createContainerListener();
106: if (contListener != null) {
107: toolBar.addContainerListener(contListener);
108: }
109:
110: rolloverListener = createRolloverListener();
111: if (rolloverListener != null) {
112: toolBar.addPropertyChangeListener(rolloverListener);
113: }
114: }
115:
116: protected void uninstallListeners() {
117: super .uninstallListeners();
118:
119: toolBar.removeContainerListener(contListener);
120: toolBar.removePropertyChangeListener(rolloverListener);
121: }
122:
123: protected Border createRolloverBorder() {
124: Border buttonBorder = new MetalBorders.RolloverButtonBorder();
125: Border marginBorder = new MetalBorders.ToolBarButtonMarginBorder();
126: return new BorderUIResource.CompoundBorderUIResource(
127: buttonBorder, marginBorder);
128: }
129:
130: protected Border createNonRolloverBorder() {
131: Border buttonBorder = new MetalBorders.ButtonBorder();
132: Border marginBorder = new MetalBorders.ToolBarButtonMarginBorder();
133: return new BorderUIResource.CompoundBorderUIResource(
134: buttonBorder, marginBorder);
135: }
136:
137: protected void setBorderToNonRollover(final Component c) {
138: super .setBorderToNonRollover(c);
139: }
140:
141: protected ContainerListener createContainerListener() {
142: return null;
143: }
144:
145: protected PropertyChangeListener createRolloverListener() {
146: return null;
147: }
148:
149: protected MouseInputListener createDockingListener() {
150: return new MetalDockingListener(toolBar);
151: }
152:
153: protected void setDragOffset(final Point p) {
154: if (dragWindow == null) {
155: dragWindow = createDragWindow(toolBar);
156: }
157: dragWindow.setOffset(p);
158: }
159: }
|