GanttDisplay.java :  » Project-Management » lgantt » com » csa » lgantt » view » Java Open Source

Java Open Source » Project Management » lgantt 
lgantt » com » csa » lgantt » view » GanttDisplay.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 com.csa.lgantt.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.URL;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import com.csa.lgantt.Messages;
import com.csa.lgantt.model.Project;
import com.csa.lgantt.model.ProjectChange;
import com.csa.lgantt.model.ProjectListener;
import com.csa.lgantt.view.adapters.ProjectViewModel;
import com.csa.lgantt.view.adapters.ProjectViewModelChange;
import com.csa.lgantt.view.adapters.ProjectViewModelListener;
import com.csa.lgantt.view.gantt.GanttGraph;
import com.csa.lgantt.view.tree.TaskTree;


public class GanttDisplay
  extends JPanel
  implements ProjectListener, ProjectViewModelListener {
  private static final long serialVersionUID = 3257283630241165369L;
  TaskTree tasksTree;
  GanttGraph ganttGraph;
  JComboBox snapShotList;
  public ProjectViewModel pvModel;
  public Project project;
  JSplitPane splitPane;

  public boolean isFocusTraversable() {
    return false;
  }

  /**
   * Constructor for GanttDisplay
   */

  public GanttDisplay(ProjectViewModel projectViewModel) {
    super();
    pvModel = projectViewModel;
    pvModel.addListener(this);

    setLayout(new BorderLayout());

    tasksTree = new TaskTree(pvModel);
    ganttGraph = new GanttGraph(pvModel);

    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tasksTree, ganttGraph);
    //splitPane.setDividerSize(12);
    splitPane.setOneTouchExpandable(true);
    splitPane.setDividerLocation(tasksTree.getPreferredSize().width);

    add(splitPane, BorderLayout.CENTER);

    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));

    buttonPane.add(createButton(null, GanttActionListener.CMD_NEW, Messages.getString("GanttDisplay.newproject.hint"), Messages.getString("GanttDisplay.newproject.img"))); 
    buttonPane.add(createButton(null, GanttActionListener.CMD_OPEN, Messages.getString("GanttDisplay.open.hint"), Messages.getString("GanttDisplay.open.img"))); 

    buttonPane.add(Box.createRigidArea(new Dimension(10, 10)));
    buttonPane.add(createButton(null, GanttActionListener.CMD_SAVE, Messages.getString("GanttDisplay.save.hint"), Messages.getString("GanttDisplay.save.img"))); 
    buttonPane.add(createButton(null, GanttActionListener.CMD_SAVE_AS, Messages.getString("GanttDisplay.saveas.hint"), Messages.getString("GanttDisplay.saveas.img"))); 

    buttonPane.add(Box.createRigidArea(new Dimension(10, 10)));
    buttonPane.add(createButton(null, GanttActionListener.CMD_INSERT_TASK, Messages.getString("GanttDisplay.insert.hint"), Messages.getString("GanttDisplay.insert.img")));
    buttonPane.add(createButton(null, GanttActionListener.CMD_APPEND_TASK, Messages.getString("GanttDisplay.append.hint"), Messages.getString("GanttDisplay.append.img")));
    buttonPane.add(createButton(null, GanttActionListener.CMD_DELETE_TASK, Messages.getString("GanttDisplay.delete.hint"), Messages.getString("GanttDisplay.delete.img")));

    buttonPane.add(Box.createRigidArea(new Dimension(10, 10)));
    buttonPane.add(createButton(null, "indentl", Messages.getString("GanttDisplay.indentLeft.hint"), Messages.getString("GanttDisplay.indentLeft.img")));
    buttonPane.add(createButton(null, "indentr", Messages.getString("GanttDisplay.indentRight.hint"), Messages.getString("GanttDisplay.indentRight.img")));

    buttonPane.add(Box.createRigidArea(new Dimension(5, 5)));
    buttonPane.add(createButton(null, "movedn", Messages.getString("GanttDisplay.moveDown.hint"), Messages.getString("GanttDisplay.moveDown.img"))); 
    buttonPane.add(createButton(null, "moveup", Messages.getString("GanttDisplay.moveUp.hint"), Messages.getString("GanttDisplay.moveUp.img"))); 

    buttonPane.add(Box.createRigidArea(new Dimension(5, 5)));
    buttonPane.add(createButton(null, GanttActionListener.CMD_EDIT_ASIGNATIONS, Messages.getString("GanttDisplay.asignments.hint"), Messages.getString("GanttDisplay.asignments.img"))); 

    snapShotList = new JComboBox();
    snapShotList.addItemListener(new SnapItemListener());

    buttonPane.add(snapShotList);

    add(buttonPane, BorderLayout.NORTH);

    assignViewModel(pvModel);
  }

  /**
   * Genera un boton para ser anexado al panel
   * @param label
   * @param action
   * @param tooltip
   * @param imgFn
   * @return
   */
  public JButton createButton(
    String label,
    String action,
    String tooltip,
    String imgFn) {
    try {
      URL res = getClass().getClassLoader().getResource(imgFn);
      Image img = Toolkit.getDefaultToolkit().getImage(res);
      MediaTracker m = new MediaTracker(this);
      m.addImage(img, 0);
      m.waitForAll();
      ImageIcon icon = new ImageIcon(img);
      JButton b = new JButton(label, icon);
      b.setActionCommand(action);
      b.setToolTipText(tooltip);
      //b.setContentAreaFilled(false);
      b.setMargin(new Insets(0, 0, 0, 0));
      //      Dimension d = new Dimension(26,26);
      //      b.setPreferredSize(d);
      //      b.setSize(d);
      //      b.setMinimumSize(d);
      b.addActionListener(pvModel.getMainActionListener());
      return b;
    } catch (Exception e) {
      e.printStackTrace();
      JButton b = new JButton(label);
      b.setActionCommand(action);
      b.setToolTipText(tooltip);
      b.setContentAreaFilled(false);
      b.setMargin(new Insets(0, 0, 0, 0));
      b.addActionListener(pvModel.getMainActionListener());
      return b;
    }
  }

  /**
   * Carga una imagen desde el paquete de la clase
   * 
   * @param imgName
   * @return Image
   */
  Image loadImage(String imgName) {
    URL imgURL = getClass().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;
  }

  public JComponent getTaskTree() {
    return tasksTree;
  }

  public ProjectViewModel getProjectViewModel() {
    return pvModel;
  }

  public Object getSelectedObject() {
    return ganttGraph.getViewer().lastSelectedObject();
  }

  /* (non-Javadoc)
   * @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());
  }

  public void assignViewModel(ProjectViewModel pvm) {
    if (project != null)
      project.removeListener(this);
    project = pvm.getProject();
    project.addListener(this);
    pvModel = pvm;
    loadSnapShots(pvm.getProject());
  }

  public void loadSnapShots(Project p) {
    snapShotList.removeAllItems();
    snapShotList.addItem("Snapshot");
    for (Iterator i = p.getSnapshots().iterator(); i.hasNext();) {
      Project.SnapShot ss = (Project.SnapShot) i.next();
      snapShotList.addItem(ss);
    }
  }

  class SnapItemListener implements ItemListener {
    public void itemStateChanged(ItemEvent ev) {
      if (ev.getStateChange() == ItemEvent.SELECTED) {
        Object item = ev.getItem();
        if (item instanceof String)
          pvModel.setCurrentSnapShot(null);
        else {
          Project.SnapShot ss = (Project.SnapShot) item;
          pvModel.setCurrentSnapShot(ss.date);
        }
      }
    }
  }

  public void projectChanged(ProjectChange change) {
    switch (change.getId()) {
      case ProjectChange.SNAPSHOT_ADDED :
      case ProjectChange.SNAPSHOT_REMOVED :
        loadSnapShots(pvModel.getProject());
        break;
      default :
        ;
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.