📄 gantttree2.java
字号:
.getTreePath().getLastPathComponent()); Task task = (Task) node.getUserObject(); /* * if (!expand.contains(new Integer(task.getTaskID()))) { * expand.add(new Integer(task.getTaskID())); } */ // task.setExpand(true); if (area != null) area.repaint(); } /** Delete a node. */ public void treeNodesRemoved(TreeModelEvent e) { if (area != null) { area.repaint(); } } /** Structur change. */ public void treeStructureChanged(TreeModelEvent e) { if (area != null) { area.repaint(); } } } private class GanttTreeDropListener implements DropTargetListener { private TreePath lastPath = null; private Rectangle2D cueLineRect = new Rectangle2D.Float(); private Rectangle2D ghostImageRect = new Rectangle2D.Float(); private Color cueLineColor; private Point lastEventPoint = new Point(); private Timer hoverTimer; public GanttTreeDropListener() { cueLineColor = new Color(SystemColor.controlShadow.getRed(), SystemColor.controlShadow.getGreen(), SystemColor.controlShadow.getBlue(), 64); // Set up a hover timer, so that a node will be automatically // expanded or collapsed // if the user lingers on it for more than a short time hoverTimer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { if (!treetable.getTree().isExpanded(lastPath)) { treetable.getTree().expandPath(lastPath); } } }); // Set timer to one-shot mode - it will be restartet when the // cursor is over a new node hoverTimer.setRepeats(false); } public void dragEnter(DropTargetDragEvent dtde) { if (ghostImage == null) { // In case if you drag a file from out and it's not an // acceptable, and it can crash if the image is null ghostImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE); } if (!isDragAcceptable(dtde)) dtde.rejectDrag(); else dtde.acceptDrag(dtde.getDropAction()); } public void dragOver(DropTargetDragEvent dtde) { if (!isDragAcceptable(dtde)) dtde.rejectDrag(); else dtde.acceptDrag(dtde.getDropAction()); // Even if the mouse is not moving, this method is still invoked // 10 times per second Point pt = dtde.getLocation(); if (pt.equals(lastEventPoint)) return; lastEventPoint = pt; Graphics2D g2 = (Graphics2D) treetable.getGraphics(); // If a drag image is not supported by the platform, then draw our // own drag image if (!DragSource.isDragImageSupported()) { // Rub out the last ghost image and cue line treetable.paintImmediately(ghostImageRect.getBounds()); // And remember where we are about to draw the new ghost image ghostImageRect.setRect(pt.x - offsetPoint.x, pt.y - offsetPoint.y, ghostImage.getWidth(), ghostImage .getHeight()); g2.drawImage(ghostImage, AffineTransform.getTranslateInstance( ghostImageRect.getX(), ghostImageRect.getY()), null); } else { // Just rub out the last cue line treetable.paintImmediately(cueLineRect.getBounds()); } TreePath path = treetable.getTree().getClosestPathForLocation(pt.x, pt.y); if (!(path == lastPath)) { lastPath = path; hoverTimer.restart(); } // In any case draw (over the ghost image if necessary) a cue line // indicating where a drop will occur Rectangle raPath = treetable.getTree().getPathBounds(path); if (raPath == null) raPath = new Rectangle(1, 1); cueLineRect.setRect(0, raPath.y + (int) raPath.getHeight(), getWidth(), 2); g2.setColor(cueLineColor); g2.fill(cueLineRect); // And include the cue line in the area to be rubbed out next time ghostImageRect = ghostImageRect.createUnion(cueLineRect); } public void dropActionChanged(DropTargetDragEvent dtde) { if (!isDragAcceptable(dtde)) dtde.rejectDrag(); else dtde.acceptDrag(dtde.getDropAction()); } public void drop(DropTargetDropEvent dtde) { if (!isDropAcceptable(dtde)) { dtde.rejectDrop(); return; } // Prevent hover timer from doing an unwanted expandPath or // collapsePath hoverTimer.stop(); dtde.acceptDrop(dtde.getDropAction()); Transferable transferable = dtde.getTransferable(); DataFlavor[] flavors = transferable.getTransferDataFlavors(); for (int i = 0; i < flavors.length; i++) { DataFlavor flavor = flavors[i]; if (flavor .isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType)) { try { Point pt = dtde.getLocation(); DefaultMutableTreeNode target = (DefaultMutableTreeNode) treetable .getTree() .getClosestPathForLocation(pt.x, pt.y) .getLastPathComponent(); TreePath pathSource = (TreePath) transferable .getTransferData(flavor); DefaultMutableTreeNode source = (DefaultMutableTreeNode) pathSource .getLastPathComponent(); TreePath pathNewChild = null; TreeNode sourceFather = source.getParent(); int index = sourceFather.getIndex(source); source.removeFromParent(); treeModel.nodesWereRemoved(sourceFather, new int[] { index }, new Object[] { source }); ((GanttTreeTableModel) treeModel).insertNodeInto( source, target, 0); pathNewChild = new TreePath( ((DefaultMutableTreeNode) pathSource .getLastPathComponent()).getPath()); if (pathNewChild != null) { treetable.getTree().setSelectionPath(pathNewChild); // Mark // this // as // the // selected // path // in // the // tree } // refreshAllFather(source.getUserObject().toString()); expandRefresh(source); forwardScheduling(); area.repaint(); appli.setAskForSave(true); break; // No need to check remaining flavors } catch (UnsupportedFlavorException ufe) { System.out.println(ufe); dtde.dropComplete(false); return; } catch (IOException ioe) { System.out.println(ioe); dtde.dropComplete(false); return; } } } dtde.dropComplete(true); } public void dragExit(DropTargetEvent dte) { if (!DragSource.isDragImageSupported()) { repaint(ghostImageRect.getBounds()); } treetable.repaint(); } public boolean isDragAcceptable(DropTargetDragEvent e) { // Only accept COPY or MOVE gestures (ie LINK is not supported) if ((e.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) == 0) { return false; } // Only accept this particular flavor if (!e .isDataFlavorSupported(GanttTransferableTreePath.TREEPATH_FLAVOR)) { return false; } // Do not accept dropping on the source node Point pt = e.getLocation(); TreePath path = treetable.getTree().getClosestPathForLocation(pt.x, pt.y); if (dragPath.isDescendant(path)) { return false; } if (path.equals(dragPath)) { return false; } // Check if the task is a milestone task Task task = (Task) (((DefaultMutableTreeNode) path .getLastPathComponent()).getUserObject()); if (task.isMilestone()) return false; return true; } public boolean isDropAcceptable(DropTargetDropEvent e) { // Only accept COPY or MOVE gestures (ie LINK is not supported) if ((e.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) == 0) { return false; } // Only accept this particular flavor if (!e .isDataFlavorSupported(GanttTransferableTreePath.TREEPATH_FLAVOR)) { return false; } // prohibit dropping onto the drag source Point pt = e.getLocation(); TreePath path = treetable.getTree().getClosestPathForLocation(pt.x, pt.y); if (path.equals(dragPath)) { return false; } return true; } } private static class GanttTransferableTreePath implements Transferable { // The type of DnD object being dragged... public static final DataFlavor TREEPATH_FLAVOR = new DataFlavor( DataFlavor.javaJVMLocalObjectMimeType, "TreePath"); private TreePath _path; private DataFlavor[] _flavors = { TREEPATH_FLAVOR }; /** * Constructs a transferrable tree path object for the specified path. */ public GanttTransferableTreePath(TreePath path) { _path = path; } // Transferable interface methods... public DataFlavor[] getTransferDataFlavors() { return _flavors; } public boolean isDataFlavorSupported(DataFlavor flavor) { return java.util.Arrays.asList(_flavors).contains(flavor); } public synchronized Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException { if (flavor.isMimeTypeEqual(TREEPATH_FLAVOR.getMimeType())) // DataFlavor.javaJVMLocalObjectMimeType)) return _path; else throw new UnsupportedFlavorException(flavor); } } /** * Render the cell of the tree */ public class GanttTreeCellRenderer extends DefaultTreeCellRenderer // JLabel-CL implements TreeCellRenderer { public GanttTreeCellRenderer() { setOpaque(true); } public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { Task task = (Task) ((DefaultMutableTreeNode) value).getUserObject();// getTask(value.toString()); if (task == null) { return this; } int type = 0; setFont(Fonts.GANTT_TREE_FONT); if (task.isMilestone()) { setIcon(new ImageIcon(getClass().getResource( "/icons/meeting.gif"))); type = 1; } else if (leaf) { if (task.getPriority() == GanttTask.LOW) { setIcon(new ImageIcon(getClass().getResource( "/icons/task1.gif"))); } else if (task.getPriority() == GanttTask.NORMAL) { setIcon(new ImageIcon(getClass().getResource( "/icons/task.gif"))); } else if (task.getPriority() == GanttTask.HIGHT) { setIcon(new ImageIcon(getClass().getResource( "/icons/task2.gif"))); } type = 2; } else { setIcon(new ImageIcon(getClass() .getResource("/icons/mtask.gif")));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -