📄 viewer.java
字号:
/** * Copyright 2004 Carlos Silva A. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package jgantt.view.gantt;import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Composite;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Point;import java.awt.Rectangle;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Vector;import javax.swing.ImageIcon;import javax.swing.JComponent;import javax.swing.JToolTip;import jgantt.model.Asignation;import jgantt.model.Constraint;import jgantt.model.DateOrganizer;import jgantt.model.GraphColors;import jgantt.model.Project;import jgantt.model.ProjectChange;import jgantt.model.ProjectListener;import jgantt.model.Resource;import jgantt.model.Task;import jgantt.model.TaskColors;import jgantt.model.ViewOptions;import jgantt.view.adapters.ProjectViewModel;import jgantt.view.adapters.ProjectViewModelChange;import jgantt.view.adapters.ProjectViewModelListener;/** * Viewer representa la parte grafica de la carta gantt, solo las lineas y * barras * <p> * $Date: 2005/08/19 15:51:26 $ * </p> * * @version $Revision: 1.48 $ * @author Carlos Silva */public class Viewer extends JComponent implements ProjectListener, ProjectViewModelListener, ViewOptions.Observer { private static final long serialVersionUID = 3833470604266714421L; // cache ProjectViewModel pvModel = null; ViewOptions viewOptions = null; // GraphColors graphColors = null; Project project; DateOrganizer cal; ConstraintsHelper constraintsHelper = null; Object lastSelectedObject = null; /** * Lista de areas importantes en el grafico. contiene la asociacion entre un * objeto y la tarea o constraint que representa. */ Vector hotSpots = new Vector(); /** * Construye un visualizador de un projecto * * @param projectViewModel */ public Viewer(ProjectViewModel projectViewModel) { super(); assignViewModel(projectViewModel); pvModel.addListener(this); setDoubleBuffered(true); setOpaque(true); setLayout(null); constraintsHelper = new ConstraintsHelper(this); addMouseMotionListener(constraintsHelper); addMouseListener(constraintsHelper); } /** * Pintado de la imagen header con divisiones mayores y menores. */ public void paintComponent(Graphics g) { super.paintComponent(g); paintComponent(g, getBounds()); } /** * Funcion de pintado de apoyo (sirve tambien para generar imagenes). */ public void paintComponent(Graphics g, Rectangle bounds) { super.paintComponent(g); hotSpots.clear(); GraphColors graphColors = project.getGraphColors(); bounds.x = 0; bounds.y = 0; g.setFont(viewOptions.stdFont); // g.setFont(new Font("宋体", 0, 12)); // g.setColor(graphColors.background); g.fillRect(0, 0, bounds.width, bounds.height); drawWeekEndsAndHolidays(g, bounds); drawMajorDivisions(g, bounds); g.translate(0, -viewOptions.getTopRow() * viewOptions.taskHeight); drawTaskBars(g, bounds); drawContraints(g, bounds); // drawTextos... } /** * dibuja los dias feriados y no habiles... ademas marca el d韆 de hoy */ private void drawWeekEndsAndHolidays(Graphics g, Rectangle bounds) { int x; GraphColors graphColors = project.getGraphColors(); int divWidth = viewOptions.divWidth; boolean drawMinorLines = graphColors.drawMinorLines; if ((viewOptions.minDivMult == 1) && (viewOptions.minDivUnit == Calendar.DAY_OF_MONTH) || (viewOptions.minDivUnit == Calendar.DAY_OF_WEEK)) { g.setColor(graphColors.weekEnd); for (int i = 0; i < viewOptions.divCount; i++) { x = bounds.x + viewOptions.divX[i]; Date fecha = new Date(viewOptions.divTimes[i]); if (!cal.isLaborable(fecha)) { if (cal.isHoliday(fecha)) { g.setColor(graphColors.holiday); g .fillRect(x + 1, bounds.y, divWidth - 1, bounds.height); g.setColor(graphColors.weekEnd); } else g .fillRect(x + 1, bounds.y, divWidth - 1, bounds.height); } if (drawMinorLines) { g.setColor(graphColors.div); g.drawLine(x, bounds.y, x, bounds.y + bounds.height); g.setColor(graphColors.weekEnd); } } } // Calcular la posicion en pantalla de "hoy" Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.HOUR_OF_DAY, 0); // Date d = new java.util.Date(); long t1 = calendar.getTime().getTime(); calendar.add(Calendar.DAY_OF_MONTH, 1); long t2 = calendar.getTime().getTime(); int x1 = bounds.x + (int) ((t1 - viewOptions.t0) / viewOptions.msPixel); int x2 = bounds.x + (int) ((t2 - viewOptions.t0) / viewOptions.msPixel); if ((x1 <= bounds.width) && (x2 >= 0)) { g.setColor(graphColors.now); // g.drawLine(x1, bounds.y, x1, bounds.y + bounds.height); g.fillRect(x1, bounds.y, (x2 - x1), bounds.y + bounds.height); } } /** * Dibuja las divisiones mayores * * @param g * @param bounds */ private void drawMajorDivisions(Graphics g, Rectangle bounds) { int x; java.util.Calendar c = java.util.Calendar.getInstance(); c.setTime(viewOptions.startDate); switch (viewOptions.majDivUnit) { case Calendar.MONTH: c.set(Calendar.DAY_OF_MONTH, 1); break; case Calendar.WEEK_OF_YEAR: c.set(Calendar.DAY_OF_WEEK, 2); break; } SimpleDateFormat majFormat = new SimpleDateFormat( viewOptions.majDivFormat); g.setColor(project.getGraphColors().mayorDiv); while (true) { // Calcular la posicion en pantalla de esta fecha Date d = c.getTime(); long t = d.getTime(); x = bounds.x + (int) ((t - viewOptions.t0) / viewOptions.msPixel); if (x > bounds.width) break; // Dibujar la linea g.drawLine(x, bounds.y, x, bounds.y + bounds.height); // Moverse a la siguiente division mayor c.add(viewOptions.majDivUnit, viewOptions.majDivMult); } ; } /** * Dibuja las barras de las tareas * * @param g * @param bounds */ private void drawTaskBars(Graphics g, Rectangle bounds) { int y; GraphColors graphColors = project.getGraphColors(); boolean drawTaskLines = graphColors.drawTaskLines; g.setFont(viewOptions.bldFont); g.setFont(new Font("宋体", 0, 12)); int barNum = 0; Date currentSnapShot = pvModel.getCurrentSnapShot(); for (int i = 0; i < project.getTaskCount(); i++) { Task t = project.getTask(i); Date d = t.getStartDate(); if (!t.isVisible()) continue; long t1 = t.getStartDate().getTime(); long t2 = t.getFinishDate().getTime(); int x1 = (int) ((t1 - viewOptions.t0) / viewOptions.msPixel); int x2 = (int) ((t2 - viewOptions.t0) / viewOptions.msPixel); y = bounds.y + barNum * viewOptions.taskHeight; // lineas horizontales if (drawTaskLines) { g.setColor(graphColors.horLine); g.drawLine(bounds.x, y + viewOptions.taskHeight - 1, bounds.x + bounds.width, y + viewOptions.taskHeight - 1); } if (t.isResume()) { drawResumeTask(g, t, x1, y, x2, y + viewOptions.taskHeight); } else { drawTask(g, t, x1, y, x2, y + viewOptions.taskHeight); } if (currentSnapShot != null) { // System.out.println("snap "+t); Task.SnapShot ss = t.getSnapshot(currentSnapShot); if (ss != null) { long st1 = ss.start.getTime(); long st2 = ss.end.getTime(); int sx1 = (int) ((st1 - viewOptions.t0) / viewOptions.msPixel); int sx2 = (int) ((st2 - viewOptions.t0) / viewOptions.msPixel); // dibujar fantasma.... // System.out.println(" "+sx1+"-----"+sx2); Graphics2D g2d = (Graphics2D) g; AlphaComposite composite = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.4f); Composite stdComposite = g2d.getComposite(); g2d.setComposite(composite); TaskColors colors = graphColors.defaultTaskColors; g.setColor(graphColors.snapshot); g.fillRect(sx1, y + 1, sx2 - sx1 + 1, viewOptions.taskBarHeight); g2d.setComposite(stdComposite); } } // colocar asignaciones y completado hasta la fecha String toPrint = getCompletionDescription(t); g.setColor(t.getTaskColors().text); g.drawString(toPrint, x2 + 10, y + viewOptions.taskBarHeight + 3); barNum++; } } /** * @param t * @return */ public String getCompletionDescription(Task t) { Vector a = t.getAsignations(); StringBuffer toPrint = new StringBuffer(30); String format = viewOptions.barTextFormat; SimpleDateFormat sdf = new SimpleDateFormat(viewOptions.dateFormat); for (int i = 0; i < format.length(); i++) { char c = format.charAt(i); if (c != '%') { toPrint.append(c); } else { i++; c = format.charAt(i); switch (c) { case 'n': toPrint.append(t.getName()); break; case 'c': toPrint.append(t.getCompleted()); break; case 's': toPrint.append(sdf.format(t.getStartDate())); break; case 'e': toPrint.append(sdf.format(t.getFinishDate())); break; case 'm': toPrint.append(t.getComments()); break; case 'R': String aFormat = viewOptions.assignTextFormat; for (int j = 0; j < a.size(); j++) { Asignation asign = (Asignation) a.elementAt(j); Resource res = asign.getResource(); toPrint.append(" "); for (int k = 0; k < aFormat.length(); k++) { char ch = aFormat.charAt(k); if (ch != '%') { toPrint.append(ch); } else { k++; ch = aFormat.charAt(k); switch (ch) { case 'n': toPrint.append(res.getName()); break; case 'a': toPrint.append(res.getAlias()); break; case 't': toPrint.append(res.getInitials()); break; case 'i': toPrint.append(res.getId()); break; case 'u': toPrint.append(asign.getUnits()); break; case 'h': toPrint.append(t .getLocalTotalResourceUnits(asign .getResource()) / 60); break; case 'w': toPrint.append(t .getLocalWorkedResourceUnits(asign .getResource()) / 60); break; default: // No puedo creer que hayan habido tantos // errores en esta linea: toPrint.append(ch); } // witch } // else } // for (formato) } // for (asignacion) break; default: toPrint.append(c); } } } return toPrint.toString(); } /** * dibuja las lineas de restricciones entre tareas */ private void drawContraints(Graphics g, Rectangle bounds) { GraphColors graphColors = project.getGraphColors(); int y; for (int i = 0; i < project.getTaskCount(); i++) { Task master = project.getTask(i); if (!master.isVisible()) continue; Date finishDate = master.getFinishDate(); Vector constraints = master.getChildConstraints(); int y1 = bounds.y + (master.getVisibleIndex() - 1) * viewOptions.taskHeight + viewOptions.taskHeight / 2; if (master.isResume()) g.setColor(graphColors.defaultResumeColors.constraint); else g.setColor(graphColors.defaultTaskColors.constraint); for (int j = 0; j < constraints.size(); j++) { Constraint ct = (Constraint) constraints.elementAt(j); Task slave = ct.getSlaveTask(); if (!slave.isVisible()) continue; int delay = ct.getDelay(); int id = (slave.getVisibleIndex()); Date startDate = slave.getStartDate(); long t1 = finishDate.getTime(); long t2 = slave.getStartDate().getTime(); int x1 = (int) ((t1 - viewOptions.t0) / viewOptions.msPixel); int x2 = (int) ((t2 - viewOptions.t0) / viewOptions.msPixel); int f = 1; if ((id - 1) < (master.getVisibleIndex() - 1))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -