1 /*********************************************************************************
2 * *
3 * Raptor - Rapid prototyping of Swing GUIs based on JavaBeans like Java objects *
4 * Copyright (C) 2003 XCOM AG *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the Free Software *
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 * *
20 *********************************************************************************/
21 package net.sf.raptor.ui.panels.persistence;
22
23 import java.awt.BorderLayout;
24 import java.beans.BeanInfo;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import javax.swing.Action;
29 import javax.swing.JButton;
30
31 import net.sf.raptor.hibernate.PersistenceManager;
32 import net.sf.raptor.ui.BeanInfoUtils;
33 import net.sf.raptor.ui.Filter;
34 import net.sf.raptor.ui.WindowUtils;
35 import net.sf.raptor.ui.XBeanInfo;
36 import net.sf.raptor.ui.actions.ActionUtils;
37 import net.sf.raptor.ui.dialog.CloseDialog;
38 import net.sf.raptor.ui.panels.BeanEditPanel;
39 import net.sf.raptor.ui.panels.ListTablePanel;
40 import net.sf.raptor.ui.panels.XPanel;
41 import net.sf.raptor.ui.tables.ClassColumnModel;
42 import net.sf.raptor.ui.tables.ListTableModel;
43 import net.sf.raptor.ui.tables.SortableTable;
44
45
46 /**
47 * @author thomasg
48 *
49 * To change the template for this generated type comment go to
50 * Window>Preferences>Java>Code Generation>Code and Comments
51 */
52 public abstract class AbstractOverviewPanel extends XPanel {
53
54 protected PersistenceManager persistenceManager;
55 protected BeanEditPanel filterPanel;
56 protected ListTablePanel resultPanel;
57 protected Class clazz;
58 protected Class detailPanelClazz = DetailPanel.class;
59
60 private ListTableModel tableModel;
61 /**
62 * constructor
63 */
64 public AbstractOverviewPanel(PersistenceManager clientManagerValue, Class classValue) {
65
66 this(clientManagerValue, classValue, DetailPanel.class);
67
68 }
69
70 /**
71 * constructor
72 */
73 public AbstractOverviewPanel(PersistenceManager persistenceManagerValue, Class classValue, Class detailPanelClassValue) {
74 persistenceManager = persistenceManagerValue;
75 clazz = classValue;
76 detailPanelClazz = detailPanelClassValue;
77
78 setLayout(new BorderLayout());
79 initComponents();
80 }
81
82 protected void initComponents() {
83 this.removeAll();
84 initializeDefault();
85 initFilterArea();
86 initResultArea();
87 }
88
89 /**
90 * @return
91 */
92 public Class getClazz() {
93 return clazz;
94 }
95
96 /**
97 * initFilterArea
98 */
99 protected void initFilterArea() {
100 filterPanel = createSelectionPanel();
101 if (filterPanel != null) {
102 List actions = ActionUtils.getAllActionsFor(this);
103 filterPanel.add(new JButton((Action) actions.get(0)), BorderLayout.EAST);
104 if (filterPanel != null) {
105 add(filterPanel, BorderLayout.NORTH);
106 }
107 }
108 }
109
110 /**
111 * initResultArea
112 */
113 protected void initResultArea() {
114
115 // table panel instantiieren und in die mitte packen
116 ClassColumnModel columnModel = createClassColumnModel();
117
118 tableModel = new ListTableModel(clazz, getResultList());
119 resultPanel = new ListTablePanel(new SortableTable(tableModel, columnModel), true);
120 resultPanel.setDetailPanelClass(detailPanelClazz);
121 add(resultPanel, BorderLayout.CENTER);
122
123 }
124
125 /**
126 * createSelectionPanel
127 *
128 * @return
129 */
130 protected BeanEditPanel createSelectionPanel() {
131
132 Filter filter = createFilter();
133
134 if (filter != null) {
135 BeanEditPanel filterPanel = new BeanEditPanel(filter);
136 return filterPanel;
137 } else {
138 return null;
139 }
140
141 }
142
143 /**
144 * createFilter
145 * @return
146 */
147 abstract protected Filter createFilter();
148
149 /**
150 * getResultList
151 */
152 protected List getResultList() {
153 return persistenceManager.find(clazz);
154 }
155
156 /**
157 * @return
158 */
159 protected ClassColumnModel createClassColumnModel() {
160 BeanInfo info = BeanInfoUtils.getBeanInfoFor(clazz);
161 if( info instanceof XBeanInfo ) {
162 List order = ((XBeanInfo)info).getOrder().getOrder();
163 String[] names = new String[order.size()];
164 int i=0;
165 for (Iterator iter = order.iterator(); iter.hasNext();) {
166 String element = (String) iter.next();
167 names[i++]=element;
168 }
169 if(names.length>0) {
170 return new ClassColumnModel(clazz, names);
171 }
172 }
173 return new ClassColumnModel(clazz);
174 }
175
176 /**
177 * doModalInDialog
178 */
179 public Object doModalInDialog() {
180 CloseDialog dialog = new CloseDialog(WindowUtils.getDefaultParentFrame());
181 return WindowUtils.wrapInDialog(this, dialog);
182 }
183
184 }