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