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 62 import javax.swing.JCheckBox ; 63 import javax.swing.JPanel ; 64 import javax.swing.JTextField ; 65 66 import org.objectstyle.cayenne.map.DataMap; 67 import org.objectstyle.cayenne.map.ObjEntity; 68 import org.objectstyle.cayenne.map.event.QueryEvent; 69 import org.objectstyle.cayenne.modeler.ProjectController; 70 import org.objectstyle.cayenne.modeler.util.ProjectUtil; 71 import org.objectstyle.cayenne.modeler.util.TextAdapter; 72 import org.objectstyle.cayenne.query.GenericSelectQuery; 73 import org.objectstyle.cayenne.query.Query; 74 import org.objectstyle.cayenne.query.SQLTemplate; 75 import org.objectstyle.cayenne.util.Util; 76 import org.objectstyle.cayenne.validation.ValidationException; 77 78 import com.jgoodies.forms.builder.PanelBuilder; 79 import com.jgoodies.forms.layout.CellConstraints; 80 import com.jgoodies.forms.layout.FormLayout; 81 82 87 public class SQLTemplateMainTab extends JPanel { 88 89 protected ProjectController mediator; 90 protected TextAdapter name; 91 protected JCheckBox selecting; 92 protected SelectPropertiesPanel properties; 93 94 public SQLTemplateMainTab(ProjectController mediator) { 95 this.mediator = mediator; 96 97 initView(); 98 initController(); 99 } 100 101 private void initView() { 102 name = new TextAdapter(new JTextField ()) { 104 105 protected void updateModel(String text) { 106 setQueryName(text); 107 } 108 }; 109 110 selecting = new JCheckBox (); 111 112 properties = new RawQueryPropertiesPanel(mediator) { 113 114 protected void setEntity(ObjEntity entity) { 115 SQLTemplateMainTab.this.setEntity(entity); 116 } 117 118 public ObjEntity getEntity(GenericSelectQuery query) { 119 if (query instanceof SQLTemplate) { 120 return SQLTemplateMainTab.this.getEntity((SQLTemplate) query); 121 } 122 123 return null; 124 } 125 }; 126 127 CellConstraints cc = new CellConstraints(); 129 FormLayout layout = new FormLayout( 130 "right:max(80dlu;pref), 3dlu, fill:max(200dlu;pref)", 131 "p, 3dlu, p, 3dlu, p"); 132 PanelBuilder builder = new PanelBuilder(layout); 133 builder.setDefaultDialogBorder(); 134 135 builder.addSeparator("SQLTemplate Settings", cc.xywh(1, 1, 3, 1)); 136 builder.addLabel("Query Name:", cc.xy(1, 3)); 137 builder.add(name.getComponent(), cc.xy(3, 3)); 138 builder.addLabel("Is Selecting:", cc.xy(1, 5)); 139 builder.add(selecting, cc.xy(3, 5)); 140 141 this.setLayout(new BorderLayout ()); 142 this.add(builder.getPanel(), BorderLayout.NORTH); 143 this.add(properties, BorderLayout.CENTER); 144 } 145 146 private void initController() { 147 148 selecting.addActionListener(new ActionListener () { 149 150 public void actionPerformed(ActionEvent event) { 151 properties.setEnabled(selecting.isSelected()); 152 153 SQLTemplate query = getQuery(); 154 if (query != null) { 155 query.setSelecting(selecting.isSelected()); 156 mediator.fireQueryEvent(new QueryEvent(this, query)); 157 } 158 } 159 }); 160 161 } 162 163 167 void initFromModel() { 168 Query query = mediator.getCurrentQuery(); 169 170 if (!(query instanceof SQLTemplate)) { 171 setVisible(false); 172 return; 173 } 174 175 SQLTemplate sqlQuery = (SQLTemplate) query; 176 177 name.setText(sqlQuery.getName()); 178 selecting.setSelected(sqlQuery.isSelecting()); 179 properties.setEnabled(sqlQuery.isSelecting()); 180 properties.initFromModel(sqlQuery); 181 182 setVisible(true); 183 } 184 185 protected SQLTemplate getQuery() { 186 Query query = mediator.getCurrentQuery(); 187 return (query instanceof SQLTemplate) ? (SQLTemplate) query : null; 188 } 189 190 193 void setQueryName(String newName) { 194 if (newName != null && newName.trim().length() == 0) { 195 newName = null; 196 } 197 198 Query query = getQuery(); 199 200 if(query == null) { 201 return; 202 } 203 204 if (Util.nullSafeEquals(newName, query.getName())) { 205 return; 206 } 207 208 if (newName == null) { 209 throw new ValidationException("Query name is required."); 210 } 211 212 DataMap map = mediator.getCurrentDataMap(); 213 214 if (map.getQuery(newName) == null) { 215 QueryEvent e = new QueryEvent(this, query, query.getName()); 217 ProjectUtil.setQueryName(map, query, newName); 218 mediator.fireQueryEvent(e); 219 } 220 else { 221 throw new ValidationException("There is another query named '" 223 + newName 224 + "'. Use a different name."); 225 } 226 } 227 228 231 ObjEntity getEntity(SQLTemplate query) { 232 return query != null && query.getRoot() instanceof ObjEntity ? (ObjEntity) query 233 .getRoot() : null; 234 } 235 236 void setEntity(ObjEntity entity) { 237 SQLTemplate template = getQuery(); 238 if (template != null) { 239 Object root = entity != null ? (Object ) entity : mediator.getCurrentDataMap(); 241 template.setRoot(root); 242 243 mediator.fireQueryEvent(new QueryEvent(this, template)); 244 } 245 } 246 } | Popular Tags |