PageRenderTime 248ms CodeModel.GetById 60ms app.highlight 106ms RepoModel.GetById 75ms app.codeStats 0ms

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

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 536 lines | 429 code | 66 blank | 41 comment | 44 complexity | 89b66a232d4f1f9bbdc70e3ffb1f7d44 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 * ContactPanel.java
 19 *
 20 * Created on Nov 20, 2008, 8:17:28 AM
 21 */
 22package mpv5.ui.dialogs.subcomponents;
 23
 24import java.awt.Color;
 25import java.awt.Component;
 26import java.awt.event.MouseEvent;
 27import java.io.File;
 28import java.util.ArrayList;
 29import java.util.Collections;
 30import java.util.logging.Level;
 31import java.util.logging.Logger;
 32import javax.swing.JTextField;
 33import javax.swing.tree.DefaultMutableTreeNode;
 34import javax.swing.tree.TreeNode;
 35import javax.swing.tree.TreeSelectionModel;
 36import mpv5.data.PropertyStore;
 37import mpv5.db.common.Context;
 38import mpv5.db.common.DatabaseObject;
 39import mpv5.db.common.DatabaseSearch;
 40import mpv5.db.common.NodataFoundException;
 41import mpv5.db.common.QueryCriteria;
 42import mpv5.db.common.QueryCriteria2;
 43import mpv5.db.common.QueryHandler;
 44import mpv5.db.common.QueryParameter;
 45import mpv5.globals.Messages;
 46import mpv5.db.objects.Group;
 47import mpv5.logging.Log;
 48import mpv5.ui.dialogs.ControlApplet;
 49import mpv5.ui.dialogs.Popup;
 50import mpv5.ui.frames.MPView;
 51import mpv5.ui.panels.DataPanel;
 52import mpv5.usermanagement.MPSecurityManager;
 53import mpv5.utils.arrays.ArrayUtilities;
 54import mpv5.utils.export.Export;
 55import mpv5.utils.models.MPTreeModel;
 56import mpv5.utils.trees.TreeFormat;
 57import mpv5.utils.ui.TextFieldUtils;
 58
 59/**
 60 *
 61 *
 62 */
 63public class ControlPanel_Groups extends javax.swing.JPanel implements ControlApplet, DataPanel {
 64
 65    private static final long serialVersionUID = 1L;
 66    private ControlPanel_Groups insta;
 67    private ArrayList<Object[]> arr;
 68    private Group dataOwner;
 69    private boolean root;
 70
 71    /**
 72     * Creates new form ContactPanel
 73     */
 74    public ControlPanel_Groups() {
 75        if (MPSecurityManager.checkAdminAccess()) {
 76            initComponents();
 77            tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
 78            refresh();
 79        }
 80    }
 81
 82    public ControlPanel_Groups(Group aThis) {
 83        if (MPSecurityManager.checkAdminAccess()) {
 84            initComponents();
 85            tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
 86            refresh();
 87            setDataOwner(aThis, true);
 88        }
 89    }
 90
 91    public void showRequiredFields() {
 92        TextFieldUtils.blinkerRed(cname);
 93        TextFieldUtils.blinkerGrey(parents);
 94    }
 95
 96    /**
 97     * This method is called from within the constructor to initialize the form.
 98     * WARNING: Do NOT modify this code. The content of this method is always
 99     * regenerated by the Form Editor.
100     */
101    @SuppressWarnings("unchecked")
102    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
103    private void initComponents() {
104
105        buttonGroup1 = new javax.swing.ButtonGroup();
106        jPanel1 = new javax.swing.JPanel();
107        jScrollPane1 = new javax.swing.JScrollPane();
108        tree = new javax.swing.JTree();
109        jLabel1 = new javax.swing.JLabel();
110        rightpane = new javax.swing.JPanel();
111        cname = new mpv5.ui.beans.LabeledTextField();
112        parents = new mpv5.ui.beans.LabeledTextField();
113        jScrollPane3 = new javax.swing.JScrollPane();
114        jTextArea2 = new javax.swing.JTextArea();
115        jLabel2 = new javax.swing.JLabel();
116        jScrollPane2 = new javax.swing.JScrollPane();
117        desc = new javax.swing.JTextArea();
118        jLabel3 = new javax.swing.JLabel();
119        path = new mpv5.ui.beans.LabeledTextField();
120        jPanel2 = new javax.swing.JPanel();
121        jButton4 = new javax.swing.JButton();
122        jButton3 = new javax.swing.JButton();
123        jButton2 = new javax.swing.JButton();
124        jButton1 = new javax.swing.JButton();
125
126        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
127        setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ControlPanel_Groups.border.title"))); // NOI18N
128        setName("Form"); // NOI18N
129        setLayout(new java.awt.BorderLayout());
130
131        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
132        jPanel1.setName("jPanel1"); // NOI18N
133
134        jScrollPane1.setName("jScrollPane1"); // NOI18N
135
136        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
137        tree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
138        tree.setToolTipText(bundle.getString("ControlPanel_Groups.tree.toolTipText")); // NOI18N
139        tree.setLargeModel(true);
140        tree.setName("tree"); // NOI18N
141        tree.addMouseListener(new java.awt.event.MouseAdapter() {
142            public void mouseClicked(java.awt.event.MouseEvent evt) {
143                treeMouseClicked(evt);
144            }
145        });
146        jScrollPane1.setViewportView(tree);
147
148        jLabel1.setText(bundle.getString("ControlPanel_Groups.jLabel1.text")); // NOI18N
149        jLabel1.setName("jLabel1"); // NOI18N
150
151        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
152        jPanel1.setLayout(jPanel1Layout);
153        jPanel1Layout.setHorizontalGroup(
154            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
155            .addGroup(jPanel1Layout.createSequentialGroup()
156                .addContainerGap()
157                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
158                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
159                    .addComponent(jLabel1))
160                .addContainerGap())
161        );
162        jPanel1Layout.setVerticalGroup(
163            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
164            .addGroup(jPanel1Layout.createSequentialGroup()
165                .addContainerGap()
166                .addComponent(jLabel1)
167                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
168                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
169                .addContainerGap())
170        );
171
172        add(jPanel1, java.awt.BorderLayout.CENTER);
173
174        rightpane.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ControlPanel_Groups.rightpane.border.title"))); // NOI18N
175        rightpane.setName("rightpane"); // NOI18N
176
177        cname.set_Label(bundle.getString("ControlPanel_Groups.cname._Label")); // NOI18N
178        cname.setName("cname"); // NOI18N
179
180        parents.set_Label(bundle.getString("ControlPanel_Groups.parents._Label")); // NOI18N
181        parents.setEnabled(false);
182        parents.setName("parents"); // NOI18N
183
184        jScrollPane3.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
185        jScrollPane3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
186        jScrollPane3.setName("jScrollPane3"); // NOI18N
187
188        jTextArea2.setColumns(20);
189        jTextArea2.setEditable(false);
190        jTextArea2.setLineWrap(true);
191        jTextArea2.setRows(5);
192        jTextArea2.setText(bundle.getString("ControlPanel_Groups.jTextArea2.text")); // NOI18N
193        jTextArea2.setWrapStyleWord(true);
194        jTextArea2.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
195        jTextArea2.setName("jTextArea2"); // NOI18N
196        jScrollPane3.setViewportView(jTextArea2);
197
198        jLabel2.setFont(new java.awt.Font("Dialog", 0, 12));
199        jLabel2.setText(bundle.getString("ControlPanel_Groups.jLabel2.text")); // NOI18N
200        jLabel2.setName("jLabel2"); // NOI18N
201
202        jScrollPane2.setName("jScrollPane2"); // NOI18N
203
204        desc.setColumns(20);
205        desc.setRows(5);
206        desc.setName("desc"); // NOI18N
207        jScrollPane2.setViewportView(desc);
208
209        jLabel3.setFont(new java.awt.Font("Dialog", 0, 12));
210        jLabel3.setText(bundle.getString("ControlPanel_Groups.jLabel3.text")); // NOI18N
211        jLabel3.setName("jLabel3"); // NOI18N
212
213        path.set_Label(bundle.getString("ControlPanel_Groups.path._Label")); // NOI18N
214        path.setName("path"); // NOI18N
215
216        javax.swing.GroupLayout rightpaneLayout = new javax.swing.GroupLayout(rightpane);
217        rightpane.setLayout(rightpaneLayout);
218        rightpaneLayout.setHorizontalGroup(
219            rightpaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
220            .addGroup(rightpaneLayout.createSequentialGroup()
221                .addContainerGap()
222                .addGroup(rightpaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
223                    .addComponent(jLabel3)
224                    .addComponent(cname, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
225                    .addComponent(jLabel2)
226                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
227                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
228                    .addComponent(parents, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
229                    .addComponent(path, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE))
230                .addContainerGap())
231        );
232        rightpaneLayout.setVerticalGroup(
233            rightpaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
234            .addGroup(rightpaneLayout.createSequentialGroup()
235                .addComponent(jLabel3)
236                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
237                .addComponent(cname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
238                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
239                .addComponent(jLabel2)
240                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
241                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
242                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
243                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
244                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
245                .addComponent(parents, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
246                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
247                .addComponent(path, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
248                .addContainerGap(42, Short.MAX_VALUE))
249        );
250
251        add(rightpane, java.awt.BorderLayout.EAST);
252
253        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
254        jPanel2.setName("jPanel2"); // NOI18N
255        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
256
257        jButton4.setText(bundle.getString("ControlPanel_Groups.jButton4.text")); // NOI18N
258        jButton4.setName("jButton4"); // NOI18N
259        jButton4.addActionListener(new java.awt.event.ActionListener() {
260            public void actionPerformed(java.awt.event.ActionEvent evt) {
261                jButton4ActionPerformed(evt);
262            }
263        });
264        jPanel2.add(jButton4);
265
266        jButton3.setText(bundle.getString("ControlPanel_Groups.jButton3.text")); // NOI18N
267        jButton3.setName("jButton3"); // NOI18N
268        jButton3.addActionListener(new java.awt.event.ActionListener() {
269            public void actionPerformed(java.awt.event.ActionEvent evt) {
270                jButton3ActionPerformed(evt);
271            }
272        });
273        jPanel2.add(jButton3);
274
275        jButton2.setText(bundle.getString("ControlPanel_Groups.jButton2.text")); // NOI18N
276        jButton2.setName("jButton2"); // NOI18N
277        jButton2.addActionListener(new java.awt.event.ActionListener() {
278            public void actionPerformed(java.awt.event.ActionEvent evt) {
279                jButton2ActionPerformed(evt);
280            }
281        });
282        jPanel2.add(jButton2);
283
284        jButton1.setText(bundle.getString("ControlPanel_Groups.jButton1.text")); // NOI18N
285        jButton1.setName("jButton1"); // NOI18N
286        jButton1.addActionListener(new java.awt.event.ActionListener() {
287            public void actionPerformed(java.awt.event.ActionEvent evt) {
288                jButton1ActionPerformed(evt);
289            }
290        });
291        jPanel2.add(jButton1);
292
293        add(jPanel2, java.awt.BorderLayout.PAGE_END);
294    }// </editor-fold>//GEN-END:initComponents
295
296    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
297        if (Popup.Y_N_dialog(Messages.REALLY_DELETE)) {
298            if (dataOwner != null) {
299                DatabaseObject dato = dataOwner;
300                dato.getPanelData(this);
301                if (dato.__getIDS().intValue() == 1 || !dato.delete()) {
302                    Popup.notice(Messages.NOT_POSSIBLE + "\n" + Messages.IN_USE);
303                }
304            }
305            refresh();
306        }
307}//GEN-LAST:event_jButton4ActionPerformed
308
309    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
310        reset();
311}//GEN-LAST:event_jButton3ActionPerformed
312
313    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
314
315        if (dataOwner != null) {
316            DatabaseObject dato = dataOwner;
317            dato.getPanelData(this);
318
319            //Issue 239 if the group id of the root group is not 0,
320            //program will end up in an infinite loop during tree building
321            if (dato.__getIDS().intValue() == 1) {
322                dato.setGroupsids(0);
323            }
324
325            if (dato.save()) {
326            } else {
327                showRequiredFields();
328            }
329        }
330}//GEN-LAST:event_jButton2ActionPerformed
331
332    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
333
334        if (dataOwner == null) {
335            dataOwner = new Group();
336        }
337        DatabaseObject dato = dataOwner;
338        if (QueryHandler.instanceOf().clone(Context.getGroup()).checkUniqueness(Context.getGroup().getUniqueColumns(), new JTextField[]{cname.getTextField()})) {
339            dato.getPanelData(this);
340            dato.setIDS(-1);
341            if (dato.save()) {
342            } else {
343                showRequiredFields();
344            }
345        }
346
347        refresh();
348}//GEN-LAST:event_jButton1ActionPerformed
349
350    private void treeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_treeMouseClicked
351        evt.consume();
352        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
353        if (node.isRoot()) {
354            root = true;
355        } else {
356            root = false;
357        }
358
359        if (evt.getButton() == MouseEvent.BUTTON2 || evt.getButton() == MouseEvent.BUTTON3) {
360            if (node != null) {
361                Group g = (Group) node.getUserObject();
362                parents.set_Text(g.__getCname());
363                path.setText(getPath(node, 0));
364            }
365        } else {
366            if (node != null) {
367                Group g = (Group) node.getUserObject();
368                setDataOwner(g, true);
369                path.setText(getPath(node, 1));
370            }
371        }
372    }//GEN-LAST:event_treeMouseClicked
373    // Variables declaration - do not modify//GEN-BEGIN:variables
374    private javax.swing.ButtonGroup buttonGroup1;
375    private mpv5.ui.beans.LabeledTextField cname;
376    private javax.swing.JTextArea desc;
377    private javax.swing.JButton jButton1;
378    private javax.swing.JButton jButton2;
379    private javax.swing.JButton jButton3;
380    private javax.swing.JButton jButton4;
381    private javax.swing.JLabel jLabel1;
382    private javax.swing.JLabel jLabel2;
383    private javax.swing.JLabel jLabel3;
384    private javax.swing.JPanel jPanel1;
385    private javax.swing.JPanel jPanel2;
386    private javax.swing.JScrollPane jScrollPane1;
387    private javax.swing.JScrollPane jScrollPane2;
388    private javax.swing.JScrollPane jScrollPane3;
389    private javax.swing.JTextArea jTextArea2;
390    private mpv5.ui.beans.LabeledTextField parents;
391    private mpv5.ui.beans.LabeledTextField path;
392    private javax.swing.JPanel rightpane;
393    private javax.swing.JTree tree;
394    // End of variables declaration//GEN-END:variables
395    public String description_ = "";
396    public String defaults_ = "";
397    public String cname_ = "";
398    public int ids_;
399    public int groupsids_ = 1;
400    public int intaddedby_ = 1;
401    public java.util.Date dateadded_ = new java.util.Date();
402    public String hierarchypath_ = "";
403
404    @Override
405    public void setValues(PropertyStore values) {
406        throw new UnsupportedOperationException("Not supported yet.");
407    }
408
409    @Override
410    public String getUname() {
411        return "Groups";
412    }
413
414    @Override
415    public void reset() {
416        if (dataOwner != null && dataOwner.isExisting()) {
417            DatabaseObject dato = dataOwner;
418
419            dato.getPanelData(this);
420            dato.reset();
421            setDataOwner(dato, true);
422        }
423    }
424
425    public void refresh() {
426
427        Group root;
428        try {
429            root = (Group) DatabaseObject.getObject(Context.getGroup(), "groupsids", 0);
430        } catch (NodataFoundException ex) {
431            root = new Group(Messages.GROUPNAMES.toString());
432            root.setIDS(-1);
433        }
434
435        tree.setModel(MPTreeModel.toTreeModel(root));
436        TreeFormat.expandTree(tree);
437    }
438
439    public boolean collectData() {
440        if (cname.getText().length() == 0) {
441            return false;
442        }
443        cname_ = cname.get_Text();
444        description_ = desc.getText();
445        hierarchypath_ = path.getText();
446        if (!root) {
447            try {
448                groupsids_ = DatabaseObject.getObject(Context.getGroup(), parents.get_Text()).__getIDS();
449            } catch (NodataFoundException ex) {
450                groupsids_ = 1;
451            }
452            if (ids_ == groupsids_) {
453                groupsids_ = 1;
454            }
455        } else {
456            groupsids_ = 0;
457        }
458        return true;
459    }
460
461    public DatabaseObject getDataOwner() {
462        return dataOwner;
463    }
464
465    public void setDataOwner(DatabaseObject object, boolean p) {
466        dataOwner = (Group) object;
467        if (p) {
468            dataOwner.setPanelData(this);
469            this.exposeData();
470        }
471    }
472
473    public void exposeData() {
474        cname.set_Text(cname_);
475        desc.setText(description_);
476        path.setText(hierarchypath_);
477        try {
478            parents.set_Text(DatabaseObject.getObject(Context.getGroup(), groupsids_).__getCname());
479        } catch (NodataFoundException ex) {
480//            Log.Debug(this, ex);
481        }
482//        defaults.set_Text(defaults_);
483    }
484
485    public void paste(DatabaseObject... dbos) {
486        for (DatabaseObject dbo : dbos) {
487            if (dbo.getDbIdentity().equals(Context.getGroup().getDbIdentity())) {
488                setDataOwner(dbo, true);
489            } else {
490                mpv5.YabsViewProxy.instance().addMessage(Messages.NOT_POSSIBLE.toString() + Messages.ACTION_PASTE, Color.RED);
491            }
492        }
493    }
494
495    public void showSearchBar(boolean show) {
496//        throw new UnsupportedOperationException("Not supported yet.");
497    }
498
499    public Component getAndRemoveActionPanel() {
500        this.remove(jPanel2);
501        validate();
502        return jPanel2;
503    }
504
505    @Override
506    public void actionAfterSave() {
507    }
508
509    @Override
510    public void actionAfterCreate() {
511    }
512
513    private String getPath(DefaultMutableTreeNode node, int shift) {
514        TreeNode[] p = node.getPath();
515        String path1 = "";
516        for (int i = 0; i < p.length - shift; i++) {
517            TreeNode treeNode = p[i];
518            path1 += treeNode.toString() + File.separator;
519        }
520
521        return path1;
522    }
523
524    public void actionBeforeCreate() {
525    }
526
527    public void actionBeforeSave() {
528    }
529
530    public void mail() {
531    }
532
533    public void print() {
534        mpv5.utils.export.Export.print(this);
535    }
536}