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