1 56 package org.objectstyle.cayenne.modeler.editor; 57 58 import java.awt.BorderLayout ; 59 import java.awt.Color ; 60 import java.awt.Component ; 61 import java.util.ArrayList ; 62 import java.util.Arrays ; 63 import java.util.Collections ; 64 import java.util.List ; 65 import java.util.Map ; 66 67 import javax.swing.DefaultComboBoxModel ; 68 import javax.swing.DefaultListCellRenderer ; 69 import javax.swing.JList ; 70 import javax.swing.JPanel ; 71 import javax.swing.JScrollPane ; 72 import javax.swing.JTextArea ; 73 import javax.swing.ListSelectionModel ; 74 import javax.swing.event.DocumentEvent ; 75 import javax.swing.event.ListSelectionEvent ; 76 import javax.swing.event.ListSelectionListener ; 77 import javax.swing.text.BadLocationException ; 78 import javax.swing.text.Document ; 79 80 import org.objectstyle.cayenne.map.event.QueryEvent; 81 import org.objectstyle.cayenne.modeler.ProjectController; 82 import org.objectstyle.cayenne.modeler.util.DbAdapterInfo; 83 import org.objectstyle.cayenne.modeler.util.TextAdapter; 84 import org.objectstyle.cayenne.query.Query; 85 import org.objectstyle.cayenne.query.SQLTemplate; 86 import org.objectstyle.cayenne.util.Util; 87 88 import com.jgoodies.forms.builder.PanelBuilder; 89 import com.jgoodies.forms.layout.CellConstraints; 90 import com.jgoodies.forms.layout.FormLayout; 91 92 97 public class SQLTemplateScriptsTab extends JPanel { 98 99 private static final String DEFAULT_LABEL = "Default"; 100 101 protected ProjectController mediator; 102 103 protected JList scripts; 104 protected TextAdapter script; 105 protected ListSelectionListener scriptRefreshHandler; 106 107 public SQLTemplateScriptsTab(ProjectController mediator) { 108 this.mediator = mediator; 109 110 initView(); 111 } 112 113 protected void initView() { 114 116 scriptRefreshHandler = new ListSelectionListener () { 117 118 public void valueChanged(ListSelectionEvent e) { 119 if (!e.getValueIsAdjusting()) { 120 displayScript(); 121 } 122 } 123 }; 124 125 scripts = new JList (); 126 scripts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 127 scripts.setCellRenderer(new DbAdapterListRenderer(DbAdapterInfo 128 .getStandardAdapterLabels())); 129 130 List keys = new ArrayList (DbAdapterInfo.getStandardAdapters().length + 1); 131 keys.addAll(Arrays.asList(DbAdapterInfo.getStandardAdapters())); 132 Collections.sort(keys); 133 keys.add(0, DEFAULT_LABEL); 134 scripts.setModel(new DefaultComboBoxModel (keys.toArray())); 135 136 script = new TextAdapter(new JTextArea (15, 30)) { 137 138 protected void updateModel(String text) { 139 setSQL(text); 140 } 141 }; 142 143 CellConstraints cc = new CellConstraints(); 145 PanelBuilder builder = new PanelBuilder(new FormLayout( 146 "fill:100dlu, 3dlu, fill:100dlu:grow", 147 "3dlu, fill:p:grow")); 148 149 builder.add(new JScrollPane ( 151 scripts, 152 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 153 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), cc.xy(1, 2)); 154 builder.add(new JScrollPane (script.getComponent()), cc.xy(3, 2)); 155 156 setLayout(new BorderLayout ()); 157 add(builder.getPanel(), BorderLayout.CENTER); 158 } 159 160 void initFromModel() { 161 Query query = mediator.getCurrentQuery(); 162 163 if (!(query instanceof SQLTemplate)) { 164 setVisible(false); 165 return; 166 } 167 168 scripts.removeListSelectionListener(scriptRefreshHandler); 170 scripts.setSelectedIndex(0); 171 displayScript(); 172 scripts.addListSelectionListener(scriptRefreshHandler); 173 174 script.getComponent().setEnabled(true); 175 setVisible(true); 176 } 177 178 181 String getSQLTemplate(String key) { 182 if (key == null) { 183 return null; 184 } 185 186 SQLTemplate query = getQuery(); 187 if (query == null) { 188 return null; 189 } 190 191 return (key.equals(DEFAULT_LABEL)) ? query.getDefaultTemplate() : query 192 .getCustomTemplate(key); 193 } 194 195 SQLTemplate getQuery() { 196 Query query = mediator.getCurrentQuery(); 197 return (query instanceof SQLTemplate) ? (SQLTemplate) query : null; 198 } 199 200 203 void displayScript() { 204 205 SQLTemplate query = getQuery(); 206 if (query == null) { 207 disableEditor(); 208 return; 209 } 210 211 String key = (String ) scripts.getSelectedValue(); 212 if (key == null) { 213 disableEditor(); 214 return; 215 } 216 217 enableEditor(); 218 219 String text = (key.equals(DEFAULT_LABEL)) ? query.getDefaultTemplate() : query 220 .getCustomTemplate(key); 221 222 script.setText(text); 223 } 224 225 void disableEditor() { 226 script.setText(null); 227 script.getComponent().setEnabled(false); 228 script.getComponent().setEditable(false); 229 script.getComponent().setBackground(getBackground()); 230 } 231 232 void enableEditor() { 233 script.getComponent().setEnabled(true); 234 script.getComponent().setEditable(true); 235 script.getComponent().setBackground(Color.WHITE); 236 } 237 238 void setSQL(DocumentEvent e) { 239 Document doc = e.getDocument(); 240 241 try { 242 setSQL(doc.getText(0, doc.getLength())); 243 } 244 catch (BadLocationException e1) { 245 e1.printStackTrace(); 246 } 247 248 } 249 250 253 void setSQL(String text) { 254 SQLTemplate query = getQuery(); 255 if (query == null) { 256 return; 257 } 258 259 String key = (String ) scripts.getSelectedValue(); 260 if (key == null) { 261 return; 262 } 263 264 if (text != null) { 265 text = text.trim(); 266 if (text.length() == 0) { 267 text = null; 268 } 269 } 270 271 if (key.equals(DEFAULT_LABEL)) { 274 if (!Util.nullSafeEquals(text, query.getDefaultTemplate())) { 275 query.setDefaultTemplate(text); 276 mediator.fireQueryEvent(new QueryEvent(this, query)); 277 } 278 } 279 else { 280 if (!Util.nullSafeEquals(text, query.getTemplate(key))) { 281 query.setTemplate(key, text); 282 mediator.fireQueryEvent(new QueryEvent(this, query)); 283 } 284 } 285 } 286 287 final class DbAdapterListRenderer extends DefaultListCellRenderer { 288 289 Map adapterLabels; 290 291 DbAdapterListRenderer(Map adapterLabels) { 292 this.adapterLabels = (adapterLabels != null) 293 ? adapterLabels 294 : Collections.EMPTY_MAP; 295 } 296 297 public Component getListCellRendererComponent( 298 JList list, 299 Object object, 300 int index, 301 boolean selected, 302 boolean hasFocus) { 303 304 if (object instanceof Class ) { 305 object = ((Class ) object).getName(); 306 } 307 308 Object label = adapterLabels.get(object); 309 if (label == null) { 310 label = object; 311 } 312 313 Component c = super.getListCellRendererComponent( 314 list, 315 label, 316 index, 317 selected, 318 hasFocus); 319 320 setForeground(selected || getSQLTemplate(object.toString()) != null 322 ? Color.BLACK 323 : Color.LIGHT_GRAY); 324 325 return c; 326 } 327 } 328 } | Popular Tags |