📄 tasktree.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.tree;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Image;import java.awt.MediaTracker;import java.awt.Rectangle;import java.awt.Toolkit;import java.awt.event.AdjustmentEvent;import java.awt.event.AdjustmentListener;import java.awt.event.KeyEvent;import java.net.URL;import java.text.SimpleDateFormat;import java.util.Hashtable;import java.util.Iterator;import java.util.ResourceBundle;import java.util.StringTokenizer;import java.util.Vector;import javax.swing.JComponent;import javax.swing.JOptionPane;import javax.swing.JScrollBar;import jgantt.Messages;import jgantt.model.Project;import jgantt.model.ProjectChange;import jgantt.model.ProjectListener;import jgantt.model.Task;import jgantt.model.TaskTreeOptions;import jgantt.model.ViewOptions;import jgantt.view.adapters.ProjectViewModel;import jgantt.view.adapters.ProjectViewModelChange;import jgantt.view.adapters.ProjectViewModelListener;/** * TaskTree2 representa el editor de datos a la izquierda * <p> * $Date: 2005/07/18 07:25:09 $ * </p> * * @version $Revision: 1.45 $ * @author Carlos Silva */public class TaskTree extends JComponent implements ProjectListener, ProjectViewModelListener, ViewOptions.Observer, AdjustmentListener { private static final long serialVersionUID = 1L; // / Scrollbar horizontal private JScrollBar scroll; // / Icono de tarea resumen cerrada private Image iplus = null; // / Icono de tarea resumen abierta private Image iminus = null; // / Ancho de los iconos, se establece despues de cargarlos. private int iconWidth = 0; private int iconHeight = 0; // / Es necesario recalcular el ancho de las columnas? boolean recalcWidths = true; // / Editor usado para escribir las filas private MovilEditor movilEditor; // / LIsta de columnas por omision (archivos .xml) private Vector defaultColumns = new Vector(); // / Todas las columnas posibles Hashtable allColumns = new Hashtable(); // / Proyecto Project project; // / Modelo ProjectViewModel pvModel; // / Columnas visibles Vector columns = new Vector(); // / formato para las fechas en columnas SimpleDateFormat sdf = null; // / Colores y opciones de la tabla TaskTreeOptions treeOptions; // / rectangulo del foco en coordenadas de pantalla(con scroll?) Rectangle focusRect = null; // / Tarea con el foco ahora. Emula la tarea con el foco de viewModel Task focusTask = null; // / Columna con el foco. Column focusColumn = null; // / indica si se esta realizando una actualizacion de los scroll boolean reactToScrolling = true; /** * Permite que el foco de teclado se asigne a este objeto */ public boolean isFocusTraversable() { return true; } /** * Constructor for TaskTree */ public TaskTree(ProjectViewModel pvm) { super(); ResourceBundle bundle = ResourceBundle .getBundle("jgantt.view.tree.Columns"); String columnList = bundle.getString("columns"); StringTokenizer st = new StringTokenizer(columnList, " ,\t"); while (st.hasMoreTokens()) { String id = st.nextToken(); String desc = bundle.getString("column." + id); StringTokenizer elems = new StringTokenizer(desc, " ,\t"); int length = Integer.parseInt(elems.nextToken()); boolean readOnly = Integer.parseInt(elems.nextToken()) == 1; int align = Integer.parseInt(elems.nextToken()); String title = Messages.getString("TaskTree.Column." + id); allColumns.put(id, new Column(this, title, length, id, readOnly, align)); } st = new StringTokenizer(bundle.getString("columns.default"), " ,\t"); while (st.hasMoreTokens()) { defaultColumns.add(st.nextToken()); } iplus = loadImage(Messages.getString("tree.img.plus")); iminus = loadImage(Messages.getString("tree.img.minus")); iconWidth = iplus.getWidth(null); iconHeight = iplus.getHeight(null); movilEditor = new MovilEditor(this); setLayout(null); scroll = new JScrollBar(JScrollBar.HORIZONTAL); scroll.addAdjustmentListener(this); add(scroll); assignViewModel(pvm); pvModel.setTaskTree(this); pvModel.addListener(this); addMouseListener(new TreeMouseHandler(this)); addKeyListener(new TreeKeyHandler(this)); requestDefaultFocus(); } /** * Retorna la posicion de partida de la coordenada 0 * * @return */ public int getLeftPos() { return scroll.getValue(); } /** * Asigna el nivel de scroll de la tabla * * @param x */ public void setLeftPos(int x) { scroll.setValue(x); } /** * Coloca los componentes en la pantalla: tabla y scroller 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, bounds.height - sbh, bounds.width, sbh); scroll.setBounds(r); adjustScrollValues(); } /** * Carga una imagen desde el paquete de la clase * * @param imgName * @return Image */ Image loadImage(String imgName) { URL imgURL = getClass().getClassLoader().getResource(imgName); Toolkit tk = Toolkit.getDefaultToolkit(); Image img = null; try { MediaTracker m = new MediaTracker(this); img = tk.getImage(imgURL); m.addImage(img, 0); m.waitForAll(); } catch (Exception e) { e.printStackTrace(); } return img; } /** * Pinta el componente */ public void paintComponent(Graphics g) { super.paintComponent(g); paintComponent(g, getBounds()); } /** * Asigna los anchos de las columnas usando los fonts adecuados * * @param g */ void guessColWidths(Graphics g) { if (!recalcWidths) return; g.setFont(treeOptions.cellResumeFont); g.setFont(new Font("宋体", 0, 12)); FontMetrics resumeFM = g.getFontMetrics(); g.setFont(treeOptions.cellFont); g.setFont(new Font("宋体", 0, 12)); FontMetrics normalFM = g.getFontMetrics(); ViewOptions viewOptions = pvModel.getViewOptions(); int xp = 0; for (Iterator j = columns.iterator(); j.hasNext();) { Column c = (Column) j.next(); int maxWidth = 0; int taskCount = project.getTaskCount(); for (int i = 0; i < taskCount; i++) { Task t = project.getTask(i); String value = c.getValue(t); if (value == null) value = ""; int w = 10; if (t.isResume()) w += resumeFM.stringWidth(value); else w += normalFM.stringWidth(value); if (c.getProperty().equals("name")) w += t.getChildLevel() * iconWidth; maxWidth = Math.max(w, maxWidth); } c.setX(xp); c.setLength(maxWidth); xp += c.getLength(); } recalcWidths = false; adjustScrollValues(); } /** * Funcion de apoyo para pintar o generar imagenes */ public void paintComponent(Graphics g, Rectangle bounds) { super.paintComponent(g); g.setColor(getBackground()); g.fillRect(0, 0, bounds.width, bounds.height); guessColWidths(g); drawHeaders(g, bounds); drawData(g, bounds); } /** * Dibuja los encabezados de las columnas * * @param g * @param bounds */ public void drawHeaders(Graphics g, Rectangle bounds) { int x = -scroll.getValue(); int y = 0; int h = treeOptions.headerHeight - 1; // drawRect incluye el pixel en width for (Iterator i = columns.iterator(); i.hasNext();) { Column c = (Column) i.next(); int l = c.getLength(); g.setColor(treeOptions.headerBorder); g.drawRect(x, y, l, h); g.setColor(treeOptions.headerBg); g.fillRect(x + 2, y + 2, l - 2, h - 2); g.setColor(treeOptions.headerFontColor); g.setFont(treeOptions.headerFont); g.setFont(new Font("宋体", 0, 12)); g.drawString(c.getName(), x + 5, y + 18); x += l; } } /** * dibuja la tabla con los valores... * * @param g * @param bounds */ public void drawData(Graphics g, Rectangle bounds) { int h = treeOptions.rowHeight; int x = -scroll.getValue(); int y = treeOptions.headerHeight - 1; int taskCount = project.getVisibleTaskCount(); Rectangle frect = null; ViewOptions viewOptions = pvModel.getViewOptions(); int fontOffset = treeOptions.rowHeight - 4; int imgOffset = (treeOptions.rowHeight - iconHeight) / 2; for (int i = viewOptions.getTopRow(); i < taskCount; i++) { Task t = project.getVisibleTask(i); if (!t.isVisible()) continue; for (Iterator j = columns.iterator(); j.hasNext();) { Column c = (Column) j.next(); int l = c.getLength(); if ((focusTask == t) && (focusColumn == c)) { frect = new Rectangle(x + 1, y + 1, l - 1, h - 1); g.setColor(treeOptions.focusBg); g.fillRect(x, y, l, h); g.setColor(treeOptions.focusBorder); g.drawRect(x, y, l, h); } else { g.setColor(treeOptions.cellBorder); g.drawRect(x, y, l, h); } g.setColor(treeOptions.cellFontColor); if (t.isResume()){ g.setFont(treeOptions.cellResumeFont); g.setFont(new Font("宋体", 0, 12)); } else{ g.setFont(treeOptions.cellFont); g.setFont(new Font("宋体", 0, 12)); } if (c.getProperty().equals("name")) { int xp = x + 3 + (t.getChildLevel() - 1) * iconWidth;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -