KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > editor > ProcedureParameterTab


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.modeler.editor;
58
59 import java.awt.BorderLayout JavaDoc;
60 import java.awt.event.ActionEvent JavaDoc;
61 import java.awt.event.ActionListener JavaDoc;
62 import java.util.EventObject JavaDoc;
63
64 import javax.swing.DefaultCellEditor JavaDoc;
65 import javax.swing.JButton JavaDoc;
66 import javax.swing.JComboBox JavaDoc;
67 import javax.swing.JPanel JavaDoc;
68 import javax.swing.JToolBar JavaDoc;
69 import javax.swing.SwingConstants JavaDoc;
70 import javax.swing.event.ChangeEvent JavaDoc;
71 import javax.swing.event.ListSelectionEvent JavaDoc;
72 import javax.swing.event.ListSelectionListener JavaDoc;
73 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
74 import javax.swing.table.TableColumn JavaDoc;
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 /**
97  * @author Andrei Adamchik
98  */

99 public class ProcedureParameterTab
100     extends JPanel JavaDoc
101     implements
102         ProcedureParameterListener,
103         ProcedureDisplayListener,
104         ExistingSelectionProcessor,
105         ActionListener JavaDoc {
106     
107     protected ProjectController eventController;
108
109     protected CayenneTable table;
110     protected JButton JavaDoc removeParameterButton;
111     protected JButton JavaDoc moveUp;
112     protected JButton JavaDoc 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 JavaDoc() {
123             public void valueChanged(ListSelectionEvent JavaDoc 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 JavaDoc());
134
135         JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
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 JavaDoc();
144         moveUp.setIcon(ModelerUtil.buildIcon("icon-move_up.gif"));
145         moveUp.setToolTipText("Move Parameter Up");
146         toolBar.add(moveUp);
147         
148         moveDown = new JButton JavaDoc();
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         // Create table with two columns and no rows.
156
table = new CayenneTable();
157         add(PanelFactory.createTablePanel(table, null), BorderLayout.CENTER);
158     }
159     
160     public void processExistingSelection(EventObject JavaDoc e) {
161         if (e instanceof ChangeEvent JavaDoc){
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             // scroll table
178
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     /**
202       * Invoked when currently selected Procedure object is changed.
203       */

204     public void currentProcedureChanged(ProcedureDisplayEvent e) {
205         Procedure procedure = e.getProcedure();
206         if (procedure != null && e.isProcedureChanged()) {
207             rebuildTable(procedure);
208         }
209     }
210
211     /**
212      * Selects a specified parameter.
213      */

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 JavaDoc 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         // number column tweaking
237
TableColumn JavaDoc numberColumn =
238             table.getColumnModel().getColumn(
239                 ProcedureParameterTableModel.PARAMETER_NUMBER);
240         numberColumn.setPreferredWidth(35);
241         DefaultTableCellRenderer JavaDoc renderer = new DefaultTableCellRenderer JavaDoc();
242         renderer.setHorizontalAlignment(SwingConstants.CENTER);
243         numberColumn.setCellRenderer(renderer);
244
245         // name column tweaking
246
TableColumn JavaDoc nameColumn =
247             table.getColumnModel().getColumn(ProcedureParameterTableModel.PARAMETER_NAME);
248         nameColumn.setMinWidth(150);
249
250         // types column tweaking
251
TableColumn JavaDoc typesColumn =
252             table.getColumnModel().getColumn(ProcedureParameterTableModel.PARAMETER_TYPE);
253         typesColumn.setMinWidth(90);
254
255         JComboBox JavaDoc typesEditor =
256             CayenneWidgetFactory.createComboBox(TypesMapping.getDatabaseTypes(), true);
257         typesEditor.setEditable(true);
258         typesColumn.setCellEditor(new DefaultCellEditor JavaDoc(typesEditor));
259
260         // direction column tweaking
261
TableColumn JavaDoc directionColumn =
262             table.getColumnModel().getColumn(
263                 ProcedureParameterTableModel.PARAMETER_DIRECTION);
264         directionColumn.setMinWidth(90);
265
266         JComboBox JavaDoc directionEditor =
267             CayenneWidgetFactory.createComboBox(
268                 ProcedureParameterTableModel.PARAMETER_DIRECTION_NAMES,
269                 false);
270         directionEditor.setEditable(false);
271         directionColumn.setCellEditor(new DefaultCellEditor JavaDoc(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 JavaDoc 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             // note that 'setCallParameters' is donw by copy internally
312
parameter.getProcedure().setCallParameters(model.getObjectList());
313             eventController.fireProcedureEvent(
314                 new ProcedureEvent(this, parameter.getProcedure(), MapEvent.CHANGE));
315         }
316     }
317 }
318
Popular Tags