📄 viewer.java
字号:
f = -1; int y2 = bounds.y + (id - 1) * viewOptions.taskHeight + viewOptions.taskHeight / 2 - f * viewOptions.taskBarHeight / 2 - 1; Vector points = new Vector(); points.add(new Point(x1 + 1, y1)); // salida desde la primera tarea if (x1 <= x2) { points.add(new Point(x2 + 5, y1)); // final de la linea horizontal // flecha hacia abajo o hacia arriba? // son tres lineas cada vez mas anchas segun X if (y2 > y1) { points.add(new Point(x2 + 5, y2 - 1)); // final drawConstraint(g, points, ct); // Flecha hacia abajo g.drawLine(x2 + 5 - 1, y2 - 2, x2 + 5 + 1, y2 - 2); g.drawLine(x2 + 5 - 2, y2 - 3, x2 + 5 + 2, y2 - 3); g.drawLine(x2 + 5 - 3, y2 - 4, x2 + 5 + 3, y2 - 4); } else { points.add(new Point(x2 + 5, y2 + 2)); // final drawConstraint(g, points, ct); // flecha hacia arriba g.drawLine(x2 + 5 - 1, y2 + 3, x2 + 5 + 1, y2 + 3); g.drawLine(x2 + 5 - 2, y2 + 4, x2 + 5 + 2, y2 + 4); g.drawLine(x2 + 5 - 3, y2 + 5, x2 + 5 + 3, y2 + 5); } // Mostrar la cantidad de dias de diferencia(solo si es !=0) if (delay != 0) g.drawString("" + delay, x2 - 10, y2 + 5 - f * 5); } else { // devolverse de inmediato y2 = y2 + f * viewOptions.taskBarHeight / 2; y = y1 + f * viewOptions.taskHeight / 2 - f * 2; points.add(new Point(x1 + 5, y1)); // final de la linea horizontal points.add(new Point(x1 + 5, y)); // bajar points.add(new Point(x2 - 10, y)); // retroceder points.add(new Point(x2 - 10, y2)); // bajar points.add(new Point(x2 - 1, y2)); // avanzar drawConstraint(g, points, ct); // flecha hacia adelante g.drawLine(x2 - 2, y2 - 1, x2 - 2, y2 + 1); g.drawLine(x2 - 3, y2 - 2, x2 - 3, y2 + 2); g.drawLine(x2 - 4, y2 - 3, x2 - 4, y2 + 3); // Mostrar la cantidad de dias de diferencia(solo si es !=0) if (delay != 0) g.drawString("" + delay, x2 - 30, y2); } } } } /** * @see jgantt.model.ProjectListener#projectChanged(jgantt.model.ProjectChange) */ public void projectChanged(ProjectChange c) { repaint(); } /** * Eventos del modelo * * @see jgantt.view.adapters.ProjectViewModelListener#projectChanged(jgantt.view.adapters.ProjectViewModelChange) */ public void viewModelChanged(ProjectViewModelChange c) { if (c.getId() == ProjectViewModelChange.NEW_PROJECT_LOADED) assignViewModel(c.getProjectViewModel()); } /** * */ public void assignViewModel(ProjectViewModel pvm) { if (project != null) project.removeListener(this); if (viewOptions != null) viewOptions.removeObserver(this); pvModel = pvm; project = pvm.getProject(); project.addListener(this); cal = project.getDateOrganizer(); viewOptions = pvm.getProject().getViewOptions(); viewOptions.addObserver(this); project.addListener(this); } Color fillColors[] = null; /** * Repinta la ventana cuando cambianlas opciones graficas */ public void viewOptionsChanged() { GraphColors graphColors = project.getGraphColors(); TaskColors colors = graphColors.defaultTaskColors; int cr = colors.barBg.getRed(); int cg = colors.barBg.getGreen(); int cb = colors.barBg.getBlue(); float hsv[] = new float[3]; Color.RGBtoHSB(cr, cg, cb, hsv); // System.out.println(hsv[0]+" "+hsv[1]+" "+hsv[2]+" "); // float factor[] = { 0.4f, 0.6f, 0.8f, 1.1f, 0.85f, 0.7f, 0.55f, // 0.4f,0.2f }; // float factor[] = { 1.2f, 0.4f, 0.60f, 0.80f, 1.0f, 1.15f }; float factor[] = { 2.2f, 0.7f, 1.0f, 1.4f, 1.8f, 2.2f }; fillColors = new Color[factor.length]; for (int scan = 0; scan < factor.length; scan++) { float f = factor[scan]; // fillColors[scan] = new Color(Math.min(255,(int) (cr * // f)),Math.min(255, (int) (cg * f)), Math.min(255,(int) (cb * f))); fillColors[scan] = Color.getHSBColor(hsv[0], Math .min(1, hsv[1] / f), Math.min(1, hsv[2] * f)); } repaint(); } Image barSkin = null; Image resSkin = null; public void drawTask(Graphics g, Task t, int x1, int y1, int x2, int y2) { GraphColors graphColors = project.getGraphColors(); int y = y1 + (viewOptions.taskHeight - viewOptions.taskBarHeight) / 2; int w = x2 - x1; int h = viewOptions.taskBarHeight; // Guardar referencia al hotspot Rectangle r = new Rectangle(x1, y - 1, w + 1, h + 1); hotSpots.add(new TaskHotSpot(r, t)); TaskColors colors = graphColors.defaultTaskColors; boolean skinned = true; if (!skinned) { g.setColor(colors.barBg); g.fillRect(x1, y, w, h); } else { if (barSkin == null) // barSkin = getImage("res/img/taskskin.gif"); barSkin = getImage("res/img/taskskin.gif"); drawSkinedBar(g, barSkin, x1, y, x1 + w, y + h); } g.setColor(colors.barLine); g.drawRect(x1, y, w, h); if (t.getNotBeforeDate() != null) { // Colocar "tierra" g.drawLine(x1 - 2, y + h + 2, x1 + 3, y + h + 2); g.drawLine(x1, y + h + 4, x1 + 1, y + h + 4); } // grado de completitud int p = t.getCompleted(); if (p > 0) { if (!skinned) { g.setColor(colors.barCompleted); g.fillRect(x1, y1 + viewOptions.taskHeight / 2 - 2, (w * p) / 100, 4); } else { Graphics2D g2d = (Graphics2D) g; Composite c = g2d.getComposite(); g2d.setComposite(AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, 0.4f)); g.setColor(colors.barCompleted); g.fillRect(x1, y, (w * p) / 100, h); g2d.setComposite(c); } } } /** * Carga una imagen desde el classpath * * @param f * nombre del archivo de la imagen * @return Imagen */ private Image getImage(String f) { ImageIcon i = new ImageIcon(ClassLoader.getSystemResource(f), f); Image im = createImage(i.getIconWidth(), i.getIconHeight()); Graphics g2 = im.getGraphics(); i.paintIcon(this, g2, 0, 0); g2.dispose(); return im; } /** * Dibuja un skin como una barra. El skin es escalado segun los parametros * y1 e y2. * * @param g * Contexto grafico * @param skin * imagen con el skin. * @param x1 * @param y1 * @param x2 * @param y2 */ void drawSkinedBar(Graphics g, Image skin, int x1, int y1, int x2, int y2) { int h = skin.getHeight(null); int w = skin.getWidth(null); int border = 4; int right = w - border; int gap = right - border; int x = x1; int maxx = x2 - border; g.drawImage(skin, x1, y1, x1 + border, y2, 0, 0, border, h, Color.white, null); x += border; while (x < maxx) { int width = Math.min(maxx - x, gap); g.drawImage(skin, x, y1, x + width, y2, border, 0, border + width, h, null); x += width; } g.drawImage(skin, maxx, y1, x2, y2, right, 0, w, h, Color.white, null); } public void drawResumeTask(Graphics g, Task t, int x1, int y1, int x2, int y2) { GraphColors graphColors = project.getGraphColors(); // int y = y1 + (viewOptions.taskHeight - viewOptions.taskBarHeight) / // 2; int y = y1 + viewOptions.taskHeight / 3; int w = x2 - x1; int h = viewOptions.taskBarHeight / 2; // Guardar referencia al hotspot Rectangle r = new Rectangle(x1, y - 1, w + 1, h + 2); hotSpots.add(new TaskHotSpot(r, t)); TaskColors colors = graphColors.defaultResumeColors; boolean skinned = false; if (!skinned) { g.setColor(colors.barBg); g.fillRect(x1, y, w, h); g.setColor(colors.barLine); g.drawRect(x1, y, w, h); } else { if (resSkin == null) resSkin = getImage("res/img/zebra.gif"); drawSkinedBar(g, resSkin, x1, y1 + 3, x1 + w, y2 - 5); } if (t.getNotBeforeDate() != null) { // Colocar "tierra" g.drawLine(x1 - 2, y + h + 2, x1 + 3, y + h + 2); g.drawLine(x1, y + h + 4, x1 + 1, y + h + 4); } g.drawLine(x1, y1, x1, y2 - 4); g.drawLine(x2, y1, x2, y2 - 4); int p = t.getCompleted(); if (p > 0) { if (!skinned) { g.setColor(colors.barCompleted); g.fillRect(x1, y1 + viewOptions.taskHeight / 2 - 2, (w * p) / 100, 4); } else { Graphics2D g2d = (Graphics2D) g; Composite c = g2d.getComposite(); g2d.setComposite(AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, 0.4f)); g.setColor(colors.barCompleted); g.fillRect(x1, y1 + 3, (w * p) / 100, y2 - y1 - 5 - 3); g2d.setComposite(c); } } } public void drawConstraint(Graphics g, Vector points, Constraint ct) { Point p1, p2; // Guardar referencia al hotspot hotSpots.add(new ConstraintHotSpot(points, ct)); p1 = (Point) points.get(0); for (int i = 1; i < points.size(); i++) { p2 = (Point) points.get(i); g.drawLine(p1.x, p1.y, p2.x, p2.y); p1 = p2; } } /** * Guarda una referencia al objeto seleccionado. Si el objeto es una tarea, * ademas asigna el foco. * * @param p * @return */ public Object selectObjectAt(Point p) { lastSelectedObject = getElementAt(p); if (lastSelectedObject instanceof Task) pvModel.setCurrentTask((Task) lastSelectedObject); return lastSelectedObject; } /** * Resultado puede ser un constraint o un Task o null * * @return null, Task o Constraint */ public Object getElementAt(Point p) { for (int i = 0; i < hotSpots.size(); i++) { HotSpot hs = (HotSpot) hotSpots.get(i); Object o = hs.hit(p); if (o != null) return o; } return null; } abstract class HotSpot { abstract public Object hit(Point p); } public class TaskHotSpot extends HotSpot { Rectangle r; Task t; public TaskHotSpot(Rectangle r, Task t) { this.r = r; this.t = t; // System.out.println(t+" -> "+r); } public Object hit(Point p) { if (r.contains(p)) return t; return null; } } public class ConstraintHotSpot extends HotSpot { Vector points; Constraint c; public ConstraintHotSpot(Vector points, Constraint c) { this.points = points; this.c = c; } public Object hit(Point p) { Point p1, p2; p1 = (Point) points.get(0); for (int i = 1; i < points.size(); i++) { p2 = (Point) points.get(i); if (checkLine(p1.x, p1.y, p2.x, p2.y, p.x, p.y)) { return c; } p1 = p2; } return null; } /** * Verifica si un punto esta en una linea. * * @param ax * coordenada inicial de la linea (x) * @param ay * coordenada inicial de la linea (y) * @param bx * coordenada final de la linea (x) * @param by * coordenada final de la linea (y) * @param px * coordenada del punto (x) * @param py * coordenada del punto (x) * @return */ boolean checkLine(int ax, int ay, int bx, int by, int px, int py) { int dx, dy, fx, fy; double lambda; dx = bx - ax; dy = by - ay; fx = px - ax; fy = py - ay; lambda = (double) (dx * fx + dy * fy) / ((dx * dx) + (dy * dy)); if ((lambda > 0) && (lambda < 1)) { // { trunc(0->0.999)=0 } double ldfx = lambda * dx - fx; ldfx = ldfx * ldfx; double ldfy = lambda * dy - fy; ldfy = ldfy * ldfy; double sqrDist = ldfx + ldfy; if (sqrDist < 4) { return true; } } return false; } } public Object lastSelectedObject() { return lastSelectedObject; } public void paintViewer(Graphics g, Rectangle r) { paintComponent(g, r); } /** * Tooltip de varias lineas * * @see javax.swing.JComponent#createToolTip() */ public JToolTip createToolTip() { JToolTip tt = super.createToolTip(); tt.setBackground(new Color(255, 255, 240)); return tt; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -