| 1 |
/* |
| 2 |
* Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are met: |
| 6 |
* |
| 7 |
* o Redistributions of source code must retain the above copyright notice, |
| 8 |
* this list of conditions and the following disclaimer. |
| 9 |
* |
| 10 |
* o Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
* this list of conditions and the following disclaimer in the documentation |
| 12 |
* and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of |
| 15 |
* its contributors may be used to endorse or promote products derived |
| 16 |
* from this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 20 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 21 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 22 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 25 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 |
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
package org.pushingpixels.lafwidget.tree.dnd; |
| 31 |
|
| 32 |
import java.awt.Component; |
| 33 |
import java.awt.Image; |
| 34 |
|
| 35 |
import javax.swing.JComponent; |
| 36 |
import javax.swing.JTree; |
| 37 |
import javax.swing.border.Border; |
| 38 |
import javax.swing.tree.TreeCellRenderer; |
| 39 |
import javax.swing.tree.TreeNode; |
| 40 |
|
| 41 |
/** |
| 42 |
* DnDCellRendererProxy is a TreeCellRenderer that proxies operations to a true |
| 43 |
* TreeCellRenderer, but that draws a border around specific TreeNodes. |
| 44 |
* |
| 45 |
* @author Antonio Vieiro ([email protected]), $Author: kirillcool $ |
| 46 |
*/ |
| 47 |
class DnDCellRendererProxy extends Component implements TreeCellRenderer { |
| 48 |
private TreeCellRenderer originalTreeCellRenderer; |
| 49 |
private DnDBorderFactory borderFactory; |
| 50 |
private TreeNode draggedNode; |
| 51 |
private TreeNode dropNode; |
| 52 |
private int dropNodeRow; |
| 53 |
private Image shadowImage; |
| 54 |
private boolean fetchBorder; |
| 55 |
private Border originalBorder; |
| 56 |
|
| 57 |
/** |
| 58 |
* Creates a new instance of DragAndDropCellRenderer. |
| 59 |
* |
| 60 |
* @param trueCellRenderer |
| 61 |
* the original cell renderer. |
| 62 |
*/ |
| 63 |
public DnDCellRendererProxy(TreeCellRenderer trueCellRenderer) { |
| 64 |
this.originalTreeCellRenderer = trueCellRenderer; |
| 65 |
this.borderFactory = new DnDBorderFactory(); |
| 66 |
this.fetchBorder = true; |
| 67 |
} |
| 68 |
|
| 69 |
public TreeCellRenderer getOriginalTreeCellRenderer() { |
| 70 |
return this.originalTreeCellRenderer; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public Component getTreeCellRendererComponent(JTree tree, Object value, |
| 75 |
boolean selected, boolean expanded, boolean leaf, int row, |
| 76 |
boolean hasFocus) { |
| 77 |
Component c = this.originalTreeCellRenderer |
| 78 |
.getTreeCellRendererComponent(tree, value, selected, expanded, |
| 79 |
leaf, row, hasFocus); |
| 80 |
|
| 81 |
TreeNode nodeToRender = (TreeNode) value; |
| 82 |
|
| 83 |
if (c instanceof JComponent) { |
| 84 |
if (this.fetchBorder) { |
| 85 |
this.fetchBorder = false; |
| 86 |
this.originalBorder = ((JComponent) c).getBorder(); |
| 87 |
} |
| 88 |
// TODO: This *REMOVES* the border in c. |
| 89 |
// TODO: Use compound borders to draw BOTH borders. |
| 90 |
JComponent jComponent = (JComponent) c; |
| 91 |
if (nodeToRender.equals(this.dropNode)) { |
| 92 |
Border border = null; |
| 93 |
if (this.isDropAllowed()) { |
| 94 |
border = this.borderFactory.getDropAllowedBorder(); |
| 95 |
this.dropNodeRow = row; |
| 96 |
} else { |
| 97 |
border = this.borderFactory.getDropNotAllowedBorder(); |
| 98 |
this.dropNodeRow = -2; |
| 99 |
} |
| 100 |
jComponent.setBorder(border); |
| 101 |
} else if (this.isDropAllowed() && (row == this.dropNodeRow + 1)) { |
| 102 |
jComponent.setBorder(this.borderFactory.getOffsetBorder()); |
| 103 |
} else { |
| 104 |
jComponent.setBorder(this.originalBorder); |
| 105 |
this.dropNodeRow = -2; |
| 106 |
} |
| 107 |
} |
| 108 |
return c; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Getter for property draggedNode. |
| 113 |
* |
| 114 |
* @return Value of property draggedNode. |
| 115 |
*/ |
| 116 |
public TreeNode getDraggedNode() { |
| 117 |
return this.draggedNode; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Setter for property draggedNode. |
| 122 |
* |
| 123 |
* @param draggedNode |
| 124 |
* New value of property draggedNode. |
| 125 |
*/ |
| 126 |
public void setDraggedNode(TreeNode draggedNode) { |
| 127 |
this.draggedNode = draggedNode; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Getter for property dropNode. |
| 132 |
* |
| 133 |
* @return Value of property dropNode. |
| 134 |
*/ |
| 135 |
public TreeNode getDropNode() { |
| 136 |
return this.dropNode; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Setter for property dropNode. |
| 141 |
* |
| 142 |
* @param dropNode |
| 143 |
* New value of property dropNode. |
| 144 |
*/ |
| 145 |
public void setDropNode(TreeNode dropNode) { |
| 146 |
this.dropNode = dropNode; |
| 147 |
if (dropNode == null) |
| 148 |
this.dropNodeRow = -2; |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
public String toString() { |
| 153 |
StringBuffer sb = new StringBuffer(); |
| 154 |
sb.append("[DnDCellRendererProxy for : ").append( |
| 155 |
this.originalTreeCellRenderer).append("]"); |
| 156 |
return sb.toString(); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Holds value of property dropAllowed. |
| 161 |
*/ |
| 162 |
private boolean dropAllowed; |
| 163 |
|
| 164 |
/** |
| 165 |
* Getter for property dropAllowed. |
| 166 |
* |
| 167 |
* @return Value of property dropAllowed. |
| 168 |
*/ |
| 169 |
public boolean isDropAllowed() { |
| 170 |
return this.dropAllowed; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Setter for property dropAllowed. |
| 175 |
* |
| 176 |
* @param dropAllowed |
| 177 |
* New value of property dropAllowed. |
| 178 |
*/ |
| 179 |
public void setDropAllowed(boolean dropAllowed) { |
| 180 |
this.dropAllowed = dropAllowed; |
| 181 |
if (!dropAllowed) |
| 182 |
this.dropNodeRow = -2; |
| 183 |
} |
| 184 |
|
| 185 |
public void setShadowImage(Image anImage) { |
| 186 |
this.shadowImage = anImage; |
| 187 |
} |
| 188 |
} |