PageRenderTime 176ms CodeModel.GetById 70ms app.highlight 69ms RepoModel.GetById 32ms app.codeStats 0ms

/src/mpv5/ui/dialogs/subcomponents/ProductSelectDialog2.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 387 lines | 323 code | 39 blank | 25 comment | 18 complexity | b5ca2d73322904a753c91ad5cf0ce410 MD5 | raw file
  1/*
  2 * This file is part of YaBS.
  3 *
  4 *    YaBS is free software: you can redistribute it and/or modify
  5 *    it under the terms of the GNU General Public License as published by
  6 *    the Free Software Foundation, either version 3 of the License, or
  7 *    (at your option) any later version.
  8 *
  9 *    YaBS is distributed in the hope that it will be useful,
 10 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *    GNU General Public License for more details.
 13 *
 14 *    You should have received a copy of the GNU General Public License
 15 *    along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17package mpv5.ui.dialogs.subcomponents;
 18
 19
 20import java.awt.event.ActionEvent;
 21import java.awt.event.ActionListener;
 22import java.awt.event.ComponentAdapter;
 23import java.awt.event.ComponentEvent;
 24import java.awt.event.ItemEvent;
 25import java.awt.event.ItemListener;
 26import java.awt.event.KeyEvent;
 27import java.awt.event.KeyListener;
 28import java.util.Date;
 29import javax.swing.AbstractAction;
 30import javax.swing.InputMap;
 31import javax.swing.JComponent;
 32import javax.swing.JTable;
 33import javax.swing.JTextArea;
 34import javax.swing.JTextField;
 35import javax.swing.KeyStroke;
 36import mpv5.db.common.Context;
 37import mpv5.db.common.NodataFoundException;
 38import mpv5.db.common.QueryCriteria2;
 39import mpv5.db.common.QueryHandler;
 40import mpv5.db.common.QueryParameter;
 41import mpv5.db.common.ReturnValue;
 42import mpv5.db.objects.Product;
 43import mpv5.db.objects.SubItem;
 44import mpv5.db.objects.User;
 45import mpv5.logging.Log;
 46import mpv5.utils.date.vTimeframe;
 47import mpv5.utils.models.MPComboBoxModelItem;
 48import mpv5.utils.models.MPComboboxModel;
 49import mpv5.utils.models.MPTableModel;
 50
 51public class ProductSelectDialog2 extends javax.swing.JDialog  {
 52
 53    protected JTable table;
 54
 55    /** Creates new form ItemTextAreaDialog */
 56    public ProductSelectDialog2(java.awt.Frame parent, boolean modal, JTable table) {
 57        super(parent, modal);
 58        this.table = table;
 59        setResizable(false);
 60        setUndecorated(true);
 61        initComponents();   
 62        InputMap inputMap = ((JComponent)getRootPane()).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
 63        KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0);
 64        inputMap.put(esc, "ESCAPE");
 65        ((JComponent)getRootPane()).getActionMap().put(inputMap.get(esc),new AbstractAction(){
 66            public void actionPerformed(ActionEvent e) {
 67                ActionListener[] listeners = cancelButton.getActionListeners();
 68                ActionEvent actionEvent = new ActionEvent(cancelButton, ActionEvent.ACTION_PERFORMED, cancelButton.getActionCommand());
 69                for (int i = 0; i < listeners.length; i++) {
 70                    listeners[i].actionPerformed(actionEvent);
 71                }
 72            }
 73        });
 74
 75        getIDTextField().addActionListener(new ActionListener() {
 76            public void actionPerformed(ActionEvent e) {
 77                cnumberActionPerformed(e);
 78            }
 79        });
 80        getIDTextField().addComponentListener(new ComponentAdapter() {
 81            @Override
 82            public void componentShown(ComponentEvent e) {
 83                getIDTextField().selectAll();
 84                getIDTextField().requestFocusInWindow();
 85            }
 86        });
 87
 88        productCombobox.getComboBox().addItemListener(new ItemListener() {
 89            public void itemStateChanged(ItemEvent e) {
 90                productComboboxStateChanged(e);
 91            }
 92        });
 93
 94        familyselect.setContext(Context.getProductGroup());
 95        familyselect.setSearchEnabled(true);
 96        suppliername.setSearchEnabled(true);
 97        suppliername.setContext(Context.getSupplier());
 98        manufacturername.setSearchEnabled(true);
 99        manufacturername.setContext(Context.getManufacturer());
