📄 activityonnodepertchart.java
字号:
/***************************************************************************ActivityOnNodePertChart.java - descriptionCopyright [2005 - ADAE]This file is part of GanttProject].***************************************************************************//*************************************************************************** * GanttProject is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * GanttProject is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. ****************************************************************************/package org.ganttproject.chart.pert;import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.image.BufferedImage;import java.awt.image.RenderedImage;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JComponent;import net.sourceforge.ganttproject.GanttExportSettings;import net.sourceforge.ganttproject.IGanttProject;import net.sourceforge.ganttproject.chart.Chart;import net.sourceforge.ganttproject.chart.TimeUnitVisitor;import net.sourceforge.ganttproject.language.GanttLanguage;import net.sourceforge.ganttproject.task.Task;import net.sourceforge.ganttproject.task.TaskContainmentHierarchyFacade;import net.sourceforge.ganttproject.task.TaskLength;import net.sourceforge.ganttproject.time.TimeUnit;import org.ganttproject.chart.pert.PertChartAbstraction.TaskGraphNode;/** * PERT char implementation where nodes are tasks and links succession * relations. * * @author bbaranne * @author Julien Seiler * */public class ActivityOnNodePertChart extends PertChart { /** * List of abstract nodes. */ private List myTaskGraphNodes; /** * List of graphical arrows. */ private List myGraphicalArrows; /** * List of graphical nodes (in relation with abstract nodes) */ private List myGraphicalNodes; //private Map myMapPositionListOfNodes; private int nbCols; /** * PERT chart abstraction used to build graph. */ private PertChartAbstraction myPertAbstraction; /** * Max and min coordinates in the graphics that paints the graphical nodes * and arrows. */ private int myMaxX = 1, myMaxY = 1; /** * The currently mouse pressed graphical node. */ private GraphicalNode myPressedGraphicalNode; // private List pressedGraphicalNodes; // // private List xClickedOffsets; // // private List yClickedOffsets; /** * Offset between the mouse pointer when clicked on a graphical node and the * top left corner of this same node. */ int myXClickedOffset, myYClickedOffset; /** * Graphics where PERT chart is painted. */ private Graphics myGraphics; private static GanttLanguage ourLanguage = GanttLanguage.getInstance(); /** * Constructs a ActivityOnNodePertChart. * */ public ActivityOnNodePertChart() { super(null); // pressedGraphicalNodes = new ArrayList(); // xClickedOffsets = new ArrayList(); // yClickedOffsets = new ArrayList(); this.setBackground(Color.WHITE.brighter()); this.addMouseMotionListener(new MouseMotionListener() { public void mouseDragged(final MouseEvent e) { if (myPressedGraphicalNode != null) { myPressedGraphicalNode.x = e.getX() - myXClickedOffset; myPressedGraphicalNode.y = e.getY() - myYClickedOffset; if (e.getX() > getPreferredSize().getWidth()) { ActivityOnNodePertChart.this .setPreferredSize(new Dimension( myPressedGraphicalNode.x + NODE_WIDTH + X_GAP, (int) getPreferredSize().getHeight())); revalidate(); } if (e.getY() > getPreferredSize().getHeight()) { ActivityOnNodePertChart.this .setPreferredSize(new Dimension( (int) getPreferredSize().getWidth(), myPressedGraphicalNode.y + NODE_HEIGHT + Y_GAP)); revalidate(); } repaint(); } } public void mouseMoved(MouseEvent e) { // nothing to do... } }); this.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent arg0) { // nothing to do... } public void mouseEntered(MouseEvent arg0) { // nothing to do... } public void mouseExited(MouseEvent arg0) { // nothing to do... } public void mousePressed(MouseEvent e) { myPressedGraphicalNode = getGraphicalNode(e.getX(), e.getY()); if (myPressedGraphicalNode != null) { myXClickedOffset = e.getX() - myPressedGraphicalNode.x; myYClickedOffset = e.getY() - myPressedGraphicalNode.y; myPressedGraphicalNode.backgroundColor = myPressedGraphicalNode.backgroundColor .darker(); } repaint(); } public void mouseReleased(MouseEvent e) { if (myPressedGraphicalNode != null) { if (myPressedGraphicalNode.node.isCritical()) myPressedGraphicalNode.backgroundColor = GraphicalNode.defaultCriticalColor; else myPressedGraphicalNode.backgroundColor = GraphicalNode.defaultBackgroundColor; myPressedGraphicalNode.x = getGridX(e.getX() - myXClickedOffset + NODE_WIDTH / 2); myPressedGraphicalNode.y = getGridY(e.getY()); myPressedGraphicalNode = null; repaint(); } recalculatPreferredSize(); revalidate(); repaint(); } }); } /** * Recalculated preferred size so that graphics fit with nodes positions. * */ private void recalculatPreferredSize() { int maxX = 0; int maxY = 0; Iterator it = myGraphicalNodes.iterator(); while (it.hasNext()) { GraphicalNode gn = (GraphicalNode) it.next(); int x = gn.x + NODE_WIDTH; int y = gn.y + NODE_HEIGHT; maxX = Math.max(maxX, x); maxY = Math.max(maxY, y); } setPreferredSize(new Dimension(maxX, maxY)); myMaxX = maxX; myMaxY = maxY; } /** * Returns <code>true</code> if the point of coordinates <code>x</code>, * <code>y</code> is in the rectangle described by is top left corner (<code>rectX</code>, * <code>rectY</code>) and dimension (<code>rectWidth</code>, * <code>rectHeight</code>), <code>false</code> ortherwise. * * @param x * @param y * @param rectX * @param rectY * @param rectWidth * @param rectHeight * @return <code>true</code> if the point of coordinates <code>x</code>, * <code>y</code> is in the rectangle described by is top left * corner (<code>rectX</code>, <code>rectY</code>) and * dimension (<code>rectWidth</code>, <code>rectHeight</code>), * <code>false</code> ortherwise. */ private static boolean isInRectancle(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight) { return (x > rectX && x < rectX + rectWidth && y > rectY && y < rectY + rectHeight); } /** * Returns the GraphicalNode at the <code>x</code>, <code>y</code> * position, or <code>null</code> if there is no node. * * @param x * @param y * @return The GraphicalNode at the <code>x</code>, <code>y</code> * position, or <code>null</code> if there is no node. */ private GraphicalNode getGraphicalNode(int x, int y) { Iterator it = myGraphicalNodes.iterator(); while (it.hasNext()) { GraphicalNode gn = (GraphicalNode) it.next(); if (isInRectancle(x, y, gn.x, gn.y, NODE_WIDTH, NODE_HEIGHT)) return gn; } return null; } /** * @inheritDoc */ protected void buildPertChart() { if (this.myPertAbstraction == null) { this.myPertAbstraction = new PertChartAbstraction(myTaskManager); this.myTaskGraphNodes = myPertAbstraction.getTaskGraphNodes(); this.myGraphicalNodes = new ArrayList(); this.myGraphicalArrows = new ArrayList(); //myMapPositionListOfNodes = new HashMap(); //this.rowsList = new HashMap(); this.nbCols = 0; this.setBackground(Color.WHITE); this.process(); //System.out.println("Position correction"); //this.correctPositionBecauseOfSuperTasks(); this.avoidCrossingNode(); this.avoidCrossingLine(); this.removeEmptyColumn(); this.calculateGraphicalNodesCoordinates(); this.calculateArrowsCoordinates(); this.setPreferredSize(new Dimension(myMaxX, myMaxY)); } else { this.myPertAbstraction = new PertChartAbstraction(myTaskManager); myTaskGraphNodes = myPertAbstraction.getTaskGraphNodes(); updateGraphNodesInfo(); } } /** * Updates the data for each nodes. * */ private void updateGraphNodesInfo() { if (myTaskGraphNodes != null) { Iterator it = myTaskGraphNodes.iterator(); while (it.hasNext()) { TaskGraphNode tgn = (TaskGraphNode) it.next(); int id = tgn.getID(); if(getGraphicalNodeByID(id) != null) getGraphicalNodeByID(id).updateData(tgn); } } } /** * * @param taskGraphNode * @return */ private boolean isZeroPosition(TaskGraphNode taskGraphNode) { Iterator it = myTaskGraphNodes.iterator(); while (it.hasNext()) { TaskGraphNode t = (TaskGraphNode) it.next(); if (t.getSuccessors().contains(taskGraphNode)) return false; } return true; } private static int getGridX(int x) { int res = X_OFFSET; int tmp = 0; while (res < x) { tmp = res; res += NODE_WIDTH + X_GAP; } return tmp; } private static int getGridY(int y) { int res = Y_OFFSET; int tmp = 0; while (res < y) { tmp = res; res += NODE_HEIGHT + Y_GAP; } return tmp; } private void process() { Iterator it = myTaskGraphNodes.iterator(); while (it.hasNext()) { TaskGraphNode tgn = (TaskGraphNode) it.next(); if (isZeroPosition(tgn)) { add(0, new GraphicalNode(tgn)); } } int col = 0; List l = getNodesThatAreInASpecificSuccessorPosition(col); // ici tous les 0 position sont faits. while (l != null) { Iterator it2 = l.iterator(); while (it2.hasNext()) { TaskGraphNode tnode = (TaskGraphNode) it2.next(); GraphicalNode gnode = this.getGraphicalNodeByID(tnode.getID()); if(gnode == null) { gnode = this.createGraphicalNode(tnode); } else { this.remove(gnode); } this.add(col + 1, gnode); } col++; l = getNodesThatAreInASpecificSuccessorPosition(col); } } /** * Creates or gets the graphical node corresponding to the taskGrahNode * * @param taskGraphNode * @return */ private GraphicalNode createGraphicalNode(TaskGraphNode taskGraphNode) { GraphicalNode res = getGraphicalNodeByID(taskGraphNode.getID()); if (res != null) return res; else return new GraphicalNode(taskGraphNode); } private void correctPositionBecauseOfSuperTasks() { TaskContainmentHierarchyFacade hierarchy = myTaskManager .getTaskHierarchy(); Task[] tasks = myTaskManager.getTasks(); for (int i = 0; i < tasks.length; i++) { Task task = tasks[i]; Task[] nestedTasks = hierarchy.getNestedTasks(task); correctPositions(nestedTasks); } } // recursive private void correctPositions(Task[] tasks) { if (tasks == null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -