KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > tree > VTreePanel


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.grid.tree;
15
16 import java.awt.*;
17 import java.awt.dnd.*;
18 import java.awt.datatransfer.*;
19 import java.awt.event.*;
20 import javax.swing.*;
21 import java.util.*;
22 import javax.swing.*;
23 import javax.swing.tree.*;
24 import java.sql.*;
25
26 import org.compiere.apps.*;
27 import org.compiere.model.*;
28 import org.compiere.util.*;
29 import org.compiere.plaf.*;
30 import org.compiere.swing.*;
31
32 /**
33  * Tree Panel displays trees.
34  * <br>
35  * When a node is selected, a propertyChange (NODE_SELECTION) event is fired
36  * <pre>
37  * PropertyChangeListener -
38  * treePanel.addPropertyChangeListener(VTreePanel.NODE_SELECTION, this);
39  * calls: public void propertyChange(PropertyChangeEvent e)
40  * </pre>
41  * To select a specific node call
42  * setSelectedNode(NodeID);
43  *
44  * @author Jorg Janke
45  * @version $Id: VTreePanel.java,v 1.20 2003/11/02 07:49:35 jjanke Exp $
46  */

47 public final class VTreePanel extends CPanel
48     implements ActionListener, DragGestureListener, DragSourceListener, DropTargetListener
49 {
50     /**
51      * Tree Panel for browsing and editing of a tree.
52      * Need to call initTree
53      * @param WindowNo WindowNo
54      * @param editable if true you can edit it
55      * @param hasBar has OutlookBar
56      */

57     public VTreePanel(int WindowNo, boolean hasBar, boolean editable)
58     {
59         super();
60         Log.trace(Log.l3_Util, "VTreePanel", "Bar=" + hasBar + ", Editable=" + editable);
61         m_WindowNo = WindowNo;
62         m_hasBar = hasBar;
63         m_editable = editable;
64
65         // static init
66
jbInit();
67         if (!hasBar)
68         {
69             bar.setPreferredSize(new Dimension(0,0));
70             centerSplitPane.setDividerLocation(0);
71             centerSplitPane.setDividerSize(0);
72             popMenuTree.remove(mBarAdd);
73         }
74         else
75             centerSplitPane.setDividerLocation(80);
76         // base settings
77
if (editable)
78             tree.setDropTarget(dropTarget);
79         else
80         {
81             popMenuTree.remove(mFrom);
82             popMenuTree.remove(mTo);
83         }
84     } // VTreePanel
85

86     /**
87      * Tree initialization.
88      * May be called several times
89      * @param AD_Tree_ID tree to load
90      * @return true if loaded ok
91      */

92     public boolean initTree (int AD_Tree_ID)
93     {
94         Log.trace(Log.l3_Util, "VTreePanel.initTree " + AD_Tree_ID);
95         //
96
m_AD_Tree_ID = AD_Tree_ID;
97
98         // Get Tree
99
MTree vTree = new MTree (Env.getCtx(), AD_Tree_ID, m_editable);
100         m_root = vTree.getRoot();
101         m_nodeTableName = vTree.getNodeTableName();
102         treeModel = new DefaultTreeModel(m_root, true);
103         tree.setModel(treeModel);
104
105         // Shortcut Bar
106
if (m_hasBar)
107         {
108             bar.removeAll(); // remove all existing buttons
109
Enumeration en = m_root.preorderEnumeration();
110             while (en.hasMoreElements())
111             {
112                 MTreeNode nd = (MTreeNode)en.nextElement();
113                 if (nd.isOnBar())
114                     addToBar(nd);
115             }
116         }
117
118         return true;
119     } // initTree
120

121
122     private BorderLayout mainLayout = new BorderLayout();
123     private JTree tree = new JTree();
124     private DefaultTreeModel treeModel;
125     private DefaultTreeSelectionModel treeSelect = new DefaultTreeSelectionModel();
126     private CPanel southPanel = new CPanel();
127     private CCheckBox treeExpand = new CCheckBox();
128     private CTextField treeSearch = new CTextField(10);
129     private JPopupMenu popMenuTree = new JPopupMenu();
130     private JPopupMenu popMenuBar = new JPopupMenu();
131     private JMenuItem mFrom = new JMenuItem();
132     private JMenuItem mTo = new JMenuItem();
133     private CPanel bar = new CPanel();
134     private JMenuItem mBarAdd = new JMenuItem();
135     private JMenuItem mBarRemove = new JMenuItem();
136     private BorderLayout southLayout = new BorderLayout();
137     private JSplitPane centerSplitPane = new JSplitPane();
138     private JScrollPane treePane = new JScrollPane();
139     private MouseListener mouseListener = new VTreePanel_mouseAdapter(this);
140     private KeyListener keyListener = new VTreePanel_keyAdapter(this);
141
142     //
143
private int m_WindowNo;
144     /** Tree ID */
145     private int m_AD_Tree_ID = 0;
146     /** Table Name for TreeNode */
147     private String JavaDoc m_nodeTableName = null;
148     /** Tree is editable (can move nodes) - also not active shown */
149     private boolean m_editable;
150     /** Tree has a shortcut Bar */
151     private boolean m_hasBar;
152     /** The root node */
153     private MTreeNode m_root = null;
154
155
156     private MTreeNode m_moveNode; // the node to move
157
private String JavaDoc m_search = "";
158     private Enumeration m_nodeEn;
159     private MTreeNode m_selectedNode; // the selected model node
160
private CButton m_buttonSelected;
161
162     // Property Listener
163
public static final String JavaDoc NODE_SELECTION = "NodeSelected";
164
165     /**
166      * Static Component initialization.
167      * <pre>
168      * - centerSplitPane
169      * - treePane
170      * - tree
171      * - bar
172      * - southPanel
173      * </pre>
174      * @throws Exception
175      */

176     private void jbInit()
177     {
178         this.setLayout(mainLayout);
179         mainLayout.setVgap(5);
180         //
181
// only one node to be selected
182
treeSelect.setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
183         tree.setSelectionModel(treeSelect);
184         //
185
tree.setEditable(false); // allows to change the text
186
tree.addMouseListener(mouseListener);
187         tree.addKeyListener(keyListener);
188         tree.setCellRenderer(new VTreeCellRenderer());
189         treePane.getViewport().add(tree, null);
190 // treePane.setPreferredSize(new Dimension(50,200));
191
// tree.setPreferredSize(new Dimension(100,150));
192
//
193
treeExpand.setText("Expand");
194         treeExpand.setActionCommand("Expand");
195         treeExpand.addMouseListener(mouseListener);
196         treeExpand.addActionListener(this);
197
198         treeSearch.setBackground(CompierePLAF.getInfoBackground());
199         treeSearch.addKeyListener(keyListener);
200         southPanel.setLayout(southLayout);
201         southPanel.add(treeExpand, BorderLayout.WEST);
202         southPanel.add(treeSearch, BorderLayout.EAST);
203         this.add(southPanel, BorderLayout.SOUTH);
204         //
205
centerSplitPane.add(treePane, JSplitPane.RIGHT);
206         centerSplitPane.add(bar, JSplitPane.LEFT);
207         this.add(centerSplitPane, BorderLayout.CENTER);
208         //
209
mFrom.setText("From.");
210         mFrom.setActionCommand("From");
211         mFrom.addActionListener(this);
212         mTo.setEnabled(false);
213         mTo.setText("To.");
214         mTo.setActionCommand("To");
215         mTo.addActionListener(this);
216         //
217
bar.setLayout(new BoxLayout(bar, BoxLayout.Y_AXIS));
218         bar.setMinimumSize(new Dimension (50,50));
219
220         mBarAdd.setText("BarAdd.");
221         mBarAdd.setActionCommand("BarAdd");
222         mBarAdd.addActionListener(this);
223         mBarRemove.setText("BarRemove.");
224         mBarRemove.setActionCommand("BarRemove");
225         mBarRemove.addActionListener(this);
226         //
227
popMenuTree.setLightWeightPopupEnabled(false);
228         popMenuTree.add(mBarAdd);
229         popMenuTree.addSeparator();
230         popMenuTree.add(mFrom);
231         popMenuTree.add(mTo);
232         popMenuBar.setLightWeightPopupEnabled(false);
233         popMenuBar.add(mBarRemove);
234
235         // translation
236
treeExpand.setText(Msg.getMsg(Env.getCtx(), "ExpandTree"));
237         treeSearch.setToolTipText(Msg.getMsg(Env.getCtx(), "EnterSearchText"));
238         mBarAdd.setText(Msg.getMsg(Env.getCtx(), "BarAdd"));
239         mBarRemove.setText(Msg.getMsg(Env.getCtx(), "BarRemove"));
240         mFrom.setText(Msg.getMsg(Env.getCtx(), "ItemMove"));
241         mTo.setText(Msg.getMsg(Env.getCtx(), "ItemInsert"));
242     } // jbInit
243

244
245     /*************************************************************************
246      * Drag & Drop
247      */

248     protected DragSource dragSource
249         = DragSource.getDefaultDragSource();
250     protected DropTarget dropTarget
251         = new DropTarget(tree, DnDConstants.ACTION_MOVE, this, true, null);
252     protected DragGestureRecognizer recognizer
253         = dragSource.createDefaultDragGestureRecognizer(tree, DnDConstants.ACTION_MOVE, this);
254
255
256     /**
257      * Drag Gesture Interface ** Start **
258      * @param e event
259      */

260     public void dragGestureRecognized(DragGestureEvent e)
261     {
262         if (!m_editable)
263             return;
264         //
265
try
266         {
267             m_moveNode = (MTreeNode)tree.getSelectionPath().getLastPathComponent();
268         }
269         catch (Exception JavaDoc ex) // nothing selected
270
{
271             return;
272         }
273         // start moving
274
StringSelection content = new StringSelection(m_moveNode.toString());
275         e.startDrag(DragSource.DefaultMoveDrop, // cursor
276
content, // Transferable
277
this);
278         Log.trace(Log.l5_DData, "Drag: " + m_moveNode.toString());
279     } // dragGestureRecognized
280

281
282     /**
283      * DragSourceListener interface
284      * @param e event
285      */

286     public void dragDropEnd(DragSourceDropEvent e) {}
287     public void dragEnter(DragSourceDragEvent e) {}
288     public void dragExit(DragSourceEvent e) {}
289     public void dragOver(DragSourceDragEvent e) {}
290     public void dropActionChanged(DragSourceDragEvent e) {}
291
292     /**
293      * DropTargetListener interface
294      * @param e event
295      */

296     public void dragEnter(DropTargetDragEvent e)
297     {
298         e.acceptDrag(DnDConstants.ACTION_MOVE);
299     }
300     public void dropActionChanged(DropTargetDragEvent e) {}
301     public void dragExit(DropTargetEvent e) {}
302
303
304     /**
305      * Drag over ** Between **
306      * @param e event
307      */

308     public void dragOver(DropTargetDragEvent e)
309     {
310         Point mouseLoc = e.getLocation(); // where are we?
311
TreePath path = tree.getClosestPathForLocation(mouseLoc.x, mouseLoc.y);
312         tree.setSelectionPath(path); // show it by selecting
313
MTreeNode toNode = (MTreeNode)path.getLastPathComponent();
314         //
315
// Log.trace(Log.l5_DData, "Move: " + toNode);
316
if (m_moveNode == null // nothing to move
317
|| toNode == null) // nothing to drop on
318
e.rejectDrag();
319         else
320             e.acceptDrag(DnDConstants.ACTION_MOVE);
321     } // dragOver
322

323
324     /**
325      * Drop ** End **
326      * @param e event
327      */

328     public void drop(DropTargetDropEvent e)
329     {
330         Point mouseLoc = e.getLocation(); // where are we?
331
TreePath path = tree.getClosestPathForLocation(mouseLoc.x, mouseLoc.y);
332         tree.setSelectionPath(path); // show it by selecting
333
MTreeNode toNode = (MTreeNode)path.getLastPathComponent();
334         //
335
Log.trace(Log.l5_DData, "Drop: " + toNode);
336         if (m_moveNode == null // nothing to move
337
|| toNode == null) // nothing to drop on
338
{
339             e.rejectDrop();
340             return;
341         }
342         //
343
e.acceptDrop(DnDConstants.ACTION_MOVE);
344         moveNode(m_moveNode, toNode);
345
346         e.dropComplete(true);
347         m_moveNode = null;
348     } // drop
349

350
351     /**
352      * Move TreeNode
353      * @param movingNode The node to be moved
354      * @param toNode The target node
355      */

356     private void moveNode(MTreeNode movingNode, MTreeNode toNode)
357     {
358         Log.trace(Log.l1_User, "VTreePanel.moveNode " + movingNode.toString() + " to " + toNode.toString());
359
360         if (movingNode == toNode)
361             return;
362
363         // remove
364
MTreeNode oldParent = (MTreeNode)movingNode.getParent();
365         movingNode.removeFromParent();
366         treeModel.nodeStructureChanged(oldParent);
367
368         // insert
369
MTreeNode newParent;
370         int index;
371         if (!toNode.isSummary()) // drop on a child node
372
{
373             newParent = (MTreeNode)toNode.getParent();
374             index = newParent.getIndex(toNode) + 1; // the next node
375
}
376         else // drop on a summary node
377
{
378             newParent = toNode;
379             index = 0; // the first node
380
}
381         newParent.insert(movingNode, index);
382         treeModel.nodeStructureChanged(newParent);
383
384         // *** Save changes to disk
385
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
386         Connection conn = DB.getConnectionRW();
387         try
388         {
389             conn.setAutoCommit(false);
390             Statement stmt = conn.createStatement();
391             // START TRANSACTION **************
392
for (int i = 0; i < oldParent.getChildCount(); i++)
393             {
394                 MTreeNode nd = (MTreeNode)oldParent.getChildAt(i);
395                 StringBuffer JavaDoc sql = new StringBuffer JavaDoc("UPDATE ");
396                 sql.append(m_nodeTableName)
397                     .append(" SET Parent_ID=").append(oldParent.getID())
398                     .append(", SeqNo=").append(i)
399                     .append(", Updated=SysDate")
400                     .append(" WHERE AD_Tree_ID=").append(m_AD_Tree_ID)
401                     .append(" AND Node_ID=").append(nd.getID());
402                 Log.trace(Log.l6_Database, "SQL", sql.toString());
403                 stmt.executeUpdate(sql.toString());
404             }
405             if (oldParent != newParent)
406                 for (int i = 0; i < newParent.getChildCount(); i++)
407                 {
408                     MTreeNode nd = (MTreeNode)newParent.getChildAt(i);
409                     StringBuffer JavaDoc sql = new StringBuffer JavaDoc("UPDATE ");
410                     sql.append(m_nodeTableName)
411                         .append(" SET Parent_ID=").append(newParent.getID())
412                         .append(", SeqNo=").append(i)
413                         .append(", Updated=SysDate")
414                         .append(" WHERE AD_Tree_ID=").append(m_AD_Tree_ID)
415                         .append(" AND Node_ID=").append(nd.getID());
416                     Log.trace(Log.l6_Database, "SQL", sql.toString());
417                     stmt.executeUpdate(sql.toString());
418                 }
419             // COMMIT *********************
420
try
421             {
422                 conn.commit(); // may throw exception if nothing to commit
423
}
424             catch (Exception JavaDoc e1)
425             {
426                 Log.error("VTreePanel.moveNode - commit", e1);
427             }
428             stmt.close();
429             conn.close();
430         }
431         catch (SQLException e)
432         {
433             try
434             {
435                 conn.rollback();
436                 conn.close();
437             }
438             catch (SQLException e2) {}
439             Log.error("VTreePanel.moveNode", e);
440             ADialog.error(m_WindowNo, this, "TreeUpdateError", e.getLocalizedMessage());
441         }
442         conn = null;
443         setCursor(Cursor.getDefaultCursor());
444         Log.trace(Log.l3_Util, "VTreePanel.moveNode complete");
445     } // moveNode
446

447
448     /*************************************************************************/
449
450     /**
451      * Enter Key
452      * @param e event
453      */

454     protected void keyPressed(KeyEvent e)
455     {
456         // *** Tree ***
457
if (e.getSource() instanceof JTree
458             || (e.getSource() == treeSearch && e.getModifiers() != 0)) // InputEvent.CTRL_MASK
459
{
460             TreePath tp = tree.getSelectionPath();
461             if (tp == null)
462                 ADialog.beep();
463             else
464             {
465                 MTreeNode tn = (MTreeNode)tp.getLastPathComponent();
466                 setSelectedNode(tn);
467             }
468         }
469
470         // *** treeSearch ***
471
else if (e.getSource() == treeSearch)
472         {
473             String JavaDoc search = treeSearch.getText();
474             boolean found = false;
475
476             // at the end - try from top
477
if (m_nodeEn != null && !m_nodeEn.hasMoreElements())
478                 m_search = "";
479
480             // this is the first time
481
if (!search.equals(m_search))
482             {
483                 // get enumeration of all nodes
484
m_nodeEn = m_root.preorderEnumeration();
485                 m_search = search;
486             }
487
488             // search the nodes
489
while(!found && m_nodeEn != null && m_nodeEn.hasMoreElements())
490             {
491                 MTreeNode nd = (MTreeNode)m_nodeEn.nextElement();
492                 // compare in upper case
493
if (nd.toString().toUpperCase().indexOf(search.toUpperCase()) != -1)
494                 {
495                     found = true;
496                     TreePath treePath = new TreePath(nd.getPath());
497                     tree.setSelectionPath(treePath);
498                     tree.makeVisible(treePath); // expand it
499
tree.scrollPathToVisible(treePath);
500                 }
501             }
502             if (!found)
503                 ADialog.beep();
504         } // treeSearch
505

506     } // keyPressed
507

508
509     /*************************************************************************/
510
511     /**
512      * Mouse clicked
513      * @param e event
514      */

515     protected void mouseClicked(MouseEvent e)
516     {
517         // *** JTree ***
518
if (e.getSource() instanceof JTree)
519         {
520             // Left Double Click
521
if (SwingUtilities.isLeftMouseButton(e)
522                 && e.getClickCount() == 2)
523             {
524                 int selRow = tree.getRowForLocation(e.getX(), e.getY());
525                 if(selRow != -1)
526                 {
527                     MTreeNode tn = (MTreeNode)tree.getPathForLocation
528                         (e.getX(), e.getY()).getLastPathComponent();
529                     setSelectedNode(tn);
530                 }
531             }
532
533             // Right Click for PopUp
534
else if ((m_editable || m_hasBar)
535                 && SwingUtilities.isRightMouseButton(e)
536                 && tree.getSelectionPath() != null) // need select first
537
{
538                 MTreeNode nd = (MTreeNode)tree.getSelectionPath().getLastPathComponent();
539             // if (nd.isLeaf()) // only leaves
540
{
541                     Rectangle r = tree.getPathBounds(tree.getSelectionPath());
542                     popMenuTree.show(tree, (int)r.getMaxX(), (int)r.getY());
543                 }
544             }
545         } // JTree
546

547         // *** JButton ***
548
else if (e.getSource() instanceof JButton)
549         {
550             if (SwingUtilities.isRightMouseButton(e))
551             {
552                 m_buttonSelected = (CButton)e.getSource();
553                 popMenuBar.show(m_buttonSelected, e.getX(), e.getY());
554             }
555         } // JButton
556

557     } // mouseClicked
558

559
560     /**
561      * Get currently selected node
562      * @return MTreeNode
563      */

564     public MTreeNode getSelectedNode()
565     {
566         return m_selectedNode;
567     } // getSelectedNode
568

569     /**
570      * Search Field
571      * @return Search Field
572      */

573     public JComponent getSearchField()
574     {
575         return treeSearch;
576     } // getSearchField
577

578     /**
579      * Set Selection to Node in Event
580      * @param nodeID Node ID
581      */

582     public void setSelectedNode (int nodeID)
583     {
584         Log.trace(Log.l3_Util, "VTreePanel.setSelectedNode - ID=" + nodeID);
585         if (nodeID != -1) // new is -1
586
selectID(nodeID, true); // show selection
587
} // setSelectedNode
588

589     /**
590      * Select ID in Tree
591      * @param nodeID Node ID
592      * @param show scroll to node
593      */

594     private void selectID (int nodeID, boolean show)
595     {
596         Log.trace(Log.l3_Util, "VTreePanel.selectID", "NodeID=" + nodeID + ", Show=" + show);
597         // try to find the node
598
MTreeNode node = m_root.findNode (nodeID);
599         if (node != null)
600         {
601             Log.trace(Log.l4_Data, node.toString());
602             TreePath treePath = new TreePath(node.getPath());
603             Log.trace(Log.l4_Data, treePath.toString());
604             tree.setSelectionPath(treePath);
605             if (show)
606             {
607                 tree.makeVisible(treePath); // expand it
608
tree.scrollPathToVisible(treePath);
609             }
610         }
611         else
612             Log.error("VTreePanel.selectID - Node not found; ID=" + nodeID);
613     } // selectID
614

615
616     /**
617      * Set the selected node & initiate all listeners
618      * @param nd node
619      */

620     private void setSelectedNode (MTreeNode nd)
621     {
622         Log.trace(Log.l3_Util, "VTreePanel.setSelectedNode = " + nd);
623         m_selectedNode = nd;
624         //
625
firePropertyChange(NODE_SELECTION, null, nd);
626     } // setSelectedNode
627

628     /*************************************************************************/
629
630     /**
631      * Node Changed - synchromize Node
632      *
633      * @param save true the node was saved (changed/added), false if the row was deleted
634      * @param keyID the ID of the row changed
635      * @param name name
636      * @param description description
637      * @param isSummary summary node
638      * @param imageIndicator image indicator
639      */

640     public void nodeChanged (boolean save, int keyID,
641         String JavaDoc name, String JavaDoc description, boolean isSummary, String JavaDoc imageIndicator)
642     {
643         Log.trace(Log.l3_Util, "VTreePanel.nodeChanged - Save=" + save + ", KeyID=" + keyID,
644             "Name=" + name + ", Description=" + description + ", IsSummary=" + isSummary + ", ImageInd=" + imageIndicator);
645         // try to find the node
646
MTreeNode node = m_root.findNode(keyID);
647
648         // Node not found and saved -> new
649
if (node == null && save)
650         {
651             node = new MTreeNode (keyID, 0, name, description,
652                 m_root.getID(), isSummary, imageIndicator, false);
653             m_root.add (node);
654         }
655
656         // Node found and saved -> change
657
else if (node != null && save)
658         {
659             node.setName (name);
660         }
661
662         // Node found and not saved -> delete
663
else if (node != null && !save)
664         {
665             MTreeNode parent = (MTreeNode)node.getParent();
666             node.removeFromParent();
667             node = parent; // select Parent
668
}
669
670         // Error
671
else
672         {
673             Log.error("VTreePanel.nodeChanged ERROR - Save=" + save + ", KeyID=" + keyID + ", Node=" + node);
674             node = null;
675         }
676
677         // Nothing to display
678
if (node == null)
679             return;
680
681         // (Re) Display Node
682
tree.updateUI();
683         TreePath treePath = new TreePath(node.getPath());
684         tree.setSelectionPath(treePath);
685         tree.makeVisible(treePath); // expand it
686
tree.scrollPathToVisible(treePath);
687     } // nodeChanged
688

689
690     /*************************************************************************/
691
692     /**
693      * ActionListener
694      * @param e event
695      */

696     public void actionPerformed(ActionEvent e)
697     {
698         // bar button pressed
699
if (e.getSource() instanceof JButton)
700         {
701             // Find Node - don't show
702
selectID(Integer.parseInt(e.getActionCommand()), false);
703             // Select it
704
MTreeNode tn = (MTreeNode)tree.getSelectionPath().getLastPathComponent();
705             setSelectedNode(tn);
706         }
707
708         // popup menu commands
709
else if (e.getSource() instanceof JMenuItem)
710         {
711             if (e.getActionCommand().equals("From"))
712                 moveFrom();
713             else if (e.getActionCommand().equals("To"))
714                 moveTo();
715             else if (e.getActionCommand().equals("BarAdd"))
716                 barAdd();
717             else if (e.getActionCommand().equals("BarRemove"))
718                 barRemove();
719         }
720
721         else if (e.getSource() instanceof JCheckBox)
722         {
723             if (e.getActionCommand().equals("Expand"))
724                 expandTree();
725         }
726     } // actionPerformed
727

728
729     /*************************************************************************/
730
731     /**
732      * Copy Node into buffer
733      */

734     private void moveFrom()
735     {
736         m_moveNode = (MTreeNode)tree.getSelectionPath().getLastPathComponent();
737         if (m_moveNode != null)
738             mTo.setEnabled(true); // enable menu
739
} // mFrom_actionPerformed
740

741     /**
742      * Move Node
743      */

744     private void moveTo()
745     {
746         mFrom.setEnabled(true);
747         mTo.setEnabled(false);
748         if (m_moveNode == null)
749             return;
750
751         MTreeNode toNode = (MTreeNode)tree.getSelectionPath().getLastPathComponent();
752         moveNode(m_moveNode, toNode);
753         // cleanup
754
m_moveNode = null;
755     } // mTo_actionPerformed
756

757     /**
758      * Add selected TreeNode to Bar
759      */

760     private void barAdd()
761     {
762         MTreeNode nd = (MTreeNode)tree.getSelectionPath().getLastPathComponent();
763         addToBar(nd);
764         barDBupdate(true, nd.getID());
765     } // barAdd
766

767     /**
768      * Add TreeNode to Bar
769      * @param nd node
770      */

771     private void addToBar(MTreeNode nd)
772     {
773         // Only first word of Label
774
String JavaDoc label = nd.toString().trim();
775         int space = label.indexOf(" ");
776     // if (space != -1)
777
// label = label.substring(0, space);
778

779         CButton button = new CButton(label); // Create the button
780
button.setToolTipText(nd.getDescription());
781         button.setActionCommand(String.valueOf(nd.getID()));
782         //
783
button.setMargin(new Insets(0, 0, 0, 0));
784         button.setIcon(nd.getIcon());
785         button.setBorderPainted(false);
786     // button.setFocusPainted(false);
787
button.setRequestFocusEnabled(false);
788         //
789
button.addActionListener(this);
790         button.addMouseListener(mouseListener);
791         //
792
bar.add(button);
793         bar.validate();
794         if (centerSplitPane.getDividerLocation() == -1)
795             centerSplitPane.setDividerLocation(button.getPreferredSize().width);
796         bar.repaint();
797     } // addToBar
798

799     /**
800      * Remove from Bar
801      */

802     private void barRemove()
803     {
804         bar.remove(m_buttonSelected);
805         bar.validate();
806         bar.repaint();
807         barDBupdate(false, Integer.parseInt(m_buttonSelected.getActionCommand()));
808     } // barRemove
809

810     /**
811      * Make Bar add/remove persistent
812      * @param add true if add - otherwise remove
813      * @param Node_ID Node ID
814      */

815     private void barDBupdate (boolean add, int Node_ID)
816     {
817         int AD_Client_ID = Env.getAD_Client_ID(Env.getCtx());
818         int AD_Org_ID = Env.getContextAsInt(Env.getCtx(), "#AD_Org_ID");
819         int AD_User_ID = Env.getContextAsInt(Env.getCtx(), "#AD_User_ID");
820         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
821         if (add)
822             sql.append("INSERT INTO AD_TreeBar "
823                 + "(AD_Tree_ID,AD_User_ID,Node_ID, "
824                 + "AD_Client_ID,AD_Org_ID, "
825                 + "IsActive,Created,CreatedBy,Updated,UpdatedBy)VALUES (")
826                 .append(m_AD_Tree_ID).append(",").append(AD_User_ID).append(",").append(Node_ID).append(",")
827                 .append(AD_Client_ID).append(",").append(AD_Org_ID).append(",")
828                 .append("'Y',SysDate,").append(AD_User_ID).append(",SysDate,").append(AD_User_ID).append(")");
829             // if already exist, will result in ORA-00001: unique constraint (COMPIERE.AD_TREEBAR_KEY)
830
else
831             sql.append("DELETE AD_TreeBar WHERE AD_Tree_ID=").append(m_AD_Tree_ID)
832                 .append(" AND AD_User_ID=").append(AD_User_ID)
833                 .append(" AND Node_ID=").append(Node_ID);
834         int no = DB.executeUpdate(sql.toString());
835     } // barDBupdate
836

837
838     /**
839      * Clicked on Expand All
840      */

841     private void expandTree()
842     {
843         if (treeExpand.isSelected())
844         {
845             for (int row = 0; row < tree.getRowCount(); row++)
846                 tree.expandRow(row);
847         }
848         else
849         {
850             for (int row = 0; row < tree.getRowCount(); row++)
851                 tree.collapseRow(row);
852         }
853     } // expandTree
854

855 } // VTreePanel
856

857 /*****************************************************************************/
858
859 /**
860  * Mouse Clicked
861  */

862 class VTreePanel_mouseAdapter extends java.awt.event.MouseAdapter JavaDoc
863 {
864     VTreePanel adaptee;
865
866     VTreePanel_mouseAdapter(VTreePanel adaptee)
867     {
868         this.adaptee = adaptee;
869     }
870
871     public void mouseClicked(MouseEvent e)
872     {
873         adaptee.mouseClicked(e);
874     }
875 } // VTreePanel_mouseAdapter
876

877 /**
878  * Key Pressed
879  */

880 class VTreePanel_keyAdapter extends java.awt.event.KeyAdapter JavaDoc
881 {
882     VTreePanel adaptee;
883
884     VTreePanel_keyAdapter(VTreePanel adaptee)
885     {
886         this.adaptee = adaptee;
887     }
888
889     public void keyPressed(KeyEvent e)
890     {
891         if (e.getKeyCode() == KeyEvent.VK_ENTER)
892             adaptee.keyPressed(e);
893     }
894 } // VTreePanel_keyAdapter
895
Popular Tags