001    package market.stdform;
002    
003    import java.awt.GridBagConstraints;
004    import java.awt.GridBagLayout;
005    import java.awt.GridLayout;
006    import java.awt.event.ItemEvent;
007    import java.awt.event.ItemListener;
008    
009    import javax.swing.Box;
010    import javax.swing.BoxLayout;
011    import javax.swing.JComboBox;
012    import javax.swing.JPanel;
013    import javax.swing.JScrollPane;
014    import javax.swing.event.ListSelectionEvent;
015    import javax.swing.event.ListSelectionListener;
016    
017    import market.CFilter;
018    import market.CIArticle;
019    import market.Conversions;
020    import market.SMarket;
021    import market.swing.ComponentFactory;
022    import market.swing.JCTimeRangeBoxes;
023    import market.swing.JTADescriptionArea;
024    import sale.FormSheet;
025    import sale.FormSheetContentCreator;
026    import util.swing.AbstractTableEntryDescriptor;
027    import util.swing.TableEntryDescriptor;
028    import data.Catalog;
029    import data.stdforms.SingleTableFormSheet;
030    import data.swing.CatalogTableModel;
031    
032    /**
033     * This FormSheet is used to choose the article and the range of time for which statistics
034     * should be computed.
035     */
036    public class FSManagerArticleStatsMain {
037    
038        private static JComboBox jcbFromMonth;
039        private static JComboBox jcbToMonth;
040        private static JComboBox jcbFromYear;
041        private static JComboBox jcbToYear;
042        private static JCTimeRangeBoxes jctrb = new JCTimeRangeBoxes();
043    
044        /**
045         * Creates a {@link SingleTableFormSheet} with some extra components. The look of the table is
046         * defined by the {@link TEDManagerArticleStatsMain}.
047         *
048         * @return the created SingleTableFormSheet.
049         */
050        public static SingleTableFormSheet create() {
051            final TableEntryDescriptor ted = new TEDManagerArticleStatsMain();
052            final Catalog source = SMarket.getArticleCatalog();
053            final SingleTableFormSheet stfs = SingleTableFormSheet.create(
054                    "Artikelstatistik",
055                    source,
056                    null,                           //UIGate
057                    ted                             //TED
058            );
059            stfs.addContentCreator(new FormSheetContentCreator() {
060                protected void createFormSheetContent(final FormSheet fs) {
061                    JPanel jpMain = new JPanel();
062                    JPanel jpTable = new JPanel();
063                    JPanel jpFilter = new JPanel();
064                    JPanel jpUpper = new JPanel();
065                    JPanel jpTimeRange = new JPanel();
066                    JPanel jpChooseArticle = new JPanel();
067                    JPanel jpFromSelection = new JPanel();
068                    JPanel jpFromDropdown = new JPanel();
069                    JPanel jpToSelection = new JPanel();
070                    JPanel jpToDropdown = new JPanel();
071                    JPanel jpDescription = new JPanel();
072                    jcbFromYear = jctrb.getFromYearBox();
073                    jcbToYear = jctrb.getToYearBox();
074                    jcbFromMonth = jctrb.getFromMonthBox();
075                    jcbToMonth = jctrb.getToMonthBox();
076                    JScrollPane jsc = new JScrollPane();
077                    GridBagConstraints c = new GridBagConstraints();
078                    GridBagLayout gridbag = new GridBagLayout();
079                    final JComboBox jcbFilter = new JComboBox(SMarket.getArticleCategories());
080                    jcbFilter.addItemListener(new ItemListener() {
081                        public void itemStateChanged(ItemEvent e) {
082                            if (e.getStateChange() == e.SELECTED) {
083                                int i = jcbFilter.getSelectedIndex();
084                                CFilter cf = new CFilter(source, i); //filter CatalogItems
085                                stfs.setTableModel(new CatalogTableModel(cf, null, null, ted));
086                            }
087                        }
088                    });
089                    final JTADescriptionArea da = new JTADescriptionArea();
090                    //add Listener to table that reacts on selection change
091                    stfs.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
092                        public void valueChanged(ListSelectionEvent e) {
093                            if (!e.getValueIsAdjusting()) {
094                                da.setDescription(Conversions.recordToCIArticle(stfs.getSelectedRecord()));
095                            }
096                        }
097                    });
098                    jpUpper.setLayout(gridbag);
099                        c.gridx = 0;
100                        c.weightx = 0;
101                        c.weighty = 1;
102                        c.gridy = 0;
103                        c.fill = GridBagConstraints.BOTH;
104                      gridbag.setConstraints(jpTimeRange, c);
105                        c.gridx = 1;
106                        c.weightx = 1;
107                        c.weighty = 1;
108                        c.gridy = 0;
109                        c.fill = GridBagConstraints.BOTH;
110                      gridbag.setConstraints(jpTable, c);
111                        c.gridx = 2;
112                        c.weightx = 0;
113                        c.weighty = 1;
114                        c.gridy = 0;
115                        c.fill = GridBagConstraints.BOTH;
116                      gridbag.setConstraints(jpFilter, c);
117                    jpUpper.add(jpTimeRange);
118                        jpTimeRange.setLayout(new BoxLayout(jpTimeRange, BoxLayout.Y_AXIS));
119                        jpTimeRange.add(jpFromSelection);
120                            jpFromSelection.setBorder(ComponentFactory.createInsetBorder("von"));
121                            jpFromSelection.add(jpFromDropdown);
122                                jpFromDropdown.setLayout(new GridLayout(2,1));
123                                jpFromDropdown.add(jcbFromMonth);
124                                jpFromDropdown.add(jcbFromYear);
125                        jpTimeRange.add(jpToSelection);
126                            jpToSelection.setBorder(ComponentFactory.createInsetBorder("bis"));
127                            jpToSelection.add(jpToDropdown);
128                                jpToDropdown.setLayout(new GridLayout(2,1));
129                                jpToDropdown.add(jcbToMonth);
130                                jpToDropdown.add(jcbToYear);
131                    jpUpper.add(jpTable);
132                        jpTable.setLayout(new BoxLayout(jpTable, BoxLayout.X_AXIS));
133                        jpTable.add(fs.getComponent());
134                    jpUpper.add(jpFilter);
135                        jpFilter.add(jcbFilter);
136                    jpDescription.setLayout(new BoxLayout(jpDescription, BoxLayout.X_AXIS));
137                    jpDescription.add(jsc);
138                        jsc.setViewportView(da);
139    
140                    jpMain.setLayout(new BoxLayout(jpMain, BoxLayout.Y_AXIS));
141                    jpMain.add(jpUpper);
142                    jpMain.add(Box.createVerticalStrut(10));
143                    jpMain.add(jpDescription);
144    
145                    fs.setComponent(jpMain);
146                    fs.removeAllButtons();
147                    fs.addButton("Details", ButtonIDs.BTN_DETAIL, null);
148                }
149            });
150            return stfs;
151        }
152    
153        /**
154         * @return the class that controls and evaluates the {@link JComboBox JComboBoxes}.
155         */
156        public JCTimeRangeBoxes getTimeRangeBoxes() {
157            return jctrb;
158        }
159    }
160    
161    
162    /**
163     * The {@link TableEntryDescriptor} used by {@link FSManagerArticleStatsMain}.
164     */
165    class TEDManagerArticleStatsMain extends AbstractTableEntryDescriptor {
166    
167        /**
168         * @return the number of the table's columns.
169         */
170        public int getColumnCount() {
171            return 2;
172        }
173    
174        /**
175         * @param nIndex the affected column.
176         * @return columns' names.
177         */
178        public String getColumnName(int nIndex) {
179            return (new String[]{ "Artikel", "Kategorie"}) [nIndex];
180        }
181    
182        /**
183         * @param nIndex the affected column.
184         * @return columns' classes. They indicate how column's values should be aligned.
185         */
186        public Class getColumnClass  (int nIndex) {
187            return String.class;
188        }
189    
190        /**
191         * @param oRecord the affected table record.
192         * @param nIndex the affected column.
193         * @return columns' values
194         */
195        public Object getValueAt(Object oRecord, int nIndex) {
196            CIArticle article = Conversions.recordToCIArticle(oRecord);
197            switch (nIndex) {
198                case 0: return article.getArticleName();
199                case 1: return article.getCategory();
200            }
201            return null;
202        }
203    
204        /**
205         * Determines if columns can be sorted by the user.
206         *
207         * @param nIndex the affected column.
208         * @return <ul><li>true: columns can be sorted</li>
209         *              <li>false: columns cannot be sorted</li></ul>
210         */
211        public boolean canSortByColumn(int nIndex) {
212            return true;
213        }
214    }
215