KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > dialog > objentity > ObjRelationshipInfoDialog


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 package org.objectstyle.cayenne.modeler.dialog.objentity;
57
58 import java.awt.BorderLayout JavaDoc;
59 import java.awt.Component JavaDoc;
60 import java.awt.Dimension JavaDoc;
61
62 import javax.swing.DefaultCellEditor JavaDoc;
63 import javax.swing.DefaultComboBoxModel JavaDoc;
64 import javax.swing.JButton JavaDoc;
65 import javax.swing.JComboBox JavaDoc;
66 import javax.swing.JScrollPane JavaDoc;
67 import javax.swing.JTable JavaDoc;
68 import javax.swing.table.TableCellEditor JavaDoc;
69
70 import org.apache.log4j.Logger;
71 import org.objectstyle.cayenne.modeler.util.PanelFactory;
72 import org.scopemvc.core.Selector;
73 import org.scopemvc.view.swing.SAction;
74 import org.scopemvc.view.swing.SButton;
75 import org.scopemvc.view.swing.SComboBox;
76 import org.scopemvc.view.swing.SLabel;
77 import org.scopemvc.view.swing.SListCellRenderer;
78 import org.scopemvc.view.swing.SPanel;
79 import org.scopemvc.view.swing.STable;
80 import org.scopemvc.view.swing.STableModel;
81 import org.scopemvc.view.swing.STextField;
82 import org.scopemvc.view.swing.SwingView;
83
84 import com.jgoodies.forms.builder.PanelBuilder;
85 import com.jgoodies.forms.layout.CellConstraints;
86 import com.jgoodies.forms.layout.FormLayout;
87
88 /**
89  * A view of the dialog for mapping an ObjRelationship to one or more
90  * DbRelationships.
91  *
92  * @since 1.1
93  * @author Andrei Adamchik
94  */

