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