📄 ganttxfigsaver.java
字号:
/*************************************************************************** GanttXFIGSaver.java ------------------- begin : 15 juin 2004 copyright : (C) 2004 by Thomas Alexandre email : alexthomas(at)ganttproject.org ***************************************************************************//*************************************************************************** * * * This program 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. * * * ***************************************************************************/package net.sourceforge.ganttproject.io;import java.awt.Color;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import java.util.List;import javax.swing.tree.DefaultMutableTreeNode;import net.sourceforge.ganttproject.GanttCalendar;import net.sourceforge.ganttproject.GanttTask;import net.sourceforge.ganttproject.IGanttProject;import net.sourceforge.ganttproject.task.Task;import net.sourceforge.ganttproject.task.TaskManager;/** * @author athomas This class is based on the octave program of A S Hodel * (hodelas(at)ieee.org) from Auburn University check the URL at * http://www.eng.auburn.edu/users/hodelas/LaTeX/ganttProject_mn.html It * allow to export a project into an xfig scalable vector image. */public class GanttXFIGSaver { // fields // private GanttTree tree; // private GanttGraphicArea area; // private PrjInfos prjInfos; List lot = new ArrayList(); // list of tasks // ArrayList lots = new ArrayList(); ArrayList loc = new ArrayList(); // list of colors ArrayList atl = new ArrayList(); // list of text object ArrayList abl = new ArrayList(); // list of box object // store the start of the project GanttCalendar dateShift = new GanttCalendar(); float scale = 0, chwidth = 0; // width of the text float fTtextwidth = 0.0f; // debug value to print some text on standrad output.. boolean debug = true; private TaskManager myTaskManager; /** Constructor. */ public GanttXFIGSaver(IGanttProject project) { // this.area = area; // this.prjInfos = prjInfos; myTaskManager = project.getTaskManager(); } /** Save the project as XFIG on a stream */ public void save(OutputStream stream) { try { OutputStreamWriter fout = new OutputStreamWriter(stream); beginToSave(fout); fout.close(); // close the stream } catch (IOException e) { System.out.println(e); System.out.println("Error in saving the xfig file"); } } /** Start saving the xfig image. */ public void beginToSave(OutputStreamWriter fout) throws IOException { if (debug) System.out.println("beginToSave begin"); float depthval = 50; // depth level - 999 is the lowest level. // targetWidthPoints is in pixels, 1200 pixels per inch, // or about 472 pixels/cm float targetWidthPoints = 1200; // get the list of tasks on an array list // lot = tree.getAllTasks(); lot = Arrays.asList(myTaskManager.getTasks()); fTtextwidth = getProjectTextWidth(); if (debug) System.out.println("Text Size : " + fTtextwidth); // write the xfig header xfigheader(fout); // Search for the user colors searchUserColor(); // Need to give user control of date range created in plot setProjectPlotTimes(targetWidthPoints, 50); // Write the list of tasks drawTasks(fout); // Write the axes labelAxes(fout); if (debug) System.out.println("beginToSave end"); } /** * @return the with of the text. Determine width of text labels (task names) * in this project assume fixed width, 14 letters to the inch, 1200 * ppi each level is indented 1/4 inch (300 points @ 1200ppi) */ public float getProjectTextWidth() { if (debug) System.out.println("getProjectTextWidth begin"); float res = 0.0f; for (Iterator it = lot.iterator(); it.hasNext();) { // get the name of the task Task task = (Task) it.next(); float taskTextWidth = getTaskTextWidth(task); if (taskTextWidth > res) { res = taskTextWidth; } } if (debug) System.out.println("getProjectTextWidth end"); return res; } /** * @return the textwith of this task for and its children tasks. Determine * width of text labels (task names) in this task assume fixed * width, 14 letters to the inch, 1200 ppi each level is indented * 1/4 inch (300 points @ 1200ppi) */ private float getTaskTextWidth(Task task) { if (debug) System.out.println("getTaskTextWidth begin"); Task t = task; float res = (float) t.getName().length() * (1.0f / 14.0f) * (1200.0f); Task[] children = task.getManager().getTaskHierarchy().getNestedTasks( task); for (int i = 0; i < children.length; i++) { float subTaskTextWidth = 0.25f + getTaskTextWidth((children[i])); if (subTaskTextWidth > res) res = subTaskTextWidth; } if (debug) System.out.println("getTaskTextWidth end"); return res; } /** * Write the header of the XFIG FILE Based on the xfig file specification. */ public void xfigheader(OutputStreamWriter fout) throws IOException { if (debug) System.out.println("xfigheader begin"); fout.write("#FIG 3.2\n"); // version fout.write("Landscape\n"); // orientation fout.write("Center\n"); // justification fout.write("Inches\n"); // units fout.write("Letter\n"); // papersize fout.write("100.0\n"); // magnification fout.write("Single\n"); // multiplePage fout.write("-2\n"); // transparentColor fout.write("1200 2\n"); // resolutionPpi origin if (debug) System.out.println("xfigheader end"); } /** Search for the corresponding colors. */ public void searchUserColor() // TODO continue to write this method { if (debug) System.out.println("searchUserColor begin"); loc.clear(); // clear the list for (Iterator it = lot.iterator(); it.hasNext();) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) it.next(); if (!node.isRoot()) { GanttTask task = (GanttTask) (node.getUserObject()); Color color = task.getColor(); String hexaColor = getHexaColor(color); } } if (debug) System.out.println("searchUserColor end"); } /** * get project time range, compute box scaling values and shifts so that the * total chart fits in targetWidthPoints units: targetWidthPoints is in * pixels, 1200 pixels per inch, or about 472 pixels/cm this also adds box * and text objects to the project tasks for plotting later */ public void setProjectPlotTimes(float targetWidthPoints, float depthval) { if (debug) System.out.println("setProjectPlotTimes begin"); float targetwidth = targetWidthPoints - fTtextwidth; GanttCalendar startDate = null, endDate = null; // Get project start, end times for (Iterator it = lot.iterator(); it.hasNext();) { // get the task Task task = (Task) (((DefaultMutableTreeNode) it.next()) .getUserObject()); if (startDate == null && endDate == null) { startDate = task.getStart(); endDate = task.getEnd(); } else { if (task.getStart().compareTo(startDate) == -1) // before startDate = task.getStart(); if (task.getEnd().compareTo(startDate) == 1) // after endDate = task.getEnd(); } } // shift all dates by this amount to plot dateShift = startDate; scale = targetwidth / Math.max(1.0f, (float) (endDate.diff(startDate))); chwidth = scale * (float) (endDate.diff(startDate)); System.out.println("Chart width =" + chwidth + " points = " + (chwidth / 1200) + " inches\n"); int index = 0; // now add text and box objects to the tasks for (Iterator it = lot.iterator(); it.hasNext();) { DefaultMutableTreeNode node = ((DefaultMutableTreeNode) it.next()); if (!node.isRoot()) { // get the task GanttTask task = (GanttTask) node.getUserObject(); // get the text infos of the task TextObject textLabel = task2text(task, index, node.getLevel(), (int) depthval); if (debug) System.out.println(" add TEXT"); atl.add(textLabel); // get the box object of the task BoxObject boxObject = task2box(task, (int) depthval, index, node.isLeaf()); if (debug) System.out.println(" add BOX"); abl.add(boxObject); index++; } } if (debug) System.out.println("setProjectPlotTimes end + index=" + index); } /** convert task structureto an xfig text data structure */ public TextObject task2text(GanttTask task, int number, int level, int depthval) { if (debug) System.out.println("task2text begin"); /* * System.out.println("task : "+task+ " number : "+number+ " level : * "+level+ " depthval : "+depthval); */ TextObject taskText = new TextObject(); taskText.sub_type = 0; // left justified taskText.color = 0; // black taskText.depth = depthval; taskText.pen_style = 0; // unused taskText.font = 0; // Times Roman taskText.font_size = 10; // fonts size in points taskText.angle = 0; // text angle in radians taskText.font_flags = 4; // not rigid, not special, postscript, not // hidden. taskText.height = 0.25f * 1200f; taskText.length = 0.125f * (float) (task.getName().length()) * 1200.0f; taskText.y = (int) (1200f * 0.25f * (float) number + 5.0f - 1200.0f / 16.0f); taskText.x = (int) (1200.0f * 0.25f * ((float) level + 1.0f)); taskText.str = task.getName(); if (debug) System.out.println("task2text end"); return taskText; } /** get the task box. */ public BoxObject task2box(GanttTask task, int depthval, int number, boolean isLeaf) { // boxObject: total box // boxObjectPct: black line in center of box object // min box width: 1. // object to return BoxObject boxObject = new BoxObject(); if (isLeaf) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -