1 /*
2 * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.swing.plaf.metal;
27
28 import java.awt;
29 import java.awt.event;
30 import javax.swing;
31 import javax.swing.event;
32 import javax.swing.border;
33 import javax.swing.plaf.basic;
34 import java.util.EventListener;
35 import java.beans.PropertyChangeListener;
36 import java.beans.PropertyChangeEvent;
37 import java.beans.PropertyVetoException;
38 import javax.swing.plaf;
39
40 /**
41 * Metal implementation of JInternalFrame.
42 * <p>
43 *
44 * @author Steve Wilson
45 */
46 public class MetalInternalFrameUI extends BasicInternalFrameUI {
47
48 private static final PropertyChangeListener metalPropertyChangeListener =
49 new MetalPropertyChangeHandler();
50
51 private static final Border handyEmptyBorder = new EmptyBorder(0,0,0,0);
52
53 protected static String IS_PALETTE = "JInternalFrame.isPalette";
54
55 private static String FRAME_TYPE = "JInternalFrame.frameType";
56 private static String NORMAL_FRAME = "normal";
57 private static String PALETTE_FRAME = "palette";
58 private static String OPTION_DIALOG = "optionDialog";
59
60 public MetalInternalFrameUI(JInternalFrame b) {
61 super(b);
62 }
63
64 public static ComponentUI createUI(JComponent c) {
65 return new MetalInternalFrameUI( (JInternalFrame) c);
66 }
67
68 public void installUI(JComponent c) {
69 super.installUI(c);
70
71 Object paletteProp = c.getClientProperty( IS_PALETTE );
72 if ( paletteProp != null ) {
73 setPalette( ((Boolean)paletteProp).booleanValue() );
74 }
75
76 Container content = frame.getContentPane();
77 stripContentBorder(content);
78 //c.setOpaque(false);
79 }
80
81 public void uninstallUI(JComponent c) {
82 frame = (JInternalFrame)c;
83
84 Container cont = ((JInternalFrame)(c)).getContentPane();
85 if (cont instanceof JComponent) {
86 JComponent content = (JComponent)cont;
87 if ( content.getBorder() == handyEmptyBorder) {
88 content.setBorder(null);
89 }
90 }
91 super.uninstallUI(c);
92 }
93
94 protected void installListeners() {
95 super.installListeners();
96 frame.addPropertyChangeListener(metalPropertyChangeListener);
97 }
98
99 protected void uninstallListeners() {
100 frame.removePropertyChangeListener(metalPropertyChangeListener);
101 super.uninstallListeners();
102 }
103
104 protected void installKeyboardActions(){
105 super.installKeyboardActions();
106 ActionMap map = SwingUtilities.getUIActionMap(frame);
107 if (map != null) {
108 // BasicInternalFrameUI creates an action with the same name, we override
109 // it as Metal frames do not have system menus.
110 map.remove("showSystemMenu");
111 }
112 }
113
114 protected void uninstallKeyboardActions(){
115 super.uninstallKeyboardActions();
116 }
117
118 protected void uninstallComponents() {
119 titlePane = null;
120 super.uninstallComponents();
121 }
122
123 private void stripContentBorder(Object c) {
124 if ( c instanceof JComponent ) {
125 JComponent contentComp = (JComponent)c;
126 Border contentBorder = contentComp.getBorder();
127 if (contentBorder == null || contentBorder instanceof UIResource) {
128 contentComp.setBorder( handyEmptyBorder );
129 }
130 }
131 }
132
133
134 protected JComponent createNorthPane(JInternalFrame w) {
135 return new MetalInternalFrameTitlePane(w);
136 }
137
138
139 private void setFrameType( String frameType )
140 {
141 if ( frameType.equals( OPTION_DIALOG ) )
142 {
143 LookAndFeel.installBorder(frame, "InternalFrame.optionDialogBorder");
144 ((MetalInternalFrameTitlePane)titlePane).setPalette( false );
145 }
146 else if ( frameType.equals( PALETTE_FRAME ) )
147 {
148 LookAndFeel.installBorder(frame, "InternalFrame.paletteBorder");
149 ((MetalInternalFrameTitlePane)titlePane).setPalette( true );
150 }
151 else
152 {
153 LookAndFeel.installBorder(frame, "InternalFrame.border");
154 ((MetalInternalFrameTitlePane)titlePane).setPalette( false );
155 }
156 }
157
158 // this should be deprecated - jcs
159 public void setPalette(boolean isPalette) {
160 if (isPalette) {
161 LookAndFeel.installBorder(frame, "InternalFrame.paletteBorder");
162 } else {
163 LookAndFeel.installBorder(frame, "InternalFrame.border");
164 }
165 ((MetalInternalFrameTitlePane)titlePane).setPalette(isPalette);
166
167 }
168
169 private static class MetalPropertyChangeHandler implements
170 PropertyChangeListener
171 {
172 public void propertyChange(PropertyChangeEvent e)
173 {
174 String name = e.getPropertyName();
175 JInternalFrame jif = (JInternalFrame)e.getSource();
176
177 if (!(jif.getUI() instanceof MetalInternalFrameUI)) {
178 return;
179 }
180
181 MetalInternalFrameUI ui = (MetalInternalFrameUI)jif.getUI();
182
183 if ( name.equals( FRAME_TYPE ) )
184 {
185 if ( e.getNewValue() instanceof String )
186 {
187 ui.setFrameType( (String) e.getNewValue() );
188 }
189 }
190 else if ( name.equals( IS_PALETTE ) )
191 {
192 if ( e.getNewValue() != null )
193 {
194 ui.setPalette( ((Boolean)e.getNewValue()).booleanValue() );
195 }
196 else
197 {
198 ui.setPalette( false );
199 }
200 } else if ( name.equals( JInternalFrame.CONTENT_PANE_PROPERTY ) ) {
201 ui.stripContentBorder(e.getNewValue());
202 }
203 }
204 } // end class MetalPropertyChangeHandler
205
206
207 private class BorderListener1 extends BorderListener implements SwingConstants
208 {
209
210 Rectangle getIconBounds() {
211 boolean leftToRight = MetalUtils.isLeftToRight(frame);
212 int xOffset = leftToRight ? 5 : titlePane.getWidth() - 5;
213 Rectangle rect = null;
214
215 Icon icon = frame.getFrameIcon();
216 if ( icon != null ) {
217 if ( !leftToRight ) {
218 xOffset -= icon.getIconWidth();
219 }
220 int iconY = ((titlePane.getHeight() / 2) - (icon.getIconHeight() /2));
221 rect = new Rectangle(xOffset, iconY,
222 icon.getIconWidth(), icon.getIconHeight());
223 }
224 return rect;
225 }
226
227 public void mouseClicked(MouseEvent e) {
228 if (e.getClickCount() == 2 && e.getSource() == getNorthPane() &&
229 frame.isClosable() && !frame.isIcon()) {
230 Rectangle rect = getIconBounds();
231 if ((rect != null) && rect.contains(e.getX(), e.getY())) {
232 frame.doDefaultCloseAction();
233 }
234 else {
235 super.mouseClicked(e);
236 }
237 }
238 else {
239 super.mouseClicked(e);
240 }
241 }
242 }; /// End BorderListener Class
243
244
245 /**
246 * Returns the <code>MouseInputAdapter</code> that will be installed
247 * on the TitlePane.
248 *
249 * @param w the <code>JInternalFrame</code>
250 * @return the <code>MouseInputAdapter</code> that will be installed
251 * on the TitlePane.
252 * @since 1.6
253 */
254 protected MouseInputAdapter createBorderListener(JInternalFrame w) {
255 return new BorderListener1();
256 }
257 }