BuddySupport { public static void add(Component c, Position pos, JTextField textField) { TextUIWrapper.getDefaultWrapper().install(textField, true); List<Component> leftBuddies = buddies(Position.LEFT, textField); List<Component> rightBuddies = buddies(Position.RIGHT, textField); setLeft(textField, leftBuddies); setRight(textField, rightBuddies); if (isBuddy(c, textField)) { throw new IllegalStateException("Component already added."); } if (Position.LEFT == pos) { leftBuddies.add(c); } else { rightBuddies.add(0, c); } addToComponentHierarchy(c, pos, textField); }  static void addLeft(Component c, JTextField textField); static void addRight(Component c, JTextField textField); static void add(Component c, Position pos, JTextField textField); static void addGap(int width, Position pos, JTextField textField); static void setRight(JTextField textField, List<Component> rightBuddies); static void setLeft(JTextField textField, List<Component> leftBuddies); static void set(List<Component> buddies, Position pos, JTextField textField); static List<Component> getLeft(JTextField textField); static List<Component> getRight(JTextField textField); static List<Component> getBuddies(Position pos, JTextField textField); static boolean isBuddy(Component c, JTextField textField); static void remove(JComponent c, JTextField textField); static void removeAll(JTextField textField); static void setOuterMargin(JTextField buddyField, Insets margin); static Insets getOuterMargin(JTextField buddyField); static void ensureBuddiesAreInComponentHierarchy(JTextField textField); static Component createGap(int width); static final String OUTER_MARGIN; }
@Test public void testInstall() throws Exception { BuddySupport.add(new JButton(), Position.LEFT, tf); Assert.assertSame(BuddyTextFieldUI.class, tf.getUI().getClass()); Assert.assertSame(BuddyLayoutAndBorder.class, tf.getBorder().getClass()); Assert.assertSame(BuddyLayoutAndBorder.class, tf.getLayout().getClass()); }
@Test public void testAdd() throws Exception { JButton button = new JButton(); BuddySupport.addLeft(button, tf); Assert.assertSame(BuddyLayoutAndBorder.class, tf.getLayout().getClass()); Assert.assertSame(1, tf.getComponentCount()); Assert.assertSame(button, tf.getComponent(0)); }