1 56 package org.objectstyle.cayenne.modeler.editor; 57 58 import java.awt.Component ; 59 import java.awt.Container ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 import java.util.Map ; 63 import java.util.TreeMap ; 64 65 import javax.swing.DefaultComboBoxModel ; 66 import javax.swing.DefaultListCellRenderer ; 67 import javax.swing.JCheckBox ; 68 import javax.swing.JComboBox ; 69 import javax.swing.JList ; 70 import javax.swing.JPanel ; 71 import javax.swing.JTextField ; 72 73 import org.apache.commons.beanutils.PropertyUtils; 74 import org.apache.log4j.Logger; 75 import org.objectstyle.cayenne.map.event.QueryEvent; 76 import org.objectstyle.cayenne.modeler.ProjectController; 77 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory; 78 import org.objectstyle.cayenne.modeler.util.TextAdapter; 79 import org.objectstyle.cayenne.query.GenericSelectQuery; 80 import org.objectstyle.cayenne.query.Query; 81 import org.objectstyle.cayenne.validation.ValidationException; 82 83 88 public abstract class SelectPropertiesPanel extends JPanel { 89 90 private static final Logger logObj = Logger.getLogger(SelectPropertiesPanel.class); 91 92 private static final Integer ZERO = new Integer (0); 93 94 private static final String NO_CACHE_LABEL = "No Result Caching"; 95 private static final String LOCAL_CACHE_LABEL = "DataContext Cache"; 96 private static final String SHARED_CACHE_LABEL = "Shared Cache"; 97 98 private static final Object [] CACHE_POLICIES = new Object [] { 99 GenericSelectQuery.NO_CACHE, GenericSelectQuery.LOCAL_CACHE, 100 GenericSelectQuery.SHARED_CACHE 101 }; 102 103 private static final Map cachePolicyLabels = new TreeMap (); 104 105 static { 106 cachePolicyLabels.put(GenericSelectQuery.NO_CACHE, NO_CACHE_LABEL); 107 cachePolicyLabels.put(GenericSelectQuery.LOCAL_CACHE, LOCAL_CACHE_LABEL); 108 cachePolicyLabels.put(GenericSelectQuery.SHARED_CACHE, SHARED_CACHE_LABEL); 109 } 110 111 protected TextAdapter fetchLimit; 112 protected TextAdapter pageSize; 113 protected JComboBox cachePolicy; 114 protected JCheckBox refreshesResults; 115 116 protected ProjectController mediator; 117 118 public SelectPropertiesPanel(ProjectController mediator) { 119 this.mediator = mediator; 120 initView(); 121 initController(); 122 } 123 124 protected void initView() { 125 fetchLimit = new TextAdapter(new JTextField (7)) { 126 127 protected void updateModel(String text) { 128 setFetchLimit(text); 129 } 130 }; 131 132 pageSize = new TextAdapter(new JTextField (7)) { 133 134 protected void updateModel(String text) { 135 setPageSize(text); 136 } 137 }; 138 139 cachePolicy = CayenneWidgetFactory.createComboBox(); 140 cachePolicy.setRenderer(new CachePolicyRenderer()); 141 refreshesResults = new JCheckBox (); 142 } 143 144 protected void initController() { 145 cachePolicy.addActionListener(new ActionListener () { 146 147 public void actionPerformed(ActionEvent event) { 148 Object policy = cachePolicy.getModel().getSelectedItem(); 149 setQueryProperty("cachePolicy", policy); 150 } 151 }); 152 153 refreshesResults.addActionListener(new ActionListener () { 154 155 public void actionPerformed(ActionEvent event) { 156 Boolean b = refreshesResults.isSelected() ? Boolean.TRUE : Boolean.FALSE; 157 setQueryProperty("refreshingObjects", b); 158 } 159 }); 160 } 161 162 166 public void initFromModel(GenericSelectQuery query) { 167 DefaultComboBoxModel cacheModel = new DefaultComboBoxModel (CACHE_POLICIES); 168 cacheModel.setSelectedItem(query.getCachePolicy()); 169 cachePolicy.setModel(cacheModel); 170 171 fetchLimit.setText(String.valueOf(query.getFetchLimit())); 172 pageSize.setText(String.valueOf(query.getPageSize())); 173 refreshesResults.setSelected(query.isRefreshingObjects()); 174 } 175 176 void setFetchLimit(String string) { 177 string = (string == null) ? "" : string.trim(); 178 179 if (string.length() == 0) { 180 setQueryProperty("fetchLimit", ZERO); 181 } 182 else { 183 try { 184 setQueryProperty("fetchLimit", new Integer (string)); 185 } 186 catch (NumberFormatException nfex) { 187 throw new ValidationException("Fetch limit must be an integer: " + string); 188 } 189 } 190 } 191 192 void setPageSize(String string) { 193 string = (string == null) ? "" : string.trim(); 194 195 if (string.length() == 0) { 196 setQueryProperty("pageSize", ZERO); 197 } 198 else { 199 try { 200 setQueryProperty("pageSize", new Integer (string)); 201 } 202 catch (NumberFormatException nfex) { 203 throw new ValidationException("Page size must be an integer: " + string); 204 } 205 } 206 } 207 208 Query getQuery() { 209 return mediator.getCurrentQuery(); 210 } 211 212 public void setEnabled(boolean flag) { 213 super.setEnabled(flag); 214 215 217 Container mainPanel = (Container ) getComponent(0); 218 Component [] children = mainPanel.getComponents(); 219 for (int i = 0; i < children.length; i++) { 220 children[i].setEnabled(flag); 221 } 222 } 223 224 void setQueryProperty(String property, Object value) { 225 Query query = getQuery(); 226 if (query != null) { 227 try { 228 PropertyUtils.setProperty(query, property, value); 229 mediator.fireQueryEvent(new QueryEvent(this, query)); 230 } 231 catch (Exception ex) { 232 logObj.warn("Error setting property: " + property, ex); 233 } 234 } 235 } 236 237 final class CachePolicyRenderer extends DefaultListCellRenderer { 238 239 public Component getListCellRendererComponent( 240 JList list, 241 Object object, 242 int arg2, 243 boolean arg3, 244 boolean arg4) { 245 246 if (object != null) { 247 object = cachePolicyLabels.get(object); 248 } 249 250 if (object == null) { 251 object = NO_CACHE_LABEL; 252 } 253 254 return super.getListCellRendererComponent(list, object, arg2, arg3, arg4); 255 } 256 } 257 258 } | Popular Tags |