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: *
021: */package org.apache.harmony.x.swing;
022:
023: import java.awt.Image;
024: import java.awt.Rectangle;
025: import java.awt.Window;
026:
027: import javax.swing.Action;
028: import javax.swing.Icon;
029: import javax.swing.ImageIcon;
030: import javax.swing.JDialog;
031: import javax.swing.JFrame;
032: import javax.swing.JInternalFrame;
033: import javax.swing.JMenu;
034: import javax.swing.JMenuItem;
035: import javax.swing.plaf.UIResource;
036:
037: /**
038: * This class contains properties that are common for all title panes,
039: * for example: <code>BasicInternalFrameTitlePane, MetalInternalFrameTitlePane,
040: * MetalRootPaneTitlePane</code>.
041: *
042: */
043: public final class TitlePaneInternals {
044: private JInternalFrame internalFrame;
045: private Window window;
046: private JFrame frame;
047: private JDialog dialog;
048:
049: private Icon frameIcon;
050: private Icon uiFrameIcon;
051: private JMenu windowMenu;
052:
053: /**
054: * The gap between title pane components (hardcoded).
055: * This value of the gap is for BasicInternalFrameTitlePane.
056: */
057: public int gapX = 2;
058:
059: /**
060: * Rectangle which represents the bounds of the title decoration area.
061: */
062: public Rectangle decorationR;
063:
064: /**
065: * Keeps if the associated frame has menubar or not.
066: */
067: public boolean hasMenuBar = true;
068:
069: /**
070: * Consctucts TitlePaneInternals and connects it to the specified frame.
071: *
072: * @param internalFrame internals are connected to
073: */
074: public TitlePaneInternals(final JInternalFrame internalFrame) {
075: this .internalFrame = internalFrame;
076: loadInternalFrameProperties();
077: }
078:
079: /**
080: * Retrieves title information of the connected frame.
081: *
082: * @return Frame title
083: */
084: public String getWindowTitle() {
085: if (internalFrame != null) {
086: return internalFrame.getTitle();
087: } else if (frame != null) {
088: return frame.getTitle();
089: } else if (dialog != null) {
090: return dialog.getTitle();
091: }
092: return "";
093: }
094:
095: /**
096: * Stores frame icon. For the user-defined icon its sizes are scaled to the
097: * frame icon sizes.
098: *
099: * @param icon Icon to be used for the frame
100: */
101: public void setFrameIcon(final Icon icon) {
102: if (icon instanceof UIResource) {
103: uiFrameIcon = icon;
104: frameIcon = uiFrameIcon;
105: } else {
106: frameIcon = getScaledIcon(icon);
107: }
108: updateWindowMenuIcon();
109: }
110:
111: /**
112: * Gets frame icon.
113: *
114: * @return Icon used for the connected frame
115: */
116: public Icon getFrameIcon() {
117: if (frameIcon != null) {
118: return frameIcon;
119: } else if (windowMenu != null) {
120: return uiFrameIcon;
121: }
122:
123: return null;
124: }
125:
126: /**
127: * Retrieves icon set to the frame by its L&F.
128: *
129: * @return Icon installed by L&F
130: */
131: public Icon getUIFrameIcon() {
132: return uiFrameIcon;
133: }
134:
135: /**
136: * Determines is the frame is active (activated if the frame is top-level or selected for internal frames).
137: *
138: * @return <code>true</code> if the connected frame is active; <code>false</code> otherwise
139: */
140: public boolean isSelected() {
141: if (internalFrame != null) {
142: return internalFrame.isSelected();
143: }
144: return window.isActive();
145: }
146:
147: /**
148: * Sets the connected window (for top-level windows only).
149: *
150: * @param window Window internals to be connected to
151: */
152: public void setWindow(final Window window) {
153: this .window = window;
154: if (window instanceof JFrame) {
155: frame = (JFrame) window;
156: } else if (window instanceof JDialog) {
157: dialog = (JDialog) window;
158: }
159: internalFrame = null;
160: }
161:
162: /**
163: * Sets system menu used for the connected frame.
164: *
165: * @param menu JMenu to be used for the connected frame
166: */
167: public void setWindowMenu(final JMenu menu) {
168: windowMenu = menu;
169: updateWindowMenuIcon();
170: }
171:
172: /**
173: * Creates the menu item for the specified system menu.
174: *
175: * @param action Action of the creating item
176: * @param text String representing item label
177: * @param mnemonic int value representing item mnemonic or -1 if no mnemonic should be used
178: *
179: * @return JMenuItem created for the frame system menu
180: */
181: public static JMenuItem createMenuItem(final Action action,
182: final String text, final int mnemonic) {
183: JMenuItem item = new JMenuItem(action);
184: item.setText(text);
185: item.setToolTipText(null);
186: item.setIcon(null);
187: item.setMnemonic(mnemonic);
188: return item;
189: }
190:
191: private void updateWindowMenuIcon() {
192: if (windowMenu != null) {
193: windowMenu.setIcon(getFrameIcon());
194: }
195: }
196:
197: private Icon getScaledIcon(final Icon icon) {
198: if (icon == null || uiFrameIcon == null
199: || icon.getIconWidth() == uiFrameIcon.getIconWidth()
200: && icon.getIconHeight() == uiFrameIcon.getIconHeight()) {
201: return icon;
202: }
203: int w = uiFrameIcon.getIconWidth();
204: int h = uiFrameIcon.getIconHeight();
205:
206: if (icon instanceof ImageIcon) {
207: Image image = ((ImageIcon) icon).getImage();
208: if (image != null) {
209: image = image.getScaledInstance(w, h, Image.SCALE_FAST);
210: ImageIcon newIcon = new ImageIcon(image);
211: return newIcon;
212: }
213: }
214: return icon;
215: }
216:
217: private void loadInternalFrameProperties() {
218: if (internalFrame != null) {
219: setFrameIcon(internalFrame.getFrameIcon());
220: }
221: }
222: }
|