1 19 20 package org.netbeans.modules.form.layoutsupport.delegates; 21 22 import java.awt.*; 23 import org.netbeans.modules.form.layoutsupport.*; 24 25 31 32 public class FlowLayoutSupport extends AbstractLayoutSupport 33 { 34 37 public Class getSupportedClass() { 38 return FlowLayout.class; 39 } 40 41 58 public int getNewIndex(Container container, 59 Container containerDelegate, 60 Component component, 61 int index, 62 Point posInCont, 63 Point posInComp) 64 { 65 if (!(containerDelegate.getLayout() instanceof FlowLayout)) 66 return -1; 67 68 int vgap = ((FlowLayout) containerDelegate.getLayout()).getVgap(); 69 Component[] components = containerDelegate.getComponents(); 70 int[] rowStarts = new int[components.length + 1]; 71 int[] rowTops = new int[components.length + 1]; 72 for (int i = 0; i < rowStarts.length; i++) { 73 rowStarts[i] = -1; 74 } 75 76 79 int lastX = Integer.MAX_VALUE; 80 int rowHeight = - vgap; 81 int r = 0; 82 int i = 0; 83 84 int compIndex = -1; 85 assistantParams = 0; 86 if ((components.length > 1) || (component.getParent() != containerDelegate)) { 87 for (int j = 0; j < components.length; j++) { 88 Component comp = components[j]; 89 if (comp == component) { 90 compIndex = j; 91 comp = components[(j == 0) ? 1 : j-1]; 92 } 93 int posX = comp.getBounds().x; 94 if (posX < lastX) { 95 rowStarts[r] = j; 96 rowTops[r] = rowHeight + vgap; 97 rowTops[r] += r > 0 ? rowTops[r-1] : 98 containerDelegate.getInsets().top; 99 r++; 100 rowHeight = 0; 101 } 102 rowHeight = Math.max(rowHeight, comp.getSize().height); 103 lastX = posX; 104 } 105 if (r > 0) { 106 rowTops[r] = rowTops[r-1] + rowHeight + vgap; 107 } 108 109 111 r = 0; 112 while (rowStarts[i] >= 0) { 113 if (posInCont.y < rowTops[i]) { 114 r = i - 1; 115 break; 116 } 117 i++; 118 } 119 120 if (rowStarts[i] < 0) { 121 if (posInCont.y >= rowTops[i]) { 122 if (component.getParent() == containerDelegate) assistantParams--; 123 assistantParams += components.length; 124 return components.length; 125 } 126 else { 127 r = i - 1; 128 } 129 } 130 131 int m = (r <= 0) ? 0 : rowStarts[r]; 132 if ((compIndex > -1) && (compIndex < m)) assistantParams--; 133 int n = rowStarts[r + 1]; 134 135 if (n > components.length || n < 0) 136 n = components.length; 137 138 for (i = m; i < n; i++) { 139 Component comp = components[i]; 140 if (comp == component) { 141 assistantParams--; 142 comp = components[(i == 0) ? 1 : i-1]; 143 } 144 Rectangle bounds = comp.getBounds(); 145 int centerX = bounds.x + bounds.width / 2; 146 if (posInCont.x < centerX) 147 break; 148 } 149 150 i = i < n ? i : n; 151 } 152 assistantParams += i; 153 return i; 154 } 155 156 private int assistantParams; 157 public String getAssistantContext() { 158 return "flowLayout"; } 160 161 public Object [] getAssistantParams() { 162 return new Object [] {Integer.valueOf(assistantParams+1)}; 163 } 164 165 180 public boolean paintDragFeedback(Container container, 181 Container containerDelegate, 182 Component component, 183 LayoutConstraints newConstraints, 184 int newIndex, 185 Graphics g) 186 { 187 if (!(containerDelegate.getLayout() instanceof FlowLayout)) 188 return false; 189 190 Component[] components = containerDelegate.getComponents(); 191 int alignment = ((FlowLayout) containerDelegate.getLayout()).getAlignment(); 192 int hgap = ((FlowLayout) containerDelegate.getLayout()).getHgap(); 193 int draggedIndex = -1; 194 if (component.getParent() == containerDelegate) { 195 for (int i=0; i<components.length; i++) { 196 if (component == components[i]) { 197 draggedIndex = i; 198 } 199 } 200 } 201 202 int x = 0, y1 = 0, y2 = 0; 203 204 if ((newIndex <= 0) || ((components.length == 1) && (draggedIndex != -1))) { 205 if ((components.length == 0) || ((components.length == 1) && (draggedIndex != -1))) { 206 if (alignment == FlowLayout.RIGHT) { 207 x = containerDelegate.getSize().width; 208 } 209 else if (alignment == FlowLayout.LEFT) { 210 x = 0; 211 } 212 else { 213 x = containerDelegate.getSize().width / 2 - 5; 214 } 215 y1 = 0; 216 y2 = component.getHeight(); 217 } 218 else { 219 Rectangle b = components[(draggedIndex == 0) ? 1 : 0].getBounds(); 220 x = b.x; 221 y1 = b.y; 222 y2 = b.y + component.getHeight(); 223 } 224 } 225 else if ((newIndex >= components.length) || 226 ((newIndex == components.length - 1) && (newIndex == draggedIndex))) { 227 int last = components.length - 1; 228 Rectangle b = components[(last == draggedIndex) ? last-1 : last].getBounds(); 229 x = b.x + b.width; 230 y1 = b.y; 231 y2 = b.y + component.getHeight(); 232 } 233 else { 234 Rectangle b = components[(newIndex == draggedIndex) ? newIndex+1 : newIndex].getBounds(); 235 x = b.x; 236 y1 = b.y; 237 y2 = b.y + component.getHeight(); 238 } 239 g.drawRect(x - 10 - hgap / 2, y1, 20, y2 - y1); 240 return true; 241 } 242 } 243 | Popular Tags |