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.map.DataMap; 70 import org.objectstyle.cayenne.map.ObjEntity; 71 import org.objectstyle.cayenne.map.event.QueryEvent; 72 import org.objectstyle.cayenne.modeler.ProjectController; 73 import org.objectstyle.cayenne.modeler.event.QueryDisplayEvent; 74 import org.objectstyle.cayenne.modeler.event.QueryDisplayListener; 75 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory; 76 import org.objectstyle.cayenne.modeler.util.CellRenderers; 77 import org.objectstyle.cayenne.modeler.util.Comparators; 78 import org.objectstyle.cayenne.modeler.util.ProjectUtil; 79 import org.objectstyle.cayenne.modeler.util.TextAdapter; 80 import org.objectstyle.cayenne.query.GenericSelectQuery; 81 import org.objectstyle.cayenne.query.ProcedureQuery; 82 import org.objectstyle.cayenne.query.Query; 83 import org.objectstyle.cayenne.util.Util; 84 import org.objectstyle.cayenne.validation.ValidationException; 85 86 import com.jgoodies.forms.builder.PanelBuilder; 87 import com.jgoodies.forms.layout.CellConstraints; 88 import com.jgoodies.forms.layout.FormLayout; 89 90 93 public class ProcedureQueryView extends JPanel { 94 95 protected ProjectController mediator; 96 protected TextAdapter name; 97 protected JComboBox queryRoot; 98 protected JCheckBox selecting; 99 protected SelectPropertiesPanel properties; 100 101 public ProcedureQueryView(ProjectController mediator) { 102 this.mediator = mediator; 103 104 initView(); 105 initController(); 106 } 107 108 private void initView() { 109 name = new TextAdapter(new JTextField ()) { 111 112 protected void updateModel(String text) { 113 setQueryName(text); 114 } 115 }; 116 selecting = new JCheckBox (); 117 queryRoot = CayenneWidgetFactory.createComboBox(); 118 queryRoot.setRenderer(CellRenderers.listRendererWithIcons()); 119 properties = new RawQueryPropertiesPanel(mediator) { 120 121 protected void setEntity(ObjEntity entity) { 122 ProcedureQueryView.this.setEntity(entity); 123 } 124 125 public ObjEntity getEntity(GenericSelectQuery query) { 126 if (query instanceof ProcedureQuery) { 127 return ProcedureQueryView.this.getEntity((ProcedureQuery) query); 128 } 129 130 return null; 131 } 132 }; 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"); 139 PanelBuilder builder = new PanelBuilder(layout); 140 builder.setDefaultDialogBorder(); 141 142 builder.addSeparator("ProcedureQuery 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("Procedure:", cc.xy(1, 5)); 146 builder.add(queryRoot, cc.xy(3, 5)); 147 builder.addLabel("Is Selecting:", cc.xy(1, 7)); 148 builder.add(selecting, cc.xy(3, 7)); 149 150 this.setLayout(new BorderLayout ()); 151 this.add(builder.getPanel(), BorderLayout.NORTH); 152 this.add(properties, BorderLayout.CENTER); 153 } 154 155 private void initController() { 156 157 queryRoot.addActionListener(new ActionListener () { 158 159 public void actionPerformed(ActionEvent event) { 160 Query query = mediator.getCurrentQuery(); 161 if (query != null) { 162 query.setRoot(queryRoot.getModel().getSelectedItem()); 163 mediator.fireQueryEvent(new QueryEvent(this, query)); 164 } 165 } 166 }); 167 168 mediator.addQueryDisplayListener(new QueryDisplayListener() { 169 170 public void currentQueryChanged(QueryDisplayEvent e) { 171 initFromModel(); 172 } 173 }); 174 175 selecting.addActionListener(new ActionListener () { 176 177 public void actionPerformed(ActionEvent event) { 178 properties.setEnabled(selecting.isSelected()); 179 180 Query query = mediator.getCurrentQuery(); 181 if (query instanceof ProcedureQuery) { 182 ((ProcedureQuery) query).setSelecting(selecting.isSelected()); 183 mediator.fireQueryEvent(new QueryEvent(this, query)); 184 } 185 } 186 }); 187 } 188 189 193 void initFromModel() { 194 Query query = mediator.getCurrentQuery(); 195 196 if (!(query instanceof ProcedureQuery)) { 197 setVisible(false); 198 return; 199 } 200 201 ProcedureQuery procedureQuery = (ProcedureQuery) query; 202 203 selecting.setSelected(procedureQuery.isSelecting()); 204 properties.setEnabled(procedureQuery.isSelecting()); 205 name.setText(procedureQuery.getName()); 206 207 209 211 215 DataMap map = mediator.getCurrentDataMap(); 216 Object [] roots = map.getProcedures().toArray(); 217 218 if (roots.length > 1) { 219 Arrays.sort(roots, Comparators.getDataMapChildrenComparator()); 220 } 221 222 DefaultComboBoxModel model = new DefaultComboBoxModel (roots); 223 model.setSelectedItem(query.getRoot()); 224 queryRoot.setModel(model); 225 226 properties.initFromModel(procedureQuery); 227 setVisible(true); 228 } 229 230 233 void setQueryName(String newName) { 234 if (newName != null && newName.trim().length() == 0) { 235 newName = null; 236 } 237 238 Query query = mediator.getCurrentQuery(); 239 if (query == null) { 240 return; 241 } 242 243 if (Util.nullSafeEquals(newName, query.getName())) { 244 return; 245 } 246 247 if (newName == null) { 248 throw new ValidationException("Query name is required."); 249 } 250 251 DataMap map = mediator.getCurrentDataMap(); 252 253 if (map.getQuery(newName) == null) { 254 QueryEvent e = new QueryEvent(this, query, query.getName()); 256 ProjectUtil.setQueryName(map, query, newName); 257 mediator.fireQueryEvent(e); 258 } 259 else { 260 throw new ValidationException("There is another query named '" 262 + newName 263 + "'. Use a different name."); 264 } 265 } 266 267 270 ObjEntity getEntity(ProcedureQuery query) { 271 String resultClass = query.getResultClassName(); 272 if (resultClass == null) { 273 return null; 274 } 275 276 280 DataMap map = mediator.getCurrentDataMap(); 281 if (map == null) { 282 return null; 283 } 284 285 return map.getObjEntityForJavaClass(resultClass); 286 } 287 288 void setEntity(ObjEntity entity) { 289 Query query = mediator.getCurrentQuery(); 290 if (query instanceof ProcedureQuery) { 291 ProcedureQuery procedureQuery = (ProcedureQuery) query; 292 String resultClass = null; 293 294 if (entity != null) { 295 resultClass = entity.getClassName(); 296 } 297 298 procedureQuery.setResultClassName(resultClass); 299 mediator.fireQueryEvent(new QueryEvent(this, procedureQuery)); 300 } 301 } 302 } | Popular Tags |