1 56 57 package org.objectstyle.cayenne.modeler.editor; 58 59 import java.awt.BorderLayout ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 import java.util.EventObject ; 63 64 import javax.swing.DefaultCellEditor ; 65 import javax.swing.JButton ; 66 import javax.swing.JComboBox ; 67 import javax.swing.JPanel ; 68 import javax.swing.JToolBar ; 69 import javax.swing.SwingConstants ; 70 import javax.swing.event.ChangeEvent ; 71 import javax.swing.event.ListSelectionEvent ; 72 import javax.swing.event.ListSelectionListener ; 73 import javax.swing.table.DefaultTableCellRenderer ; 74 import javax.swing.table.TableColumn ; 75 76 import org.objectstyle.cayenne.dba.TypesMapping; 77 import org.objectstyle.cayenne.map.Procedure; 78 import org.objectstyle.cayenne.map.ProcedureParameter; 79 import org.objectstyle.cayenne.map.event.MapEvent; 80 import org.objectstyle.cayenne.map.event.ProcedureEvent; 81 import org.objectstyle.cayenne.map.event.ProcedureParameterEvent; 82 import org.objectstyle.cayenne.map.event.ProcedureParameterListener; 83 import org.objectstyle.cayenne.modeler.Application; 84 import org.objectstyle.cayenne.modeler.ProjectController; 85 import org.objectstyle.cayenne.modeler.action.CreateProcedureParameterAction; 86 import org.objectstyle.cayenne.modeler.action.RemoveProcedureParameterAction; 87 import org.objectstyle.cayenne.modeler.event.ProcedureDisplayEvent; 88 import org.objectstyle.cayenne.modeler.event.ProcedureDisplayListener; 89 import org.objectstyle.cayenne.modeler.event.ProcedureParameterDisplayEvent; 90 import org.objectstyle.cayenne.modeler.util.CayenneTable; 91 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory; 92 import org.objectstyle.cayenne.modeler.util.ModelerUtil; 93 import org.objectstyle.cayenne.modeler.util.PanelFactory; 94 import org.objectstyle.cayenne.modeler.util.UIUtil; 95 96 99 public class ProcedureParameterTab 100 extends JPanel 101 implements 102 ProcedureParameterListener, 103 ProcedureDisplayListener, 104 ExistingSelectionProcessor, 105 ActionListener { 106 107 protected ProjectController eventController; 108 109 protected CayenneTable table; 110 protected JButton removeParameterButton; 111 protected JButton moveUp; 112 protected JButton moveDown; 113 114 public ProcedureParameterTab(ProjectController eventController) { 115 this.eventController = eventController; 116 117 init(); 118 119 eventController.addProcedureDisplayListener(this); 120 eventController.addProcedureParameterListener(this); 121 122 table.getSelectionModel().addListSelectionListener(new ListSelectionListener () { 123 public void valueChanged(ListSelectionEvent e) { 124 processExistingSelection(e); 125 } 126 }); 127 128 moveDown.addActionListener(this); 129 moveUp.addActionListener(this); 130 } 131 132 protected void init() { 133 setLayout(new BorderLayout ()); 134 135 JToolBar toolBar = new JToolBar (); 136 137 Application app = Application.getInstance(); 138 toolBar.add(app.getAction(CreateProcedureParameterAction.getActionName()).buildButton()); 139 removeParameterButton = app.getAction(RemoveProcedureParameterAction.getActionName()).buildButton(); 140 toolBar.add(removeParameterButton); 141 toolBar.addSeparator(); 142 143 moveUp = new JButton (); 144 moveUp.setIcon(ModelerUtil.buildIcon("icon-move_up.gif")); 145 moveUp.setToolTipText("Move Parameter Up"); 146 toolBar.add(moveUp); 147 148 moveDown = new JButton (); 149 moveDown.setIcon(ModelerUtil.buildIcon("icon-move_down.gif")); 150 moveDown.setToolTipText("Move Parameter Down"); 151 toolBar.add(moveDown); 152 153 add(toolBar, BorderLayout.NORTH); 154 155 table = new CayenneTable(); 157 add(PanelFactory.createTablePanel(table, null), BorderLayout.CENTER); 158 } 159 160 public void processExistingSelection(EventObject e) { 161 if (e instanceof ChangeEvent ){ 162 table.clearSelection(); 163 } 164 165 ProcedureParameter parameter = null; 166 boolean enableUp = false; 167 boolean enableDown = false; 168 boolean enableRemoveButton = false; 169 170 int selectedRow = table.getSelectedRow(); 171 if (selectedRow >= 0) { 172 enableRemoveButton = true; 173 ProcedureParameterTableModel model = 174 (ProcedureParameterTableModel) table.getModel(); 175 parameter = model.getParameter(table.getSelectedRow()); 176 177 UIUtil.scrollToSelectedRow(table); 179 180 int rowCount = table.getRowCount(); 181 if (rowCount > 1){ 182 if (selectedRow >0) enableUp = true; 183 if (selectedRow < (rowCount - 1)) enableDown = true; 184 } 185 } 186 187 removeParameterButton.setEnabled(enableRemoveButton); 188 moveUp.setEnabled(enableUp); 189 moveDown.setEnabled(enableDown); 190 191 ProcedureParameterDisplayEvent ppde = 192 new ProcedureParameterDisplayEvent( 193 this, 194 parameter, 195 eventController.getCurrentProcedure(), 196 eventController.getCurrentDataMap(), 197 eventController.getCurrentDataDomain()); 198 eventController.fireProcedureParameterDisplayEvent(ppde); 199 } 200 201 204 public void currentProcedureChanged(ProcedureDisplayEvent e) { 205 Procedure procedure = e.getProcedure(); 206 if (procedure != null && e.isProcedureChanged()) { 207 rebuildTable(procedure); 208 } 209 } 210 211 214 public void selectParameter(ProcedureParameter parameter) { 215 if (parameter == null) { 216 return; 217 } 218 219 ProcedureParameterTableModel model = 220 (ProcedureParameterTableModel) table.getModel(); 221 java.util.List parameters = model.getObjectList(); 222 int pos = parameters.indexOf(parameter); 223 if (pos >= 0) { 224 table.select(pos); 225 } 226 } 227 228 protected void rebuildTable(Procedure procedure) { 229 ProcedureParameterTableModel model = 230 new ProcedureParameterTableModel(procedure, eventController, this); 231 232 table.setModel(model); 233 table.setRowHeight(25); 234 table.setRowMargin(3); 235 236 TableColumn numberColumn = 238 table.getColumnModel().getColumn( 239 ProcedureParameterTableModel.PARAMETER_NUMBER); 240 numberColumn.setPreferredWidth(35); 241 DefaultTableCellRenderer renderer = new DefaultTableCellRenderer (); 242 renderer.setHorizontalAlignment(SwingConstants.CENTER); 243 numberColumn.setCellRenderer(renderer); 244 245 TableColumn nameColumn = 247 table.getColumnModel().getColumn(ProcedureParameterTableModel.PARAMETER_NAME); 248 nameColumn.setMinWidth(150); 249 250 TableColumn typesColumn = 252 table.getColumnModel().getColumn(ProcedureParameterTableModel.PARAMETER_TYPE); 253 typesColumn.setMinWidth(90); 254 255 JComboBox typesEditor = 256 CayenneWidgetFactory.createComboBox(TypesMapping.getDatabaseTypes(), true); 257 typesEditor.setEditable(true); 258 typesColumn.setCellEditor(new DefaultCellEditor (typesEditor)); 259 260 TableColumn directionColumn = 262 table.getColumnModel().getColumn( 263 ProcedureParameterTableModel.PARAMETER_DIRECTION); 264 directionColumn.setMinWidth(90); 265 266 JComboBox directionEditor = 267 CayenneWidgetFactory.createComboBox( 268 ProcedureParameterTableModel.PARAMETER_DIRECTION_NAMES, 269 false); 270 directionEditor.setEditable(false); 271 directionColumn.setCellEditor(new DefaultCellEditor (directionEditor)); 272 273 moveUp.setEnabled(false); 274 moveDown.setEnabled(false); 275 } 276 277 public void procedureParameterAdded(ProcedureParameterEvent e) { 278 rebuildTable(e.getParameter().getProcedure()); 279 table.select(e.getParameter()); 280 } 281 282 public void procedureParameterChanged(ProcedureParameterEvent e) { 283 table.select(e.getParameter()); 284 } 285 286 public void procedureParameterRemoved(ProcedureParameterEvent e) { 287 ProcedureParameterTableModel model = 288 (ProcedureParameterTableModel) table.getModel(); 289 int ind = model.getObjectList().indexOf(e.getParameter()); 290 model.removeRow(e.getParameter()); 291 table.select(ind); 292 } 293 294 public void actionPerformed(ActionEvent e) { 295 ProcedureParameterTableModel model = 296 (ProcedureParameterTableModel) table.getModel(); 297 ProcedureParameter parameter = model.getParameter(table.getSelectedRow()); 298 299 int index = -1; 300 301 if (e.getSource() == moveUp) { 302 index = model.moveRowUp(parameter); 303 } 304 else if (e.getSource() == moveDown) { 305 index = model.moveRowDown(parameter); 306 } 307 308 if (index >= 0) { 309 table.select(index); 310 311 parameter.getProcedure().setCallParameters(model.getObjectList()); 313 eventController.fireProcedureEvent( 314 new ProcedureEvent(this, parameter.getProcedure(), MapEvent.CHANGE)); 315 } 316 } 317 } 318 | Popular Tags |