1 19 20 package org.netbeans.modules.form.layoutsupport.delegates; 21 22 import java.awt.*; 23 import java.util.*; 24 import org.netbeans.modules.form.layoutsupport.*; 25 import org.netbeans.modules.form.codestructure.*; 26 27 33 34 public class GridLayoutSupport extends AbstractLayoutSupport 35 { 36 39 public Class getSupportedClass() { 40 return GridLayout.class; 41 } 42 43 60 public int getNewIndex(Container container, 61 Container containerDelegate, 62 Component component, 63 int index, 64 Point posInCont, 65 Point posInComp) 66 { 67 if (!(containerDelegate.getLayout() instanceof GridLayout)) 68 return -1; 69 70 Component[] components = containerDelegate.getComponents(); 71 GridLayout layout = (GridLayout) containerDelegate.getLayout(); 72 int nrows = layout.getRows(); 73 int ncols = layout.getColumns(); 74 75 if ((nrows <= 0 && ncols <= 0) || components.length == 0) 76 return components.length; 77 78 if (nrows != 0) 79 ncols = (components.length + nrows - 1) / nrows; 80 else 81 nrows = (components.length + ncols - 1) / ncols; 82 83 Dimension sz = containerDelegate.getSize(); 84 Insets insets = containerDelegate.getInsets(); 85 sz.width -= insets.left + insets.right; 86 sz.height -= insets.top + insets.bottom; 87 88 int colwidth = sz.width / ncols; 89 if (colwidth <= 0) { 90 assistantParams = components.length; 91 return components.length; 92 } 93 int col = (posInCont.x - insets.left + colwidth / 2) / colwidth; 94 95 int rowheight = sz.height / nrows; 96 if (rowheight <= 0) { 97 assistantParams = components.length; 98 return components.length; 99 } 100 int row = (posInCont.y - insets.top) / rowheight; 101 102 int newIndex = row * ncols + col; 103 newIndex = newIndex >= components.length ? components.length : newIndex; 104 assistantParams = newIndex; 105 return newIndex; 106 } 107 108 private int assistantParams; 109 public String getAssistantContext() { 110 return "gridLayout"; } 112 113 public Object [] getAssistantParams() { 114 return new Object [] {Integer.valueOf(assistantParams+1)}; 115 } 116 117 132 public boolean paintDragFeedback(Container container, 133 Container containerDelegate, 134 Component component, 135 LayoutConstraints newConstraints, 136 int newIndex, 137 Graphics g) 138 { 139 if (!(containerDelegate.getLayout() instanceof GridLayout)) 140 return false; 141 142 Component[] components = containerDelegate.getComponents(); 143 GridLayout layout = (GridLayout) containerDelegate.getLayout(); 144 int dx = 12 + layout.getHgap() / 2; 145 int x = 0, w = 24, y = 0, h = 0; 146 147 if ((newIndex <= 0) || ((components.length == 1) && (components[0] == component))) { 148 if ((components.length > 1) || ((components.length == 1) && (components[0] != component))) { 149 Component comp = components[0]; 150 if (comp == component) { 151 comp = components[1]; 152 } 153 Rectangle b = comp.getBounds(); 154 x = b.x - dx ; 155 y = b.y; 156 h = b.height; 157 } 158 else { 159 Insets ins = containerDelegate.getInsets(); 160 x = ins.left + 1; 161 w = containerDelegate.getWidth() - ins.right - ins.left - 2; 162 y = ins.top + 1; 163 h = containerDelegate.getHeight() - ins.bottom - ins.top - 2; 164 } 165 } 166 else if ((newIndex >= components.length) || 167 ((newIndex == components.length - 1) && (components[newIndex] == component))) { 168 Component comp = components[components.length-1]; 169 if (comp == component) { 170 comp = components[components.length-2]; 171 } 172 Rectangle b = comp.getBounds(); 173 x = b.x + b.width - dx; 174 y = b.y; 175 h = b.height; 176 } 177 else { 178 Component comp = components[newIndex]; 179 if (comp == component) { 180 comp = components[newIndex+1]; 181 } 182 Rectangle b = comp.getBounds(); 183 x = b.x - dx; 184 y = b.y; 185 h = b.height; 186 } 187 188 g.drawRect(x, y, w, h); 189 return true; 190 } 191 192 194 202 protected void readInitLayoutCode(CodeExpression layoutExp, 203 CodeGroup initLayoutCode) 204 { 205 CodeExpression[] params = layoutExp.getOrigin().getCreationParameters(); 206 if (params.length > 0) { 207 Object rowsValue = params[0].getOrigin().getValue(); 208 if (rowsValue instanceof Integer 209 && ((Integer )rowsValue).intValue() == 0) 210 { try { 213 getProperty("columns").setValue(new Integer (1)); 214 } 215 catch (Exception ex) {} } 217 } 218 219 super.readInitLayoutCode(layoutExp, initLayoutCode); 220 } 221 } 222 | Popular Tags |