1 19 20 package org.netbeans.modules.form.layoutsupport.delegates; 21 22 import java.awt.*; 23 import javax.swing.*; 24 import java.lang.reflect.Method ; 25 26 import org.netbeans.modules.form.layoutsupport.*; 27 import org.netbeans.modules.form.codestructure.*; 28 29 34 35 public class JScrollPaneSupport extends AbstractLayoutSupport { 36 37 private static Method setViewportViewMethod; 38 39 42 public Class getSupportedClass() { 43 return JScrollPane.class; 44 } 45 46 52 public boolean checkEmptyContainer(Container cont) { 53 return cont instanceof JScrollPane 54 && ((JScrollPane)cont).getViewport().getView() == null; 55 } 56 57 72 public int getNewIndex(Container container, 73 Container containerDelegate, 74 Component component, 75 int index, 76 Point posInCont, 77 Point posInComp) 78 { 79 assistantParams = (container instanceof JScrollPane 80 && ((JScrollPane)container).getViewport().getView() == null); 81 return assistantParams ? 0 : -1; 82 } 83 84 private boolean assistantParams; 85 public String getAssistantContext() { 86 return assistantParams ? "jscrollPaneLayout" : null; } 88 89 102 public boolean paintDragFeedback(Container container, 103 Container containerDelegate, 104 Component component, 105 LayoutConstraints newConstraints, 106 int newIndex, 107 Graphics g) 108 { 109 if (container instanceof JScrollPane 110 && ((JScrollPane)container).getViewport().getView() == null) 111 { Dimension sz = container.getSize(); 113 Insets insets = container.getInsets(); 114 sz.width -= insets.left + insets.right; 115 sz.height -= insets.top + insets.bottom; 116 117 g.drawRect(0, 0, sz.width, sz.height); 118 return true; 119 } 120 return false; 121 } 122 123 130 public void addComponentsToContainer(Container container, 131 Container containerDelegate, 132 Component[] components, 133 int index) 134 { 135 if (components.length == 0) 136 return; 137 138 if (container instanceof JScrollPane) 139 ((JScrollPane)container).setViewportView(components[0]); 140 } 141 142 149 public boolean removeComponentFromContainer(Container container, 150 Container containerDelegate, 151 Component component) 152 { 153 return false; } 155 156 162 public boolean clearContainer(Container container, 163 Container containerDelegate) 164 { 165 if (container instanceof JScrollPane) { 166 JScrollPane scrollPane = (JScrollPane) container; 167 Component comp = scrollPane.getViewport().getView(); 168 if (comp != null) { 169 comp.removeNotify(); 170 comp.setBounds(0, 0, 0, 0); 171 } 172 scrollPane.setViewportView(null); 173 return true; 174 } 175 else return super.clearContainer(container, containerDelegate); 176 } 177 178 180 188 protected CodeExpression getActiveContainerCodeExpression() { 189 return getLayoutContext().getContainerCodeExpression(); 190 } 191 192 203 protected CodeExpression readComponentCode(CodeStatement statement, 204 CodeGroup componentCode) 205 { 206 if (getSetViewportViewMethod().equals(statement.getMetaObject()) 207 || getSimpleAddMethod().equals(statement.getMetaObject())) 208 { 209 componentCode.addStatement(statement); 210 getConstraintsList().add(null); return statement.getStatementParameters()[0]; 212 } 213 214 return null; 215 } 216 217 225 protected void createComponentCode(CodeGroup componentCode, 226 CodeExpression componentExpression, 227 int index) 228 { 229 CodeStatement addStatement = CodeStructure.createStatement( 230 getLayoutContext().getContainerCodeExpression(), 231 getSetViewportViewMethod(), 232 new CodeExpression[] { componentExpression }); 233 componentCode.addStatement(addStatement); 234 } 235 236 private static Method getSetViewportViewMethod() { 237 if (setViewportViewMethod == null) { 238 try { 239 setViewportViewMethod = JScrollPane.class.getMethod( 240 "setViewportView", new Class [] { Component.class }); 242 } 243 catch (NoSuchMethodException ex) { ex.printStackTrace(); 245 } 246 } 247 return setViewportViewMethod; 248 } 249 } 250 | Popular Tags |