1 /* 2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.apple.laf; 27 28 import java.awt.event.*; 29 import java.beans.*; 30 31 import javax.swing.JComponent; 32 import javax.swing.border.Border; 33 import javax.swing.plaf.ComponentUI; 34 import javax.swing.plaf.basic.*; 35 36 public class AquaSplitPaneUI extends BasicSplitPaneUI implements MouseListener, PropertyChangeListener { 37 static final String DIVIDER_PAINTER_KEY = "JSplitPane.dividerPainter"; 38 AquaSplitPaneUI()39 public AquaSplitPaneUI() { 40 super(); 41 } 42 createUI(final JComponent x)43 public static ComponentUI createUI(final JComponent x) { 44 return new AquaSplitPaneUI(); 45 } 46 createDefaultDivider()47 public BasicSplitPaneDivider createDefaultDivider() { 48 return new AquaSplitPaneDividerUI(this); 49 } 50 installListeners()51 protected void installListeners() { 52 super.installListeners(); 53 splitPane.addPropertyChangeListener(DIVIDER_PAINTER_KEY, this); 54 divider.addMouseListener(this); 55 } 56 uninstallListeners()57 protected void uninstallListeners() { 58 divider.removeMouseListener(this); 59 splitPane.removePropertyChangeListener(DIVIDER_PAINTER_KEY, this); 60 super.uninstallListeners(); 61 } 62 mouseClicked(final MouseEvent e)63 public void mouseClicked(final MouseEvent e) { 64 if (e.getClickCount() < 2) return; 65 if (!splitPane.isOneTouchExpandable()) return; 66 67 final double resizeWeight = splitPane.getResizeWeight(); 68 final int paneWidth = splitPane.getWidth(); 69 final int divSize = splitPane.getDividerSize(); 70 final int divLocation = splitPane.getDividerLocation(); 71 final int lastDivLocation = splitPane.getLastDividerLocation(); 72 73 // if we are at the far edge 74 if (paneWidth - divSize <= divLocation + 5) { 75 splitPane.setDividerLocation(lastDivLocation); 76 return; 77 } 78 79 // if we are at the starting edge 80 if (divSize >= divLocation - 5) { 81 splitPane.setDividerLocation(lastDivLocation); 82 return; 83 } 84 85 // otherwise, jump to the most "appropriate" end 86 if (resizeWeight > 0.5) { 87 splitPane.setDividerLocation(0); 88 } else { 89 splitPane.setDividerLocation(paneWidth); 90 } 91 } 92 mouseEntered(final MouseEvent e)93 public void mouseEntered(final MouseEvent e) { } mouseExited(final MouseEvent e)94 public void mouseExited(final MouseEvent e) { } mousePressed(final MouseEvent e)95 public void mousePressed(final MouseEvent e) { } mouseReleased(final MouseEvent e)96 public void mouseReleased(final MouseEvent e) { } 97 propertyChange(final PropertyChangeEvent evt)98 public void propertyChange(final PropertyChangeEvent evt) { 99 if (!DIVIDER_PAINTER_KEY.equals(evt.getPropertyName())) return; 100 101 final Object value = evt.getNewValue(); 102 if (value instanceof Border) { 103 divider.setBorder((Border)value); 104 } else { 105 divider.setBorder(null); 106 } 107 } 108 } 109