📄 vtreepanel.java
字号:
}
conn = null;
setCursor(Cursor.getDefaultCursor());
Log.trace(Log.l3_Util, "VTreePanel.moveNode complete");
} // moveNode
/*************************************************************************/
/**
* Enter Key
* @param e event
*/
protected void keyPressed(KeyEvent e)
{
// *** Tree ***
if (e.getSource() instanceof JTree
|| (e.getSource() == treeSearch && e.getModifiers() != 0)) // InputEvent.CTRL_MASK
{
TreePath tp = tree.getSelectionPath();
if (tp == null)
ADialog.beep();
else
{
MTreeNode tn = (MTreeNode)tp.getLastPathComponent();
setSelectedNode(tn);
}
}
// *** treeSearch ***
else if (e.getSource() == treeSearch)
{
String search = treeSearch.getText();
boolean found = false;
// at the end - try from top
if (m_nodeEn != null && !m_nodeEn.hasMoreElements())
m_search = "";
// this is the first time
if (!search.equals(m_search))
{
// get enumeration of all nodes
m_nodeEn = m_root.preorderEnumeration();
m_search = search;
}
// search the nodes
while(!found && m_nodeEn != null && m_nodeEn.hasMoreElements())
{
MTreeNode nd = (MTreeNode)m_nodeEn.nextElement();
// compare in upper case
if (nd.toString().toUpperCase().indexOf(search.toUpperCase()) != -1)
{
found = true;
TreePath treePath = new TreePath(nd.getPath());
tree.setSelectionPath(treePath);
tree.makeVisible(treePath); // expand it
tree.scrollPathToVisible(treePath);
}
}
if (!found)
ADialog.beep();
} // treeSearch
} // keyPressed
/*************************************************************************/
/**
* Mouse clicked
* @param e event
*/
protected void mouseClicked(MouseEvent e)
{
// *** JTree ***
if (e.getSource() instanceof JTree)
{
// Left Double Click
if (SwingUtilities.isLeftMouseButton(e)
&& e.getClickCount() == 2)
{
int selRow = tree.getRowForLocation(e.getX(), e.getY());
if(selRow != -1)
{
MTreeNode tn = (MTreeNode)tree.getPathForLocation
(e.getX(), e.getY()).getLastPathComponent();
setSelectedNode(tn);
}
}
// Right Click for PopUp
else if ((m_editable || m_hasBar)
&& SwingUtilities.isRightMouseButton(e)
&& tree.getSelectionPath() != null) // need select first
{
MTreeNode nd = (MTreeNode)tree.getSelectionPath().getLastPathComponent();
// if (nd.isLeaf()) // only leaves
{
Rectangle r = tree.getPathBounds(tree.getSelectionPath());
popMenuTree.show(tree, (int)r.getMaxX(), (int)r.getY());
}
}
} // JTree
// *** JButton ***
else if (e.getSource() instanceof JButton)
{
if (SwingUtilities.isRightMouseButton(e))
{
m_buttonSelected = (CButton)e.getSource();
popMenuBar.show(m_buttonSelected, e.getX(), e.getY());
}
} // JButton
} // mouseClicked
/**
* Get currently selected node
* @return MTreeNode
*/
public MTreeNode getSelectedNode()
{
return m_selectedNode;
} // getSelectedNode
/**
* Search Field
* @return Search Field
*/
public JComponent getSearchField()
{
return treeSearch;
} // getSearchField
/**
* Set Selection to Node in Event
* @param nodeID Node ID
*/
public void setSelectedNode (int nodeID)
{
Log.trace(Log.l3_Util, "VTreePanel.setSelectedNode - ID=" + nodeID);
if (nodeID != -1) // new is -1
selectID(nodeID, true); // show selection
} // setSelectedNode
/**
* Select ID in Tree
* @param nodeID Node ID
* @param show scroll to node
*/
private void selectID (int nodeID, boolean show)
{
Log.trace(Log.l3_Util, "VTreePanel.selectID", "NodeID=" + nodeID + ", Show=" + show);
// try to find the node
MTreeNode node = m_root.findNode (nodeID);
if (node != null)
{
Log.trace(Log.l4_Data, node.toString());
TreePath treePath = new TreePath(node.getPath());
Log.trace(Log.l4_Data, treePath.toString());
tree.setSelectionPath(treePath);
if (show)
{
tree.makeVisible(treePath); // expand it
tree.scrollPathToVisible(treePath);
}
}
else
Log.error("VTreePanel.selectID - Node not found; ID=" + nodeID);
} // selectID
/**
* Set the selected node & initiate all listeners
* @param nd node
*/
private void setSelectedNode (MTreeNode nd)
{
Log.trace(Log.l3_Util, "VTreePanel.setSelectedNode = " + nd);
m_selectedNode = nd;
//
firePropertyChange(NODE_SELECTION, null, nd);
} // setSelectedNode
/*************************************************************************/
/**
* Node Changed - synchromize Node
*
* @param save true the node was saved (changed/added), false if the row was deleted
* @param keyID the ID of the row changed
* @param name name
* @param description description
* @param isSummary summary node
* @param imageIndicator image indicator
*/
public void nodeChanged (boolean save, int keyID,
String name, String description, boolean isSummary, String imageIndicator)
{
Log.trace(Log.l3_Util, "VTreePanel.nodeChanged - Save=" + save + ", KeyID=" + keyID,
"Name=" + name + ", Description=" + description + ", IsSummary=" + isSummary + ", ImageInd=" + imageIndicator);
// try to find the node
MTreeNode node = m_root.findNode(keyID);
// Node not found and saved -> new
if (node == null && save)
{
node = new MTreeNode (keyID, 0, name, description,
m_root.getID(), isSummary, imageIndicator, false);
m_root.add (node);
}
// Node found and saved -> change
else if (node != null && save)
{
node.setName (name);
}
// Node found and not saved -> delete
else if (node != null && !save)
{
MTreeNode parent = (MTreeNode)node.getParent();
node.removeFromParent();
node = parent; // select Parent
}
// Error
else
{
Log.error("VTreePanel.nodeChanged ERROR - Save=" + save + ", KeyID=" + keyID + ", Node=" + node);
node = null;
}
// Nothing to display
if (node == null)
return;
// (Re) Display Node
tree.updateUI();
TreePath treePath = new TreePath(node.getPath());
tree.setSelectionPath(treePath);
tree.makeVisible(treePath); // expand it
tree.scrollPathToVisible(treePath);
} // nodeChanged
/*************************************************************************/
/**
* ActionListener
* @param e event
*/
public void actionPerformed(ActionEvent e)
{
// bar button pressed
if (e.getSource() instanceof JButton)
{
// Find Node - don't show
selectID(Integer.parseInt(e.getActionCommand()), false);
// Select it
MTreeNode tn = (MTreeNode)tree.getSelectionPath().getLastPathComponent();
setSelectedNode(tn);
}
// popup menu commands
else if (e.getSource() instanceof JMenuItem)
{
if (e.getActionCommand().equals("From"))
moveFrom();
else if (e.getActionCommand().equals("To"))
moveTo();
else if (e.getActionCommand().equals("BarAdd"))
barAdd();
else if (e.getActionCommand().equals("BarRemove"))
barRemove();
}
else if (e.getSource() instanceof JCheckBox)
{
if (e.getActionCommand().equals("Expand"))
expandTree();
}
} // actionPerformed
/*************************************************************************/
/**
* Copy Node into buffer
*/
private void moveFrom()
{
m_moveNode = (MTreeNode)tree.getSelectionPath().getLastPathComponent();
if (m_moveNode != null)
mTo.setEnabled(true); // enable menu
} // mFrom_actionPerformed
/**
* Move Node
*/
private void moveTo()
{
mFrom.setEnabled(true);
mTo.setEnabled(false);
if (m_moveNode == null)
return;
MTreeNode toNode = (MTreeNode)tree.getSelectionPath().getLastPathComponent();
moveNode(m_moveNode, toNode);
// cleanup
m_moveNode = null;
} // mTo_actionPerformed
/**
* Add selected TreeNode to Bar
*/
private void barAdd()
{
MTreeNode nd = (MTreeNode)tree.getSelectionPath().getLastPathComponent();
addToBar(nd);
barDBupdate(true, nd.getID());
} // barAdd
/**
* Add TreeNode to Bar
* @param nd node
*/
private void addToBar(MTreeNode nd)
{
// Only first word of Label
String label = nd.toString().trim();
int space = label.indexOf(" ");
// if (space != -1)
// label = label.substring(0, space);
CButton button = new CButton(label); // Create the button
button.setToolTipText(nd.getDescription());
button.setActionCommand(String.valueOf(nd.getID()));
//
button.setMargin(new Insets(0, 0, 0, 0));
button.setIcon(nd.getIcon());
button.setBorderPainted(false);
// button.setFocusPainted(false);
button.setRequestFocusEnabled(false);
//
button.addActionListener(this);
button.addMouseListener(mouseListener);
//
bar.add(button);
bar.validate();
if (centerSplitPane.getDividerLocation() == -1)
centerSplitPane.setDividerLocation(button.getPreferredSize().width);
bar.repaint();
} // addToBar
/**
* Remove from Bar
*/
private void barRemove()
{
bar.remove(m_buttonSelected);
bar.validate();
bar.repaint();
barDBupdate(false, Integer.parseInt(m_buttonSelected.getActionCommand()));
} // barRemove
/**
* Make Bar add/remove persistent
* @param add true if add - otherwise remove
* @param Node_ID Node ID
*/
private void barDBupdate (boolean add, int Node_ID)
{
StringBuffer sql = new StringBuffer();
if (add)
sql.append("INSERT INTO AD_TreeBar "
+ "(AD_Tree_ID,AD_User_ID,Node_ID, "
+ "AD_Client_ID,AD_Org_ID, "
+ "IsActive,Created,CreatedBy,Updated,UpdatedBy)VALUES (")
.append(m_AD_Tree_ID).append(",").append(m_AD_User_ID).append(",").append(Node_ID).append(",")
.append(m_AD_Client_ID).append(",").append(m_AD_Org_ID).append(",")
.append("'Y',SysDate,").append(m_AD_User_ID).append(",SysDate,").append(m_AD_User_ID).append(")");
// if already exist, will result in ORA-00001: unique constraint (COMPIERE.AD_TREEBAR_KEY)
else
sql.append("DELETE AD_TreeBar WHERE AD_Tree_ID=").append(m_AD_Tree_ID)
.append(" AND AD_User_ID=").append(m_AD_User_ID)
.append(" AND Node_ID=").append(Node_ID);
int no = DB.executeUpdate(sql.toString());
} // barDBupdate
/**
* Clicked on Expand All
*/
private void expandTree()
{
if (treeExpand.isSelected())
{
for (int row = 0; row < tree.getRowCount(); row++)
tree.expandRow(row);
}
else
{
for (int row = 0; row < tree.getRowCount(); row++)
tree.collapseRow(row);
}
} // expandTree
} // VTreePanel
/*****************************************************************************/
/**
* Mouse Clicked
*/
class VTreePanel_mouseAdapter extends java.awt.event.MouseAdapter
{
VTreePanel adaptee;
VTreePanel_mouseAdapter(VTreePanel adaptee)
{
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e)
{
adaptee.mouseClicked(e);
}
} // VTreePanel_mouseAdapter
/**
* Key Pressed
*/
class VTreePanel_keyAdapter extends java.awt.event.KeyAdapter
{
VTreePanel adaptee;
VTreePanel_keyAdapter(VTreePanel adaptee)
{
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
adaptee.keyPressed(e);
}
} // VTreePanel_keyAdapter
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -