PageRenderTime 39ms CodeModel.GetById 10ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 1ms

/src/mpv5/ui/panels/GeneralListPanel.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 342 lines | 237 code | 37 blank | 68 comment | 14 complexity | e88e1d78f6ab1be8099b39077278aef8 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 * GeneralListPanel.java
 19 *
 20 * Created on 03.04.2009, 15:26:37
 21 */
 22package mpv5.ui.panels;
 23
 24import java.awt.Color;
 25import java.awt.Component;
 26import java.util.Date;
 27import java.util.HashMap;
 28import java.util.Iterator;
 29import java.util.List;
 30import java.util.Vector;
 31import javax.swing.ImageIcon;
 32import javax.swing.JComponent;
 33import javax.swing.JTable;
 34import javax.swing.table.TableCellRenderer;
 35import mpv5.db.common.Context;
 36import mpv5.db.common.DatabaseObject;
 37import mpv5.db.common.NodataFoundException;
 38import mpv5.db.objects.Group;
 39import mpv5.db.objects.User;
 40import mpv5.logging.Log;
 41import mpv5.ui.frames.MPView;
 42import mpv5.ui.misc.MPTable;
 43import mpv5.utils.date.vTimeframe;
 44import mpv5.utils.models.MPTableModel;
 45import mpv5.utils.renderer.TableCellRendererForDatabaseObjects;
 46import mpv5.utils.tables.TableFormat;
 47import mpv5.ui.misc.TableViewPersistenceHandler;
 48
 49/**
 50 *
 51 *  
 52 */
 53public class GeneralListPanel extends javax.swing.JPanel {
 54
 55    private static final long serialVersionUID = 1L;
 56    private List odata;
 57    TableCellRendererForDatabaseObjects rend = new TableCellRendererForDatabaseObjects();
 58    private java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle();
 59
 60    /** Creates new form GeneralListPanel
 61     * @param <T>
 62     * @param list
 63     */
 64    public <T extends DatabaseObject> GeneralListPanel(List<T> list) {
 65        this();
 66
 67        labeledCombobox1.setSearchEnabled(true);
 68        labeledCombobox1.setContext(Context.getGroup());
 69        labeledCombobox1.triggerSearch();
 70        setData(list);
 71        odata = list;
 72
 73        ((MPTable) listtable).setDefaultColumns(new Integer[]{100, 100, 100, 100, 100}, new Boolean[]{true, true, true, true, true});
 74        TableFormat.hideHeader(listtable);
 75    }
 76
 77    /**
 78     * 
 79     */
 80    public GeneralListPanel() {
 81        initComponents();
 82        setName("generallistpanel");
 83        listtable.setDefaultRenderer(String.class, rend);
 84        listtable.setDefaultRenderer(Date.class, rend);
 85        listtable.setDefaultRenderer(DatabaseObject.class, rend);
 86
 87        ((MPTable) listtable).setDefaultColumns(new Integer[]{100, 100, 100, 100, 100}, new Boolean[]{true, true, true, true, true});
 88        ((MPTable) listtable).setPersistanceHandler(new TableViewPersistenceHandler((MPTable)listtable, this));
 89        TableFormat.hideHeader(listtable);
 90    }
 91
 92    /**
 93     *
 94     * @param c
 95     * @throws NodataFoundException
 96     */
 97    public GeneralListPanel(Context c) throws NodataFoundException {
 98        this(DatabaseObject.getObjects(c));
 99    }
100
101    /**
102     * Show the data
103     * @param <T>
104     * @param map
105     */
106    public <T extends DatabaseObject> void show(HashMap<Color, List<T>> map) {
107
108        Iterator<Color> it = map.keySet().iterator();
109        List<DatabaseObject> ndata = new Vector<DatabaseObject>();
110        while (it.hasNext()) {
111            Color c = it.next();
112            List<T> data = map.get(c);
113            for (int i = 0; i < data.size(); i++) {
114                T databaseObject = data.get(i);
115                databaseObject.defineColor(c);
116                ndata.add(databaseObject);
117            }
118        }
119
120        setData(ndata);
121        odata = ndata;
122        labeledCombobox1.setSearchEnabled(true);
123        labeledCombobox1.setContext(Context.getGroup());
124        labeledCombobox1.triggerSearch();
125    }
126
127    /**
128     * Set the data of the list
129     * @param <T>
130     * @param list
131     */
132    public <T extends DatabaseObject> void setData(List<T> list) {
133
134
135        Object[][] data = new Object[list.size()][5];
136
137        for (int i = 0; i < list.size(); i++) {
138            DatabaseObject databaseObject = list.get(i);
139            data[i][0] = databaseObject;
140            data[i][1] = User.getUsername(databaseObject.__getIntaddedby());
141            data[i][2] = databaseObject.__getDateadded();
142            try {
143                data[i][3] = DatabaseObject.getObject(Context.getGroup(), databaseObject.__getGroupsids());
144            } catch (NodataFoundException ex) {
145                data[i][3] = "N/A";
146            }
147
148//            data[i][4] = databaseObject.getColor();
149            data[i][4] = databaseObject.getIcon();
150        }
151
152        MPTableModel m = new MPTableModel(data);
153        m.setTypes(new Class[]{DatabaseObject.class, String.class, Date.class, DatabaseObject.class, ImageIcon.class, Object.class, Object.class});
154        listtable.setModel(m);
155
156//        TableFormat.hideHeader(listtable);
157//        TableFormat.stripColumn(listtable, 4);
158
159//        TableFormat.stripColumn(jTable1, 5);
160    }
161
162    /**
163     * Filter by group
164     * @param g
165     */
166    @SuppressWarnings({"unchecked"})
167    public void filterByGroup(Group g) {
168        setData(odata);
169        Object[][] data = ((MPTableModel) listtable.getModel()).getData();
170        List<DatabaseObject> list = new Vector<DatabaseObject>();
171        if (g.__getIDS().intValue() != 1) {
172            for (int i = 0; i < data.length; i++) {
173                DatabaseObject d = (DatabaseObject) data[i][0];
174                if (d.__getGroupsids() == g.__getIDS()) {
175                    list.add(d);
176                }
177            }
178        } else {
179            for (int i = 0; i < data.length; i++) {
180                DatabaseObject d = (DatabaseObject) data[i][0];
181                list.add(d);
182            }
183        }
184        setData(list);
185    }
186
187    /**
188     * Filter by group
189     * @param g
190     */
191    @SuppressWarnings("unchecked")
192    public void filterByTimeframe(vTimeframe g) {
193        setData(odata);
194        Object[][] data = ((MPTableModel) listtable.getModel()).getData();
195        List<DatabaseObject> list = new Vector<DatabaseObject>();
196        for (int i = 0; i < data.length; i++) {
197            DatabaseObject d = (DatabaseObject) data[i][0];
198            if (g.contains(d.__getDateadded())) {
199                list.add(d);
200            }
201        }
202        setData(list);
203    }
204
205    /** This method is called from within the constructor to
206     * initialize the form.
207     * WARNING: Do NOT modify this code. The content of this method is
208     * always regenerated by the Form Editor.
209     */
210    @SuppressWarnings("unchecked")
211    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
212    private void initComponents() {
213
214        jScrollPane1 = new javax.swing.JScrollPane();
215        listtable = new  mpv5.ui.misc.MPTable(this) {
216            public Component prepareRenderer(TableCellRenderer renderer,
217                int rowIndex, int vColIndex) {
218                Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
219                if (c instanceof JComponent) {
220                    JComponent jc = (JComponent)c;
221                    jc.setToolTipText(String.valueOf(getValueAt(rowIndex, vColIndex)));
222                }
223                return c;
224            }
225        };
226        timeframeChooser1 = new mpv5.ui.beans.TimeframeChooser();
227        labeledCombobox1 = new mpv5.ui.beans.LabeledCombobox();
228        jButton1 = new javax.swing.JButton();
229        jButton2 = new javax.swing.JButton();
230
231        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle();
232        setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("GeneralListPanel.border.title"))); // NOI18N
233        setName("Form"); // NOI18N
234
235        jScrollPane1.setName("jScrollPane1"); // NOI18N
236
237        listtable.setAutoCreateRowSorter(true);
238        listtable.setModel(new javax.swing.table.DefaultTableModel(
239            new Object [][] {
240                {},
241                {},
242                {},
243                {}
244            },
245            new String [] {
246
247            }
248        ));
249        listtable.setName("listtable"); // NOI18N
250        listtable.setShowVerticalLines(false);
251        listtable.addMouseListener(new java.awt.event.MouseAdapter() {
252            public void mouseClicked(java.awt.event.MouseEvent evt) {
253                listtableMouseClicked(evt);
254            }
255        });
256        jScrollPane1.setViewportView(listtable);
257
258        timeframeChooser1.setName("timeframeChooser1"); // NOI18N
259
260        labeledCombobox1.set_Label(bundle.getString("GeneralListPanel.labeledCombobox1._Label")); // NOI18N
261        labeledCombobox1.setName("labeledCombobox1"); // NOI18N
262
263        jButton1.setText(bundle.getString("GeneralListPanel.jButton1.text")); // NOI18N
264        jButton1.setName("jButton1"); // NOI18N
265        jButton1.addActionListener(new java.awt.event.ActionListener() {
266            public void actionPerformed(java.awt.event.ActionEvent evt) {
267                jButton1ActionPerformed(evt);
268            }
269        });
270
271        jButton2.setText(bundle.getString("GeneralListPanel.jButton2.text")); // NOI18N
272        jButton2.setName("jButton2"); // NOI18N
273        jButton2.addActionListener(new java.awt.event.ActionListener() {
274            public void actionPerformed(java.awt.event.ActionEvent evt) {
275                jButton2ActionPerformed(evt);
276            }
277        });
278
279        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
280        this.setLayout(layout);
281        layout.setHorizontalGroup(
282            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
283            .addGroup(layout.createSequentialGroup()
284                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
285                    .addComponent(labeledCombobox1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
286                    .addComponent(timeframeChooser1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
287                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
288                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
289                    .addComponent(jButton1)
290                    .addComponent(jButton2))
291                .addGap(33, 33, 33))
292            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 451, Short.MAX_VALUE)
293        );
294        layout.setVerticalGroup(
295            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
296            .addGroup(layout.createSequentialGroup()
297                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
298                    .addGroup(layout.createSequentialGroup()
299                        .addComponent(jButton1, 0, 0, Short.MAX_VALUE)
300                        .addGap(1, 1, 1))
301                    .addComponent(timeframeChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
302                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
303                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
304                    .addComponent(labeledCombobox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
305                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
306                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
307                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE))
308        );
309    }// </editor-fold>//GEN-END:initComponents
310
311    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
312
313        filterByTimeframe(timeframeChooser1.getTime());
314    }//GEN-LAST:event_jButton1ActionPerformed
315
316    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
317        try {
318            filterByGroup((Group) Group.getObject(Context.getGroup(), Integer.valueOf(labeledCombobox1.getSelectedItem().getId())));
319        } catch (Exception ex) {
320//            Log.Debug(ex);
321        }
322    }//GEN-LAST:event_jButton2ActionPerformed
323
324    private void listtableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listtableMouseClicked
325        if (evt.getClickCount() > 1) {
326            try {
327                System.err.println(mpv5.YabsViewProxy.instance().getIdentifierView());
328                mpv5.YabsViewProxy.instance().getIdentifierView().addTab(((DatabaseObject) listtable.getModel().getValueAt(listtable.getSelectedRow(), 0)));
329            } catch (Exception e) {
330                Log.Debug(e);
331            }
332        }
333    }//GEN-LAST:event_listtableMouseClicked
334    // Variables declaration - do not modify//GEN-BEGIN:variables
335    private javax.swing.JButton jButton1;
336    private javax.swing.JButton jButton2;
337    private javax.swing.JScrollPane jScrollPane1;
338    private mpv5.ui.beans.LabeledCombobox labeledCombobox1;
339    private javax.swing.JTable listtable;
340    private mpv5.ui.beans.TimeframeChooser timeframeChooser1;
341    // End of variables declaration//GEN-END:variables
342}