📄 ganttmpxjopen.java
字号:
/* * file: GanttMPXJOpen.java * author: Jon Iles * copyright: (c) Tapster Rock Limited 2005 * date: 10/01/2005 *//* * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation; either version 2.1 of the License, or (at your * option) any later version. * * This library 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 Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */package org.ganttproject.impex.msproject;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.lang.reflect.Constructor;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.Locale;import javax.swing.tree.DefaultMutableTreeNode;import net.sourceforge.ganttproject.GanttCalendar;import net.sourceforge.ganttproject.GanttProject;import net.sourceforge.ganttproject.GanttTask;import net.sourceforge.ganttproject.GanttTaskRelationship;import net.sourceforge.ganttproject.GanttTree2;import net.sourceforge.ganttproject.gui.GanttDialogInfo;import net.sourceforge.ganttproject.language.GanttLanguage;import net.sourceforge.ganttproject.resource.HumanResource;import net.sourceforge.ganttproject.resource.HumanResourceManager;import net.sourceforge.ganttproject.resource.ProjectResource;import net.sourceforge.ganttproject.task.ResourceAssignment;import net.sourceforge.ganttproject.task.TaskManager;import net.sourceforge.ganttproject.task.TaskNode;import net.sourceforge.ganttproject.task.dependency.TaskDependency;import net.sourceforge.ganttproject.task.dependency.TaskDependencyException;import com.tapsterrock.mpx.MPXCalendar;import com.tapsterrock.mpx.MPXDuration;import com.tapsterrock.mpx.MPXException;import com.tapsterrock.mpx.MPXFile;import com.tapsterrock.mpx.Priority;import com.tapsterrock.mpx.Relation;import com.tapsterrock.mpx.RelationList;import com.tapsterrock.mpx.RelationType;import com.tapsterrock.mpx.Resource;import com.tapsterrock.mpx.Task;import com.tapsterrock.mpx.TimeUnit;/** * This class implements the mechanism used to import Microsoft Project data * into the GanttProject application using MPXJ. */public abstract class GanttMPXJOpen { /** * Constructor. */ public GanttMPXJOpen(GanttTree2 tasks, GanttProject project, Locale mpxLocale) { m_tasks = tasks; m_project = project; m_mpxImportLocale = mpxLocale; } /** * Constructor. */ public GanttMPXJOpen(GanttTree2 tasks, GanttProject project) { m_tasks = tasks; m_project = project; } /** * This method opens a file based on the supplied filename. * * @param filename * filename * @return boolean flag * @throws MPXException */ public boolean load(String filename) throws MPXException { return (load(new File(filename))); } /** * Load the contents of a file. * * @param file * input file * @return boolean flag * @throws MPXException */ public abstract boolean load(File file) throws MPXException; /** * Load the contents of a project from an input stream. * * @param is * input stream * @return boolean flag * @throws MPXException */ public abstract boolean load(InputStream is) throws MPXException; /** * This method loads project data from an input stream. The type of data * expected in the stream is indicated by the class passed as the first * argument to this method. The file argument is used purely for error * reporting, if it is not null then error messages generated by this method * can refer to the source file. Finally, if the input stream argument is * not null, the data is loaded from the supplied input stream. If it is * null, then a new input stream is created from the supplied file object. * * @param mpxjClass * MPXJ class representing the type of file to be imported * @param file * file instance * @param is * input stream instance * @return boolean flag * @throws Exception */ protected boolean load(MPXFile mpx) throws MPXException { processResources(mpx); try { processTasks(mpx); } catch (Exception e) { throw new MPXException(e.getMessage(), e); } processRelationships(mpx); processResourceAssignments(mpx); return true; } /** * This method extracts all of the resources from the MPXFile instance and * creates the appropriate GanttProject resource representation. * * @param mpx * Current MPXFile instance */ private void processResources(MPXFile mpx) { HumanResourceManager hrm = (HumanResourceManager) m_project .getHumanResourceManager(); LinkedList resources = mpx.getAllResources(); Iterator iter = resources.iterator(); Resource resource; HumanResource people; while (iter.hasNext() == true) { resource = (Resource) iter.next(); if (resource.getName() != null) { people = hrm.newHumanResource(); people.setName(resource.getName()); people.setMail(resource.getEmailAddress()); hrm.add(people); m_resourceMap .put(resource.getID(), new Integer(people.getId())); } } } /** * This method is used to extract all tasks from an MPXFile instance, and * create the equivalent data structures in GanttProject. * * @param mpx * Current MPXFile instance * @throws Exception */ private void processTasks(MPXFile mpx) throws Exception { TaskManager tm = m_project.getTaskManager(); MPXCalendar cal = mpx.getBaseCalendar("Standard"); LinkedList tasks = mpx.getChildTasks(); Iterator iter = tasks.iterator(); while (iter.hasNext() == true) { processTask(tm, cal, (Task) iter.next(), null); } } /** * This method is called recursively to process the task hierarchy and * create the equivalent data structure in GanttProject. * * @param defaultCalendar * BaseCalendar instance * @param task * Parent task * @param node * Parent node * @throws Exception */ private void processTask(TaskManager tm, MPXCalendar defaultCalendar, Task task, DefaultMutableTreeNode node) throws Exception { // // Calculate the duration in days // MPXCalendar taskCalendar = task.getCalendar(); MPXCalendar cal; if (taskCalendar != null) { cal = taskCalendar; } else { cal = defaultCalendar; } final MPXDuration duration; boolean milestone = task.getMilestoneValue(); if (milestone == true) { duration = MILESTONE_DURATION; } else { Date taskStart = task.getStart(); Date taskFinish = task.getFinish(); if (taskStart != null && taskFinish != null) { //duration = cal.getDuration(taskStart, taskFinish); duration = task.getDuration(); } else { duration = task.getDuration(); } } // // Create the new task object // GanttTask gtask = tm.createTask(); // gtask.setChecked(); // gtask.setColor(); gtask.setCompletionPercentage((int) task.getPercentageCompleteValue());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -