PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 207 lines | 169 code | 28 blank | 10 comment | 6 complexity | c553ee192d274d28256a39b38df28b62 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. package mpv5.ui.dialogs.subcomponents;
  2. import java.awt.Cursor;
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.concurrent.ExecutionException;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import javax.swing.SwingUtilities;
  9. import javax.swing.SwingWorker;
  10. import javax.swing.text.TableView.TableRow;
  11. import mpv5.db.common.DatabaseObject;
  12. import mpv5.globals.Messages;
  13. import mpv5.logging.Log;
  14. import mpv5.ui.dialogs.Popup;
  15. import mpv5.ui.dialogs.WizardMaster;
  16. import mpv5.ui.dialogs.Wizardable;
  17. import mpv5.utils.models.ImportTableModel;
  18. import mpv5.utils.tables.TableFormat;
  19. import mpv5.utils.xml.XMLReader;
  20. /**
  21. *
  22. *
  23. */
  24. public class wizard_XMLImport_2 extends javax.swing.JPanel implements Wizardable {
  25. private static final long serialVersionUID = -8347532498124147821L;
  26. private WizardMaster master;
  27. private boolean isConsumed = false;
  28. public wizard_XMLImport_2(WizardMaster w) {
  29. this.master = w;
  30. initComponents();
  31. }
  32. private void importXML() {
  33. master.setCursor(new Cursor(Cursor.WAIT_CURSOR));
  34. isConsumed = true;
  35. XMLReader x;
  36. ArrayList<ArrayList<DatabaseObject>> objs = null;
  37. if (master.getStore().hasProperty("file")) {
  38. x = new XMLReader();
  39. x.setOverwriteExisting(true);
  40. try {
  41. x.newDoc(new File(master.getStore().getProperty("file")), false);
  42. objs = x.getObjects();
  43. jTable1.setModel(ImportTableModel.getModel(objs, !master.getStore().getProperty("overwrite", false)));
  44. jLabel2.setText(jLabel2.getText() + " " + master.getStore().getProperty("file") + " (" + jTable1.getRowCount() + ")");
  45. TableFormat.format(jTable1, 0, 0);
  46. TableFormat.format(jTable1, 1, 33);
  47. TableFormat.format(jTable1, 2, 100);
  48. } catch (Exception ex) {
  49. Popup.error(ex);
  50. Log.Debug(ex);
  51. } finally {
  52. master.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  53. }
  54. }
  55. }
  56. private void imports() {
  57. jProgressBar1.setMinimum(0);
  58. jProgressBar1.setMaximum(jTable1.getRowCount());
  59. Runnable runnable = new Runnable() {
  60. public void run() {
  61. int imp = 0;
  62. for (int i = 0; i < jTable1.getRowCount(); i++) {
  63. if ((Boolean) jTable1.getValueAt(i, 1)) {
  64. master.setCursor(new Cursor(Cursor.WAIT_CURSOR));
  65. final int p = i;
  66. String val = "";
  67. try {
  68. DatabaseObject dog = ((DatabaseObject) jTable1.getValueAt(p, 0));
  69. if (!dog.saveImport()) {
  70. val = "<html><p><font color =red>" + Messages.ERROR_OCCURED;
  71. } else {
  72. val = "<html><p><font color =green>" + Messages.IMPORTED + " ID: " + dog.__getIDS();
  73. imp++;
  74. }
  75. } catch (Exception e) {
  76. val = "<html><p><font color =red>" + Messages.ERROR_OCCURED + ": " + e.getMessage();
  77. } finally {
  78. jTable1.setValueAt(val, p, 4);
  79. jProgressBar1.setValue(p);
  80. master.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  81. }
  82. }
  83. }
  84. jProgressBar1.setString( imp + " " + Messages.IMPORTED);
  85. }
  86. }; new Thread(runnable).start();
  87. master.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  88. master.isEnd(true);
  89. }
  90. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  91. private void initComponents() {
  92. jPanel1 = new javax.swing.JPanel();
  93. jLabel2 = new javax.swing.JLabel();
  94. jScrollPane1 = new javax.swing.JScrollPane();
  95. jTable1 = new javax.swing.JTable();
  96. jProgressBar1 = new javax.swing.JProgressBar();
  97. setBackground(new java.awt.Color(255, 255, 255));
  98. setName("Form"); // NOI18N
  99. setLayout(new java.awt.BorderLayout());
  100. jPanel1.setBackground(new java.awt.Color(255, 255, 255));
  101. java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
  102. jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("wizard_XMLImport_2.jPanel1.border.title"))); // NOI18N
  103. jPanel1.setName("jPanel1"); // NOI18N
  104. jLabel2.setText(bundle.getString("wizard_XMLImport_2.jLabel2.text")); // NOI18N
  105. jLabel2.setName("jLabel2"); // NOI18N
  106. jScrollPane1.setName("jScrollPane1"); // NOI18N
  107. jTable1.setAutoCreateRowSorter(true);
  108. jTable1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
  109. jTable1.setModel(new javax.swing.table.DefaultTableModel(
  110. new Object [][] {
  111. {null}
  112. },
  113. new String [] {
  114. "Title 1"
  115. }
  116. ));
  117. jTable1.setCellSelectionEnabled(true);
  118. jTable1.setDoubleBuffered(true);
  119. jTable1.setFillsViewportHeight(true);
  120. jTable1.setName("jTable1"); // NOI18N
  121. jScrollPane1.setViewportView(jTable1);
  122. jProgressBar1.setName("jProgressBar1"); // NOI18N
  123. jProgressBar1.setStringPainted(true);
  124. javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  125. jPanel1.setLayout(jPanel1Layout);
  126. jPanel1Layout.setHorizontalGroup(
  127. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  128. .addGroup(jPanel1Layout.createSequentialGroup()
  129. .addContainerGap()
  130. .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
  131. .addContainerGap())
  132. .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
  133. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  134. .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE))
  135. );
  136. jPanel1Layout.setVerticalGroup(
  137. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  138. .addGroup(jPanel1Layout.createSequentialGroup()
  139. .addComponent(jLabel2)
  140. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 343, Short.MAX_VALUE)
  141. .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  142. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  143. .addGroup(jPanel1Layout.createSequentialGroup()
  144. .addGap(20, 20, 20)
  145. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
  146. .addGap(21, 21, 21)))
  147. );
  148. add(jPanel1, java.awt.BorderLayout.CENTER);
  149. }// </editor-fold>//GEN-END:initComponents
  150. // Variables declaration - do not modify//GEN-BEGIN:variables
  151. private javax.swing.JLabel jLabel2;
  152. private javax.swing.JPanel jPanel1;
  153. private javax.swing.JProgressBar jProgressBar1;
  154. private javax.swing.JScrollPane jScrollPane1;
  155. private javax.swing.JTable jTable1;
  156. // End of variables declaration//GEN-END:variables
  157. public boolean next() {
  158. try {
  159. imports();
  160. } catch (Exception e) {
  161. Log.Debug(e);
  162. master.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  163. }
  164. return false;
  165. }
  166. public boolean back() {
  167. return true;
  168. }
  169. /**
  170. *
  171. */
  172. public void load() {
  173. master.enableBackButton(false);
  174. if (!isConsumed) {
  175. Log.Debug(this, "Overwrite is set to " + master.getStore().getProperty("overwrite", false));
  176. importXML();
  177. }
  178. }
  179. }