1 package org.oddjob.designer.view; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Dimension ; 5 import java.awt.event.MouseAdapter ; 6 import java.awt.event.MouseEvent ; 7 8 import javax.swing.JPanel ; 9 import javax.swing.JScrollPane ; 10 import javax.swing.JSplitPane ; 11 import javax.swing.JTree ; 12 import javax.swing.event.TreeModelEvent ; 13 import javax.swing.event.TreeModelListener ; 14 import javax.swing.event.TreeSelectionEvent ; 15 import javax.swing.event.TreeSelectionListener ; 16 import javax.swing.tree.TreePath ; 17 import javax.swing.tree.TreeSelectionModel ; 18 19 import org.oddjob.designer.Looks; 20 import org.oddjob.designer.actions.DCTransferHandler; 21 import org.oddjob.designer.model.DesignTreeNode; 22 import org.oddjob.designer.model.DesignerModel; 23 24 29 30 public class DesignerPanel extends JPanel { 31 32 33 private final JTree tree; 34 35 private final JScrollPane treeScroll; 36 37 private final DesignerMenuBar menuBar; 38 39 44 45 public DesignerPanel(final DesignerModel model, DesignerMenuBar menuBar) { 46 this.menuBar = menuBar; 48 49 tree = new JTree (model.getTreeModel()); 51 tree.setShowsRootHandles(true); 52 tree.setExpandsSelectedPaths(true); 53 tree.getModel().addTreeModelListener(new TreeModelListener () { 54 public void treeNodesChanged(TreeModelEvent e) { 55 } 56 public void treeNodesInserted(TreeModelEvent e) { 57 TreePath p = e.getTreePath().pathByAddingChild(e.getChildren()[0]); 58 tree.setSelectionPath(p); 59 } 60 public void treeNodesRemoved(TreeModelEvent e) { 61 tree.setSelectionPath(null); 62 } 63 public void treeStructureChanged(TreeModelEvent e) { 64 } 65 }); 66 67 tree.setDragEnabled(true); 69 DCTransferHandler transferHandler = new DCTransferHandler(model); 70 tree.setTransferHandler(transferHandler); 71 72 tree.getSelectionModel().setSelectionMode 73 (TreeSelectionModel.SINGLE_TREE_SELECTION); 74 75 tree.addTreeSelectionListener(new TreeSelectionListener () { 76 public void valueChanged(TreeSelectionEvent event) { 77 JTree tree = (JTree )event.getSource(); 79 DesignTreeNode node = (DesignTreeNode)tree.getLastSelectedPathComponent(); 80 model.setCurrentSelection(node); 81 } 82 }); 83 84 tree.addMouseListener(new PopupListener()); 85 86 setLayout(new BorderLayout ()); 87 treeScroll = new JScrollPane (); 88 treeScroll.setPreferredSize(new Dimension ( 89 Looks.DESIGNER_TREE_WIDTH, Looks.DESIGNER_HEIGHT)); 90 91 treeScroll.setViewportView(tree); 92 93 DesignerDetail dd = new DesignerDetail(); 95 dd.setPreferredSize(new Dimension ( 96 Looks.DETAIL_FORM_WIDTH, Looks.DESIGNER_HEIGHT)); 97 model.addObserver(dd); 98 99 JSplitPane split = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, 100 treeScroll, dd); 101 102 add(split); 103 104 } 105 106 class PopupListener extends MouseAdapter { 107 public void mousePressed(MouseEvent e) { 108 maybeShowPopup(e); 109 } 110 public void mouseClicked(MouseEvent e) { 111 maybeShowPopup(e); 112 } 113 public void mouseReleased(MouseEvent e) { 114 maybeShowPopup(e); 115 } 116 117 private void maybeShowPopup(MouseEvent e) { 118 if (!e.isPopupTrigger()) { 119 return; 120 } 121 TreePath path = tree.getPathForLocation(e.getX(), e.getY()); 122 tree.setSelectionPath(path); 123 if (menuBar.getPopupMenu() != null) { 124 menuBar.getPopupMenu().show(e.getComponent(), e.getX(), e.getY()); 125 } 126 } 127 } 128 129 } 130 | Popular Tags |