PageRenderTime 242ms CodeModel.GetById 96ms app.highlight 88ms RepoModel.GetById 25ms app.codeStats 0ms

/src/mpv5/ui/panels/ProductsOverview.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 368 lines | 295 code | 55 blank | 18 comment | 19 complexity | ab214971ff448160b6a0dae251d82e9e MD5 | raw file
  1
  2/*
  3 * ProductsOverview.java
  4 *
  5 * Created on Aug 27, 2010, 1:48:39 PM
  6 */
  7package mpv5.ui.panels;
  8
  9import java.awt.event.ActionEvent;
 10import java.awt.event.MouseEvent;
 11import java.util.ArrayList;
 12import java.util.Date;
 13import java.util.List;
 14import javax.swing.AbstractAction;
 15import mpv5.ui.misc.MPTable;
 16import javax.swing.SwingUtilities;
 17import javax.swing.tree.DefaultMutableTreeNode;
 18import mpv5.db.common.Context;
 19import mpv5.db.common.DatabaseObject;
 20import mpv5.db.common.NodataFoundException;
 21import mpv5.db.common.QueryCriteria2;
 22import mpv5.db.common.QueryHandler;
 23import mpv5.db.common.QueryParameter;
 24import mpv5.db.objects.Product;
 25import mpv5.db.objects.ProductGroup;
 26import mpv5.globals.Messages;
 27import mpv5.logging.Log;
 28import mpv5.ui.dialogs.BigPopup;
 29import mpv5.ui.misc.DragNDropTreeForGroups;
 30import mpv5.ui.misc.DragTableHandlerForDBOs;
 31
 32import mpv5.utils.date.DateConverter;
 33import mpv5.utils.date.vTimeframe;
 34import mpv5.utils.models.MPTableModel;
 35import mpv5.utils.tables.TableFormat;
 36import mpv5.utils.trees.TreeFormat;
 37import mpv5.ui.misc.TableViewPersistenceHandler;
 38
 39/**
 40 *
 41 * @author andreas.weber
 42 */
 43public class ProductsOverview extends javax.swing.JPanel implements ListPanel {
 44
 45    /** Creates new form ProductsOverview */
 46    public ProductsOverview() {
 47        initComponents();
 48        gtree.setLargeModel(true);
 49
 50        ((DragNDropTreeForGroups) gtree).setContainerToNotify(this);
 51        DragTableHandlerForDBOs t = new DragTableHandlerForDBOs(listtable, Context.getProduct());
 52        setName("productsoverview");
 53        both.setSelected(true);
 54        addedafter.setDate(DateConverter.addYears(new Date(), -10));
 55        fillTree();
 56        filltable(null, null);
 57        search.getTextField().addActionListener(new AbstractAction() {
 58
 59            public void actionPerformed(ActionEvent e) {
 60                search();
 61            }
 62        });
 63        ((MPTable) listtable).setPersistanceHandler(new TableViewPersistenceHandler((MPTable) listtable, this));
 64    }
 65
 66    private void search() {
 67        List<ProductGroup> gs = new ArrayList<ProductGroup>();
 68        DefaultMutableTreeNode node = (DefaultMutableTreeNode) gtree.getLastSelectedPathComponent();
 69        if (node != null && !node.isRoot()) {
 70            ProductGroup g = (ProductGroup) node.getUserObject();
 71            gs.add(g);
 72        }
 73
 74        filltable(search.getText(), gs);
 75    }
 76
 77    /** This method is called from within the constructor to
 78     * initialize the form.
 79     * WARNING: Do NOT modify this code. The content of this method is
 80     * always regenerated by the Form Editor.
 81     */
 82    @SuppressWarnings("unchecked")
 83    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 84    private void initComponents() {
 85
 86        buttonGroup1 = new javax.swing.ButtonGroup();
 87        jSplitPane1 = new javax.swing.JSplitPane();
 88        jScrollPane1 = new javax.swing.JScrollPane();
 89        gtree =  new DragNDropTreeForGroups(1);
 90        jPanel1 = new javax.swing.JPanel();
 91        jScrollPane2 = new javax.swing.JScrollPane();
 92        listtable = new MPTable(this);
 93        jToolBar1 = new javax.swing.JToolBar();
 94        products = new javax.swing.JRadioButton();
 95        services = new javax.swing.JRadioButton();
 96        both = new javax.swing.JRadioButton();
 97        jSeparator2 = new javax.swing.JToolBar.Separator();
 98        addedafter = new mpv5.ui.beans.LabeledDateChooser();
 99        jSeparator1 = new javax.swing.JToolBar.Separator();
100        search = new mpv5.ui.beans.LabeledTextField();
101        jButton1 = new javax.swing.JButton();
102
103        setName("Form"); // NOI18N
104        setLayout(new java.awt.BorderLayout());
105
106        jSplitPane1.setName("jSplitPane1"); // NOI18N
107
108        jScrollPane1.setName("jScrollPane1"); // NOI18N
109
110        gtree.setLargeModel(true);
111        gtree.setName("gtree"); // NOI18N
112        gtree.setPreferredSize(new java.awt.Dimension(250, 64));
113        gtree.addMouseListener(new java.awt.event.MouseAdapter() {
114            public void mouseClicked(java.awt.event.MouseEvent evt) {
115                gtreeMouseClicked(evt);
116            }
117        });
118        jScrollPane1.setViewportView(gtree);
119
120        jSplitPane1.setLeftComponent(jScrollPane1);
121
122        jPanel1.setName("jPanel1"); // NOI18N
123        jPanel1.setLayout(new java.awt.BorderLayout());
124
125        jScrollPane2.setName("jScrollPane2"); // NOI18N
126
127        listtable.setAutoCreateRowSorter(true);
128        listtable.setModel(new javax.swing.table.DefaultTableModel(
129            new Object [][] {
130
131            },
132            new String [] {
133
134            }
135        ));
136        listtable.setDragEnabled(true);
137        listtable.setName("listtable"); // NOI18N
138        listtable.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
139        listtable.addMouseListener(new java.awt.event.MouseAdapter() {
140            public void mouseClicked(java.awt.event.MouseEvent evt) {
141                listtableMouseClicked(evt);
142            }
143        });
144        jScrollPane2.setViewportView(listtable);
145
146        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);
147
148        jToolBar1.setFloatable(false);
149        jToolBar1.setRollover(true);
150        jToolBar1.setName("jToolBar1"); // NOI18N
151
152        buttonGroup1.add(products);
153        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
154        products.setText(bundle.getString("ProductsOverview.products.text")); // NOI18N
155        products.setFocusable(false);
156        products.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
157        products.setMaximumSize(new java.awt.Dimension(333, 20));
158        products.setMinimumSize(new java.awt.Dimension(100, 20));
159        products.setName("products"); // NOI18N
160        products.setPreferredSize(new java.awt.Dimension(100, 20));
161        products.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
162        jToolBar1.add(products);
163
164        buttonGroup1.add(services);
165        services.setText(bundle.getString("ProductsOverview.services.text")); // NOI18N
166        services.setFocusable(false);
167        services.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
168        services.setMaximumSize(new java.awt.Dimension(333, 20));
169        services.setMinimumSize(new java.awt.Dimension(100, 20));
170        services.setName("services"); // NOI18N
171        services.setPreferredSize(new java.awt.Dimension(100, 20));
172        services.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
173        jToolBar1.add(services);
174
175        buttonGroup1.add(both);
176        both.setSelected(true);
177        both.setText(bundle.getString("ProductsOverview.both.text")); // NOI18N
178        both.setFocusable(false);
179        both.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
180        both.setMaximumSize(new java.awt.Dimension(333, 20));
181        both.setMinimumSize(new java.awt.Dimension(100, 20));
182        both.setName("both"); // NOI18N
183        both.setPreferredSize(new java.awt.Dimension(100, 20));
184        both.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
185        jToolBar1.add(both);
186
187        jSeparator2.setMaximumSize(new java.awt.Dimension(10, 0));
188        jSeparator2.setMinimumSize(new java.awt.Dimension(10, 0));
189        jSeparator2.setName("jSeparator2"); // NOI18N
190        jSeparator2.setPreferredSize(new java.awt.Dimension(10, 0));
191        jToolBar1.add(jSeparator2);
192
193        addedafter.set_Label(bundle.getString("ProductsOverview.addedafter._Label")); // NOI18N
194        addedafter.setMaximumSize(new java.awt.Dimension(226, 21));
195        addedafter.setMinimumSize(new java.awt.Dimension(226, 21));
196        addedafter.setName("addedafter"); // NOI18N
197        jToolBar1.add(addedafter);
198
199        jSeparator1.setMaximumSize(new java.awt.Dimension(10, 0));
200        jSeparator1.setMinimumSize(new java.awt.Dimension(10, 0));
201        jSeparator1.setName("jSeparator1"); // NOI18N
202        jSeparator1.setPreferredSize(new java.awt.Dimension(10, 0));
203        jToolBar1.add(jSeparator1);
204
205        search.set_Label(bundle.getString("ProductsOverview.search._Label")); // NOI18N
206        search.setName("search"); // NOI18N
207        jToolBar1.add(search);
208
209        jButton1.setText(bundle.getString("ProductsOverview.jButton1.text")); // NOI18N
210        jButton1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
211        jButton1.setFocusable(false);
212        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
213        jButton1.setName("jButton1"); // NOI18N
214        jButton1.setPreferredSize(new java.awt.Dimension(55, 19));
215        jButton1.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/16/kalarm.png"))); // NOI18N
216        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
217        jButton1.addActionListener(new java.awt.event.ActionListener() {
218            public void actionPerformed(java.awt.event.ActionEvent evt) {
219                jButton1ActionPerformed(evt);
220            }
221        });
222        jToolBar1.add(jButton1);
223
224        jPanel1.add(jToolBar1, java.awt.BorderLayout.PAGE_START);
225
226        jSplitPane1.setRightComponent(jPanel1);
227
228        add(jSplitPane1, java.awt.BorderLayout.CENTER);
229    }// </editor-fold>//GEN-END:initComponents
230
231    private void listtableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listtableMouseClicked
232        doPopup(evt);
233    }//GEN-LAST:event_listtableMouseClicked
234
235    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
236        search();
237    }//GEN-LAST:event_jButton1ActionPerformed
238
239    private void gtreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gtreeMouseClicked
240        search();
241    }//GEN-LAST:event_gtreeMouseClicked
242    // Variables declaration - do not modify//GEN-BEGIN:variables
243    private mpv5.ui.beans.LabeledDateChooser addedafter;
244    private javax.swing.JRadioButton both;
245    private javax.swing.ButtonGroup buttonGroup1;
246    private javax.swing.JTree gtree;
247    private javax.swing.JButton jButton1;
248    private javax.swing.JPanel jPanel1;
249    private javax.swing.JScrollPane jScrollPane1;
250    private javax.swing.JScrollPane jScrollPane2;
251    private javax.swing.JToolBar.Separator jSeparator1;
252    private javax.swing.JToolBar.Separator jSeparator2;
253    private javax.swing.JSplitPane jSplitPane1;
254    private javax.swing.JToolBar jToolBar1;
255    private javax.swing.JTable listtable;
256    private javax.swing.JRadioButton products;
257    private mpv5.ui.beans.LabeledTextField search;
258    private javax.swing.JRadioButton services;
259    // End of variables declaration//GEN-END:variables
260
261    private void fillTree() {
262        ArrayList<ProductGroup> data = null;
263        try {
264            data = DatabaseObject.getObjects(Context.getProductGroup());
265        } catch (NodataFoundException ex) {
266            Log.Debug(this, ex.getMessage());
267        }
268
269        ProductGroup g;
270        try {
271            g = (ProductGroup) DatabaseObject.getObject(Context.getProductGroup(), 1);
272        } catch (NodataFoundException ex) {
273            g = new ProductGroup(Messages.GROUPNAMES.toString());
274            g.setIDS(-1);
275        }
276
277        gtree.setModel(ProductGroup.toTreeModel(data, g));
278        Runnable runnable = new Runnable() {
279
280            @Override
281            public void run() {
282                TreeFormat.expandTree(gtree);
283            }
284        };
285        SwingUtilities.invokeLater(runnable);
286    }
287
288    private void doPopup(MouseEvent evt) {
289
290        if (evt.isPopupTrigger()) {
291        } else {
292            if (evt.getClickCount() > 1) {
293                try {
294                    Product p = (Product) DatabaseObject.getObject(Context.getProduct(), Integer.valueOf(listtable.getModel().getValueAt(listtable.getSelectedRow(), 0).toString()));
295                    ProductPanel pan = new ProductPanel(p);
296                    BigPopup.showPopup(this, pan, p.__getCname(), true);
297                } catch (NodataFoundException ex) {
298                }
299            }
300
301        }
302    }
303
304    private void filltable(String search, List<ProductGroup> groups) {
305
306        QueryCriteria2 qc = new QueryCriteria2();
307
308        if (search != null) {
309            List<List<QueryParameter>> ps = new ArrayList<List<QueryParameter>>();
310            List<QueryParameter> not = new ArrayList<QueryParameter>();
311            String[] strings = search.split("\\s+");
312            for (int i = 0; i < strings.length; i++) {
313                ArrayList<QueryParameter> psx = new ArrayList<QueryParameter>();
314                ps.add(psx);
315                String string = strings[i];
316                for (String en : new Product().getStringVars()) {
317                    if (!string.startsWith("-")) {
318                        psx.add(new QueryParameter(Context.getProduct(), en, string, QueryParameter.LIKE));
319                    } else {
320                        not.add(new QueryParameter(Context.getProduct(), en, string.substring(1), QueryParameter.NOTLIKE));
321                    }
322                }
323            }
324            for (int i = 0; i < ps.size(); i++) {
325                qc.or(ps.get(i));
326            }
327
328            qc.and(not);
329        }
330
331        if (!both.isSelected()) {
332            int type = Product.TYPE_PRODUCT;
333            if (services.isSelected()) {
334                type = Product.TYPE_SERVICE;
335            }
336            qc.and(new QueryParameter(Context.getProduct(), "inttype", type, QueryParameter.EQUALS));
337        }
338
339        if (groups != null && !groups.isEmpty()) {
340            List<QueryParameter> ps = new ArrayList<QueryParameter>();
341            for (int i = 0; i < groups.size(); i++) {
342                ProductGroup group = groups.get(i);
343                ps.add(new QueryParameter(Context.getProduct(), "productgroupsids", group.__getIDS(), QueryParameter.EQUALS));
344            }
345            qc.or(ps);
346        }
347
348        Context c = Context.getProduct();
349        c.addReference(Context.getContact().getDbIdentity(), "ids", "suppliersids");
350        c.addReference(Context.getGroup());
351
352        try {
353            listtable.setModel(new MPTableModel(QueryHandler.instanceOf().clone(c).select(Context.DETAILS_PRODUCTS, qc, new vTimeframe(this.addedafter.getDate(), new Date())).getData()));
354        } catch (NodataFoundException ex) {
355            listtable.setModel(new MPTableModel());
356        }
357
358        TableFormat.hideHeader(listtable);
359    }
360
361    public void refresh() {
362        search();
363    }
364
365    public void flush() {
366        search();
367    }
368}