List of usage examples for javax.swing JTree getPathBounds
public Rectangle getPathBounds(TreePath path)
Rectangle
that the specified node will be drawn into. From source file:DndTree.java
public static void main(String args[]) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel top = new JPanel(new BorderLayout()); JLabel dragLabel = new JLabel("Drag me:"); JTextField text = new JTextField(); text.setDragEnabled(true);//from w w w . ja va2 s . co m top.add(dragLabel, BorderLayout.WEST); top.add(text, BorderLayout.CENTER); f.add(top, BorderLayout.NORTH); final JTree tree = new JTree(); final DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); tree.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); return dropLocation.getPath() != null; } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); TreePath path = dropLocation.getPath(); Transferable transferable = support.getTransferable(); String transferData; try { transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor); } catch (IOException e) { return false; } catch (UnsupportedFlavorException e) { return false; } int childIndex = dropLocation.getChildIndex(); if (childIndex == -1) { childIndex = model.getChildCount(path.getLastPathComponent()); } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent(); model.insertNodeInto(newNode, parentNode, childIndex); TreePath newPath = path.pathByAddingChild(newNode); tree.makeVisible(newPath); tree.scrollRectToVisible(tree.getPathBounds(newPath)); return true; } }); JScrollPane pane = new JScrollPane(tree); f.add(pane, BorderLayout.CENTER); tree.setDropMode(DropMode.USE_SELECTION); f.setSize(300, 400); f.setVisible(true); }
From source file:DndTree.java
public static void main(String args[]) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel top = new JPanel(new BorderLayout()); JLabel dragLabel = new JLabel("Drag me:"); JTextField text = new JTextField(); text.setDragEnabled(true);/*from www .j ava 2 s . c o m*/ top.add(dragLabel, BorderLayout.WEST); top.add(text, BorderLayout.CENTER); f.add(top, BorderLayout.NORTH); final JTree tree = new JTree(); final DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); tree.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); return dropLocation.getPath() != null; } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); TreePath path = dropLocation.getPath(); Transferable transferable = support.getTransferable(); String transferData; try { transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor); } catch (IOException e) { return false; } catch (UnsupportedFlavorException e) { return false; } int childIndex = dropLocation.getChildIndex(); if (childIndex == -1) { childIndex = model.getChildCount(path.getLastPathComponent()); } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent(); model.insertNodeInto(newNode, parentNode, childIndex); TreePath newPath = path.pathByAddingChild(newNode); tree.makeVisible(newPath); tree.scrollRectToVisible(tree.getPathBounds(newPath)); return true; } }); JScrollPane pane = new JScrollPane(tree); f.add(pane, BorderLayout.CENTER); tree.setDropMode(DropMode.INSERT); f.setSize(300, 400); f.setVisible(true); }
From source file:DndTree.java
public static void main(String args[]) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel top = new JPanel(new BorderLayout()); JLabel dragLabel = new JLabel("Drag me:"); JTextField text = new JTextField(); text.setDragEnabled(true);/*from ww w. j a va2 s.c o m*/ top.add(dragLabel, BorderLayout.WEST); top.add(text, BorderLayout.CENTER); f.add(top, BorderLayout.NORTH); final JTree tree = new JTree(); final DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); tree.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); return dropLocation.getPath() != null; } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); TreePath path = dropLocation.getPath(); Transferable transferable = support.getTransferable(); String transferData; try { transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor); } catch (IOException e) { return false; } catch (UnsupportedFlavorException e) { return false; } int childIndex = dropLocation.getChildIndex(); if (childIndex == -1) { childIndex = model.getChildCount(path.getLastPathComponent()); } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent(); model.insertNodeInto(newNode, parentNode, childIndex); TreePath newPath = path.pathByAddingChild(newNode); tree.makeVisible(newPath); tree.scrollRectToVisible(tree.getPathBounds(newPath)); return true; } }); JScrollPane pane = new JScrollPane(tree); f.add(pane, BorderLayout.CENTER); tree.setDropMode(DropMode.ON_OR_INSERT); f.setSize(300, 400); f.setVisible(true); }
From source file:Main.java
@Override public boolean isCellEditable(EventObject e) { if (e instanceof MouseEvent && e.getSource() instanceof JTree) { MouseEvent me = (MouseEvent) e; JTree tree = (JTree) e.getSource(); TreePath path = tree.getPathForLocation(me.getX(), me.getY()); Rectangle r = tree.getPathBounds(path); if (r == null) { return false; }/* ww w .jav a2s. c om*/ Dimension d = check.getPreferredSize(); r.setSize(new Dimension(d.width, r.height)); if (r.contains(me.getX(), me.getY())) { check.setBounds(new Rectangle(0, 0, d.width, r.height)); return true; } } return false; }
From source file:it.unibas.spicygui.vista.listener.ScrollPaneAdjustmentListener.java
private Point findNewLocationForTree(ICaratteristicheWidget icaratteristicheWidget, JTree albero) { CaratteristicheWidgetTree caratteristicheWidget = (CaratteristicheWidgetTree) icaratteristicheWidget; Point oldPoint = caratteristicheWidget.getPosizione(); if (logger.isTraceEnabled()) logger.trace("oldPoint: " + oldPoint); TreePath treePath = caratteristicheWidget.getTreePath(); Rectangle rect = albero.getPathBounds(treePath); if (rect != null && this.type.equalsIgnoreCase(caratteristicheWidget.getTreeType())) { Point newPoint = albero.getPathBounds(treePath).getLocation(); Point convertedPoint = SwingUtilities.convertPoint(source, newPoint, glassPane); if (logger.isTraceEnabled()) logger.trace(" -- newPoint: " + convertedPoint); if (caratteristicheWidget.getTreeType().equalsIgnoreCase(Costanti.TREE_SOURCE)) { return new Point( convertedPoint.x + (albero.getPathBounds(treePath).width - Costanti.OFFSET_X_WIDGET_SOURCE), convertedPoint.y/*from www .ja va 2s .com*/ + (albero.getPathBounds(treePath).height / Costanti.OFFSET_Y_WIDGET_SOURCE)); } return new Point(convertedPoint.x, convertedPoint.y + 5); } else if (this.type.equalsIgnoreCase(caratteristicheWidget.getTreeType())) { TreePath treePathInterno = treePath; Rectangle rectInterno = albero.getPathBounds(treePathInterno); while (rectInterno == null) { if (treePathInterno == null) { return null; } treePathInterno = treePathInterno.getParentPath(); rectInterno = albero.getPathBounds(treePathInterno); } Point newPoint = albero.getPathBounds(treePathInterno).getLocation(); Point convertedPoint = SwingUtilities.convertPoint(source, newPoint, glassPane); if (logger.isTraceEnabled()) logger.trace(" -- newPoint: " + convertedPoint); if (caratteristicheWidget.getTreeType().equalsIgnoreCase(Costanti.TREE_SOURCE)) { return new Point( convertedPoint.x + (albero.getPathBounds(treePathInterno).width - Costanti.OFFSET_X_WIDGET_SOURCE), convertedPoint.y + (albero.getPathBounds(treePathInterno).height / Costanti.OFFSET_Y_WIDGET_SOURCE)); } return new Point(convertedPoint.x, convertedPoint.y + 5); } return null; }
From source file:it.unibas.spicygui.vista.listener.WidgetMoveExpansionListener.java
private Point findNewLocationForTree(ICaratteristicheWidget icaratteristicheWidget, JTree albero) { CaratteristicheWidgetTree caratteristicheWidget = (CaratteristicheWidgetTree) icaratteristicheWidget; Point oldPoint = caratteristicheWidget.getPosizione(); if (logger.isTraceEnabled()) logger.trace("oldPoint: " + oldPoint); TreePath treePath = caratteristicheWidget.getTreePath(); Rectangle rect = albero.getPathBounds(treePath); if (rect != null && this.type.equalsIgnoreCase(caratteristicheWidget.getTreeType())) { Point newPoint = albero.getPathBounds(treePath).getLocation(); Point convertedPoint = SwingUtilities.convertPoint(source, newPoint, glassPane); if (logger.isTraceEnabled()) logger.trace(" -- newPoint: " + convertedPoint); if (caratteristicheWidget.getTreeType().equalsIgnoreCase(Costanti.TREE_SOURCE)) { return new Point( convertedPoint.x + (albero.getPathBounds(treePath).width - Costanti.OFFSET_X_WIDGET_SOURCE), convertedPoint.y/*from w ww. j a v a2s.c om*/ + (albero.getPathBounds(treePath).height / Costanti.OFFSET_Y_WIDGET_SOURCE)); } return new Point(convertedPoint.x, convertedPoint.y + 5); } else if (this.type.equalsIgnoreCase(caratteristicheWidget.getTreeType())) { TreePath treePathInterno = treePath; Rectangle rectInterno = albero.getPathBounds(treePathInterno); while (rectInterno == null) { if (treePathInterno == null) { return null; } treePathInterno = treePathInterno.getParentPath(); rectInterno = albero.getPathBounds(treePathInterno); } Point newPoint = albero.getPathBounds(treePathInterno).getLocation(); Point convertedPoint = SwingUtilities.convertPoint(source, newPoint, glassPane); if (logger.isTraceEnabled()) logger.trace(" -- newPoint: " + convertedPoint); if (caratteristicheWidget.getTreeType().equalsIgnoreCase(Costanti.TREE_SOURCE)) { return new Point( convertedPoint.x + (albero.getPathBounds(treePathInterno).width - Costanti.OFFSET_X_WIDGET_SOURCE), convertedPoint.y + (albero.getPathBounds(treePathInterno).height / Costanti.OFFSET_Y_WIDGET_SOURCE)); } return new Point(convertedPoint.x, convertedPoint.y + 5); } return null; }
From source file:Main.java
@Override public boolean isCellEditable(final EventObject event) { Object source = event.getSource(); if (!(source instanceof JTree) || !(event instanceof MouseEvent)) { return false; }//from w w w .j av a 2s .c om JTree tree = (JTree) source; MouseEvent mouseEvent = (MouseEvent) event; TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY()); if (path == null) { return false; } Object node = path.getLastPathComponent(); if (node == null || !(node instanceof DefaultMutableTreeNode)) { return false; } Rectangle r = tree.getPathBounds(path); if (r == null) { return false; } Dimension d = panel.getPreferredSize(); r.setSize(new Dimension(d.width, r.height)); if (r.contains(mouseEvent.getX(), mouseEvent.getY())) { Point pt = SwingUtilities.convertPoint(tree, mouseEvent.getPoint(), panel); Object o = SwingUtilities.getDeepestComponentAt(panel, pt.x, pt.y); if (o instanceof JComboBox) { comboBox.showPopup(); } else if (o instanceof Component) { Object oo = SwingUtilities.getAncestorOfClass(JComboBox.class, (Component) o); if (oo instanceof JComboBox) { comboBox.showPopup(); } } return true; } return delegate.isCellEditable(event); }
From source file:it.unibas.spicygui.controllo.datasource.operators.CreaWidgetAlberi.java
private void createWidget(Point point, JTree albero, TreePath treePath, boolean sourceFlag) { Point punto = null;/* w ww . java 2 s .c o m*/ TreeNode treeNode = (TreeNode) treePath.getLastPathComponent(); TreeNodeAdapter nodeAdapter = (TreeNodeAdapter) ((DefaultMutableTreeNode) treeNode).getUserObject(); INode iNode = nodeAdapter.getINode(); if (sourceFlag) { VMDPinWidgetSource pin = new VMDPinWidgetSource(scene); settaWidgetAlbero(pin, sourceFlag); int width = albero.getPathBounds(treePath).width; int height = albero.getPathBounds(treePath).height; if (logger.isTraceEnabled()) { logger.trace("width: " + width + " height: " + height); } punto = new Point(point.x + (width - Costanti.OFFSET_X_WIDGET_SOURCE), point.y + (height / Costanti.OFFSET_Y_WIDGET_SOURCE)); pin.setPreferredLocation(punto); pin.getActions().addAction(ActionFactory.createConnectAction(connectionLayer, new ActionSceneConnection(scene, connectionLayer, mainLayer, this.correspondencesCreator))); mainLayer.addChild(pin, new CaratteristicheWidgetTree(albero, point, treePath, Costanti.TREE_SOURCE, width, iNode)); iNode.addAnnotation(pinWidgetTreeConstant, pin); //TODO Da togliere l'ultima condizione nel momento in cui le tgd possano tracciare corrispondenze if ((iNode.isExcluded()) || !(iNode instanceof AttributeNode) || (pinWidgetTreeConstant.equals(Costanti.PIN_WIDGET_TREE_TGD))) { pin.setEnabled(false); } } else { VMDPinWidgetTarget pin = new VMDPinWidgetTarget(scene); settaWidgetAlbero(pin, sourceFlag); punto = new Point(point.x, point.y + 5); pin.setPreferredLocation(punto); pin.getActions().addAction( ActionFactory.createConnectAction(connectionLayer, new ActionSceneConnectionTarget(scene, connectionLayer, mainLayer, this.correspondencesCreator))); mainLayer.addChild(pin, new CaratteristicheWidgetTree(albero, point, treePath, Costanti.TREE_TARGET, 0, iNode)); iNode.addAnnotation(pinWidgetTreeConstant, pin); //TODO Da togliere l'ultima condizione nel momento in cui le tgd possano tracciare corrispondenze if ((iNode.isExcluded()) || !(iNode instanceof AttributeNode) || (pinWidgetTreeConstant.equals(Costanti.PIN_WIDGET_TREE_TGD))) { pin.setEnabled(false); } } scene.validate(); }
From source file:it.unibas.spicygui.controllo.datasource.operators.CreaWidgetAlberi.java
private void scansioneAlbero(JTree albero, Component source, Component target, boolean sourceFlag) { //VEcchio codice che mette widget solo per le foglie // for (int i = 0; i < listaFoglie.size(); i++) { // int tmp = (Integer) listaFoglie.get(i); for (int i = 0; i < albero.getRowCount(); i++) { TreePath treePath = albero.getPathForRow(i); if (treePath != null && albero.isVisible(treePath)) { if (logger.isTraceEnabled()) { logger.trace("punto trovato: " + albero.getPathBounds(treePath).getLocation()); }//from w w w . ja v a2s .c o m Point convertedPoint = SwingUtilities.convertPoint(source, albero.getPathBounds(treePath).getLocation(), target); createWidget(convertedPoint, albero, treePath, sourceFlag); if (logger.isTraceEnabled()) { logger.trace("punto convertito: " + convertedPoint); } } } contatore = -1; listaFoglie = new ArrayList(); }
From source file:gov.sandia.umf.platform.ui.jobs.RunPanel.java
public RunPanel() { root = new NodeBase(); model = new DefaultTreeModel(root); tree = new JTree(model); tree.setRootVisible(false);//from w ww.jav a2s .co m tree.setShowsRootHandles(true); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); tree.setCellRenderer(new DefaultTreeCellRenderer() { @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); NodeBase node = (NodeBase) value; Icon icon = node.getIcon(expanded); // A node knows whether it should hold other nodes or not, so don't pass leaf to it. if (icon == null) { if (leaf) icon = getDefaultLeafIcon(); else if (expanded) icon = getDefaultOpenIcon(); else icon = getDefaultClosedIcon(); } setIcon(icon); return this; } }); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { NodeBase newNode = (NodeBase) tree.getLastSelectedPathComponent(); if (newNode == null) return; if (newNode == displayNode) return; if (displayThread != null) synchronized (displayText) { displayThread.stop = true; } displayNode = newNode; if (displayNode instanceof NodeFile) viewFile(); else if (displayNode instanceof NodeJob) viewJob(); } }); tree.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { int keycode = e.getKeyCode(); if (keycode == KeyEvent.VK_DELETE || keycode == KeyEvent.VK_BACK_SPACE) { delete(); } } }); tree.addTreeWillExpandListener(new TreeWillExpandListener() { public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException { TreePath path = event.getPath(); // TODO: can this ever be null? Object o = path.getLastPathComponent(); if (o instanceof NodeJob) ((NodeJob) o).build(tree); } public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException { } }); tree.addTreeExpansionListener(new TreeExpansionListener() { public void treeExpanded(TreeExpansionEvent event) { Rectangle node = tree.getPathBounds(event.getPath()); Rectangle visible = treePane.getViewport().getViewRect(); visible.height -= node.y - visible.y; visible.y = node.y; tree.repaint(visible); } public void treeCollapsed(TreeExpansionEvent event) { Rectangle node = tree.getPathBounds(event.getPath()); Rectangle visible = treePane.getViewport().getViewRect(); visible.height -= node.y - visible.y; visible.y = node.y; tree.repaint(visible); } }); Thread refreshThread = new Thread() { public void run() { try { // Initial load synchronized (running) { for (MNode n : AppData.runs) running.add(0, new NodeJob(n)); // This should be efficient on a doubly-linked list. for (NodeJob job : running) root.add(job); } EventQueue.invokeLater(new Runnable() { public void run() { model.nodeStructureChanged(root); if (model.getChildCount(root) > 0) tree.setSelectionRow(0); } }); // Periodic refresh to show status of running jobs int shortCycles = 100; // Force full scan on first cycle. while (true) { NodeBase d = displayNode; // Make local copy (atomic action) to prevent it changing from under us if (d instanceof NodeJob) ((NodeJob) d).monitorProgress(RunPanel.this); if (shortCycles++ < 20) { Thread.sleep(1000); continue; } shortCycles = 0; synchronized (running) { Iterator<NodeJob> i = running.iterator(); while (i.hasNext()) { NodeJob job = i.next(); if (job != d) job.monitorProgress(RunPanel.this); if (job.complete >= 1) i.remove(); } } } } catch (InterruptedException e) { } } }; refreshThread.setDaemon(true); refreshThread.start(); displayText = new JTextArea(); displayText.setEditable(false); final JCheckBox chkFixedWidth = new JCheckBox("Fixed-Width Font"); chkFixedWidth.setFocusable(false); chkFixedWidth.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int size = displayText.getFont().getSize(); if (chkFixedWidth.isSelected()) { displayText.setFont(new Font(Font.MONOSPACED, Font.PLAIN, size)); } else { displayText.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, size)); } } }); displayPane.setViewportView(displayText); ActionListener graphListener = new ActionListener() { public void actionPerformed(ActionEvent e) { if (displayNode instanceof NodeFile) { NodeFile nf = (NodeFile) displayNode; if (nf.type == NodeFile.Type.Output || nf.type == NodeFile.Type.Result) { String graphType = e.getActionCommand(); if (displayPane.getViewport().getView() instanceof ChartPanel && displayGraph.equals(graphType)) { viewFile(); displayGraph = ""; } else { if (graphType.equals("Graph")) { Plot plot = new Plot(nf.path.getAbsolutePath()); if (!plot.columns.isEmpty()) displayPane.setViewportView(plot.createGraphPanel()); } else // Raster { Raster raster = new Raster(nf.path.getAbsolutePath(), displayPane.getHeight()); displayPane.setViewportView(raster.createGraphPanel()); } displayGraph = graphType; } } } } }; buttonGraph = new JButton("Graph", ImageUtil.getImage("analysis.gif")); buttonGraph.setFocusable(false); buttonGraph.addActionListener(graphListener); buttonGraph.setActionCommand("Graph"); buttonRaster = new JButton("Raster", ImageUtil.getImage("prnplot.gif")); buttonRaster.setFocusable(false); buttonRaster.addActionListener(graphListener); buttonRaster.setActionCommand("Raster"); Lay.BLtg(this, "C", Lay.SPL(Lay.BL("C", treePane = Lay.sp(tree)), Lay.BL("N", Lay.FL(chkFixedWidth, Lay.FL(buttonGraph, buttonRaster), "hgap=50"), "C", displayPane), "divpixel=250")); setFocusCycleRoot(true); }