J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2004 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 com.sun.java.swing.plaf.motif; |
| 27 | |
| 28 | import java.awt.*; |
| 29 | import java.awt.event.*; |
| 30 | import javax.swing.*; |
| 31 | import javax.swing.border.*; |
| 32 | import javax.swing.event.InternalFrameEvent; |
| 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.VetoableChangeListener; |
| 38 | import java.beans.PropertyVetoException; |
| 39 | |
| 40 | /** |
| 41 | * Class that manages a Motif title bar |
| 42 | * |
| 43 | * @since 1.3 |
| 44 | */ |
| 45 | public class MotifInternalFrameTitlePane |
| 46 | extends BasicInternalFrameTitlePane implements LayoutManager, ActionListener, PropertyChangeListener |
| 47 | { |
| 48 | SystemButton systemButton; |
| 49 | MinimizeButton minimizeButton; |
| 50 | MaximizeButton maximizeButton; |
| 51 | JPopupMenu systemMenu; |
| 52 | Title title; |
| 53 | Color color; |
| 54 | Color highlight; |
| 55 | Color shadow; |
| 56 | |
| 57 | // The width and height of a title pane button |
| 58 | public final static int BUTTON_SIZE = 19; // 17 + 1 pixel border |
| 59 | |
| 60 | |
| 61 | public MotifInternalFrameTitlePane(JInternalFrame frame) { |
| 62 | super(frame); |
| 63 | } |
| 64 | |
| 65 | protected void installDefaults() { |
| 66 | setFont(UIManager.getFont("InternalFrame.titleFont")); |
| 67 | setPreferredSize(new Dimension(100, BUTTON_SIZE)); |
| 68 | } |
| 69 | |
| 70 | protected void uninstallListeners() { |
| 71 | // Get around protected method in superclass |
| 72 | super.uninstallListeners(); |
| 73 | } |
| 74 | |
| 75 | protected PropertyChangeListener createPropertyChangeListener() { |
| 76 | return this; |
| 77 | } |
| 78 | |
| 79 | protected LayoutManager createLayout() { |
| 80 | return this; |
| 81 | } |
| 82 | |
| 83 | JPopupMenu getSystemMenu() { |
| 84 | return systemMenu; |
| 85 | } |
| 86 | |
| 87 | protected void assembleSystemMenu() { |
| 88 | systemMenu = new JPopupMenu(); |
| 89 | JMenuItem mi = (JMenuItem)systemMenu.add(new JMenuItem(restoreAction)); |
| 90 | mi.setMnemonic('R'); |
| 91 | mi = (JMenuItem) systemMenu.add(new JMenuItem(moveAction)); |
| 92 | mi.setMnemonic('M'); |
| 93 | mi = (JMenuItem) systemMenu.add(new JMenuItem(sizeAction)); |
| 94 | mi.setMnemonic('S'); |
| 95 | mi = (JMenuItem) systemMenu.add(new JMenuItem(iconifyAction)); |
| 96 | mi.setMnemonic('n'); |
| 97 | mi = (JMenuItem) systemMenu.add(new JMenuItem(maximizeAction)); |
| 98 | mi.setMnemonic('x'); |
| 99 | systemMenu.add(new JSeparator()); |
| 100 | mi = (JMenuItem) systemMenu.add(new JMenuItem(closeAction)); |
| 101 | mi.setMnemonic('C'); |
| 102 | |
| 103 | systemButton = new SystemButton(); |
| 104 | systemButton.addActionListener(new ActionListener() { |
| 105 | public void actionPerformed(ActionEvent e) { |
| 106 | systemMenu.show(systemButton, 0, BUTTON_SIZE); |
| 107 | } |
| 108 | }); |
| 109 | |
| 110 | systemButton.addMouseListener(new MouseAdapter() { |
| 111 | public void mousePressed(MouseEvent evt) { |
| 112 | try { |
| 113 | frame.setSelected(true); |
| 114 | } catch (PropertyVetoException pve) { |
| 115 | } |
| 116 | if ((evt.getClickCount() == 2)) { |
| 117 | closeAction.actionPerformed(new |
| 118 | ActionEvent(evt.getSource(), |
| 119 | ActionEvent.ACTION_PERFORMED, |
| 120 | null, evt.getWhen(), 0)); |
| 121 | systemMenu.setVisible(false); |
| 122 | } |
| 123 | } |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | protected void createButtons() { |
| 129 | minimizeButton = new MinimizeButton(); |
| 130 | minimizeButton.addActionListener(iconifyAction); |
| 131 | |
| 132 | maximizeButton = new MaximizeButton(); |
| 133 | maximizeButton.addActionListener(maximizeAction); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | protected void addSubComponents() { |
| 138 | title = new Title(frame.getTitle()); |
| 139 | title.setFont(getFont()); |
| 140 | |
| 141 | add(systemButton); |
| 142 | add(title); |
| 143 | add(minimizeButton); |
| 144 | add(maximizeButton); |
| 145 | } |
| 146 | |
| 147 | public void paintComponent(Graphics g) { |
| 148 | } |
| 149 | |
| 150 | void setColors(Color c, Color h, Color s) { |
| 151 | color = c; |
| 152 | highlight = h; |
| 153 | shadow = s; |
| 154 | } |
| 155 | |
| 156 | public void actionPerformed(ActionEvent e) { |
| 157 | } |
| 158 | |
| 159 | public void propertyChange(PropertyChangeEvent evt) { |
| 160 | String prop = (String)evt.getPropertyName(); |
| 161 | JInternalFrame f = (JInternalFrame)evt.getSource(); |
| 162 | boolean value = false; |
| 163 | if (JInternalFrame.IS_SELECTED_PROPERTY.equals(prop)) { |
| 164 | repaint(); |
| 165 | } else if (prop.equals("maximizable")) { |
| 166 | if ((Boolean)evt.getNewValue() == Boolean.TRUE) |
| 167 | add(maximizeButton); |
| 168 | else |
| 169 | remove(maximizeButton); |
| 170 | revalidate(); |
| 171 | repaint(); |
| 172 | } else if (prop.equals("iconable")) { |
| 173 | if ((Boolean)evt.getNewValue() == Boolean.TRUE) |
| 174 | add(minimizeButton); |
| 175 | else |
| 176 | remove(minimizeButton); |
| 177 | revalidate(); |
| 178 | repaint(); |
| 179 | } else if (prop.equals(JInternalFrame.TITLE_PROPERTY)) { |
| 180 | repaint(); |
| 181 | } |
| 182 | enableActions(); |
| 183 | } |
| 184 | |
| 185 | public void addLayoutComponent(String name, Component c) {} |
| 186 | public void removeLayoutComponent(Component c) {} |
| 187 | public Dimension preferredLayoutSize(Container c) { |
| 188 | return minimumLayoutSize(c); |
| 189 | } |
| 190 | |
| 191 | public Dimension minimumLayoutSize(Container c) { |
| 192 | return new Dimension(100, BUTTON_SIZE); |
| 193 | } |
| 194 | |
| 195 | public void layoutContainer(Container c) { |
| 196 | int w = getWidth(); |
| 197 | systemButton.setBounds(0, 0, BUTTON_SIZE, BUTTON_SIZE); |
| 198 | int x = w - BUTTON_SIZE; |
| 199 | |
| 200 | if(frame.isMaximizable()) { |
| 201 | maximizeButton.setBounds(x, 0, BUTTON_SIZE, BUTTON_SIZE); |
| 202 | x -= BUTTON_SIZE; |
| 203 | } else if(maximizeButton.getParent() != null) { |
| 204 | maximizeButton.getParent().remove(maximizeButton); |
| 205 | } |
| 206 | |
| 207 | if(frame.isIconifiable()) { |
| 208 | minimizeButton.setBounds(x, 0, BUTTON_SIZE, BUTTON_SIZE); |
| 209 | x -= BUTTON_SIZE; |
| 210 | } else if(minimizeButton.getParent() != null) { |
| 211 | minimizeButton.getParent().remove(minimizeButton); |
| 212 | } |
| 213 | |
| 214 | title.setBounds(BUTTON_SIZE, 0, x, BUTTON_SIZE); |
| 215 | } |
| 216 | |
| 217 | protected void showSystemMenu(){ |
| 218 | systemMenu.show(systemButton, 0, BUTTON_SIZE); |
| 219 | } |
| 220 | |
| 221 | protected void hideSystemMenu(){ |
| 222 | systemMenu.setVisible(false); |
| 223 | } |
| 224 | |
| 225 | static Dimension buttonDimension = new Dimension(BUTTON_SIZE, BUTTON_SIZE); |
| 226 | |
| 227 | private abstract class FrameButton extends JButton { |
| 228 | |
| 229 | FrameButton() { |
| 230 | super(); |
| 231 | setFocusPainted(false); |
| 232 | setBorderPainted(false); |
| 233 | } |
| 234 | |
| 235 | public boolean isFocusTraversable() { |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | public void requestFocus() { |
| 240 | // ignore request. |
| 241 | } |
| 242 | |
| 243 | public Dimension getMinimumSize() { |
| 244 | return buttonDimension; |
| 245 | } |
| 246 | |
| 247 | public Dimension getPreferredSize() { |
| 248 | return buttonDimension; |
| 249 | } |
| 250 | |
| 251 | public void paintComponent(Graphics g) { |
| 252 | Dimension d = getSize(); |
| 253 | int maxX = d.width - 1; |
| 254 | int maxY = d.height - 1; |
| 255 | |
| 256 | // draw background |
| 257 | g.setColor(color); |
| 258 | g.fillRect(1, 1, d.width, d.height); |
| 259 | |
| 260 | // draw border |
| 261 | boolean pressed = getModel().isPressed(); |
| 262 | g.setColor(pressed ? shadow : highlight); |
| 263 | g.drawLine(0, 0, maxX, 0); |
| 264 | g.drawLine(0, 0, 0, maxY); |
| 265 | g.setColor(pressed ? highlight : shadow); |
| 266 | g.drawLine(1, maxY, maxX, maxY); |
| 267 | g.drawLine(maxX, 1, maxX, maxY); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | private class MinimizeButton extends FrameButton { |
| 272 | public void paintComponent(Graphics g) { |
| 273 | super.paintComponent(g); |
| 274 | g.setColor(highlight); |
| 275 | g.drawLine(7, 8, 7, 11); |
| 276 | g.drawLine(7, 8, 10, 8); |
| 277 | g.setColor(shadow); |
| 278 | g.drawLine(8, 11, 10, 11); |
| 279 | g.drawLine(11, 9, 11, 11); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | private class MaximizeButton extends FrameButton { |
| 284 | public void paintComponent(Graphics g) { |
| 285 | super.paintComponent(g); |
| 286 | int max = BUTTON_SIZE - 5; |
| 287 | boolean isMaxed = frame.isMaximum(); |
| 288 | g.setColor(isMaxed ? shadow : highlight); |
| 289 | g.drawLine(4, 4, 4, max); |
| 290 | g.drawLine(4, 4, max, 4); |
| 291 | g.setColor(isMaxed ? highlight : shadow); |
| 292 | g.drawLine(5, max, max, max); |
| 293 | g.drawLine(max, 5, max, max); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | private class SystemButton extends FrameButton { |
| 298 | public boolean isFocusTraversable() { return false; } |
| 299 | public void requestFocus() {} |
| 300 | |
| 301 | public void paintComponent(Graphics g) { |
| 302 | super.paintComponent(g); |
| 303 | g.setColor(highlight); |
| 304 | g.drawLine(4, 8, 4, 11); |
| 305 | g.drawLine(4, 8, BUTTON_SIZE - 5, 8); |
| 306 | g.setColor(shadow); |
| 307 | g.drawLine(5, 11, BUTTON_SIZE - 5, 11); |
| 308 | g.drawLine(BUTTON_SIZE - 5, 9, BUTTON_SIZE - 5, 11); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | private class Title extends FrameButton { |
| 313 | Title(String title) { |
| 314 | super(); |
| 315 | setText(title); |
| 316 | setHorizontalAlignment(SwingConstants.CENTER); |
| 317 | setBorder(BorderFactory.createBevelBorder( |
| 318 | BevelBorder.RAISED, |
| 319 | UIManager.getColor("activeCaptionBorder"), |
| 320 | UIManager.getColor("inactiveCaptionBorder"))); |
| 321 | |
| 322 | // Forward mouse events to titlebar for moves. |
| 323 | addMouseMotionListener(new MouseMotionListener() { |
| 324 | public void mouseDragged(MouseEvent e) { |
| 325 | forwardEventToParent(e); |
| 326 | } |
| 327 | public void mouseMoved(MouseEvent e) { |
| 328 | forwardEventToParent(e); |
| 329 | } |
| 330 | }); |
| 331 | addMouseListener(new MouseListener() { |
| 332 | public void mouseClicked(MouseEvent e) { |
| 333 | forwardEventToParent(e); |
| 334 | } |
| 335 | public void mousePressed(MouseEvent e) { |
| 336 | forwardEventToParent(e); |
| 337 | } |
| 338 | public void mouseReleased(MouseEvent e) { |
| 339 | forwardEventToParent(e); |
| 340 | } |
| 341 | public void mouseEntered(MouseEvent e) { |
| 342 | forwardEventToParent(e); |
| 343 | } |
| 344 | public void mouseExited(MouseEvent e) { |
| 345 | forwardEventToParent(e); |
| 346 | } |
| 347 | }); |
| 348 | } |
| 349 | |
| 350 | void forwardEventToParent(MouseEvent e) { |
| 351 | getParent().dispatchEvent(new MouseEvent( |
| 352 | getParent(), e.getID(), e.getWhen(), e.getModifiers(), |
| 353 | e.getX(), e.getY(), e.getXOnScreen(), |
| 354 | e.getYOnScreen(), e.getClickCount(), |
| 355 | e.isPopupTrigger(), MouseEvent.NOBUTTON)); |
| 356 | } |
| 357 | |
| 358 | public void paintComponent(Graphics g) { |
| 359 | super.paintComponent(g); |
| 360 | if (frame.isSelected()) { |
| 361 | g.setColor(UIManager.getColor("activeCaptionText")); |
| 362 | } else { |
| 363 | g.setColor(UIManager.getColor("inactiveCaptionText")); |
| 364 | } |
| 365 | Dimension d = getSize(); |
| 366 | String frameTitle = frame.getTitle(); |
| 367 | if (frameTitle != null) { |
| 368 | MotifGraphicsUtils.drawStringInRect(frame, g, frameTitle, |
| 369 | 0, 0, d.width, d.height, |
| 370 | SwingConstants.CENTER); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |