Java Code Examples for javax.swing.JTree#addTreeSelectionListener()

The following examples show how to use javax.swing.JTree#addTreeSelectionListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppleDiskTab.java    From DiskBrowser with GNU General Public License v3.0 6 votes vote down vote up
private void setSelectionListener (JTree tree)
// ---------------------------------------------------------------------------------//
{
  tree.addTreeSelectionListener (new TreeSelectionListener ()
  {
    @Override
    public void valueChanged (TreeSelectionEvent e)
    {
      // A null happens when there is a click in the DiskLayoutPanel, in order
      // to turn off the currently selected file
      AppleFileSource afs = (AppleFileSource) getSelectedObject ();
      if (afs != null)
        eventHandler.fireFileSelectionEvent (afs);
    }
  });
}
 
Example 2
Source File: PackagesBrowserPanel.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
private void initGUI() {
	setLayout(new BorderLayout(0, 0));

	model = new DefaultTreeModel(new DefaultMutableTreeNode("Packaging"));
	panelDraw = new ZoomableJPanel() ;
	if(view) {
		add(panelDraw, BorderLayout.CENTER);
	}
	
	JPanel panel = new JPanel();
	panel.setLayout(new BorderLayout(0, 0));
	if(!view)
	{
		add(panel, BorderLayout.CENTER);
	}
	else
	{
		add(panel, BorderLayout.WEST);	
	}
	
	tree = new JTree(model);
	tree.setCellRenderer(new MagicCardsTreeCellRenderer());

	panel.add(new JScrollPane(tree),BorderLayout.CENTER);
	
	
	
	tree.addTreeSelectionListener(e-> {
			DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
			
			if(selectedNode!=null && (selectedNode.getUserObject() instanceof Packaging))
				load((Packaging)selectedNode.getUserObject());
		});
}
 
Example 3
Source File: GroupingConfigurationPanel.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
public GroupingConfigurationPanel(JTree plotConfigurationTree, PlotInstance plotIntance, PlotDimension dimension) {
	super(plotIntance);
	this.setLayout(new CardLayout());

	this.dimension = dimension;
	createComponents(dimension);
	registerAsPlotConfigurationListener();
	plotConfigurationTree.addTreeSelectionListener(this);

	adaptGUI();

}
 
Example 4
Source File: TDA.java    From tda with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * add a tree listener for enabling/disabling menu and toolbar icons.
 * @param tree
 */
private void addTreeListener(JTree tree) {
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        ViewScrollPane emptyView = null;

        public void valueChanged(TreeSelectionEvent e) {
            getMainMenu().getCloseMenuItem().setEnabled(e.getPath() != null);
            if(getMainMenu().getCloseToolBarButton() != null) {
                getMainMenu().getCloseToolBarButton().setEnabled(e.getPath() != null);
            }
            // reset right pane of the top view:
            
            if(emptyView == null) {
                JEditorPane emptyPane = new JEditorPane("text/html", "<html><body bgcolor=\"ffffff\">   </body></html>");
                emptyPane.setEditable(false);

                emptyView = new ViewScrollPane(emptyPane, runningAsVisualVMPlugin);                    
            }

            if(e.getPath() == null || 
                    !(((DefaultMutableTreeNode) e.getPath().getLastPathComponent()).
                    getUserObject() instanceof Category)) {
                resetPane();
            }
        }
        
        private void resetPane() {
            int dividerLocation = topSplitPane.getDividerLocation();
            topSplitPane.setRightComponent(emptyView);
            topSplitPane.setDividerLocation(dividerLocation);
        }
        
    });
}
 
Example 5
Source File: MethodsCountPanel.java    From android-classyshark with Apache License 2.0 5 votes vote down vote up
private void setup() {
    this.setLayout(new BorderLayout());
    treeModel = new DefaultTreeModel(new DefaultMutableTreeNode(null));
    jTree = new JTree(treeModel);
    jTree.setRootVisible(false);
    jTree.setCellRenderer(new CellRenderer());
    theme.applyTo(jTree);

    DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer) jTree.getCellRenderer();

    cellRenderer.setFont(new Font("Monospaced", Font.PLAIN, 20));
    jTree.setCellRenderer(cellRenderer);
    jTree.addTreeSelectionListener(new TreeSelectionListener() {
        @Override
        public void valueChanged(TreeSelectionEvent e) {
            Object selection = jTree.getLastSelectedPathComponent();
            DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode)selection;
            ClassNode node = (ClassNode) defaultMutableTreeNode.getUserObject();
            viewerController.onSelectedMethodCount(node);
        }
    });

    JScrollPane jScrollPane = new JScrollPane(jTree);
    this.setBorder(new EmptyBorder(0,0,0,0));
    this.add(jScrollPane, BorderLayout.CENTER);
    theme.applyTo(jScrollPane);

    jTree.setDragEnabled(true);
    jTree.setTransferHandler(new FileTransferHandler(viewerController));
}
 
Example 6
Source File: TreeView.java    From RobotBuilder with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public TreeView(Palette palette) {
    this.palette = palette;

    tree = new JTree(palette.getPaletteModel()) {
        @Override
        public String getToolTipText(MouseEvent e) {
            try {
                TreePath path = getClosestPathForLocation(e.getX(), e.getY());
                return ((PaletteComponent) ((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject()).getHelp();
            } catch (ClassCastException ex) { // Ignore folders
                return null;
            }
        }
    };
    tree.setRootVisible(false);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setTransferHandler(new PaletteTreeTransferHandler(tree.getTransferHandler()));
    tree.setDragEnabled(true);
    ToolTipManager.sharedInstance().registerComponent(tree);
    tree.addTreeSelectionListener(this);

    for (int i = 0; i < tree.getRowCount(); i++) {
        tree.expandRow(i);
    }
    setLayout(new BorderLayout());
    add(tree, BorderLayout.CENTER);
}
 
Example 7
Source File: ObjectSelector.java    From jaamsim with Apache License 2.0 5 votes vote down vote up
public ObjectSelector() {
	super( "Object Selector" );
	setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	addWindowListener(FrameBox.getCloseListener("ShowObjectSelector"));
	addWindowFocusListener(new MyFocusListener());

	top = new DefaultMutableTreeNode();
	treeModel = new DefaultTreeModel(top);
	tree = new JTree();
	tree.setModel(treeModel);
	tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
	tree.setRootVisible(false);
	tree.setShowsRootHandles(true);
	tree.setInvokesStopCellEditing(true);

	treeView = new JScrollPane(tree);
	getContentPane().add(treeView);

	entSequence = 0;

	addComponentListener(FrameBox.getSizePosAdapter(this, "ObjectSelectorSize", "ObjectSelectorPos"));

	tree.addTreeSelectionListener( new MyTreeSelectionListener() );
	treeModel.addTreeModelListener( new MyTreeModelListener(tree) );

	tree.addMouseListener(new MyMouseListener());
	tree.addKeyListener(new MyKeyListener());
}
 
Example 8
Source File: GUIBrowser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void addWindowTab() {
    if (compNode instanceof WindowNode
            && ((WindowNode) compNode).getWindows().length > 0) {
        winTree = new JTree(((WindowNode) compNode).getWindowModel());
        winTree.setCellRenderer(renderer);
        winTree.setEditable(false);
        winTree.addTreeSelectionListener(selManager);
        winSplit = createUnderPane(winTree);
        tbd.add(WINDOWS_TAB, winSplit);
    }

}
 
Example 9
Source File: EntityTreePanel.java    From Robot-Overlord-App with GNU General Public License v2.0 5 votes vote down vote up
/**
    * list all entities in the world.  Double click an item to get its panel.
    * See https://docs.oracle.com/javase/7/docs/api/javax/swing/JTree.html
    */
public void updateEntityTree() {
	// list all objects in scene
    DefaultMutableTreeNode newTop = createTreeNodes(ro);
	JTree newTree = new JTree(newTop);

    newTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    newTree.setShowsRootHandles(true);
    newTree.addTreeSelectionListener(this);
	//tree.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

	if(oldTree!=null) {
		// preserve the original expansions
		ArrayList<TreePath> expanded = new ArrayList<TreePath>();
		for(int i=0;i<oldTree.getRowCount();++i) {
			if(oldTree.isExpanded(i)) {
				expanded.add(oldTree.getPathForRow(i));
			}
		}
		// restore the expanded paths
		for(TreePath p : expanded) {
			newTree.expandPath(p);
		}
		// restore the selected paths
		TreePath[] paths = oldTree.getSelectionPaths();
		newTree.setSelectionPaths(paths);
	}
	
	if(!newTree.equals(oldTree)) {
		scroll.setViewportView(newTree);
		oldTree=newTree;
		oldTop =newTop;
	}
}
 
Example 10
Source File: ManagementModel.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private JTree makeTree(ManagementModelNode root) {
    root.explore();
    JTree tree = new CommandBuilderTree(cliGuiCtx, new DefaultTreeModel(root));
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.addTreeExpansionListener(new ManagementTreeExpansionListener((DefaultTreeModel) tree.getModel()));
    tree.addTreeSelectionListener(new ManagementTreeSelectionListener());
    tree.addMouseListener(new ManagementTreeMouseListener(tree));
    return tree;
}
 
Example 11
Source File: ProcessInfoDialog.java    From nmonvisualizer with Apache License 2.0 5 votes vote down vote up
public void linkToTree(JTree tree) {
    if (this.tree != null) {
        this.tree.removeTreeSelectionListener(treeListener);
    }

    this.tree = tree;

    if (tree != null) {
        tree.addTreeSelectionListener(treeListener);
        followTree.setSelected(true);
    }
    else {
        followTree.setSelected(false);
    }
}
 
Example 12
Source File: ElementTreePanel.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public ElementTreePanel(JTextComponent editor) {
    this.editor = editor;

    Document document = editor.getDocument();

    // Create the tree.
    treeModel = new ElementTreeModel(document);
    tree = new JTree(treeModel) {

        @Override
        public String convertValueToText(Object value, boolean selected,
                boolean expanded, boolean leaf,
                int row, boolean hasFocus) {
            // Should only happen for the root
            if (!(value instanceof Element)) {
                return value.toString();
            }

            Element e = (Element) value;
            AttributeSet as = e.getAttributes().copyAttributes();
            String asString;

            if (as != null) {
                StringBuilder retBuffer = new StringBuilder("[");
                Enumeration names = as.getAttributeNames();

                while (names.hasMoreElements()) {
                    Object nextName = names.nextElement();

                    if (nextName != StyleConstants.ResolveAttribute) {
                        retBuffer.append(" ");
                        retBuffer.append(nextName);
                        retBuffer.append("=");
                        retBuffer.append(as.getAttribute(nextName));
                    }
                }
                retBuffer.append(" ]");
                asString = retBuffer.toString();
            } else {
                asString = "[ ]";
            }

            if (e.isLeaf()) {
                return e.getName() + " [" + e.getStartOffset() + ", " + e.
                        getEndOffset() + "] Attributes: " + asString;
            }
            return e.getName() + " [" + e.getStartOffset() + ", " + e.
                    getEndOffset() + "] Attributes: " + asString;
        }
    };
    tree.addTreeSelectionListener(this);
    tree.setDragEnabled(true);
    // Don't show the root, it is fake.
    tree.setRootVisible(false);
    // Since the display value of every node after the insertion point
    // changes every time the text changes and we don't generate a change
    // event for all those nodes the display value can become off.
    // This can be seen as '...' instead of the complete string value.
    // This is a temporary workaround, increase the needed size by 15,
    // hoping that will be enough.
    tree.setCellRenderer(new DefaultTreeCellRenderer() {

        @Override
        public Dimension getPreferredSize() {
            Dimension retValue = super.getPreferredSize();
            if (retValue != null) {
                retValue.width += 15;
            }
            return retValue;
        }
    });
    // become a listener on the document to update the tree.
    document.addDocumentListener(this);

    // become a PropertyChangeListener to know when the Document has
    // changed.
    editor.addPropertyChangeListener(this);

    // Become a CaretListener
    editor.addCaretListener(this);

    // configure the panel and frame containing it.
    setLayout(new BorderLayout());
    add(new JScrollPane(tree), BorderLayout.CENTER);

    // Add a label above tree to describe what is being shown
    JLabel label = new JLabel("Elements that make up the current document",
            SwingConstants.CENTER);

    label.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label, BorderLayout.NORTH);

    setPreferredSize(new Dimension(400, 400));
}
 
Example 13
Source File: ElementTreePanel.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public ElementTreePanel(JTextComponent editor) {
    this.editor = editor;

    Document document = editor.getDocument();

    // Create the tree.
    treeModel = new ElementTreeModel(document);
    tree = new JTree(treeModel) {

        @Override
        public String convertValueToText(Object value, boolean selected,
                boolean expanded, boolean leaf,
                int row, boolean hasFocus) {
            // Should only happen for the root
            if (!(value instanceof Element)) {
                return value.toString();
            }

            Element e = (Element) value;
            AttributeSet as = e.getAttributes().copyAttributes();
            String asString;

            if (as != null) {
                StringBuilder retBuffer = new StringBuilder("[");
                Enumeration names = as.getAttributeNames();

                while (names.hasMoreElements()) {
                    Object nextName = names.nextElement();

                    if (nextName != StyleConstants.ResolveAttribute) {
                        retBuffer.append(" ");
                        retBuffer.append(nextName);
                        retBuffer.append("=");
                        retBuffer.append(as.getAttribute(nextName));
                    }
                }
                retBuffer.append(" ]");
                asString = retBuffer.toString();
            } else {
                asString = "[ ]";
            }

            if (e.isLeaf()) {
                return e.getName() + " [" + e.getStartOffset() + ", " + e.
                        getEndOffset() + "] Attributes: " + asString;
            }
            return e.getName() + " [" + e.getStartOffset() + ", " + e.
                    getEndOffset() + "] Attributes: " + asString;
        }
    };
    tree.addTreeSelectionListener(this);
    tree.setDragEnabled(true);
    // Don't show the root, it is fake.
    tree.setRootVisible(false);
    // Since the display value of every node after the insertion point
    // changes every time the text changes and we don't generate a change
    // event for all those nodes the display value can become off.
    // This can be seen as '...' instead of the complete string value.
    // This is a temporary workaround, increase the needed size by 15,
    // hoping that will be enough.
    tree.setCellRenderer(new DefaultTreeCellRenderer() {

        @Override
        public Dimension getPreferredSize() {
            Dimension retValue = super.getPreferredSize();
            if (retValue != null) {
                retValue.width += 15;
            }
            return retValue;
        }
    });
    // become a listener on the document to update the tree.
    document.addDocumentListener(this);

    // become a PropertyChangeListener to know when the Document has
    // changed.
    editor.addPropertyChangeListener(this);

    // Become a CaretListener
    editor.addCaretListener(this);

    // configure the panel and frame containing it.
    setLayout(new BorderLayout());
    add(new JScrollPane(tree), BorderLayout.CENTER);

    // Add a label above tree to describe what is being shown
    JLabel label = new JLabel("Elements that make up the current document",
            SwingConstants.CENTER);

    label.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label, BorderLayout.NORTH);

    setPreferredSize(new Dimension(400, 400));
}
 
Example 14
Source File: ElementTreePanel.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public ElementTreePanel(JTextComponent editor) {
    this.editor = editor;

    Document document = editor.getDocument();

    // Create the tree.
    treeModel = new ElementTreeModel(document);
    tree = new JTree(treeModel) {

        @Override
        public String convertValueToText(Object value, boolean selected,
                boolean expanded, boolean leaf,
                int row, boolean hasFocus) {
            // Should only happen for the root
            if (!(value instanceof Element)) {
                return value.toString();
            }

            Element e = (Element) value;
            AttributeSet as = e.getAttributes().copyAttributes();
            String asString;

            if (as != null) {
                StringBuilder retBuffer = new StringBuilder("[");
                Enumeration names = as.getAttributeNames();

                while (names.hasMoreElements()) {
                    Object nextName = names.nextElement();

                    if (nextName != StyleConstants.ResolveAttribute) {
                        retBuffer.append(" ");
                        retBuffer.append(nextName);
                        retBuffer.append("=");
                        retBuffer.append(as.getAttribute(nextName));
                    }
                }
                retBuffer.append(" ]");
                asString = retBuffer.toString();
            } else {
                asString = "[ ]";
            }

            if (e.isLeaf()) {
                return e.getName() + " [" + e.getStartOffset() + ", " + e.
                        getEndOffset() + "] Attributes: " + asString;
            }
            return e.getName() + " [" + e.getStartOffset() + ", " + e.
                    getEndOffset() + "] Attributes: " + asString;
        }
    };
    tree.addTreeSelectionListener(this);
    tree.setDragEnabled(true);
    // Don't show the root, it is fake.
    tree.setRootVisible(false);
    // Since the display value of every node after the insertion point
    // changes every time the text changes and we don't generate a change
    // event for all those nodes the display value can become off.
    // This can be seen as '...' instead of the complete string value.
    // This is a temporary workaround, increase the needed size by 15,
    // hoping that will be enough.
    tree.setCellRenderer(new DefaultTreeCellRenderer() {

        @Override
        public Dimension getPreferredSize() {
            Dimension retValue = super.getPreferredSize();
            if (retValue != null) {
                retValue.width += 15;
            }
            return retValue;
        }
    });
    // become a listener on the document to update the tree.
    document.addDocumentListener(this);

    // become a PropertyChangeListener to know when the Document has
    // changed.
    editor.addPropertyChangeListener(this);

    // Become a CaretListener
    editor.addCaretListener(this);

    // configure the panel and frame containing it.
    setLayout(new BorderLayout());
    add(new JScrollPane(tree), BorderLayout.CENTER);

    // Add a label above tree to describe what is being shown
    JLabel label = new JLabel("Elements that make up the current document",
            SwingConstants.CENTER);

    label.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label, BorderLayout.NORTH);

    setPreferredSize(new Dimension(400, 400));
}
 
Example 15
Source File: BookBrowser.java    From audiveris with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Creates a new {@code BookBrowser} object.
 *
 * @param book the related book
 */
public BookBrowser (Book book)
{
    this.book = book;

    component = new JPanel();

    // Set up the tree
    model = new Model(book);

    ///model.addTreeModelListener(new ModelListener()); // Debug
    /** The tree entity */
    JTree tree = new JTree(model);

    // Build left-side view
    JScrollPane treeView = new JScrollPane(tree);

    // Build right-side view
    htmlPane = new JEditorPane("text/html", "");
    htmlPane.setEditable(false);

    JScrollPane htmlView = new JScrollPane(htmlPane);

    // Allow only single selections
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    // Display lines to explicit relationships
    tree.putClientProperty("JTree.lineStyle", "Angled");

    // Wire the two views together. Use a selection listener
    // created with an anonymous inner-class adapter.
    // Listen for when the selection changes.
    tree.addTreeSelectionListener(new SelectionListener());

    // To be notified of expansion / collapse actions (debug ...)
    ///tree.addTreeExpansionListener(new ExpansionListener());
    // Build split-pane view
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeView, htmlView);
    splitPane.setName("treeHtmlSplitPane");
    splitPane.setContinuousLayout(true);
    splitPane.setBorder(null);
    splitPane.setDividerSize(2);

    // Add GUI components
    component.setLayout(new BorderLayout());
    component.add("Center", splitPane);
}
 
Example 16
Source File: ElementTreePanel.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public ElementTreePanel(JTextComponent editor) {
    this.editor = editor;

    Document document = editor.getDocument();

    // Create the tree.
    treeModel = new ElementTreeModel(document);
    tree = new JTree(treeModel) {

        @Override
        public String convertValueToText(Object value, boolean selected,
                boolean expanded, boolean leaf,
                int row, boolean hasFocus) {
            // Should only happen for the root
            if (!(value instanceof Element)) {
                return value.toString();
            }

            Element e = (Element) value;
            AttributeSet as = e.getAttributes().copyAttributes();
            String asString;

            if (as != null) {
                StringBuilder retBuffer = new StringBuilder("[");
                Enumeration names = as.getAttributeNames();

                while (names.hasMoreElements()) {
                    Object nextName = names.nextElement();

                    if (nextName != StyleConstants.ResolveAttribute) {
                        retBuffer.append(" ");
                        retBuffer.append(nextName);
                        retBuffer.append("=");
                        retBuffer.append(as.getAttribute(nextName));
                    }
                }
                retBuffer.append(" ]");
                asString = retBuffer.toString();
            } else {
                asString = "[ ]";
            }

            if (e.isLeaf()) {
                return e.getName() + " [" + e.getStartOffset() + ", " + e.
                        getEndOffset() + "] Attributes: " + asString;
            }
            return e.getName() + " [" + e.getStartOffset() + ", " + e.
                    getEndOffset() + "] Attributes: " + asString;
        }
    };
    tree.addTreeSelectionListener(this);
    tree.setDragEnabled(true);
    // Don't show the root, it is fake.
    tree.setRootVisible(false);
    // Since the display value of every node after the insertion point
    // changes every time the text changes and we don't generate a change
    // event for all those nodes the display value can become off.
    // This can be seen as '...' instead of the complete string value.
    // This is a temporary workaround, increase the needed size by 15,
    // hoping that will be enough.
    tree.setCellRenderer(new DefaultTreeCellRenderer() {

        @Override
        public Dimension getPreferredSize() {
            Dimension retValue = super.getPreferredSize();
            if (retValue != null) {
                retValue.width += 15;
            }
            return retValue;
        }
    });
    // become a listener on the document to update the tree.
    document.addDocumentListener(this);

    // become a PropertyChangeListener to know when the Document has
    // changed.
    editor.addPropertyChangeListener(this);

    // Become a CaretListener
    editor.addCaretListener(this);

    // configure the panel and frame containing it.
    setLayout(new BorderLayout());
    add(new JScrollPane(tree), BorderLayout.CENTER);

    // Add a label above tree to describe what is being shown
    JLabel label = new JLabel("Elements that make up the current document",
            SwingConstants.CENTER);

    label.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label, BorderLayout.NORTH);

    setPreferredSize(new Dimension(400, 400));
}
 
Example 17
Source File: ScreenInfoPanel.java    From hprof-tools with MIT License 4 votes vote down vote up
public ScreenInfoPanel(@Nonnull final TabbedInfoWindow mainWindow, @Nonnull List<Screen> screens) {
    super(new BorderLayout());

    JPopupMenu popupMenu = new JPopupMenu();
    JMenuItem item = new JMenuItem("Inspect View");
    item.addActionListener(new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            mainWindow.showInstancesListTab(rightClickedViewed.getClassName(), Collections.singletonList(rightClickedViewed.getInstance()));
        }
    });
    popupMenu.add(item);

    imagePanel = new ImagePanel();
    viewTree = new JTree(new DefaultMutableTreeNode("Loading..."));
    viewTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    viewTree.addTreeSelectionListener(this);
    viewTree.addMouseListener(new PopupListener(popupMenu));
    JScrollPane treeScroller = new JScrollPane(viewTree);

    rootPicker = new JComboBox(new Vector<Object>(screens));
    rootPicker.addItemListener(this);

    JPanel settingsPanel = new JPanel(new GridLayout(1, 2));
    showBoundsBox = new JCheckBox("Show layout bounds", true);
    showBoundsBox.addItemListener(this);
    forceAlpha = new JCheckBox("Force alpha", true);
    forceAlpha.addItemListener(this);
    settingsPanel.add(showBoundsBox);
    settingsPanel.add(forceAlpha);
    settingsPanel.setBorder(new EmptyBorder(10, 0, 0, 0));

    infoTable = new JTable();
    infoTable.setRowSelectionAllowed(false);
    infoTable.setColumnSelectionAllowed(false);
    infoTable.setCellSelectionEnabled(false);
    infoTable.setShowGrid(true);

    JPanel bottomPanel = new JPanel(new BorderLayout());
    bottomPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
    bottomPanel.add(infoTable, BorderLayout.CENTER);
    bottomPanel.add(infoTable.getTableHeader(), BorderLayout.NORTH);
    bottomPanel.add(settingsPanel, BorderLayout.SOUTH);

    JPanel leftPanel = new JPanel(new BorderLayout());
    leftPanel.add(rootPicker, BorderLayout.NORTH);
    leftPanel.add(treeScroller, BorderLayout.CENTER);
    leftPanel.add(bottomPanel, BorderLayout.SOUTH);

    // Split pane for the tree and image views
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.setLeftComponent(leftPanel);
    splitPane.setRightComponent(imagePanel);
    add(splitPane, BorderLayout.CENTER);
    selectedScreen = screens.get(0);
    update();
}
 
