1 7 8 package javax.swing.plaf.basic; 9 10 import com.sun.java.swing.SwingUtilities2; 11 import java.awt.*; 12 import java.awt.event.*; 13 import javax.accessibility.AccessibleContext ; 14 import javax.swing.*; 15 import javax.swing.plaf.*; 16 import javax.swing.border.*; 17 import javax.swing.event.InternalFrameEvent ; 18 import java.util.EventListener ; 19 import java.beans.PropertyChangeListener ; 20 import java.beans.PropertyChangeEvent ; 21 import java.beans.VetoableChangeListener ; 22 import java.beans.PropertyVetoException ; 23 24 import sun.swing.DefaultLookup; 25 import sun.swing.UIAction; 26 27 43 public class BasicInternalFrameTitlePane extends JComponent 44 { 45 protected JMenuBar menuBar; 46 protected JButton iconButton; 47 protected JButton maxButton; 48 protected JButton closeButton; 49 50 protected JMenu windowMenu; 51 protected JInternalFrame frame; 52 53 protected Color selectedTitleColor; 54 protected Color selectedTextColor; 55 protected Color notSelectedTitleColor; 56 protected Color notSelectedTextColor; 57 58 protected Icon maxIcon; 59 protected Icon minIcon; 60 protected Icon iconIcon; 61 protected Icon closeIcon; 62 63 protected PropertyChangeListener propertyChangeListener; 64 65 protected Action closeAction; 66 protected Action maximizeAction; 67 protected Action iconifyAction; 68 protected Action restoreAction; 69 protected Action moveAction; 70 protected Action sizeAction; 71 72 protected static final String CLOSE_CMD = 73 UIManager.getString("InternalFrameTitlePane.closeButtonText"); 74 protected static final String ICONIFY_CMD = 75 UIManager.getString("InternalFrameTitlePane.minimizeButtonText"); 76 protected static final String RESTORE_CMD = 77 UIManager.getString("InternalFrameTitlePane.restoreButtonText"); 78 protected static final String MAXIMIZE_CMD = 79 UIManager.getString("InternalFrameTitlePane.maximizeButtonText"); 80 protected static final String MOVE_CMD = 81 UIManager.getString("InternalFrameTitlePane.moveButtonText"); 82 protected static final String SIZE_CMD = 83 UIManager.getString("InternalFrameTitlePane.sizeButtonText"); 84 85 private String closeButtonToolTip; 86 private String iconButtonToolTip; 87 private String restoreButtonToolTip; 88 private String maxButtonToolTip; 89 private Handler handler; 90 91 public BasicInternalFrameTitlePane(JInternalFrame f) { 92 frame = f; 93 installTitlePane(); 94 } 95 96 protected void installTitlePane() { 97 installDefaults(); 98 installListeners(); 99 100 createActions(); 101 enableActions(); 102 createActionMap(); 103 104 setLayout(createLayout()); 105 106 assembleSystemMenu(); 107 createButtons(); 108 addSubComponents(); 109 110 } 111 112 protected void addSubComponents() { 113 add(menuBar); 114 add(iconButton); 115 add(maxButton); 116 add(closeButton); 117 } 118 119 protected void createActions() { 120 maximizeAction = new MaximizeAction(); 121 iconifyAction = new IconifyAction(); 122 closeAction = new CloseAction(); 123 restoreAction = new RestoreAction(); 124 moveAction = new MoveAction(); 125 sizeAction = new SizeAction(); 126 } 127 128 ActionMap createActionMap() { 129 ActionMap map = new ActionMapUIResource(); 130 map.put("showSystemMenu", new ShowSystemMenuAction(true)); 131 map.put("hideSystemMenu", new ShowSystemMenuAction(false)); 132 return map; 133 } 134 135 protected void installListeners() { 136 if( propertyChangeListener == null ) { 137 propertyChangeListener = createPropertyChangeListener(); 138 } 139 frame.addPropertyChangeListener(propertyChangeListener); 140 } 141 142 protected void uninstallListeners() { 143 frame.removePropertyChangeListener(propertyChangeListener); 144 handler = null; 145 } 146 147 protected void installDefaults() { 148 maxIcon = UIManager.getIcon("InternalFrame.maximizeIcon"); 149 minIcon = UIManager.getIcon("InternalFrame.minimizeIcon"); 150 iconIcon = UIManager.getIcon("InternalFrame.iconifyIcon"); 151 closeIcon = UIManager.getIcon("InternalFrame.closeIcon"); 152 153 selectedTitleColor = UIManager.getColor("InternalFrame.activeTitleBackground"); 154 selectedTextColor = UIManager.getColor("InternalFrame.activeTitleForeground"); 155 notSelectedTitleColor = UIManager.getColor("InternalFrame.inactiveTitleBackground"); 156 notSelectedTextColor = UIManager.getColor("InternalFrame.inactiveTitleForeground"); 157 setFont(UIManager.getFont("InternalFrame.titleFont")); 158 closeButtonToolTip = 159 UIManager.getString("InternalFrame.closeButtonToolTip"); 160 iconButtonToolTip = 161 UIManager.getString("InternalFrame.iconButtonToolTip"); 162 restoreButtonToolTip = 163 UIManager.getString("InternalFrame.restoreButtonToolTip"); 164 maxButtonToolTip = 165 UIManager.getString("InternalFrame.maxButtonToolTip"); 166 } 167 168 169 protected void uninstallDefaults() { 170 } 171 172 protected void createButtons() { 173 iconButton = new NoFocusButton( 174 "InternalFrameTitlePane.iconifyButtonAccessibleName"); 175 iconButton.addActionListener(iconifyAction); 176 if (iconButtonToolTip != null && iconButtonToolTip.length() != 0) { 177 iconButton.setToolTipText(iconButtonToolTip); 178 } 179 180 maxButton = new NoFocusButton( 181 "InternalFrameTitlePane.maximizeButtonAccessibleName"); 182 maxButton.addActionListener(maximizeAction); 183 184 closeButton = new NoFocusButton( 185 "InternalFrameTitlePane.closeButtonAccessibleName"); 186 closeButton.addActionListener(closeAction); 187 if (closeButtonToolTip != null && closeButtonToolTip.length() != 0) { 188 closeButton.setToolTipText(closeButtonToolTip); 189 } 190 191 setButtonIcons(); 192 } 193 194 protected void setButtonIcons() { 195 if(frame.isIcon()) { 196 if (minIcon != null) { 197 iconButton.setIcon(minIcon); 198 } 199 if (restoreButtonToolTip != null && 200 restoreButtonToolTip.length() != 0) { 201 iconButton.setToolTipText(restoreButtonToolTip); 202 } 203 if (maxIcon != null) { 204 maxButton.setIcon(maxIcon); 205 } 206 if (maxButtonToolTip != null && maxButtonToolTip.length() != 0) { 207 maxButton.setToolTipText(maxButtonToolTip); 208 } 209 } else if (frame.isMaximum()) { 210 if (iconIcon != null) { 211 iconButton.setIcon(iconIcon); 212 } 213 if (iconButtonToolTip != null && iconButtonToolTip.length() != 0) { 214 iconButton.setToolTipText(iconButtonToolTip); 215 } 216 if (minIcon != null) { 217 maxButton.setIcon(minIcon); 218 } 219 if (restoreButtonToolTip != null && 220 restoreButtonToolTip.length() != 0) { 221 maxButton.setToolTipText(restoreButtonToolTip); 222 } 223 } else { 224 if (iconIcon != null) { 225 iconButton.setIcon(iconIcon); 226 } 227 if (iconButtonToolTip != null && iconButtonToolTip.length() != 0) { 228 iconButton.setToolTipText(iconButtonToolTip); 229 } 230 if (maxIcon != null) { 231 maxButton.setIcon(maxIcon); 232 } 233 if (maxButtonToolTip != null && maxButtonToolTip.length() != 0) { 234 maxButton.setToolTipText(maxButtonToolTip); 235 } 236 } 237 if (closeIcon != null) { 238 closeButton.setIcon(closeIcon); 239 } 240 } 241 242 protected void assembleSystemMenu() { 243 menuBar = createSystemMenuBar(); 244 windowMenu = createSystemMenu(); 245 menuBar.add(windowMenu); 246 addSystemMenuItems(windowMenu); 247 enableActions(); 248 } 249 250 protected void addSystemMenuItems(JMenu systemMenu) { 251 JMenuItem mi = (JMenuItem)systemMenu.add(restoreAction); 252 mi.setMnemonic('R'); 253 mi = (JMenuItem)systemMenu.add(moveAction); 254 mi.setMnemonic('M'); 255 mi = (JMenuItem)systemMenu.add(sizeAction); 256 mi.setMnemonic('S'); 257 mi = (JMenuItem)systemMenu.add(iconifyAction); 258 mi.setMnemonic('n'); 259 mi = (JMenuItem)systemMenu.add(maximizeAction); 260 mi.setMnemonic('x'); 261 systemMenu.add(new JSeparator()); 262 mi = (JMenuItem)systemMenu.add(closeAction); 263 mi.setMnemonic('C'); 264 } 265 266 protected JMenu createSystemMenu() { 267 return new JMenu(" "); 268 } 269 270 protected JMenuBar createSystemMenuBar() { 271 menuBar = new SystemMenuBar(); 272 menuBar.setBorderPainted(false); 273 return menuBar; 274 } 275 276 protected void showSystemMenu(){ 277 windowMenu.doClick(); 280 } 281 282 public void paintComponent(Graphics g) { 283 paintTitleBackground(g); 284 285 if(frame.getTitle() != null) { 286 boolean isSelected = frame.isSelected(); 287 Font f = g.getFont(); 288 g.setFont(getFont()); 289 if(isSelected) 290 g.setColor(selectedTextColor); 291 else 292 g.setColor(notSelectedTextColor); 293 294 FontMetrics fm = SwingUtilities2.getFontMetrics(frame, g); 296 int baseline = (getHeight() + fm.getAscent() - fm.getLeading() - 297 fm.getDescent()) / 2; 298 299 int titleX; 300 Rectangle r = new Rectangle(0, 0, 0, 0); 301 if (frame.isIconifiable()) r = iconButton.getBounds(); 302 else if (frame.isMaximizable()) r = maxButton.getBounds(); 303 else if (frame.isClosable()) r = closeButton.getBounds(); 304 int titleW; 305 306 String title = frame.getTitle(); 307 if( BasicGraphicsUtils.isLeftToRight(frame) ) { 308 if (r.x == 0) r.x = frame.getWidth()-frame.getInsets().right; 309 titleX = menuBar.getX() + menuBar.getWidth() + 2; 310 titleW = r.x - titleX - 3; 311 title = getTitle(frame.getTitle(), fm, titleW); 312 } else { 313 titleX = menuBar.getX() - 2 314 - SwingUtilities2.stringWidth(frame,fm,title); 315 } 316 317 SwingUtilities2.drawString(frame, g, title, titleX, baseline); 318 g.setFont(f); 319 } 320 } 321 322 329 protected void paintTitleBackground(Graphics g) { 330 boolean isSelected = frame.isSelected(); 331 332 if(isSelected) 333 g.setColor(selectedTitleColor); 334 else 335 g.setColor(notSelectedTitleColor); 336 g.fillRect(0, 0, getWidth(), getHeight()); 337 } 338 339 protected String getTitle(String text, FontMetrics fm, int availTextWidth) { 340 return SwingUtilities2.clipStringIfNecessary( 341 frame, fm, text, availTextWidth); 342 } 343 344 348 protected void postClosingEvent(JInternalFrame frame) { 349 InternalFrameEvent e = new InternalFrameEvent ( 350 frame, InternalFrameEvent.INTERNAL_FRAME_CLOSING); 351 if (JInternalFrame.class.getClassLoader() == null) { 353 try { 354 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e); 355 return; 356 } catch (SecurityException se) { 357 } 359 } 360 frame.dispatchEvent(e); 361 } 362 363 364 protected void enableActions() { 365 restoreAction.setEnabled(frame.isMaximum() || frame.isIcon()); 366 maximizeAction.setEnabled( 367 (frame.isMaximizable() && !frame.isMaximum() && !frame.isIcon()) || 368 (frame.isMaximizable() && frame.isIcon())); 369 iconifyAction.setEnabled(frame.isIconifiable() && !frame.isIcon()); 370 closeAction.setEnabled(frame.isClosable()); 371 sizeAction.setEnabled(false); 372 moveAction.setEnabled(false); 373 } 374 375 private Handler getHandler() { 376 if (handler == null) { 377 handler = new Handler(); 378 } 379 return handler; 380 } 381 382 protected PropertyChangeListener createPropertyChangeListener() { 383 return getHandler(); 384 } 385 386 protected LayoutManager createLayout() { 387 return getHandler(); 388 } 389 390 391 private class Handler implements LayoutManager, PropertyChangeListener { 392 public void propertyChange(PropertyChangeEvent evt) { 396 String prop = (String )evt.getPropertyName(); 397 398 if (prop == JInternalFrame.IS_SELECTED_PROPERTY) { 399 repaint(); 400 return; 401 } 402 403 if (prop == JInternalFrame.IS_ICON_PROPERTY || 404 prop == JInternalFrame.IS_MAXIMUM_PROPERTY) { 405 setButtonIcons(); 406 enableActions(); 407 return; 408 } 409 410 if ("closable" == prop) { 411 if ((Boolean )evt.getNewValue() == Boolean.TRUE) { 412 add(closeButton); 413 } else { 414 remove(closeButton); 415 } 416 } else if ("maximizable" == prop) { 417 if ((Boolean )evt.getNewValue() == Boolean.TRUE) { 418 add(maxButton); 419 } else { 420 remove(maxButton); 421 } 422 } else if ("iconable" == prop) { 423 if ((Boolean )evt.getNewValue() == Boolean.TRUE) { 424 add(iconButton); 425 } else { 426 remove(iconButton); 427 } 428 } 429 enableActions(); 430 431 revalidate(); 432 repaint(); 433 } 434 435 436 public void addLayoutComponent(String name, Component c) {} 440 public void removeLayoutComponent(Component c) {} 441 public Dimension preferredLayoutSize(Container c) { 442 return minimumLayoutSize(c); 443 } 444 445 public Dimension minimumLayoutSize(Container c) { 446 int width = 22; 448 449 if (frame.isClosable()) { 450 width += 19; 451 } 452 if (frame.isMaximizable()) { 453 width += 19; 454 } 455 if (frame.isIconifiable()) { 456 width += 19; 457 } 458 459 FontMetrics fm = frame.getFontMetrics(getFont()); 460 String frameTitle = frame.getTitle(); 461 int title_w = frameTitle != null ? SwingUtilities2.stringWidth( 462 frame, fm, frameTitle) : 0; 463 int title_length = frameTitle != null ? frameTitle.length() : 0; 464 465 if (title_length > 3) { 467 int subtitle_w = SwingUtilities2.stringWidth( 468 frame, fm, frameTitle.substring(0, 3) + "..."); 469 width += (title_w < subtitle_w) ? title_w : subtitle_w; 470 } else { 471 width += title_w; 472 } 473 474 Icon icon = frame.getFrameIcon(); 476 int fontHeight = fm.getHeight(); 477 fontHeight += 2; 478 int iconHeight = 0; 479 if (icon != null) { 480 iconHeight = Math.min(icon.getIconHeight(), 16); 482 } 483 iconHeight += 2; 484 485 int height = Math.max( fontHeight, iconHeight ); 486 487 Dimension dim = new Dimension(width, height); 488 489 if (getBorder() != null) { 491 Insets insets = getBorder().getBorderInsets(c); 492 dim.height += insets.top + insets.bottom; 493 dim.width += insets.left + insets.right; 494 } 495 return dim; 496 } 497 498 public void layoutContainer(Container c) { 499 boolean leftToRight = BasicGraphicsUtils.isLeftToRight(frame); 500 501 int w = getWidth(); 502 int h = getHeight(); 503 int x; 504 505 int buttonHeight = closeButton.getIcon().getIconHeight(); 506 507 Icon icon = frame.getFrameIcon(); 508 int iconHeight = 0; 509 if (icon != null) { 510 iconHeight = icon.getIconHeight(); 511 } 512 x = (leftToRight) ? 2 : w - 16 - 2; 513 menuBar.setBounds(x, (h - iconHeight) / 2, 16, 16); 514 515 x = (leftToRight) ? w - 16 - 2 : 2; 516 517 if (frame.isClosable()) { 518 closeButton.setBounds(x, (h - buttonHeight) / 2, 16, 14); 519 x += (leftToRight) ? -(16 + 2) : 16 + 2; 520 } 521 522 if (frame.isMaximizable()) { 523 maxButton.setBounds(x, (h - buttonHeight) / 2, 16, 14); 524 x += (leftToRight) ? -(16 + 2) : 16 + 2; 525 } 526 527 if (frame.isIconifiable()) { 528 iconButton.setBounds(x, (h - buttonHeight) / 2, 16, 14); 529 } 530 } 531 } 532 533 537 public class PropertyChangeHandler implements PropertyChangeListener { 538 public void propertyChange(PropertyChangeEvent evt) { 543 getHandler().propertyChange(evt); 544 } 545 } 546 547 551 public class TitlePaneLayout implements LayoutManager { 552 public void addLayoutComponent(String name, Component c) { 557 getHandler().addLayoutComponent(name, c); 558 } 559 560 public void removeLayoutComponent(Component c) { 561 getHandler().removeLayoutComponent(c); 562 } 563 564 public Dimension preferredLayoutSize(Container c) { 565 return getHandler().preferredLayoutSize(c); 566 } 567 568 public Dimension minimumLayoutSize(Container c) { 569 return getHandler().minimumLayoutSize(c); 570 } 571 572 public void layoutContainer(Container c) { 573 getHandler().layoutContainer(c); 574 } 575 } 576 577 581 public class CloseAction extends AbstractAction { 582 public CloseAction() { 583 super(CLOSE_CMD); 584 } 585 586 public void actionPerformed(ActionEvent e) { 587 if(frame.isClosable()) { 588 frame.doDefaultCloseAction(); 589 } 590 } 591 } 593 597 public class MaximizeAction extends AbstractAction { 598 public MaximizeAction() { 599 super(MAXIMIZE_CMD); 600 } 601 602 public void actionPerformed(ActionEvent evt) { 603 if (frame.isMaximizable()) { 604 if (frame.isMaximum() && frame.isIcon()) { 605 try { 606 frame.setIcon(false); 607 } catch (PropertyVetoException e) { } 608 } else if (!frame.isMaximum()) { 609 try { 610 frame.setMaximum(true); 611 } catch (PropertyVetoException e) { } 612 } else { 613 try { 614 frame.setMaximum(false); 615 } catch (PropertyVetoException e) { } 616 } 617 } 618 } 619 } 620 621 625 public class IconifyAction extends AbstractAction { 626 public IconifyAction() { 627 super(ICONIFY_CMD); 628 } 629 630 public void actionPerformed(ActionEvent e) { 631 if(frame.isIconifiable()) { 632 if(!frame.isIcon()) { 633 try { frame.setIcon(true); } catch (PropertyVetoException e1) { } 634 } else{ 635 try { frame.setIcon(false); } catch (PropertyVetoException e1) { } 636 } 637 } 638 } 639 } 641 645 public class RestoreAction extends AbstractAction { 646 public RestoreAction() { 647 super(RESTORE_CMD); 648 } 649 650 public void actionPerformed(ActionEvent evt) { 651 if (frame.isMaximizable() && frame.isMaximum() && frame.isIcon()) { 652 try { 653 frame.setIcon(false); 654 } catch (PropertyVetoException e) { } 655 } else if (frame.isMaximizable() && frame.isMaximum()) { 656 try { 657 frame.setMaximum(false); 658 } catch (PropertyVetoException e) { } 659 } else if (frame.isIconifiable() && frame.isIcon()) { 660 try { 661 frame.setIcon(false); 662 } catch (PropertyVetoException e) { } 663 } 664 } 665 } 666 667 671 public class MoveAction extends AbstractAction { 672 public MoveAction() { 673 super(MOVE_CMD); 674 } 675 676 public void actionPerformed(ActionEvent e) { 677 } 679 } 681 684 private class ShowSystemMenuAction extends AbstractAction { 685 private boolean show; 687 public ShowSystemMenuAction(boolean show) { 688 this.show = show; 689 } 690 691 public void actionPerformed(ActionEvent e) { 692 if (show) { 693 windowMenu.doClick(); 694 } else { 695 windowMenu.setVisible(false); 696 } 697 } 698 } 699 700 704 public class SizeAction extends AbstractAction { 705 public SizeAction() { 706 super(SIZE_CMD); 707 } 708 709 public void actionPerformed(ActionEvent e) { 710 } 712 } 714 715 719 public class SystemMenuBar extends JMenuBar { 720 public boolean isFocusTraversable() { return false; } 721 public void requestFocus() {} 722 public void paint(Graphics g) { 723 Icon icon = frame.getFrameIcon(); 724 if (icon == null) { 725 icon = (Icon)DefaultLookup.get(frame, frame.getUI(), 726 "InternalFrame.icon"); 727 } 728 if (icon != null) { 729 if (icon instanceof ImageIcon && (icon.getIconWidth() > 16 || icon.getIconHeight() > 16)) { 731 Image img = ((ImageIcon)icon).getImage(); 732 ((ImageIcon)icon).setImage(img.getScaledInstance(16, 16, Image.SCALE_SMOOTH)); 733 } 734 icon.paintIcon(this, g, 0, 0); 735 } 736 } 737 738 public boolean isOpaque() { 739 return true; 740 } 741 } 743 744 private class NoFocusButton extends JButton { 745 private String uiKey; 746 public NoFocusButton(String uiKey) { 747 setFocusPainted(false); 748 setMargin(new Insets(0,0,0,0)); 749 setOpaque(true); 750 this.uiKey = uiKey; 751 } 752 public boolean isFocusTraversable() { return false; } 753 public void requestFocus() {}; 754 public AccessibleContext getAccessibleContext() { 755 AccessibleContext ac = super.getAccessibleContext(); 756 if (uiKey != null) { 757 ac.setAccessibleName(UIManager.getString(uiKey)); 758 uiKey = null; 759 } 760 return ac; 761 } 762 }; 764 } 766 | Popular Tags |