KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > dialog > EditDerivedParamsDialog


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;
57
58 import java.awt.BorderLayout JavaDoc;
59 import java.awt.Container JavaDoc;
60 import java.awt.event.ActionEvent JavaDoc;
61 import java.awt.event.ActionListener JavaDoc;
62 import java.util.ArrayList JavaDoc;
63 import java.util.Iterator JavaDoc;
64 import java.util.List JavaDoc;
65
66 import javax.swing.DefaultCellEditor JavaDoc;
67 import javax.swing.JButton JavaDoc;
68 import javax.swing.JComboBox JavaDoc;
69 import javax.swing.JPanel JavaDoc;
70 import javax.swing.JTable JavaDoc;
71 import javax.swing.table.TableColumn JavaDoc;
72
73 import org.objectstyle.cayenne.map.DbAttribute;
74 import org.objectstyle.cayenne.map.DbEntity;
75 import org.objectstyle.cayenne.map.DerivedDbAttribute;
76 import org.objectstyle.cayenne.map.DerivedDbEntity;
77 import org.objectstyle.cayenne.map.event.AttributeEvent;
78 import org.objectstyle.cayenne.modeler.Application;
79 import org.objectstyle.cayenne.modeler.editor.DerivedAttributeParamsTableModel;
80 import org.objectstyle.cayenne.modeler.util.CayenneDialog;
81 import org.objectstyle.cayenne.modeler.util.CayenneTable;
82 import org.objectstyle.cayenne.modeler.util.CayenneTableModel;
83 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory;
84 import org.objectstyle.cayenne.modeler.util.PanelFactory;
85
86 /**
87  * Dialog window that alows selecting DbAttributes
88  * for derived attribute expression.
89  *
90  * @author Andrei Adamchik
91  */

92 public class EditDerivedParamsDialog extends CayenneDialog implements ActionListener JavaDoc {
93
94     protected DerivedDbAttribute attr;
95
96     protected JTable JavaDoc table = new CayenneTable();
97     protected JButton JavaDoc add = new JButton JavaDoc("Add");
98     protected JButton JavaDoc remove = new JButton JavaDoc("Remove");
99     protected JButton JavaDoc save = new JButton JavaDoc("Save");
100     protected JButton JavaDoc cancel = new JButton JavaDoc("Cancel");
101
102     /**
103      * Constructor for EditDerivedParamsDialog.
104      */

105     public EditDerivedParamsDialog(DerivedDbAttribute attr) {
106         super(Application.getFrame(), "Edit Derived Attribute Parameters", true);
107
108         this.attr = attr;
109
110         init();
111         pack();
112         centerWindow();
113     }
114
115     protected void init() {
116         Container JavaDoc pane = getContentPane();
117         pane.setLayout(new BorderLayout JavaDoc());
118
119         buildTable();
120
121         JPanel JavaDoc panel =
122             PanelFactory.createTablePanel(
123                 table,
124                 new JButton JavaDoc[] { add, remove, save, cancel });
125         pane.add(panel, BorderLayout.CENTER);
126
127         add.addActionListener(this);
128         remove.addActionListener(this);
129         save.addActionListener(this);
130         cancel.addActionListener(this);
131     }
132
133     protected void buildTable() {
134         DerivedAttributeParamsTableModel model =
135             new DerivedAttributeParamsTableModel(attr, getMediator(), this);
136         table.setModel(model);
137         table.setRowHeight(25);
138         table.setRowMargin(3);
139         TableColumn JavaDoc nameCol = table.getColumnModel().getColumn(model.nameColumnInd());
140         nameCol.setMinWidth(150);
141
142         TableColumn JavaDoc typeCol = table.getColumnModel().getColumn(model.typeColumnInd());
143         typeCol.setMinWidth(90);
144
145         DbEntity parent = ((DerivedDbEntity) attr.getEntity()).getParentEntity();
146
147         List JavaDoc list = new ArrayList JavaDoc(32);
148         list.add("");
149         list.addAll(parent.getAttributeMap().keySet());
150         String JavaDoc[] names = (String JavaDoc[]) (list.toArray(new String JavaDoc[list.size()]));
151
152         JComboBox JavaDoc comboBox = CayenneWidgetFactory.createComboBox(names, true);
153         comboBox.setEditable(false);
154         nameCol.setCellEditor(new DefaultCellEditor JavaDoc(comboBox));
155     }
156
157     /**
158      * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
159      */

160     public void actionPerformed(ActionEvent JavaDoc e) {
161         Object JavaDoc src = e.getSource();
162         if (src == add) {
163             addRow();
164         }
165         else if (src == remove) {
166             removeRow();
167         }
168         else if (src == save) {
169             save();
170         }
171         else if (src == cancel) {
172             cancel();
173         }
174     }
175
176     protected void removeRow() {
177         DerivedAttributeParamsTableModel model =
178             (DerivedAttributeParamsTableModel) table.getModel();
179         model.removeRow(model.getAttribute(table.getSelectedRow()));
180     }
181
182     protected void addRow() {
183         ((CayenneTableModel) table.getModel()).addRow(null);
184     }
185
186     protected void save() {
187         // update parameters of the derived attribute
188
attr.clearParams();
189         Iterator JavaDoc it = ((CayenneTableModel) table.getModel()).getObjectList().iterator();
190         while (it.hasNext()) {
191             DbAttribute at = (DbAttribute) it.next();
192             attr.addParam(at);
193         }
194
195         // notify interested parties about the changes
196
getMediator().fireDbAttributeEvent(
197             new AttributeEvent(
198                 this,
199                 attr,
200                 attr.getEntity(),
201                 AttributeEvent.CHANGE));
202
203         setVisible(false);
204     }
205
206     protected void cancel() {
207         setVisible(false);
208     }
209 }
210
Popular Tags