0N/A/*
2362N/A * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing.tree;
0N/A
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.border.*;
0N/Aimport javax.swing.event.*;
0N/Aimport javax.swing.plaf.FontUIResource;
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.beans.*;
0N/Aimport java.io.*;
0N/Aimport java.util.EventObject;
0N/Aimport java.util.Vector;
0N/A
0N/A/**
0N/A * A <code>TreeCellEditor</code>. You need to supply an
0N/A * instance of <code>DefaultTreeCellRenderer</code>
0N/A * so that the icons can be obtained. You can optionally supply
0N/A * a <code>TreeCellEditor</code> that will be layed out according
0N/A * to the icon in the <code>DefaultTreeCellRenderer</code>.
0N/A * If you do not supply a <code>TreeCellEditor</code>,
0N/A * a <code>TextField</code> will be used. Editing is started
0N/A * on a triple mouse click, or after a click, pause, click and
0N/A * a delay of 1200 miliseconds.
0N/A *<p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @see javax.swing.JTree
0N/A *
0N/A * @author Scott Violet
0N/A */
0N/Apublic class DefaultTreeCellEditor implements ActionListener, TreeCellEditor,
0N/A TreeSelectionListener {
0N/A /** Editor handling the editing. */
0N/A protected TreeCellEditor realEditor;
0N/A
0N/A /** Renderer, used to get border and offsets from. */
0N/A protected DefaultTreeCellRenderer renderer;
0N/A
0N/A /** Editing container, will contain the <code>editorComponent</code>. */
0N/A protected Container editingContainer;
0N/A
0N/A /**
0N/A * Component used in editing, obtained from the
0N/A * <code>editingContainer</code>.
0N/A */
0N/A transient protected Component editingComponent;
0N/A
0N/A /**
0N/A * As of Java 2 platform v1.4 this field should no longer be used. If
0N/A * you wish to provide similar behavior you should directly override
0N/A * <code>isCellEditable</code>.
0N/A */
0N/A protected boolean canEdit;
0N/A
0N/A /**
0N/A * Used in editing. Indicates x position to place
0N/A * <code>editingComponent</code>.
0N/A */
0N/A protected transient int offset;
0N/A
0N/A /** <code>JTree</code> instance listening too. */
0N/A protected transient JTree tree;
0N/A
0N/A /** Last path that was selected. */
0N/A protected transient TreePath lastPath;
0N/A
0N/A /** Used before starting the editing session. */
0N/A protected transient Timer timer;
0N/A
0N/A /**
0N/A * Row that was last passed into
0N/A * <code>getTreeCellEditorComponent</code>.
0N/A */
0N/A protected transient int lastRow;
0N/A
0N/A /** True if the border selection color should be drawn. */
0N/A protected Color borderSelectionColor;
0N/A
0N/A /** Icon to use when editing. */
0N/A protected transient Icon editingIcon;
0N/A
0N/A /**
0N/A * Font to paint with, <code>null</code> indicates
0N/A * font of renderer is to be used.
0N/A */
0N/A protected Font font;
0N/A
0N/A
0N/A /**
0N/A * Constructs a <code>DefaultTreeCellEditor</code>
0N/A * object for a JTree using the specified renderer and
0N/A * a default editor. (Use this constructor for normal editing.)
0N/A *
0N/A * @param tree a <code>JTree</code> object
0N/A * @param renderer a <code>DefaultTreeCellRenderer</code> object
0N/A */
0N/A public DefaultTreeCellEditor(JTree tree,
0N/A DefaultTreeCellRenderer renderer) {
0N/A this(tree, renderer, null);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>DefaultTreeCellEditor</code>
0N/A * object for a <code>JTree</code> using the
0N/A * specified renderer and the specified editor. (Use this constructor
0N/A * for specialized editing.)
0N/A *
0N/A * @param tree a <code>JTree</code> object
0N/A * @param renderer a <code>DefaultTreeCellRenderer</code> object
0N/A * @param editor a <code>TreeCellEditor</code> object
0N/A */
0N/A public DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer,
0N/A TreeCellEditor editor) {
0N/A this.renderer = renderer;
0N/A realEditor = editor;
0N/A if(realEditor == null)
0N/A realEditor = createTreeCellEditor();
0N/A editingContainer = createContainer();
0N/A setTree(tree);
0N/A setBorderSelectionColor(UIManager.getColor
0N/A ("Tree.editorBorderSelectionColor"));
0N/A }
0N/A
0N/A /**
0N/A * Sets the color to use for the border.
0N/A * @param newColor the new border color
0N/A */
0N/A public void setBorderSelectionColor(Color newColor) {
0N/A borderSelectionColor = newColor;
0N/A }
0N/A
0N/A /**
0N/A * Returns the color the border is drawn.
0N/A * @return the border selection color
0N/A */
0N/A public Color getBorderSelectionColor() {
0N/A return borderSelectionColor;
0N/A }
0N/A
0N/A /**
0N/A * Sets the font to edit with. <code>null</code> indicates
0N/A * the renderers font should be used. This will NOT
0N/A * override any font you have set in the editor
0N/A * the receiver was instantied with. If <code>null</code>
0N/A * for an editor was passed in a default editor will be
0N/A * created that will pick up this font.
0N/A *
0N/A * @param font the editing <code>Font</code>
0N/A * @see #getFont
0N/A */
0N/A public void setFont(Font font) {
0N/A this.font = font;
0N/A }
0N/A
0N/A /**
0N/A * Gets the font used for editing.
0N/A *
0N/A * @return the editing <code>Font</code>
0N/A * @see #setFont
0N/A */
0N/A public Font getFont() {
0N/A return font;
0N/A }
0N/A
0N/A //
0N/A // TreeCellEditor
0N/A //
0N/A
0N/A /**
0N/A * Configures the editor. Passed onto the <code>realEditor</code>.
0N/A */
0N/A public Component getTreeCellEditorComponent(JTree tree, Object value,
0N/A boolean isSelected,
0N/A boolean expanded,
0N/A boolean leaf, int row) {
0N/A setTree(tree);
0N/A lastRow = row;
0N/A determineOffset(tree, value, isSelected, expanded, leaf, row);
0N/A
0N/A if (editingComponent != null) {
0N/A editingContainer.remove(editingComponent);
0N/A }
0N/A editingComponent = realEditor.getTreeCellEditorComponent(tree, value,
0N/A isSelected, expanded,leaf, row);
0N/A
0N/A
0N/A // this is kept for backwards compatability but isn't really needed
0N/A // with the current BasicTreeUI implementation.
0N/A TreePath newPath = tree.getPathForRow(row);
0N/A
0N/A canEdit = (lastPath != null && newPath != null &&
0N/A lastPath.equals(newPath));
0N/A
0N/A Font font = getFont();
0N/A
0N/A if(font == null) {
0N/A if(renderer != null)
0N/A font = renderer.getFont();
0N/A if(font == null)
0N/A font = tree.getFont();
0N/A }
0N/A editingContainer.setFont(font);
0N/A prepareForEditing();
0N/A return editingContainer;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value currently being edited.
0N/A * @return the value currently being edited
0N/A */
0N/A public Object getCellEditorValue() {
0N/A return realEditor.getCellEditorValue();
0N/A }
0N/A
0N/A /**
0N/A * If the <code>realEditor</code> returns true to this
0N/A * message, <code>prepareForEditing</code>
0N/A * is messaged and true is returned.
0N/A */
0N/A public boolean isCellEditable(EventObject event) {
0N/A boolean retValue = false;
0N/A boolean editable = false;
0N/A
0N/A if (event != null) {
0N/A if (event.getSource() instanceof JTree) {
0N/A setTree((JTree)event.getSource());
0N/A if (event instanceof MouseEvent) {
0N/A TreePath path = tree.getPathForLocation(
0N/A ((MouseEvent)event).getX(),
0N/A ((MouseEvent)event).getY());
0N/A editable = (lastPath != null && path != null &&
0N/A lastPath.equals(path));
0N/A if (path!=null) {
0N/A lastRow = tree.getRowForPath(path);
0N/A Object value = path.getLastPathComponent();
0N/A boolean isSelected = tree.isRowSelected(lastRow);
0N/A boolean expanded = tree.isExpanded(path);
0N/A TreeModel treeModel = tree.getModel();
0N/A boolean leaf = treeModel.isLeaf(value);
0N/A determineOffset(tree, value, isSelected,
0N/A expanded, leaf, lastRow);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A if(!realEditor.isCellEditable(event))
0N/A return false;
0N/A if(canEditImmediately(event))
0N/A retValue = true;
0N/A else if(editable && shouldStartEditingTimer(event)) {
0N/A startEditingTimer();
0N/A }
0N/A else if(timer != null && timer.isRunning())
0N/A timer.stop();
0N/A if(retValue)
0N/A prepareForEditing();
0N/A return retValue;
0N/A }
0N/A
0N/A /**
0N/A * Messages the <code>realEditor</code> for the return value.
0N/A */
0N/A public boolean shouldSelectCell(EventObject event) {
0N/A return realEditor.shouldSelectCell(event);
0N/A }
0N/A
0N/A /**
0N/A * If the <code>realEditor</code> will allow editing to stop,
0N/A * the <code>realEditor</code> is removed and true is returned,
0N/A * otherwise false is returned.
0N/A */
0N/A public boolean stopCellEditing() {
0N/A if(realEditor.stopCellEditing()) {
0N/A cleanupAfterEditing();
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Messages <code>cancelCellEditing</code> to the
0N/A * <code>realEditor</code> and removes it from this instance.
0N/A */
0N/A public void cancelCellEditing() {
0N/A realEditor.cancelCellEditing();
0N/A cleanupAfterEditing();
0N/A }
0N/A
0N/A /**
0N/A * Adds the <code>CellEditorListener</code>.
0N/A * @param l the listener to be added
0N/A */
0N/A public void addCellEditorListener(CellEditorListener l) {
0N/A realEditor.addCellEditorListener(l);
0N/A }
0N/A
0N/A /**
0N/A * Removes the previously added <code>CellEditorListener</code>.
0N/A * @param l the listener to be removed
0N/A */
0N/A public void removeCellEditorListener(CellEditorListener l) {
0N/A realEditor.removeCellEditorListener(l);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the <code>CellEditorListener</code>s added
0N/A * to this DefaultTreeCellEditor with addCellEditorListener().
0N/A *
0N/A * @return all of the <code>CellEditorListener</code>s added or an empty
0N/A * array if no listeners have been added
0N/A * @since 1.4
0N/A */
0N/A public CellEditorListener[] getCellEditorListeners() {
0N/A return ((DefaultCellEditor)realEditor).getCellEditorListeners();
0N/A }
0N/A
0N/A //
0N/A // TreeSelectionListener
0N/A //
0N/A
0N/A /**
0N/A * Resets <code>lastPath</code>.
0N/A */
0N/A public void valueChanged(TreeSelectionEvent e) {
0N/A if(tree != null) {
0N/A if(tree.getSelectionCount() == 1)
0N/A lastPath = tree.getSelectionPath();
0N/A else
0N/A lastPath = null;
0N/A }
0N/A if(timer != null) {
0N/A timer.stop();
0N/A }
0N/A }
0N/A
0N/A //
0N/A // ActionListener (for Timer).
0N/A //
0N/A
0N/A /**
0N/A * Messaged when the timer fires, this will start the editing
0N/A * session.
0N/A */
0N/A public void actionPerformed(ActionEvent e) {
0N/A if(tree != null && lastPath != null) {
0N/A tree.startEditingAtPath(lastPath);
0N/A }
0N/A }
0N/A
0N/A //
0N/A // Local methods
0N/A //
0N/A
0N/A /**
0N/A * Sets the tree currently editing for. This is needed to add
0N/A * a selection listener.
0N/A * @param newTree the new tree to be edited
0N/A */
0N/A protected void setTree(JTree newTree) {
0N/A if(tree != newTree) {
0N/A if(tree != null)
0N/A tree.removeTreeSelectionListener(this);
0N/A tree = newTree;
0N/A if(tree != null)
0N/A tree.addTreeSelectionListener(this);
0N/A if(timer != null) {
0N/A timer.stop();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if <code>event</code> is a <code>MouseEvent</code>
0N/A * and the click count is 1.
0N/A * @param event the event being studied
0N/A */
0N/A protected boolean shouldStartEditingTimer(EventObject event) {
0N/A if((event instanceof MouseEvent) &&
0N/A SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
0N/A MouseEvent me = (MouseEvent)event;
0N/A
0N/A return (me.getClickCount() == 1 &&
0N/A inHitRegion(me.getX(), me.getY()));
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Starts the editing timer.
0N/A */
0N/A protected void startEditingTimer() {
0N/A if(timer == null) {
0N/A timer = new Timer(1200, this);
0N/A timer.setRepeats(false);
0N/A }
0N/A timer.start();
0N/A }
0N/A
0N/A /**
0N/A * Returns true if <code>event</code> is <code>null</code>,
0N/A * or it is a <code>MouseEvent</code> with a click count > 2
0N/A * and <code>inHitRegion</code> returns true.
0N/A * @param event the event being studied
0N/A */
0N/A protected boolean canEditImmediately(EventObject event) {
0N/A if((event instanceof MouseEvent) &&
0N/A SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
0N/A MouseEvent me = (MouseEvent)event;
0N/A
0N/A return ((me.getClickCount() > 2) &&
0N/A inHitRegion(me.getX(), me.getY()));
0N/A }
0N/A return (event == null);
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the passed in location is a valid mouse location
0N/A * to start editing from. This is implemented to return false if
0N/A * <code>x</code> is <= the width of the icon and icon gap displayed
0N/A * by the renderer. In other words this returns true if the user
0N/A * clicks over the text part displayed by the renderer, and false
0N/A * otherwise.
0N/A * @param x the x-coordinate of the point
0N/A * @param y the y-coordinate of the point
0N/A * @return true if the passed in location is a valid mouse location
0N/A */
0N/A protected boolean inHitRegion(int x, int y) {
0N/A if(lastRow != -1 && tree != null) {
0N/A Rectangle bounds = tree.getRowBounds(lastRow);
0N/A ComponentOrientation treeOrientation = tree.getComponentOrientation();
0N/A
0N/A if ( treeOrientation.isLeftToRight() ) {
0N/A if (bounds != null && x <= (bounds.x + offset) &&
0N/A offset < (bounds.width - 5)) {
0N/A return false;
0N/A }
0N/A } else if ( bounds != null &&
0N/A ( x >= (bounds.x+bounds.width-offset+5) ||
0N/A x <= (bounds.x + 5) ) &&
0N/A offset < (bounds.width - 5) ) {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A protected void determineOffset(JTree tree, Object value,
0N/A boolean isSelected, boolean expanded,
0N/A boolean leaf, int row) {
0N/A if(renderer != null) {
0N/A if(leaf)
0N/A editingIcon = renderer.getLeafIcon();
0N/A else if(expanded)
0N/A editingIcon = renderer.getOpenIcon();
0N/A else
0N/A editingIcon = renderer.getClosedIcon();
0N/A if(editingIcon != null)
0N/A offset = renderer.getIconTextGap() +
0N/A editingIcon.getIconWidth();
0N/A else
0N/A offset = renderer.getIconTextGap();
0N/A }
0N/A else {
0N/A editingIcon = null;
0N/A offset = 0;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Invoked just before editing is to start. Will add the
0N/A * <code>editingComponent</code> to the
0N/A * <code>editingContainer</code>.
0N/A */
0N/A protected void prepareForEditing() {
0N/A if (editingComponent != null) {
0N/A editingContainer.add(editingComponent);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates the container to manage placement of
0N/A * <code>editingComponent</code>.
0N/A */
0N/A protected Container createContainer() {
0N/A return new EditorContainer();
0N/A }
0N/A
0N/A /**
0N/A * This is invoked if a <code>TreeCellEditor</code>
0N/A * is not supplied in the constructor.
0N/A * It returns a <code>TextField</code> editor.
0N/A * @return a new <code>TextField</code> editor
0N/A */
0N/A protected TreeCellEditor createTreeCellEditor() {
0N/A Border aBorder = UIManager.getBorder("Tree.editorBorder");
0N/A DefaultCellEditor editor = new DefaultCellEditor
0N/A (new DefaultTextField(aBorder)) {
0N/A public boolean shouldSelectCell(EventObject event) {
0N/A boolean retValue = super.shouldSelectCell(event);
0N/A return retValue;
0N/A }
0N/A };
0N/A
0N/A // One click to edit.
0N/A editor.setClickCountToStart(1);
0N/A return editor;
0N/A }
0N/A
0N/A /**
0N/A * Cleans up any state after editing has completed. Removes the
0N/A * <code>editingComponent</code> the <code>editingContainer</code>.
0N/A */
0N/A private void cleanupAfterEditing() {
0N/A if (editingComponent != null) {
0N/A editingContainer.remove(editingComponent);
0N/A }
0N/A editingComponent = null;
0N/A }
0N/A
0N/A // Serialization support.
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
625N/A Vector<Object> values = new Vector<Object>();
0N/A
0N/A s.defaultWriteObject();
0N/A // Save the realEditor, if its Serializable.
0N/A if(realEditor != null && realEditor instanceof Serializable) {
0N/A values.addElement("realEditor");
0N/A values.addElement(realEditor);
0N/A }
0N/A s.writeObject(values);
0N/A }
0N/A
0N/A private void readObject(ObjectInputStream s)
0N/A throws IOException, ClassNotFoundException {
0N/A s.defaultReadObject();
0N/A
0N/A Vector values = (Vector)s.readObject();
0N/A int indexCounter = 0;
0N/A int maxCounter = values.size();
0N/A
0N/A if(indexCounter < maxCounter && values.elementAt(indexCounter).
0N/A equals("realEditor")) {
0N/A realEditor = (TreeCellEditor)values.elementAt(++indexCounter);
0N/A indexCounter++;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * <code>TextField</code> used when no editor is supplied.
0N/A * This textfield locks into the border it is constructed with.
0N/A * It also prefers its parents font over its font. And if the
0N/A * renderer is not <code>null</code> and no font
0N/A * has been specified the preferred height is that of the renderer.
0N/A */
0N/A public class DefaultTextField extends JTextField {
0N/A /** Border to use. */
0N/A protected Border border;
0N/A
0N/A /**
0N/A * Constructs a
0N/A * <code>DefaultTreeCellEditor.DefaultTextField</code> object.
0N/A *
0N/A * @param border a <code>Border</code> object
0N/A * @since 1.4
0N/A */
0N/A public DefaultTextField(Border border) {
0N/A setBorder(border);
0N/A }
0N/A
0N/A /**
0N/A * Sets the border of this component.<p>
0N/A * This is a bound property.
0N/A *
0N/A * @param border the border to be rendered for this component
0N/A * @see Border
0N/A * @see CompoundBorder
0N/A * @beaninfo
0N/A * bound: true
0N/A * preferred: true
0N/A * attribute: visualUpdate true
0N/A * description: The component's border.
0N/A */
0N/A public void setBorder(Border border) {
0N/A super.setBorder(border);
0N/A this.border = border;
0N/A }
0N/A
0N/A /**
0N/A * Overrides <code>JComponent.getBorder</code> to
0N/A * returns the current border.
0N/A */
0N/A public Border getBorder() {
0N/A return border;
0N/A }
0N/A
0N/A // implements java.awt.MenuContainer
0N/A public Font getFont() {
0N/A Font font = super.getFont();
0N/A
0N/A // Prefer the parent containers font if our font is a
0N/A // FontUIResource
0N/A if(font instanceof FontUIResource) {
0N/A Container parent = getParent();
0N/A
0N/A if(parent != null && parent.getFont() != null)
0N/A font = parent.getFont();
0N/A }
0N/A return font;
0N/A }
0N/A
0N/A /**
0N/A * Overrides <code>JTextField.getPreferredSize</code> to
0N/A * return the preferred size based on current font, if set,
0N/A * or else use renderer's font.
0N/A * @return a <code>Dimension</code> object containing
0N/A * the preferred size
0N/A */
0N/A public Dimension getPreferredSize() {
0N/A Dimension size = super.getPreferredSize();
0N/A
0N/A // If not font has been set, prefer the renderers height.
0N/A if(renderer != null &&
0N/A DefaultTreeCellEditor.this.getFont() == null) {
0N/A Dimension rSize = renderer.getPreferredSize();
0N/A
0N/A size.height = rSize.height;
0N/A }
0N/A return size;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Container responsible for placing the <code>editingComponent</code>.
0N/A */
0N/A public class EditorContainer extends Container {
0N/A /**
0N/A * Constructs an <code>EditorContainer</code> object.
0N/A */
0N/A public EditorContainer() {
0N/A setLayout(null);
0N/A }
0N/A
0N/A // This should not be used. It will be removed when new API is
0N/A // allowed.
0N/A public void EditorContainer() {
0N/A setLayout(null);
0N/A }
0N/A
0N/A /**
0N/A * Overrides <code>Container.paint</code> to paint the node's
0N/A * icon and use the selection color for the background.
0N/A */
0N/A public void paint(Graphics g) {
0N/A int width = getWidth();
0N/A int height = getHeight();
0N/A
0N/A // Then the icon.
0N/A if(editingIcon != null) {
0N/A int yLoc = calculateIconY(editingIcon);
0N/A
0N/A if (getComponentOrientation().isLeftToRight()) {
0N/A editingIcon.paintIcon(this, g, 0, yLoc);
0N/A } else {
0N/A editingIcon.paintIcon(
0N/A this, g, width - editingIcon.getIconWidth(),
0N/A yLoc);
0N/A }
0N/A }
0N/A
0N/A // Border selection color
0N/A Color background = getBorderSelectionColor();
0N/A if(background != null) {
0N/A g.setColor(background);
0N/A g.drawRect(0, 0, width - 1, height - 1);
0N/A }
0N/A super.paint(g);
0N/A }
0N/A
0N/A /**
0N/A * Lays out this <code>Container</code>. If editing,
0N/A * the editor will be placed at
0N/A * <code>offset</code> in the x direction and 0 for y.
0N/A */
0N/A public void doLayout() {
0N/A if(editingComponent != null) {
0N/A int width = getWidth();
0N/A int height = getHeight();
0N/A if (getComponentOrientation().isLeftToRight()) {
0N/A editingComponent.setBounds(
0N/A offset, 0, width - offset, height);
0N/A } else {
0N/A editingComponent.setBounds(
0N/A 0, 0, width - offset, height);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Calculate the y location for the icon.
0N/A */
0N/A private int calculateIconY(Icon icon) {
0N/A // To make sure the icon position matches that of the
0N/A // renderer, use the same algorithm as JLabel
0N/A // (SwingUtilities.layoutCompoundLabel).
0N/A int iconHeight = icon.getIconHeight();
0N/A int textHeight = editingComponent.getFontMetrics(
0N/A editingComponent.getFont()).getHeight();
0N/A int textY = iconHeight / 2 - textHeight / 2;
0N/A int totalY = Math.min(0, textY);
0N/A int totalHeight = Math.max(iconHeight, textY + textHeight) -
0N/A totalY;
0N/A return getHeight() / 2 - (totalY + (totalHeight / 2));
0N/A }
0N/A
0N/A /**
0N/A * Returns the preferred size for the <code>Container</code>.
0N/A * This will be at least preferred size of the editor plus
0N/A * <code>offset</code>.
0N/A * @return a <code>Dimension</code> containing the preferred
0N/A * size for the <code>Container</code>; if
0N/A * <code>editingComponent</code> is <code>null</code> the
0N/A * <code>Dimension</code> returned is 0, 0
0N/A */
0N/A public Dimension getPreferredSize() {
0N/A if(editingComponent != null) {
0N/A Dimension pSize = editingComponent.getPreferredSize();
0N/A
0N/A pSize.width += offset + 5;
0N/A
0N/A Dimension rSize = (renderer != null) ?
0N/A renderer.getPreferredSize() : null;
0N/A
0N/A if(rSize != null)
0N/A pSize.height = Math.max(pSize.height, rSize.height);
0N/A if(editingIcon != null)
0N/A pSize.height = Math.max(pSize.height,
0N/A editingIcon.getIconHeight());
0N/A
0N/A // Make sure width is at least 100.
0N/A pSize.width = Math.max(pSize.width, 100);
0N/A return pSize;
0N/A }
0N/A return new Dimension(0, 0);
0N/A }
0N/A }
0N/A}