95 public class ObjRelationshipInfoDialog extends SPanel {
96     static final Logger logObj = Logger.getLogger(ObjRelationshipInfoDialog.class);
97
98     protected STable pathTable;
99
100     public ObjRelationshipInfoDialog() {
101         init();
102     }
103
104     protected void init() {
105         // create widgets
106
SButton saveButton =
107             new SButton(new SAction(ObjRelationshipInfoController.SAVE_CONTROL));
108         saveButton.setEnabled(true);
109
110         SButton cancelButton =
111             new SButton(new SAction(ObjRelationshipInfoController.CANCEL_CONTROL));
112         cancelButton.setEnabled(true);
113
114         SButton newToOneButton =
115             new SButton(new SAction(ObjRelationshipInfoController.NEW_TOONE_CONTROL));
116         newToOneButton.setEnabled(true);
117         SButton newToManyButton =
118             new SButton(new SAction(ObjRelationshipInfoController.NEW_TOMANY_CONTROL));
119         newToManyButton.setEnabled(true);
120
121         STextField relationshipName = new STextField(25);
122         relationshipName.setSelector(ObjRelationshipInfoModel.RELATIONSHIP_NAME_SELECTOR);
123
124         SLabel sourceEntityLabel = new SLabel();
125         sourceEntityLabel.setSelector(
126             ObjRelationshipInfoModel.SOURCE_ENTITY_NAME_SELECTOR);
127
128         SComboBox targetCombo = new SComboBox();
129         targetCombo.setSelector(ObjRelationshipInfoModel.OBJECT_TARGETS_SELECTOR);
130         targetCombo.setSelectionSelector(ObjRelationshipInfoModel.OBJECT_TARGET_SELECTOR);
131         SListCellRenderer renderer = (SListCellRenderer) targetCombo.getRenderer();
132         renderer.setTextSelector("name");
133
134         pathTable = new ObjRelationshipPathTable();
135         STableModel pathTableModel = new STableModel(pathTable);
136         pathTableModel.setSelector(
137             ObjRelationshipInfoModel.DB_RELATIONSHIP_PATH_SELECTOR);
138         pathTableModel.setColumnNames(new String JavaDoc[] { "DbRelationships" });
139         pathTableModel.setColumnSelectors(
140             new Selector[] {
141                  EntityRelationshipsModel.RELATIONSHIP_DISPLAY_NAME_SELECTOR });
142
143         pathTable.setModel(pathTableModel);
144         pathTable.setSelectionSelector(
145             ObjRelationshipInfoModel.SELECTED_PATH_COMPONENT_SELECTOR);
146         pathTable.getColumn("DbRelationships").setCellEditor(
147             RelationshipPicker.createEditor(this));
148
149         // assemble
150
setDisplayMode(SwingView.MODAL_DIALOG);
151         setTitle("ObjRelationship Inspector");
152         setLayout(new BorderLayout JavaDoc());
153
154         CellConstraints cc = new CellConstraints();
155         PanelBuilder builder =
156             new PanelBuilder(
157                 new FormLayout(
158                     "right:max(50dlu;pref), 3dlu, fill:min(150dlu;pref), 3dlu, fill:min(150dlu;pref)",
159                     "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, top:14dlu, 3dlu, top:p:grow"));
160         builder.setDefaultDialogBorder();
161
162         builder.addSeparator("ObjRelationship Information", cc.xywh(1, 1, 5, 1));
163         builder.addLabel("Relationship:", cc.xy(1, 3));
164         builder.add(relationshipName, cc.xywh(3, 3, 1, 1));
165         builder.addLabel("Source:", cc.xy(1, 5));
166         builder.add(sourceEntityLabel, cc.xywh(3, 5, 1, 1));
167         builder.addLabel("Target:", cc.xy(1, 7));
168         builder.add(targetCombo, cc.xywh(3, 7, 1, 1));
169
170         builder.addSeparator("Mapping to DbRelationships", cc.xywh(1, 9, 5, 1));
171         builder.add(new JScrollPane JavaDoc(pathTable), cc.xywh(1, 11, 3, 3));
172         builder.add(newToOneButton, cc.xywh(5, 11, 1, 1));
173         builder.add(newToManyButton, cc.xywh(5, 13, 1, 1));
174
175         add(builder.getPanel(), BorderLayout.CENTER);
176         add(
177             PanelFactory.createButtonPanel(new JButton JavaDoc[] { saveButton, cancelButton }),
178             BorderLayout.SOUTH);
179     }
180
181     /**
182      * Cancels any editing that might be going on in the path table.
183      */

184     public void cancelTableEditing() {
185         int row = pathTable.getEditingRow();
186         if (row < 0) {
187             return;
188         }
189
190         int column = pathTable.getEditingColumn();
191         if (column < 0) {
192             return;
193         }
194
195         TableCellEditor JavaDoc editor = pathTable.getCellEditor(row, column);
196         if (editor != null) {
197             editor.cancelCellEditing();
198         }
199     }
200
201     class ObjRelationshipPathTable extends STable {
202         final Dimension JavaDoc preferredSize = new Dimension JavaDoc(203, 100);
203
204         ObjRelationshipPathTable() {
205             setRowHeight(25);
206             setRowMargin(3);
207         }
208
209         public Dimension JavaDoc getPreferredScrollableViewportSize() {
210             return preferredSize;
211         }
212     }
213
214     static final class RelationshipPicker extends DefaultCellEditor JavaDoc {
215         JComboBox JavaDoc comboBox;
216         SwingView view;
217
218         static TableCellEditor JavaDoc createEditor(SwingView view) {
219             JComboBox JavaDoc relationshipCombo = new JComboBox JavaDoc();
220             relationshipCombo.setEditable(false);
221             return new RelationshipPicker(view, relationshipCombo);
222         }
223
224         RelationshipPicker(SwingView view, JComboBox JavaDoc comboBox) {
225             super(comboBox);
226             this.comboBox = comboBox;
227             this.view = view;
228         }
229
230         public Component JavaDoc getTableCellEditorComponent(
231             JTable JavaDoc table,
232             Object JavaDoc value,
233             boolean isSelected,
234             int row,
235             int column) {
236
237             // initialize combo box
238
ObjRelationshipInfoModel model =
239                 (ObjRelationshipInfoModel) view.getBoundModel();
240
241             EntityRelationshipsModel relationshipWrapper =
242                 (EntityRelationshipsModel) model.getDbRelationshipPath().get(row);
243
244             DefaultComboBoxModel JavaDoc comboModel =
245                 new DefaultComboBoxModel JavaDoc(relationshipWrapper.getRelationshipNames());
246             comboModel.setSelectedItem(value);
247             comboBox.setModel(comboModel);
248
249             // call super
250
return super.getTableCellEditorComponent(
251                 table,
252                 value,
253                 isSelected,
254                 row,
255                 column);
256         }
257     }
258 }
259
Popular Tags