1 30 31 package com.jgoodies.looks.common; 32 33 import java.awt.Component ; 34 import java.awt.Container ; 35 import java.awt.Dimension ; 36 import java.awt.Insets ; 37 import java.awt.LayoutManager ; 38 39 import javax.swing.UIManager ; 40 41 42 50 public final class ExtBasicSpinnerLayout implements LayoutManager { 51 52 56 private static final Dimension ZERO_SIZE = new Dimension (0, 0); 57 58 59 private Component nextButton = null; 60 private Component previousButton = null; 61 private Component editor = null; 62 63 64 public void addLayoutComponent(String name, Component c) { 65 if ("Next".equals(name)) { 66 nextButton = c; 67 } else if ("Previous".equals(name)) { 68 previousButton = c; 69 } else if ("Editor".equals(name)) { 70 editor = c; 71 } 72 } 73 74 75 public void removeLayoutComponent(Component c) { 76 if (c == nextButton) { 77 c = null; 78 } else if (c == previousButton) { 79 previousButton = null; 80 } else if (c == editor) { 81 editor = null; 82 } 83 } 84 85 86 private Dimension preferredSize(Component c) { 87 return (c == null) ? ZERO_SIZE : c.getPreferredSize(); 88 } 89 90 91 public Dimension preferredLayoutSize(Container parent) { 92 Dimension nextD = preferredSize(nextButton); 93 Dimension previousD = preferredSize(previousButton); 94 Dimension editorD = preferredSize(editor); 95 96 Dimension size = new Dimension (editorD.width, editorD.height); 97 size.width += Math.max(nextD.width, previousD.width); 98 Insets insets = parent.getInsets(); 99 size.width += insets.left + insets.right; 100 size.height += insets.top + insets.bottom; 101 return size; 102 } 103 104 105 public Dimension minimumLayoutSize(Container parent) { 106 return preferredLayoutSize(parent); 107 } 108 109 110 private void setBounds(Component c, int x, int y, int width, int height) { 111 if (c != null) { 112 c.setBounds(x, y, width, height); 113 } 114 } 115 116 117 public void layoutContainer(Container parent) { 118 int width = parent.getWidth(); 119 int height = parent.getHeight(); 120 121 Insets insets = parent.getInsets(); 122 Dimension nextD = preferredSize(nextButton); 123 Dimension previousD = preferredSize(previousButton); 124 int buttonsWidth = Math.max(nextD.width, previousD.width); 125 int editorHeight = height - (insets.top + insets.bottom); 126 127 Insets buttonInsets = UIManager 133 .getInsets("Spinner.arrowButtonInsets"); 134 if (buttonInsets == null) { 135 buttonInsets = insets; 136 } 137 138 141 int editorX, editorWidth, buttonsX; 142 if (parent.getComponentOrientation().isLeftToRight()) { 143 editorX = insets.left; 144 editorWidth = width - insets.left - buttonsWidth 145 - buttonInsets.right; 146 buttonsX = width - buttonsWidth - buttonInsets.right; 147 } else { 148 buttonsX = buttonInsets.left; 149 editorX = buttonsX + buttonsWidth; 150 editorWidth = width - buttonInsets.left - buttonsWidth 151 - insets.right; 152 } 153 154 int nextY = buttonInsets.top; 155 int nextHeight = (height / 2) + (height % 2) - nextY; 156 int previousY = buttonInsets.top + nextHeight; 157 int previousHeight = height - previousY - buttonInsets.bottom; 158 159 setBounds(editor, editorX, insets.top, editorWidth, editorHeight); 160 setBounds(nextButton, buttonsX, nextY, buttonsWidth, nextHeight); 161 setBounds(previousButton, buttonsX, previousY, buttonsWidth, 162 previousHeight); 163 } 164 } | Popular Tags |