NavigatorPane
This class is tasked with presenting shapes in a hierarchical tree.
Contents
classdef NavigatorPane < handle
% This defines the NavigatorPane class. % URL : $URL: $ % Log : $Id: NavigatorPane.html,v 1.1 2008/07/23 12:51:19 jberg Exp $ % Copyright (c) 2008 The MathWorks, Inc.
Class Properties
properties ( Dependent = true )
selected_node_string; % (char)
end
properties ( SetAccess = 'private', GetAccess = 'public' )
pane; % (javax.swing.JPanel)
end
properties ( SetAccess = 'private', GetAccess = 'private' )
bodyBuilder; % (BodyBuilder)
end
methods
Constructor
function obj = NavigatorPane(bodyBuilder) % Load function handles fh = svSupport; % Build the Gui and set pane obj.pane = obj.build_gui(fh); % Set bodyBuilder obj.bodyBuilder = bodyBuilder; end
SET Methods
function set.bodyBuilder(obj, in) prop_name = 'bodyBuilder'; prop_type = 'BodyBuilder'; [valid_flag in] = Validate.SCALAR(in,'type',prop_type); if valid_flag obj.(prop_name) = in; end end function set.pane(obj, in) prop_name = 'pane'; % prop_type = 'javax.swing.JPanel'; prop_type = 'javax.swing.JScrollPane'; [valid_flag in] = Validate.SCALAR(in,'type',prop_type); if valid_flag obj.(prop_name) = in; end end
Public GET Methods
function the_parent = get.selected_node_string(obj) % This method returns the highlighted node's String. If there is no % tree, then return empty if obj.pane.getComponentCount && obj.bodyBuilder.tree.getSelectionCount the_parent = obj.bodyBuilder.tree.getSelectionPath.toString(); the_parent = the_parent.toCharArray(); the_parent = the_parent(2:end-1); else the_parent = ''; end end
Public HELPER Methods
function add_shape_to_navigatorPane(obj, the_shape, parent_path) % This method adds a shape name to the navigator tree. If the % shape is a ComplexShape, then we need to check for children % to complete the additions to the tree. This requires % recursion. the_tree = obj.bodyBuilder.tree; the_model = the_tree.getModel; % Create child node child_node = javaObjectEDT('javax.swing.tree.DefaultMutableTreeNode',the_shape.name); if isempty(parent_path) % Get where in the tree we are pointing parent_path = the_tree.getSelectionPath(); % TreePath if isempty(parent_path) % Nothing Selected. I.e., there is nothing in the model % the_shape is at the top level (i.e., body). % Therefore, the_shape define's the tree's top node (root). the_model.setRoot(child_node); % Now select the top node. This may not keep if the GUI is not % ready. Has to do with returing out cb_tree_ValueChangedCallback % because one or more are not ready: the_details_pane, the_shape, % the_materials, or ui. the_tree.setSelectionPath(the_tree.getPathForRow(0)); else
% Add child_node to parent_path
parent_node = parent_path.getLastPathComponent();
idx = parent_node.getChildCount();
On the first time thru, the next line of code results in:
A Swing component is being accessed on a thread (main) other than the Event Dispatch Thread. Please update this code to use the correct thread (e.g., via java.awt.EventQueue.invokeLater() or javaObjectEDT, javaMethodEDT from M). Component: javax.swing.JTree[,26,20,77x16,alignmentX=0.0,alignmentY=0.0,border=,flags=16777576,maximumSize=,minimumSize=,preferredSize=,editable=true,invokesStopCellEditing=false,largeModel=false,rootVisible=true,rowHeight=16,scrollsOnExpand=true,showsRootHandles=false,toggleClickCount=2,visibleRowCount=20] at java.awt.Component.repaint(Unknown Source) at javax.swing.JTree.treeDidChange(Unknown Source) at javax.swing.plaf.basic.BasicTreeUI.updateSize(Unknown Source) at javax.swing.plaf.basic.BasicTreeUI$Handler.treeNodesInserted(Unknown Source) at javax.swing.tree.DefaultTreeModel.fireTreeNodesInserted(Unknown Source) at javax.swing.tree.DefaultTreeModel.nodesWereInserted(Unknown Source) at javax.swing.tree.DefaultTreeModel.insertNodeInto(Unknown Source)
the_model.insertNodeInto(child_node,parent_node,idx);
end else % TBD. Don't like having this a second time. % Add child_node to parent_path parent_node = parent_path.getLastPathComponent(); idx = parent_node(1).getChildCount(); the_model.insertNodeInto(child_node,parent_node(1),idx); end % If the_shape is a ComplexShape, check its children if isa(the_shape,'ComplexShape') the_children = the_shape.children; n_children = length(the_children); for ii = 1:n_children tp = javaObjectEDT('javax.swing.tree.TreePath',child_node.getPath()); obj.add_shape_to_navigatorPane(the_children{ii}, tp); end end end function clear_tree(obj) the_tree = obj.bodyBuilder.tree; the_tree.getModel.setRoot([]); end
end methods ( Access = 'private' )
Private HELPER Methods
function pane = build_gui(obj,fh) % ============================================================== % NAVIGATOR_PANE CONSISTS OF: % 1. Tree (Shape Hierarchy) % Creating the Tree with the Body name (default) rootNode = javaObjectEDT('javax.swing.tree.DefaultMutableTreeNode',''); treeModel = javaObjectEDT('javax.swing.tree.DefaultTreeModel',rootNode); tree = javaObjectEDT('javax.swing.JTree',treeModel); tree.setEditable(true); tree.getSelectionModel.setSelectionMode(javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION); tree.setShowsRootHandles(true); set(handle(tree.getModel, 'CallbackProperties'),'TreeNodesChangedCallback', ... {fh.tree_model_TreeNodesChangedCallback,obj,tree}); set(handle(tree, 'CallbackProperties'),'ValueChangedCallback', ... {fh.tree_ValueChangedCallback,obj}); set(handle(tree, 'CallbackProperties'),'MouseClickedCallback',{fh.tree_MouseClickedCallback,obj}); % ============================================================== % NAVIGATOR_PANE % pane = javaObjectEDT('javax.swing.JPanel',java.awt.GridBagLayout); % pane.setMinimumSize(javaObjectEDT('java.awt.Dimension',300,300)); % pane.setBorder(javax.swing.BorderFactory.createTitledBorder('Shape Hierarchy')); % % gb = javaObjectEDT('java.awt.GridBagConstraints'); % gb.anchor = gb.PAGE_START; % gb.weighty = 1; % % tree.setBackground(pane.getBackground); % pane.add(tree, gb); % tree.setBackground(pane.getBackground); pane = javaObjectEDT('javax.swing.JScrollPane',tree); pane.setMinimumSize(javaObjectEDT('java.awt.Dimension',300,300)); pane.setBorder(javax.swing.BorderFactory.createTitledBorder('Shape Hierarchy')); % tree.setBackground(scrollPane.getBackground); tree.setBackground(pane.getBackground); end
end
end