Example 18
Source File: TreeDemo.java    From OpenDA with GNU Lesser General Public License v3.0 4 votes vote down vote up
public TreeDemo() {
    super(new GridLayout(1,0));

    //Create the nodes.
    DefaultMutableTreeNode top =
        new DefaultMutableTreeNode("The Java Series");
    createNodes(top);

    //Create a tree that allows one selection at a time.
    tree = new JTree(top);
    tree.getSelectionModel().setSelectionMode
            (TreeSelectionModel.SINGLE_TREE_SELECTION);

    //Listen for when the selection changes.
    tree.addTreeSelectionListener(this);

    if (playWithLineStyle) {
        System.out.println("line style = " + lineStyle);
        tree.putClientProperty("JTree.lineStyle", lineStyle);
    }

    //Create the scroll pane and add the tree to it. 
    JScrollPane treeView = new JScrollPane(tree);

    //Create the HTML viewing pane.
    htmlPane = new JEditorPane();
    htmlPane.setEditable(false);
    initHelp();
    JScrollPane htmlView = new JScrollPane(htmlPane);

    //Add the scroll panes to a split pane.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setTopComponent(treeView);
    splitPane.setBottomComponent(htmlView);

    Dimension minimumSize = new Dimension(100, 50);
    htmlView.setMinimumSize(minimumSize);
    treeView.setMinimumSize(minimumSize);
    splitPane.setDividerLocation(100); 
    splitPane.setPreferredSize(new Dimension(500, 300));

    //Add the split pane to this panel.
    add(splitPane);
}
 
Example 19
Source File: ElementTreePanel.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public ElementTreePanel(JTextComponent editor) {
    this.editor = editor;

    Document document = editor.getDocument();

    // Create the tree.
    treeModel = new ElementTreeModel(document);
    tree = new JTree(treeModel) {

        @Override
        public String convertValueToText(Object value, boolean selected,
                boolean expanded, boolean leaf,
                int row, boolean hasFocus) {
            // Should only happen for the root
            if (!(value instanceof Element)) {
                return value.toString();
            }

            Element e = (Element) value;
            AttributeSet as = e.getAttributes().copyAttributes();
            String asString;

            if (as != null) {
                StringBuilder retBuffer = new StringBuilder("[");
                Enumeration names = as.getAttributeNames();

                while (names.hasMoreElements()) {
                    Object nextName = names.nextElement();

                    if (nextName != StyleConstants.ResolveAttribute) {
                        retBuffer.append(" ");
                        retBuffer.append(nextName);
                        retBuffer.append("=");
                        retBuffer.append(as.getAttribute(nextName));
                    }
                }
                retBuffer.append(" ]");
                asString = retBuffer.toString();
            } else {
                asString = "[ ]";
            }

            if (e.isLeaf()) {
                return e.getName() + " [" + e.getStartOffset() + ", " + e.
                        getEndOffset() + "] Attributes: " + asString;
            }
            return e.getName() + " [" + e.getStartOffset() + ", " + e.
                    getEndOffset() + "] Attributes: " + asString;
        }
    };
    tree.addTreeSelectionListener(this);
    tree.setDragEnabled(true);
    // Don't show the root, it is fake.
    tree.setRootVisible(false);
    // Since the display value of every node after the insertion point
    // changes every time the text changes and we don't generate a change
    // event for all those nodes the display value can become off.
    // This can be seen as '...' instead of the complete string value.
    // This is a temporary workaround, increase the needed size by 15,
    // hoping that will be enough.
    tree.setCellRenderer(new DefaultTreeCellRenderer() {

        @Override
        public Dimension getPreferredSize() {
            Dimension retValue = super.getPreferredSize();
            if (retValue != null) {
                retValue.width += 15;
            }
            return retValue;
        }
    });
    // become a listener on the document to update the tree.
    document.addDocumentListener(this);

    // become a PropertyChangeListener to know when the Document has
    // changed.
    editor.addPropertyChangeListener(this);

    // Become a CaretListener
    editor.addCaretListener(this);

    // configure the panel and frame containing it.
    setLayout(new BorderLayout());
    add(new JScrollPane(tree), BorderLayout.CENTER);

    // Add a label above tree to describe what is being shown
    JLabel label = new JLabel("Elements that make up the current document",
            SwingConstants.CENTER);

    label.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label, BorderLayout.NORTH);

    setPreferredSize(new Dimension(400, 400));
}
 
