1 56 package org.objectstyle.cayenne.modeler.editor; 57 58 import java.awt.BorderLayout ; 59 import java.awt.event.ActionEvent ; 60 import java.awt.event.ActionListener ; 61 import java.util.Arrays ; 62 63 import javax.swing.DefaultComboBoxModel ; 64 import javax.swing.JCheckBox ; 65 import javax.swing.JComboBox ; 66 import javax.swing.JPanel ; 67 import javax.swing.JTextField ; 68 69 import org.objectstyle.cayenne.exp.Expression; 70 import org.objectstyle.cayenne.map.DataMap; 71 import org.objectstyle.cayenne.map.event.QueryEvent; 72 import org.objectstyle.cayenne.modeler.ProjectController; 73 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory; 74 import org.objectstyle.cayenne.modeler.util.CellRenderers; 75 import org.objectstyle.cayenne.modeler.util.Comparators; 76 import org.objectstyle.cayenne.modeler.util.ProjectUtil; 77 import org.objectstyle.cayenne.modeler.util.TextAdapter; 78 import org.objectstyle.cayenne.query.Query; 79 import org.objectstyle.cayenne.query.SelectQuery; 80 import org.objectstyle.cayenne.util.Util; 81 import org.objectstyle.cayenne.validation.ValidationException; 82 import org.scopemvc.util.convertor.StringConvertor; 83 import org.scopemvc.util.convertor.StringConvertors; 84 85 import com.jgoodies.forms.builder.PanelBuilder; 86 import com.jgoodies.forms.layout.CellConstraints; 87 import com.jgoodies.forms.layout.FormLayout; 88 89 94 public class SelectQueryMainTab extends JPanel { 95 96 protected ProjectController mediator; 97 98 protected TextAdapter name; 99 protected JComboBox queryRoot; 100 protected TextAdapter qualifier; 101 protected JCheckBox distinct; 102 protected ObjectQueryPropertiesPanel properties; 103 104 public SelectQueryMainTab(ProjectController mediator) { 105 this.mediator = mediator; 106 107 initView(); 108 initController(); 109 } 110 111 private void initView() { 112 name = new TextAdapter(new JTextField ()) { 114 115 protected void updateModel(String text) { 116 setQueryName(text); 117 } 118 }; 119 120 queryRoot = CayenneWidgetFactory.createComboBox(); 121 queryRoot.setRenderer(CellRenderers.listRendererWithIcons()); 122 123 qualifier = new TextAdapter(new JTextField ()) { 124 125 protected void updateModel(String text) { 126 setQueryQualifier(text); 127 } 128 }; 129 130 distinct = new JCheckBox (); 131 132 properties = new ObjectQueryPropertiesPanel(mediator); 133 134 CellConstraints cc = new CellConstraints(); 136 FormLayout layout = new FormLayout( 137 "right:max(80dlu;pref), 3dlu, fill:max(200dlu;pref)", 138 "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p"); 139 PanelBuilder builder = new PanelBuilder(layout); 140 builder.setDefaultDialogBorder(); 141 142 builder.addSeparator("SelectQuery Settings", cc.xywh(1, 1, 3, 1)); 143 builder.addLabel("Query Name:", cc.xy(1, 3)); 144 builder.add(name.getComponent(), cc.xy(3, 3)); 145 builder.addLabel("Query Root:", cc.xy(1, 5)); 146 builder.add(queryRoot, cc.xy(3, 5)); 147 builder.addLabel("Qualifier:", cc.xy(1, 7)); 148 builder.add(qualifier.getComponent(), cc.xy(3, 7)); 149 builder.addLabel("Distinct:", cc.xy(1, 9)); 150 builder.add(distinct, cc.xy(3, 9)); 151 152 this.setLayout(new BorderLayout ()); 153 this.add(builder.getPanel(), BorderLayout.NORTH); 154 this.add(properties, BorderLayout.CENTER); 155 } 156 157 private void initController() { 158 159 queryRoot.addActionListener(new ActionListener () { 160 161 public void actionPerformed(ActionEvent event) { 162 Query query = getQuery(); 163 if (query != null) { 164 query.setRoot(queryRoot.getModel().getSelectedItem()); 165 mediator.fireQueryEvent(new QueryEvent(this, query)); 166 } 167 } 168 }); 169 170 distinct.addActionListener(new ActionListener () { 171 172 public void actionPerformed(ActionEvent event) { 173 SelectQuery query = getQuery(); 174 if (query != null) { 175 query.setDistinct(distinct.isSelected()); 176 mediator.fireQueryEvent(new QueryEvent(this, query)); 177 } 178 } 179 }); 180 181 } 182 183 187 void initFromModel() { 188 Query query = mediator.getCurrentQuery(); 189 190 if (!(query instanceof SelectQuery)) { 191 setVisible(false); 192 return; 193 } 194 195 SelectQuery selectQuery = (SelectQuery) query; 196 197 name.setText(query.getName()); 198 distinct.setSelected(selectQuery.isDistinct()); 199 qualifier.setText(selectQuery.getQualifier() != null ? selectQuery 200 .getQualifier() 201 .toString() : null); 202 203 205 207 211 DataMap map = mediator.getCurrentDataMap(); 212 Object [] roots = map.getObjEntities().toArray(); 213 214 if (roots.length > 1) { 215 Arrays.sort(roots, Comparators.getDataMapChildrenComparator()); 216 } 217 218 DefaultComboBoxModel model = new DefaultComboBoxModel (roots); 219 model.setSelectedItem(query.getRoot()); 220 queryRoot.setModel(model); 221 222 properties.initFromModel(selectQuery); 223 224 setVisible(true); 225 } 226 227 protected SelectQuery getQuery() { 228 return (SelectQuery) mediator.getCurrentQuery(); 229 } 230 231 234 void setQueryQualifier(String text) { 235 if (text != null && text.trim().length() == 0) { 236 text = null; 237 } 238 239 SelectQuery query = getQuery(); 240 if(query == null) { 241 return; 242 } 243 244 StringConvertor convertor = StringConvertors.forClass(Expression.class); 245 try { 246 String oldQualifier = convertor.valueAsString(query.getQualifier()); 247 if (!Util.nullSafeEquals(oldQualifier, text)) { 248 Expression exp = (Expression) convertor.stringAsValue(text); 249 query.setQualifier(exp); 250 mediator.fireQueryEvent(new QueryEvent(this, query)); 251 } 252 } 253 catch (IllegalArgumentException ex) { 254 throw new ValidationException(ex.getMessage()); 256 } 257 } 258 259 262 void setQueryName(String newName) { 263 if (newName != null && newName.trim().length() == 0) { 264 newName = null; 265 } 266 267 Query query = getQuery(); 268 269 if(query == null) { 270 return; 271 } 272 273 if (Util.nullSafeEquals(newName, query.getName())) { 274 return; 275 } 276 277 if (newName == null) { 278 throw new ValidationException("SelectQuery name is required."); 279 } 280 281 DataMap map = mediator.getCurrentDataMap(); 282 Query matchingQuery = map.getQuery(newName); 283 284 if (matchingQuery == null) { 285 QueryEvent e = new QueryEvent(this, query, query.getName()); 287 ProjectUtil.setQueryName(map, query, newName); 288 mediator.fireQueryEvent(e); 289 } 290 else if (matchingQuery != query) { 291 throw new ValidationException("There is another query named '" 293 + newName 294 + "'. Use a different name."); 295 } 296 } 297 } | Popular Tags |