100        stype.setModel(Product.getTypes(), MPComboBoxModelItem.COMPARE_BY_ID, new java.util.Vector<Integer>());
101
102        cname.getTextField().addKeyListener(new KeyListener() {
103            public void keyTyped(KeyEvent e) {}
104            public void keyPressed(KeyEvent e) {
105            int key = e.getKeyCode();
106            if (key == KeyEvent.VK_ENTER) {
107              search();
108             }
109            }
110            public void keyReleased(KeyEvent e) {}
111        });
112    }
113
114    @Override
115    public void setVisible(boolean visible){
116        super.setVisible(visible);
117        getIDTextField().grabFocus();
118    }
119
120    public JTextField getIDTextField(){
121        return cnumber.getTextField();
122    }
123
124    private void cnumberActionPerformed(ActionEvent e){
125        try {
126            productCombobox.setSelectedItem(Integer.valueOf(cnumber.getText()));
127            Product p = (Product) Product.getObject(Context.getProduct(), Integer.valueOf(productCombobox.getSelectedItem().getId()));
128        } catch (Exception ex) {
129        }
130    }
131
132    private void productComboboxStateChanged(ItemEvent e){
133        try {
134            Product p = (Product) Product.getObject(Context.getProduct(), Integer.valueOf(productCombobox.getSelectedItem().getId()));
135            cnumber.setText(p.__getCnumber());
136            description.setText(p.__getDescription());
137        } catch (NodataFoundException ex) {
138        }
139    }
140
141
142    /** This method is called from within the constructor to
143     * initialize the form.
144     * WARNING: Do NOT modify this code. The content of this method is
145     * always regenerated by the Form Editor.
146     */
147    @SuppressWarnings("unchecked")
148    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
149    private void initComponents() {
150
151        okButton = new javax.swing.JButton();
152        cancelButton = new javax.swing.JButton();
153        cnumber = new mpv5.ui.beans.LabeledTextField();
154        productCombobox = new mpv5.ui.beans.LabeledCombobox();
155        cname = new mpv5.ui.beans.LabeledTextField();
156        stype = new mpv5.ui.beans.LabeledCombobox();
157        familyselect = new mpv5.ui.beans.LabeledCombobox();
158        suppliername = new mpv5.ui.beans.LabeledCombobox();
159        manufacturername = new mpv5.ui.beans.LabeledCombobox();
160        searchButton = new javax.swing.JButton();
161        resetButton = new javax.swing.JButton();
162        jScrollPane3 = new javax.swing.JScrollPane();
163        class NoTabTextArea extends JTextArea {
164            protected void processComponentKeyEvent( KeyEvent e ) {
165                if ( e.getID() == KeyEvent.KEY_PRESSED &&
166                    e.getKeyCode() == KeyEvent.VK_TAB ) {
167                    e.consume();
168                    if (e.isShiftDown()) {
169                        transferFocusBackward();
170                    } else {
171                        transferFocus();
172                    }
173                } else {
174                    super.processComponentKeyEvent( e );
175                }
176            }
177        }
178        description = new NoTabTextArea()
179        ;
180
181        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
182        setName("Form"); // NOI18N
183
184        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
185        okButton.setText(bundle.getString("okButton")); // NOI18N
186        okButton.setName("okButton"); // NOI18N
187        okButton.addActionListener(new java.awt.event.ActionListener() {
188            public void actionPerformed(java.awt.event.ActionEvent evt) {
189                okButtonActionPerformed(evt);
190            }
191        });
192
193        cancelButton.setText(bundle.getString("cancelButton")); // NOI18N
194        cancelButton.setActionCommand("CANCEL"); // NOI18N
195        cancelButton.setName("cancelButton"); // NOI18N
196
197        cnumber.set_Label(bundle.getString("ProductPanel.contactid.text")); // NOI18N
198        cnumber.setName("cnumber"); // NOI18N
199
200        productCombobox.set_Label(bundle.getString("ProductSelectDialog.labeledCombobox1._Label")); // NOI18N
201        productCombobox.setName("productCombobox"); // NOI18N
202        productCombobox.setSearchEnabled(false);
203        productCombobox.setSearchOnEnterEnabled(false);
204
205        cname.set_Label(bundle.getString("ProductPanel.cname._Label_1")); // NOI18N
206        cname.setName("cname"); // NOI18N
207
208        stype.set_Label(bundle.getString("ProductPanel.stype._Label")); // NOI18N
209        stype.setName("stype"); // NOI18N
210
211        familyselect.set_Label(bundle.getString("ProductPanel.familyselect._Label")); // NOI18N
212        familyselect.setName("familyselect"); // NOI18N
213        familyselect.setSearchOnEnterEnabled(false);
214
215        suppliername.set_Label(bundle.getString("ProductSelectDialog.suppliername._Label")); // NOI18N
216        suppliername.setName("suppliername"); // NOI18N
217
218        manufacturername.set_Label(bundle.getString("ProductSelectDialog.manufacturername._Label")); // NOI18N
219        manufacturername.setName("manufacturername"); // NOI18N
220
221        searchButton.setText(bundle.getString("searchButton")); // NOI18N
222        searchButton.setName("searchButton"); // NOI18N
223        searchButton.addActionListener(new java.awt.event.ActionListener() {
224            public void actionPerformed(java.awt.event.ActionEvent evt) {
225                searchButtonActionPerformed(evt);
226            }
227        });
228
229        resetButton.setText(bundle.getString("resetButton")); // NOI18N
230        resetButton.setName("resetButton"); // NOI18N
231        resetButton.addActionListener(new java.awt.event.ActionListener() {
232            public void actionPerformed(java.awt.event.ActionEvent evt) {
233                resetButtonActionPerformed(evt);
234            }
235        });
236
237        jScrollPane3.setName("jScrollPane3"); // NOI18N
238
239        description.setColumns(20);
240        description.setEditable(false);
241        description.setLineWrap(true);
242        description.setRows(5);
243        description.setWrapStyleWord(true);
244        description.setFocusTraversalPolicyProvider(true);
245        description.setHighlighter(null);
246        description.setName("description"); // NOI18N
247        jScrollPane3.setViewportView(description);
248
249        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
250        getContentPane().setLayout(layout);
251        layout.setHorizontalGroup(
252            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
253            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
254                .addContainerGap()
255                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
256                    .addComponent(productCombobox, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)
257                    .addGroup(layout.createSequentialGroup()
258                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
259                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
260                        .addComponent(cancelButton))
261                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
262                        .addComponent(cnumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
263                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
264                        .addComponent(stype, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE))
265                    .addComponent(familyselect, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)
266                    .addGroup(layout.createSequentialGroup()
267                        .addComponent(suppliername, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
268                        .addGap(18, 18, 18)
269                        .addComponent(manufacturername, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE))
270                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
271                        .addGap(82, 82, 82)
272                        .addComponent(searchButton)
273                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
274                        .addComponent(resetButton))
275                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)
276                    .addComponent(cname, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE))
277                .addGap(42, 42, 42))
278        );
279        layout.setVerticalGroup(
280            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
281            .addGroup(layout.createSequentialGroup()
282                .addGap(13, 13, 13)
283                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
284                    .addComponent(cnumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
285                    .addComponent(stype, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
286                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
287                .addComponent(cname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
288                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
289                .addComponent(familyselect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
290                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
291                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
292                    .addGroup(layout.createSequentialGroup()
293                        .addComponent(suppliername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
294                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
295                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
296                            .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
297                            .addComponent(resetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)))
298                    .addComponent(manufacturername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
299                .addGap(12, 12, 12)
300                .addComponent(productCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
301                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
302                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
303                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
304                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
305                    .addComponent(cancelButton)
306                    .addComponent(okButton))
307                .addContainerGap())
308        );
309
310        pack();
311    }// </editor-fold>//GEN-END:initComponents
312
313    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
314        int row = table.getSelectedRow();
315        if(row<0){
316            Log.Debug(this,"row is "+row);
317            return;
318        }
319        try {
320            MPComboBoxModelItem item = productCombobox.getSelectedItem();
321            if(item!=null){
322                SubItem s = new SubItem((Product) Product.getObject(Context.getProduct(), Integer.valueOf(item.getId())));
323                ((MPTableModel)table.getModel()).setRowAt(s.getRowData(row), row, 1);
324            }
325        } catch (Exception ex) {
326        }
327    }//GEN-LAST:event_okButtonActionPerformed
328
329    private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
330        cnumber.setText("");
331        suppliername.setSelectedItem(null);
332        manufacturername.setSelectedItem(null);
333        cname.setText("");
334    }//GEN-LAST:event_resetButtonActionPerformed
335
336    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
337        search();
338    }//GEN-LAST:event_searchButtonActionPerformed
339
340    // Variables declaration - do not modify//GEN-BEGIN:variables
341    public javax.swing.JButton cancelButton;
342    private mpv5.ui.beans.LabeledTextField cname;
343    private mpv5.ui.beans.LabeledTextField cnumber;
344    private javax.swing.JTextArea description;
345    private mpv5.ui.beans.LabeledCombobox familyselect;
346    private javax.swing.JScrollPane jScrollPane3;
347    private mpv5.ui.beans.LabeledCombobox manufacturername;
348    public javax.swing.JButton okButton;
349    private mpv5.ui.beans.LabeledCombobox productCombobox;
350    private javax.swing.JButton resetButton;
351    private javax.swing.JButton searchButton;
352    private mpv5.ui.beans.LabeledCombobox stype;
353    private mpv5.ui.beans.LabeledCombobox suppliername;
354    // End of variables declaration//GEN-END:variables
355
356    private void search() {
357        QueryCriteria2 qc = new QueryCriteria2();
358        String cnametext = cname.getText();
359        if(cnametext!=null && !cnametext.equals("")){
360            qc.and(new QueryParameter(Context.getProduct(), "cname",cnametext, QueryParameter.LIKE));
361        }
362        qc.and(new QueryParameter(Context.getProduct(), "inttype",Integer.parseInt(stype.getSelectedItem().getId()), QueryParameter.EQUALS));
363        MPComboBoxModelItem item = familyselect.getSelectedItem();
364        if(item!=null&& Integer.parseInt(item.getId())>0){
365            qc.and(new QueryParameter(Context.getProduct(), "productgroupsids",Integer.parseInt(item.getId()), QueryParameter.EQUALS));
366        }
367        item = suppliername.getSelectedItem();
368        if(item!=null && Integer.parseInt(item.getId())>0){
369            qc.and(new QueryParameter(Context.getProduct(), "suppliersids",Integer.parseInt(item.getId()), QueryParameter.EQUALS));
370        }
371        item = manufacturername.getSelectedItem();
372        if(item!=null&& Integer.parseInt(item.getId())>0){
373            qc.and(new QueryParameter(Context.getProduct(), "manufacturersids",Integer.parseInt(item.getId()), QueryParameter.EQUALS));
374        }
375        if(User.getCurrentUser().isGroupRestricted()){
376            qc.and(new QueryParameter(Context.getProduct(), "groupsids", User.getCurrentUser().__getGroupsids(), QueryParameter.EQUALS));
377        }
378        
379        try{
380            ReturnValue data = QueryHandler.instanceOf().clone(Context.getProduct()).select("ids, cname", qc, new vTimeframe(new Date(0), new Date()));
381            productCombobox.setModel(new MPComboboxModel(MPComboBoxModelItem.toItems(data.getData())));
382        }catch(Exception e){
383            Log.Debug(this,e.getMessage());
384        }
385    }
386
387}