Example 20
Source File: ElementTreePanel.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public ElementTreePanel(JTextComponent editor) {
    this.editor = editor;

    Document document = editor.getDocument();

    // Create the tree.
    treeModel = new ElementTreeModel(document);
    tree = new JTree(treeModel) {

        @Override
        public String convertValueToText(Object value, boolean selected,
                boolean expanded, boolean leaf,
                int row, boolean hasFocus) {
            // Should only happen for the root
            if (!(value instanceof Element)) {
                return value.toString();
            }

            Element e = (Element) value;
            AttributeSet as = e.getAttributes().copyAttributes();
            String asString;

            if (as != null) {
                StringBuilder retBuffer = new StringBuilder("[");
                Enumeration names = as.getAttributeNames();

                while (names.hasMoreElements()) {
                    Object nextName = names.nextElement();

                    if (nextName != StyleConstants.ResolveAttribute) {
                        retBuffer.append(" ");
                        retBuffer.append(nextName);
                        retBuffer.append("=");
                        retBuffer.append(as.getAttribute(nextName));
                    }
                }
                retBuffer.append(" ]");
                asString = retBuffer.toString();
            } else {
                asString = "[ ]";
            }

            if (e.isLeaf()) {
                return e.getName() + " [" + e.getStartOffset() + ", " + e.
                        getEndOffset() + "] Attributes: " + asString;
            }
            return e.getName() + " [" + e.getStartOffset() + ", " + e.
                    getEndOffset() + "] Attributes: " + asString;
        }
    };
    tree.addTreeSelectionListener(this);
    tree.setDragEnabled(true);
    // Don't show the root, it is fake.
    tree.setRootVisible(false);
    // Since the display value of every node after the insertion point
    // changes every time the text changes and we don't generate a change
    // event for all those nodes the display value can become off.
    // This can be seen as '...' instead of the complete string value.
    // This is a temporary workaround, increase the needed size by 15,
    // hoping that will be enough.
    tree.setCellRenderer(new DefaultTreeCellRenderer() {

        @Override
        public Dimension getPreferredSize() {
            Dimension retValue = super.getPreferredSize();
            if (retValue != null) {
                retValue.width += 15;
            }
            return retValue;
        }
    });
    // become a listener on the document to update the tree.
    document.addDocumentListener(this);

    // become a PropertyChangeListener to know when the Document has
    // changed.
    editor.addPropertyChangeListener(this);

    // Become a CaretListener
    editor.addCaretListener(this);

    // configure the panel and frame containing it.
    setLayout(new BorderLayout());
    add(new JScrollPane(tree), BorderLayout.CENTER);

    // Add a label above tree to describe what is being shown
    JLabel label = new JLabel("Elements that make up the current document",
            SwingConstants.CENTER);

    label.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label, BorderLayout.NORTH);

    setPreferredSize(new Dimension(400, 400));
}