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: /**
019: * @author Vadim L. Bogdanov
020: * @version $Revision$
021: */package javax.swing.plaf.metal;
022:
023: import java.awt.Color;
024: import java.awt.Container;
025: import java.awt.Dimension;
026: import java.awt.Graphics;
027: import java.awt.LayoutManager;
028: import java.awt.Rectangle;
029:
030: import java.beans.PropertyChangeListener;
031:
032: import javax.swing.BorderFactory;
033: import javax.swing.Icon;
034: import javax.swing.JButton;
035: import javax.swing.JInternalFrame;
036: import javax.swing.JMenu;
037: import javax.swing.SwingUtilities;
038: import javax.swing.UIManager;
039:
040: import javax.swing.border.Border;
041: import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
042:
043: import org.apache.harmony.x.swing.TitlePaneInternals;
044:
045: public class MetalInternalFrameTitlePane extends
046: BasicInternalFrameTitlePane {
047:
048: private class MetalTitlePaneLayout extends TitlePaneLayout {
049: public Dimension minimumLayoutSize(final Container c) {
050: if (!isPalette) {
051: return super .minimumLayoutSize(c);
052: }
053:
054: return new Dimension(43, paletteTitleHeight);
055: }
056:
057: public void layoutContainer(final Container c) {
058: if (!isPalette) {
059: super .layoutContainer(c);
060: return;
061: }
062:
063: // dimensions of the container
064: Rectangle inner = SwingUtilities.calculateInnerArea(
065: MetalInternalFrameTitlePane.this , null);
066: int width = inner.width;
067: int height = inner.height;
068:
069: // palette can have only "Close" button
070: if (frame.isClosable()) {
071: int buttonHeight = closeButton.getIcon()
072: .getIconHeight();
073: int buttonWidth = buttonHeight;
074: int x = inner.x + width - buttonWidth;
075: int y = inner.y + (height - buttonHeight) / 2;
076: closeButton.setBounds(x, y, buttonWidth, buttonHeight);
077: inner.width = closeButton.getX() - inner.x
078: - internals.gapX;
079: }
080:
081: internals.decorationR.setBounds(inner);
082: }
083: }
084:
085: protected boolean isPalette;
086: protected Icon paletteCloseIcon;
087: protected int paletteTitleHeight;
088:
089: TitlePaneInternals internals;
090: Color selectedShadowColor;
091: Color notSelectedShadowColor;
092:
093: private Border commonBorder = BorderFactory.createEmptyBorder(3, 0,
094: 4, 0);
095: private Border paletteBorder = BorderFactory.createEmptyBorder(1,
096: 3, 2, 3);
097:
098: public MetalInternalFrameTitlePane(final JInternalFrame frame) {
099: super (frame);
100: setBorder(commonBorder);
101:
102: installInternals();
103: }
104:
105: protected void addSubComponents() {
106: // overridden to add everything except menu bar,
107: // but we handle the situation with menuBar == null in the ancestor
108: super .addSubComponents();
109: }
110:
111: protected void addSystemMenuItems(final JMenu menu) {
112: // do nothing
113: }
114:
115: protected void assembleSystemMenu() {
116: // do nothing
117: }
118:
119: protected void showSystemMenu() {
120: // do nothing
121: }
122:
123: protected void createButtons() {
124: super .createButtons();
125:
126: setupTitlePaneButton(maxButton,
127: "InternalFrameTitlePane.maximizeButtonAccessibleName");
128: setupTitlePaneButton(iconButton,
129: "InternalFrameTitlePane.iconifyButtonAccessibleName");
130: setupTitlePaneButton(closeButton,
131: "InternalFrameTitlePane.closeButtonAccessibleName");
132: }
133:
134: private void setupTitlePaneButton(final JButton button,
135: final String accessibleNameKey) {
136: button.getAccessibleContext().setAccessibleName(
137: UIManager.getString(accessibleNameKey));
138: button.setBorderPainted(false);
139: button.setOpaque(false);
140: button.setContentAreaFilled(false);
141: }
142:
143: protected PropertyChangeListener createPropertyChangeListener() {
144: // Note: just call the method from super
145: return super .createPropertyChangeListener();
146: }
147:
148: protected LayoutManager createLayout() {
149: return new MetalTitlePaneLayout();
150: }
151:
152: public void paintPalette(final Graphics g) {
153: g.setColor(MetalLookAndFeel.getPrimaryControlShadow());
154: g.fillRect(0, 0, getWidth(), getHeight());
155: paintDecoration(g);
156: g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
157: g.drawLine(0, getHeight() - 1, getWidth() - 1, getHeight() - 1);
158: }
159:
160: private void paintDecoration(final Graphics g) {
161: Color shadow = internals.isSelected() ? selectedShadowColor
162: : notSelectedShadowColor;
163: Color highlight = internals.isSelected() ? MetalLookAndFeel
164: .getPrimaryControlHighlight() : MetalLookAndFeel
165: .getControlHighlight();
166:
167: MetalBumps.paintBumps(g, internals.decorationR, shadow,
168: highlight);
169: }
170:
171: public void paintComponent(final Graphics g) {
172: if (isPalette) {
173: paintPalette(g);
174: return;
175: }
176:
177: super .paintComponent(g);
178: paintDecoration(g);
179:
180: // paint the line under the titlePane
181: g.setColor(internals.isSelected() ? selectedShadowColor
182: : notSelectedShadowColor);
183: g.drawLine(0, getHeight() - 1, getWidth() - 1, getHeight() - 1);
184: }
185:
186: public void setPalette(final boolean b) {
187: isPalette = b;
188:
189: closeButton.setIcon(isPalette ? paletteCloseIcon : closeIcon);
190:
191: if (isPalette) {
192: setBorder(paletteBorder);
193: remove(iconButton);
194: remove(maxButton);
195: } else {
196: setBorder(commonBorder);
197: if (frame.isIconifiable()) {
198: add(iconButton);
199: }
200: if (frame.isMaximizable()) {
201: add(maxButton);
202: }
203: }
204: }
205:
206: protected void installDefaults() {
207: super .installDefaults();
208:
209: paletteTitleHeight = UIManager
210: .getInt("InternalFrame.paletteTitleHeight");
211: paletteCloseIcon = UIManager
212: .getIcon("InternalFrame.paletteCloseIcon");
213:
214: selectedShadowColor = MetalLookAndFeel
215: .getPrimaryControlDarkShadow();
216: notSelectedShadowColor = MetalLookAndFeel
217: .getControlDarkShadow();
218: }
219:
220: private void installInternals() {
221: internals = (TitlePaneInternals) getClientProperty("internals");
222:
223: internals.decorationR = new Rectangle();
224: internals.gapX = 6;
225: }
226:
227: protected void uninstallDefaults() {
228: // no need to uninstall anything because
229: // the title pane is replaced while changing L&F
230: super .uninstallDefaults();
231: }
232:
233: public void addNotify() {
234: // Note: just call the method from super
235: super.addNotify();
236: }
237: }
|