/**
* Copyright (C) 1998-2002 by ILOG.
* All rights reserved.
*
* This source code is an addition to the ILOG JViews Reference Manual
* delivered with the JViews library.
* It is supplied as an example to show you how to code with JViews.
* Feel free to use, copy, or modify it within the framework of your
* JViews license agreement.
*
*/
package demos.gantt.charts;
import shared.*;
import shared.data.*;
import shared.swing.*;
import ilog.views.gantt.*;
import ilog.views.gantt.action.*;
import ilog.views.gantt.model.*;
import ilog.views.gantt.swing.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
/**
* A Gantt chart example.
*/
public class GanttExample extends AbstractGanttExample
{
/**
* The various menu and toolbar actions.
*/
protected IlvAction makeConstraintAction;
protected IlvAction deleteConstraintAction;
// =========================================
// Instance Construction and Initialization
// =========================================
/**
* Creates the Gantt chart.
*/
protected IlvHierarchyChart createChart()
{
return new IlvGanttChart();
}
/**
* Customizes the data model factories and graphic renderers of the
* Gantt chart.
*/
protected void customizeFactories()
{
// Change the default activity and resource factories of the Gantt chart
// to ones that sequentially number the activities and resources.
chart.setActivityFactory(new SimpleActivityFactory(chart));
chart.setResourceFactory(new SimpleResourceFactory(chart));
}
/**
* Customizes the appearance of the Gantt chart.
*/
protected void customizeChart()
{
super.customizeChart();
// Load the resource bundles containing the locale dependent table header
// labels for the gantt chart.
ResourceBundle bundle =
ResourceBundle.getBundle("demos.gantt.charts.resources.gantt",
getLocale());
// Default column widths are 75. Resize some of the columns so that the
// text is not truncated.
IlvJTable table = chart.getTable();
table.getColumn("Name").setPreferredWidth(175);
table.getColumn("Start").setPreferredWidth(90);
table.getColumn("End").setPreferredWidth(90);
// Add a duration column after the End column.
CalendarDurationProperty durationProperty = new CalendarDurationProperty(getDurationFormat());
IlvStringColumn durationColumn =
new IlvStringColumn(bundle.getString("TableColumn.Duration.label"),
durationProperty,
"Duration")
{
public boolean isEditable(IlvHierarchyNode row)
{
boolean retVal = super.isEditable(row);
if (retVal && row instanceof IlvSimpleActivity && getGanttModel().getChildCount(row) > 0)
if (((IlvSimpleActivity)row).getAutoCalcTimeIntervalFromChildren())
retVal = false;
return retVal;
}
};
durationColumn.getColumn().setPreferredWidth(90);
table.addColumn(durationColumn);
table.moveColumn(durationColumn.getColumnIndex(), 3);
}
// =========================================
// Accessing
// =========================================
/**
* Returns whether any activities are currently selected.
*/
public boolean isActivitySelected()
{
return chart.getSelectedRows().length > 0;
}
/**
* Returns whether any constraints are currently selected.
*/
public boolean isConstraintSelected()
{
return chart.getSelectedConstraints().length > 0;
}
// =========================================
// Menus
// =========================================
/**
* Creates the Edit menu.
*/
public JMenu createEditMenu()
{
JMenu menu = new JMenu("Edit");
menu.setMnemonic(KeyEvent.VK_E);
setHelpID(menu, "Menu.Edit.helpID");
setStatusText(menu, "Adds, removes, or modifies activities and constraints.");
JMenuItem item;
KeyStroke accelerator;
// Menu item for "Insert new activity".
insertRowAction = new GanttCommand.InsertActivityAction((IlvGanttChart)chart);
addAction(menu, insertRowAction);
// Menu item for "Delete selected activity".
deleteRowAction = new GanttCommand.DeleteSelectedRowsAction(chart);
addAction(menu, deleteRowAction);
// Menu item for "Create new constraint".
makeConstraintAction = new GanttCommand.MakeConstraintAction(chart);
addAction(menu, makeConstraintAction);
// Menu item for "Delete selected constraints".
deleteConstraintAction = new GanttCommand.DeleteSelectedConstraintsAction(chart);
addAction(menu, deleteConstraintAction);
menu.addSeparator();
// Menu item for "Delete all".
deleteAllAction = new GanttCommand.DeleteSelectedAction(chart);
addAction(menu, deleteAllAction);
menu.addSeparator();
// Menu item for "Expand/Collapse".
rowExpandCollapseAction = new GanttCommand.RowExpandCollapseAction(chart);
addAction(menu, rowExpandCollapseAction);
// Menu item for "Indent activity".
rowIndentAction = new GanttCommand.RowIndentAction(chart);
addAction(menu, rowIndentAction);
// Menu item for "Outdent activity".
rowOutdentAction = new GanttCommand.RowOutdentAction(chart);
addAction(menu, rowOutdentAction);
// Menu item for "Move up".
rowUpAction = new GanttCommand.RowUpAction(chart);
addAction(menu, rowUpAction);
// Menu item for "Move down".
rowDownAction = new GanttCommand.RowDownAction(chart);
addAction(menu, rowDownAction);
// Menu item for "Exit".
if (!isApplet()) {
menu.addSeparator();
exitAction = new GanttCommand.ExitAction();
addAction(menu, exitAction);
}
return menu;
}
// =========================================
// Customizer Panel
// =========================================
/**
* Creates and returns a customizer for view options and formatting.
*/
protected JComponent createViewCustomizer()
{
CustomizerPanel panel = (CustomizerPanel)super.createViewCustomizer();
// Add the Duration formatting combobox.
panel.addSeparator(5);
panel.addHeading("Duration Format");
DurationFormatCombo durationCombo = new DurationFormatCombo(this);
durationCombo.setToolTipText("Select Displayed Duration Format");
panel.add(durationCombo);
return panel;
}
// =========================================
// Toolbar
// =========================================
/**
* Populates the toolbar edit actions.
*/
protected void populateToolBarEditActions(JToolBar toolbar)
{
super.populateToolBarEditActions(toolbar);
addAction(toolbar, makeConstraintAction);
addAction(toolbar, deleteConstraintAction);
addAction(toolbar, deleteAllAction);
}
// =========================================
// Data Model
// =========================================
/**
* Creates and returns the Gantt data model.
*/
protected IlvGanttModel createGanttModel()
{
return new SimpleEngineeringProject(chart);
}
// =========================================
// Example Application
// =========================================
/**
* Returns the title of the example.
*/
public String getTitle()
{
return "Gantt Chart Example";
}
/**
* Application mainline.
*/
public static void main (String[] args)
{
GanttExample ganttChart = new GanttExample();
JFrame frame = new ExampleFrame(ganttChart);
frame.setVisible(true);
}
}