📄 ganttgraph.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.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.event.AdjustmentEvent;import java.awt.event.AdjustmentListener;import javax.swing.JPanel;import javax.swing.JScrollBar;import jgantt.model.Project;import jgantt.model.ProjectChange;import jgantt.model.ProjectListener;import jgantt.model.Task;import jgantt.model.ViewOptions;import jgantt.view.adapters.ProjectViewModel;import jgantt.view.adapters.ProjectViewModelChange;import jgantt.view.adapters.ProjectViewModelListener;/** * Representa un grafico de una carta gantt. * La carta gantt contiene :</p> * <ul> * <li>Un header con las fechas en la parte superior * <li>Un visor de gantt en la parte central * <li>Un ScrollBar horizontal en la parte inferior * </ul> * * <p>$Header: /cvs/java/App_JGantt/source/jgantt/view/gantt/GanttGraph.java,v 1.23 2005/05/05 08:25:50 csilva Exp $</p> * @author csilva */public class GanttGraph extends JPanel implements ViewOptions.Observer, AdjustmentListener, ProjectListener, ProjectViewModelListener { private static final long serialVersionUID = 3618700798939706678L; Viewer viewer; Header header; JScrollBar horizScroll; JScrollBar vertScroll; Project project = null; ViewOptions viewOptions = null; boolean reactToScrolling = true; ProjectViewModel pvModel; public GanttGraph(ProjectViewModel projectViewModel) { super(); this.setFont(new Font("宋体", 0, 12)); assignViewModel(projectViewModel); pvModel.addListener(this); pvModel.setGanttGraph(this); setLayout(null); header = new Header(projectViewModel); viewer = new Viewer(projectViewModel); horizScroll = new JScrollBar(JScrollBar.HORIZONTAL); horizScroll.addAdjustmentListener(this); vertScroll= new JScrollBar(JScrollBar.VERTICAL); vertScroll.addAdjustmentListener(this); add(header); add(viewer); add(horizScroll); add(vertScroll); } /** * Esta funcion altera los limites del scrollbar de acuerdo al * largo del proyecto y el tama駉 de la ventana.</p> */ public void updateScroll() { int sbh = 18; Rectangle bounds = getBounds(); long visibleTime = viewOptions.msPixel * bounds.width / (1000 * 60 * 60 * 24); Task mainTask = project.getMainTask(); long projectDuration = (mainTask.getFinishDate().getTime() - mainTask.getStartDate().getTime()) / (1000 * 60 * 60 * 24); int halfDuration = (int) (projectDuration / 2); int min, max, val, va; if (visibleTime > projectDuration * 2) { // no hay scroll. min = viewOptions.startOffset; max = viewOptions.startOffset; val = viewOptions.startOffset; va = 1; } else { min = viewOptions.startOffset; max = (int) (projectDuration + halfDuration); val = (int) ((viewOptions.startDate.getTime() - project.getStartDate().getTime()) / (1000 * 60 * 60 * 24)); va = (int) visibleTime; } //Logger.log(5,"adjustScrollBar "+min+"/"+max+" va="+va+" pos="+val); reactToScrolling = false; horizScroll.setMinimum(min); horizScroll.setMaximum(max); horizScroll.setValue(val); horizScroll.setVisibleAmount(va); horizScroll.repaint(); // Scroll vertical min = 0; max = project.getVisibleTaskCount(); //val = vertScroll.getValue(); val = project.getViewOptions().getTopRow(); va = (bounds.height-viewOptions.headerHeight-sbh)/viewOptions.taskHeight; vertScroll.setMinimum(min); vertScroll.setMaximum(max); vertScroll.setValue(val); vertScroll.setVisibleAmount(va); vertScroll.repaint(); reactToScrolling = true; } /** * Coloca los componentes en la pantalla: header, gantt y scroll. * Luego llama a la funcion que indica que las propiedades graficas han cambiado * * @see java.awt.Component#doLayout() */ public void doLayout() { int sbh = 18; Rectangle bounds = getBounds(); Rectangle r = new Rectangle(0, 0, bounds.width - sbh, viewOptions.headerHeight); header.setBounds(r); r = new Rectangle( 0, viewOptions.headerHeight, bounds.width-sbh, bounds.height - viewOptions.headerHeight - sbh); viewer.setBounds(r); r = new Rectangle(0, bounds.height - sbh, bounds.width, sbh); horizScroll.setBounds(r); r = new Rectangle(bounds.width-sbh, 0, sbh, bounds.height - sbh); vertScroll.setBounds(r); viewOptionsChanged(); } /** * Se ejecuta cuando cambia la posicion del scrollbar de la carta gantt, se debe alterar el inicio de la carta gantt. * @see java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event.AdjustmentEvent) */ public void adjustmentValueChanged(AdjustmentEvent ev) { if (!reactToScrolling) return; if (ev.getSource()==horizScroll) viewOptions.setStartDate(project.getStartDate(), ev.getValue()); if (ev.getSource()==vertScroll) viewOptions.setTopRow(ev.getValue()); } /** * Cuando se modifican las propiedades de visualizacion se deben * alterar todos los datos en la pantalla. */ public void viewOptionsChanged() { updateScroll(); } /** * Atiende modificaciones en el proyecto parar actualizar el scroll * @see jgantt.model.ProjectListener#projectChanged(jgantt.model.ProjectChange) */ public void projectChanged(ProjectChange c) { switch (c.getId()){ case ProjectChange.CONSTRAINT: case ProjectChange.FORCED_CHANGE: case ProjectChange.START_DATE: case ProjectChange.STRUCTURE: case ProjectChange.TASK_ADDED: case ProjectChange.TASK_REMOVED: case ProjectChange.TASK_MODIFIED: updateScroll(); } } /** * * @see jgantt.view.adapters.ProjectViewModelListener#viewModelChanged(jgantt.view.adapters.ProjectViewModelChange) */ public void viewModelChanged(ProjectViewModelChange c) { if (c.getId()==ProjectViewModelChange.NEW_PROJECT_LOADED) assignViewModel(c.getProjectViewModel()); } /** * Asigna el modelo */ public void assignViewModel(ProjectViewModel pvm) { if (project != null) project.removeListener(this); if (viewOptions != null) viewOptions.removeObserver(this); pvModel = pvm; project = pvm.getProject(); viewOptions = pvm.getViewOptions(); viewOptions.addObserver(this); project.addListener(this); repaint(); } /** * Eventos del modelo * @see jgantt.view.adapters.ProjectViewModelListener#projectChanged(jgantt.view.adapters.ProjectViewModelChange) */ public void projectChanged(ProjectViewModelChange c) { if (c.getId()==ProjectViewModelChange.NEW_PROJECT_LOADED) assignViewModel(c.getProjectViewModel()); } public Viewer getViewer() { return viewer; } /** * Retorna el tama駉 de esta imagen * @return */ public Dimension getImageSize() { Task mainTask = project.getMainTask(); long projectDuration = (mainTask.getFinishDate().getTime() - mainTask.getStartDate().getTime() ); long visibleLen = ( mainTask.getFinishDate().getTime() - viewOptions.divTimes[0]); int width = (int) (visibleLen / viewOptions.msPixel)+100; width = Math.min(width, 1024); // altura tiene que ver solo con lo visible int visibles =0; for (int i=0;i<project.getTaskCount(); i++){ if (project.getTask(i).isVisible()) { visibles++; } } int height = viewOptions.headerHeight + viewOptions.taskHeight * visibles; return new Dimension(width, height); } /** * Pinta una carta gantt. incluye el header y el viewer. * Ignora la opcion {@link viewOptionsData#topRow}. * @param g * @param pvm * @param rect */ public void paintGraph(Graphics g) { Dimension dim = getImageSize(); Rectangle bounds = new Rectangle(0, 0, dim.width, dim.height); g.setColor(Color.gray); //Background g.fillRect(0, 0, bounds.width, bounds.height); g.setClip(0, 0, bounds.width, bounds.height); bounds.height = viewOptions.headerHeight; header.paintHeader(g, bounds); bounds.height = dim.height - viewOptions.headerHeight; g.translate( 0 , viewOptions.headerHeight ); g.setClip(0,0,dim.width, dim.height); int topRow = viewOptions.getTopRow(); viewer.paintViewer(g, bounds); viewOptions.setTopRow(topRow); g.setClip(null); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -