PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/ui/dialogs/ListView.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 317 lines | 247 code | 34 blank | 36 comment | 17 complexity | ef1edb51b03d3d285037f84ac05db95a 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. /*
  18. * ListView.java
  19. *
  20. * Created on 08.11.2009, 20:13:52
  21. */
  22. package mpv5.ui.dialogs;
  23. import java.awt.Cursor;
  24. import java.util.ArrayList;
  25. import java.util.Vector;
  26. import javax.swing.ListModel;
  27. import javax.swing.SwingUtilities;
  28. import mpv5.data.MPList;
  29. import mpv5.db.common.Context;
  30. import mpv5.db.common.DatabaseObject;
  31. import mpv5.db.common.NodataFoundException;
  32. import mpv5.globals.Messages;
  33. import mpv5.logging.Log;
  34. import mpv5.ui.frames.MPView;
  35. import mpv5.ui.panels.DataPanel;
  36. import mpv5.usermanagement.MPSecurityManager;
  37. import mpv5.utils.date.DateConverter;
  38. import mpv5.utils.xml.XMLReader;
  39. import mpv5.utils.xml.XMLWriter;
  40. /**
  41. *
  42. * @author andreasw
  43. */
  44. public class ListView extends javax.swing.JPanel {
  45. private final MPList list;
  46. public void addElement(DatabaseObject obj) {
  47. list.add(obj);
  48. validate();
  49. }
  50. /** Creates new form ListView
  51. * @param list
  52. */
  53. public ListView(MPList list) {
  54. initComponents();
  55. jList1.setCellRenderer(MPList.getDBORenderer());
  56. this.list = list;
  57. validate();
  58. }
  59. @Override
  60. public void validate() {
  61. jList1.setModel(list.getListModel());
  62. Log.Debug(this, "Having content: " + list.size());
  63. super.validate();
  64. }
  65. /** This method is called from within the constructor to
  66. * initialize the form.
  67. * WARNING: Do NOT modify this code. The content of this method is
  68. * always regenerated by the Form Editor.
  69. */
  70. @SuppressWarnings("unchecked")
  71. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  72. private void initComponents() {
  73. jScrollPane1 = new javax.swing.JScrollPane();
  74. jList1 = new javax.swing.JList();
  75. jToolBar1 = new javax.swing.JToolBar();
  76. jButton1 = new javax.swing.JButton();
  77. jButton2 = new javax.swing.JButton();
  78. jButton3 = new javax.swing.JButton();
  79. jButton6 = new javax.swing.JButton();
  80. jButton4 = new javax.swing.JButton();
  81. jButton5 = new javax.swing.JButton();
  82. setName("Form"); // NOI18N
  83. jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  84. jScrollPane1.setName("jScrollPane1"); // NOI18N
  85. jList1.setName("jList1"); // NOI18N
  86. jList1.addMouseListener(new java.awt.event.MouseAdapter() {
  87. public void mouseClicked(java.awt.event.MouseEvent evt) {
  88. jList1MouseClicked(evt);
  89. }
  90. });
  91. jScrollPane1.setViewportView(jList1);
  92. jToolBar1.setFloatable(false);
  93. jToolBar1.setRollover(true);
  94. jToolBar1.setName("jToolBar1"); // NOI18N
  95. java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
  96. jButton1.setText(bundle.getString("ListView.jButton1.text_1")); // NOI18N
  97. jButton1.setBorder(javax.swing.BorderFactory.createEmptyBorder(3, 3, 3, 3));
  98. jButton1.setName("jButton1"); // NOI18N
  99. jButton1.addActionListener(new java.awt.event.ActionListener() {
  100. public void actionPerformed(java.awt.event.ActionEvent evt) {
  101. jButton1ActionPerformed(evt);
  102. }
  103. });
  104. jToolBar1.add(jButton1);
  105. jButton2.setText(bundle.getString("ListView.jButton2.text_1")); // NOI18N
  106. jButton2.setBorder(javax.swing.BorderFactory.createEmptyBorder(3, 3, 3, 3));
  107. jButton2.setName("jButton2"); // NOI18N
  108. jButton2.addActionListener(new java.awt.event.ActionListener() {
  109. public void actionPerformed(java.awt.event.ActionEvent evt) {
  110. jButton2ActionPerformed(evt);
  111. }
  112. });
  113. jToolBar1.add(jButton2);
  114. jButton3.setText(bundle.getString("ListView.jButton3.text_1")); // NOI18N
  115. jButton3.setBorder(javax.swing.BorderFactory.createEmptyBorder(3, 3, 3, 3));
  116. jButton3.setName("jButton3"); // NOI18N
  117. jButton3.addActionListener(new java.awt.event.ActionListener() {
  118. public void actionPerformed(java.awt.event.ActionEvent evt) {
  119. jButton3ActionPerformed(evt);
  120. }
  121. });
  122. jToolBar1.add(jButton3);
  123. jButton6.setText(bundle.getString("ListView.jButton6.text")); // NOI18N
  124. jButton6.setBorder(javax.swing.BorderFactory.createEmptyBorder(3, 3, 3, 3));
  125. jButton6.setFocusable(false);
  126. jButton6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  127. jButton6.setName("jButton6"); // NOI18N
  128. jButton6.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  129. jButton6.addActionListener(new java.awt.event.ActionListener() {
  130. public void actionPerformed(java.awt.event.ActionEvent evt) {
  131. jButton6ActionPerformed(evt);
  132. }
  133. });
  134. jToolBar1.add(jButton6);
  135. jButton4.setText(bundle.getString("ListView.jButton4.text")); // NOI18N
  136. jButton4.setBorder(javax.swing.BorderFactory.createEmptyBorder(3, 3, 3, 3));
  137. jButton4.setFocusable(false);
  138. jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  139. jButton4.setName("jButton4"); // NOI18N
  140. jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  141. jButton4.addActionListener(new java.awt.event.ActionListener() {
  142. public void actionPerformed(java.awt.event.ActionEvent evt) {
  143. jButton4ActionPerformed(evt);
  144. }
  145. });
  146. jToolBar1.add(jButton4);
  147. jButton5.setText(bundle.getString("ListView.jButton5.text")); // NOI18N
  148. jButton5.setBorder(javax.swing.BorderFactory.createEmptyBorder(3, 3, 3, 3));
  149. jButton5.setFocusable(false);
  150. jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  151. jButton5.setName("jButton5"); // NOI18N
  152. jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  153. jButton5.addActionListener(new java.awt.event.ActionListener() {
  154. public void actionPerformed(java.awt.event.ActionEvent evt) {
  155. jButton5ActionPerformed(evt);
  156. }
  157. });
  158. jToolBar1.add(jButton5);
  159. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  160. this.setLayout(layout);
  161. layout.setHorizontalGroup(
  162. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  163. .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
  164. .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
  165. );
  166. layout.setVerticalGroup(
  167. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  168. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  169. .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
  170. .addGap(0, 0, 0)
  171. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
  172. );
  173. }// </editor-fold>//GEN-END:initComponents
  174. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
  175. try {
  176. list.remove((DatabaseObject) jList1.getSelectedValue());
  177. validate();
  178. } catch (Exception e) {
  179. Log.Debug(this, e);
  180. }
  181. }//GEN-LAST:event_jButton2ActionPerformed
  182. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
  183. list.clear();
  184. validate();
  185. }//GEN-LAST:event_jButton1ActionPerformed
  186. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
  187. DataPanel view = mpv5.YabsViewProxy.instance().getIdentifierView().getCurrentTab();
  188. if (view != null) {
  189. view.paste(list.toArray());
  190. try {
  191. BigPopup.hide(this);
  192. } catch (Exception exception) {
  193. }
  194. }
  195. }//GEN-LAST:event_jButton3ActionPerformed
  196. private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
  197. if (evt.getClickCount() > 1) {
  198. try {
  199. if (jList1.getSelectedValue() != null) {
  200. mpv5.YabsViewProxy.instance().getIdentifierView().addTab((DatabaseObject) jList1.getSelectedValue());
  201. }
  202. } catch (Exception e) {
  203. Log.Debug(e);
  204. }
  205. }
  206. }//GEN-LAST:event_jList1MouseClicked
  207. private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
  208. ListModel data;
  209. if (mpv5.usermanagement.MPSecurityManager.check(Context.getContact(), MPSecurityManager.EXPORT)
  210. && mpv5.usermanagement.MPSecurityManager.check(Context.getItem(), MPSecurityManager.EXPORT)
  211. && mpv5.usermanagement.MPSecurityManager.check(Context.getProduct(), MPSecurityManager.EXPORT)) {
  212. XMLWriter xmlw = new XMLWriter();
  213. xmlw.newDoc(true);
  214. String name = Messages.ACTION_EXPORT.toString() + "-" + DateConverter.getTodayDefDate();
  215. data = jList1.getModel();
  216. ArrayList<Context> exportable = Context.getImportableContexts();
  217. for (int i = 0; i < exportable.size(); i++) {
  218. Context context = exportable.get(i);
  219. ArrayList<DatabaseObject> d = new ArrayList<DatabaseObject>();
  220. for (int j = 0; j < data.getSize(); j++) {
  221. if (((DatabaseObject) data.getElementAt(j)).getContext().equals(context)) {
  222. d.add((DatabaseObject) data.getElementAt(j));
  223. }
  224. }
  225. xmlw.add(d);
  226. }
  227. mpv5.YabsViewProxy.instance().showFilesaveDialogFor(xmlw.createFile(name));
  228. }
  229. }//GEN-LAST:event_jButton4ActionPerformed
  230. private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
  231. final ListView v = this;
  232. Runnable runnable = new Runnable() {
  233. public void run() {
  234. try {
  235. v.setCursor(new Cursor(Cursor.WAIT_CURSOR));
  236. mpv5.YabsViewProxy.instance().setWaiting(true);
  237. DialogForFile d = new DialogForFile(DialogForFile.FILES_ONLY);
  238. d.setFileFilter(DialogForFile.XML_FILES);
  239. XMLReader x;
  240. ArrayList<ArrayList<DatabaseObject>> objs = null;
  241. if (d.chooseFile()) {
  242. x = new XMLReader();
  243. x.newDoc(d.getFile(), false);
  244. x.setOverwriteExisting(true);
  245. objs = x.getObjects();
  246. }
  247. if (objs != null && objs.size() > 0) {
  248. for (int i = 0; i < objs.size(); i++) {
  249. ArrayList<DatabaseObject> arrayList = objs.get(i);
  250. for (int j = 0; j < arrayList.size(); j++) {
  251. DatabaseObject databaseObject = arrayList.get(j);
  252. Log.Debug(this, "Parsing " + databaseObject.getDbIdentity() + " : " + databaseObject.__getCname() + " from file: " + d.getFile());
  253. addElement(databaseObject);
  254. }
  255. }
  256. }
  257. } catch (Exception e) {
  258. Popup.error(e);
  259. Log.Debug(e);
  260. } finally {
  261. v.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  262. mpv5.YabsViewProxy.instance().setWaiting(false);
  263. }
  264. }
  265. };
  266. SwingUtilities.invokeLater(runnable);
  267. }//GEN-LAST:event_jButton5ActionPerformed
  268. private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
  269. jButton3ActionPerformed(evt);
  270. jButton1ActionPerformed(evt);
  271. }//GEN-LAST:event_jButton6ActionPerformed
  272. // Variables declaration - do not modify//GEN-BEGIN:variables
  273. private javax.swing.JButton jButton1;
  274. private javax.swing.JButton jButton2;
  275. private javax.swing.JButton jButton3;
  276. private javax.swing.JButton jButton4;
  277. private javax.swing.JButton jButton5;
  278. private javax.swing.JButton jButton6;
  279. private javax.swing.JList jList1;
  280. private javax.swing.JScrollPane jScrollPane1;
  281. private javax.swing.JToolBar jToolBar1;
  282. // End of variables declaration//GEN-END:variables
  283. }