1 56 package org.objectstyle.cayenne.modeler.editor; 57 58 import java.awt.event.ActionEvent ; 59 import java.awt.event.ActionListener ; 60 61 import javax.swing.JButton ; 62 import javax.swing.table.AbstractTableModel ; 63 import javax.swing.table.TableModel ; 64 import javax.swing.tree.TreeModel ; 65 66 import org.objectstyle.cayenne.exp.Expression; 67 import org.objectstyle.cayenne.exp.ExpressionException; 68 import org.objectstyle.cayenne.map.Entity; 69 import org.objectstyle.cayenne.map.Relationship; 70 import org.objectstyle.cayenne.map.event.QueryEvent; 71 import org.objectstyle.cayenne.modeler.ProjectController; 72 import org.objectstyle.cayenne.modeler.util.EntityTreeModel; 73 74 79 public class SelectQueryPrefetchTab extends SelectQueryOrderingTab { 80 81 public SelectQueryPrefetchTab(ProjectController mediator) { 82 super(mediator); 83 } 84 85 protected JButton createAddPathButton() { 86 JButton button = new JButton ("Add Prefetch"); 87 button.addActionListener(new ActionListener () { 88 89 public void actionPerformed(ActionEvent event) { 90 addPrefetch(); 91 } 92 }); 93 94 return button; 95 } 96 97 protected JButton createRemovePathButton() { 98 JButton button = new JButton ("Remove Prefetch"); 99 button.addActionListener(new ActionListener () { 100 101 public void actionPerformed(ActionEvent event) { 102 removePrefetch(); 103 } 104 }); 105 106 return button; 107 } 108 109 protected TreeModel createBrowserModel(Entity entity) { 110 EntityTreeModel treeModel = new EntityTreeModel(entity); 111 treeModel.setHideAttributes(true); 112 return treeModel; 113 } 114 115 protected TableModel createTableModel() { 116 return new PrefetchModel(); 117 } 118 119 void addPrefetch() { 120 String prefetch = getSelectedPath(); 121 if (prefetch == null) { 122 return; 123 } 124 125 if (selectQuery.getPrefetches().contains(prefetch)) { 127 return; 128 } 129 130 selectQuery.addPrefetch(prefetch); 131 132 table.setModel(createTableModel()); 134 mediator.fireQueryEvent(new QueryEvent(SelectQueryPrefetchTab.this, selectQuery)); 135 } 136 137 void removePrefetch() { 138 int selection = table.getSelectedRow(); 139 if (selection < 0) { 140 return; 141 } 142 143 String prefetch = (String ) table.getModel().getValueAt(selection, 0); 144 selectQuery.removePrefetch(prefetch); 145 146 table.setModel(createTableModel()); 148 mediator.fireQueryEvent(new QueryEvent(SelectQueryPrefetchTab.this, selectQuery)); 149 } 150 151 boolean isToMany(String prefetch) { 152 if (selectQuery == null) { 153 return false; 154 } 155 156 Object root = selectQuery.getRoot(); 157 158 try { 160 Expression exp = Expression.fromString(prefetch); 161 Object object = exp.evaluate(root); 162 if (object instanceof Relationship) { 163 return ((Relationship) object).isToMany(); 164 } 165 else { 166 return false; 167 } 168 } 169 catch (ExpressionException e) { 170 return false; 171 } 172 } 173 174 177 final class PrefetchModel extends AbstractTableModel { 178 179 String [] prefetches; 180 181 PrefetchModel() { 182 if (selectQuery != null) { 183 prefetches = new String [selectQuery.getPrefetches().size()]; 184 selectQuery.getPrefetches().toArray(prefetches); 185 } 186 } 187 188 public int getColumnCount() { 189 return 2; 190 } 191 192 public int getRowCount() { 193 return (prefetches != null) ? prefetches.length : 0; 194 } 195 196 public Object getValueAt(int row, int column) { 197 switch (column) { 198 case 0: 199 return prefetches[row]; 200 case 1: 201 return isToMany(prefetches[row]) ? Boolean.TRUE : Boolean.FALSE; 202 default: 203 throw new IndexOutOfBoundsException ("Invalid column: " + column); 204 } 205 } 206 207 public Class getColumnClass(int column) { 208 switch (column) { 209 case 0: 210 return String .class; 211 case 1: 212 return Boolean .class; 213 default: 214 throw new IndexOutOfBoundsException ("Invalid column: " + column); 215 } 216 } 217 218 public String getColumnName(int column) { 219 switch (column) { 220 case 0: 221 return "Prefetch Path"; 222 case 1: 223 return "To Many"; 224 default: 225 throw new IndexOutOfBoundsException ("Invalid column: " + column); 226 } 227 } 228 229 public boolean isCellEditable(int row, int column) { 230 return false; 231 } 232 } 233 } | Popular Tags |