forked from LupusNobilis/java-org.hwo.ui
340 lines
7.2 KiB
Java
340 lines
7.2 KiB
Java
package org.hwo.ui.netplan;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Dimension;
|
|
import java.awt.FontMetrics;
|
|
import java.awt.Graphics;
|
|
import java.awt.Graphics2D;
|
|
import java.awt.Rectangle;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import javax.swing.JComponent;
|
|
import javax.swing.JScrollPane;
|
|
import javax.swing.JViewport;
|
|
import javax.swing.Scrollable;
|
|
import javax.swing.SwingConstants;
|
|
|
|
import org.hwo.datetime.Date;
|
|
|
|
public class JNetPlan extends JComponent {
|
|
|
|
List<INetPlanObject> netPlanObjects;
|
|
List<INetPlanResource> netPlanResources;
|
|
|
|
Date firstDate;
|
|
int days;
|
|
|
|
boolean timeVisible;
|
|
|
|
|
|
boolean relevantChange;
|
|
|
|
JViewport topPane,
|
|
leftPane;
|
|
|
|
|
|
|
|
public JNetPlan() {
|
|
netPlanObjects = new ArrayList<INetPlanObject>();
|
|
netPlanResources = new ArrayList<INetPlanResource>();
|
|
|
|
firstDate = new Date();
|
|
days = 31;
|
|
|
|
timeVisible = false;
|
|
|
|
relevantChange = true;
|
|
|
|
setDoubleBuffered(true);
|
|
|
|
topPane = new JViewport();
|
|
topPane.add(new TopPane());
|
|
leftPane = new JViewport();
|
|
leftPane.add(new Leftpane());
|
|
}
|
|
|
|
public void addNetPlanObject(INetPlanObject object){
|
|
if (!this.netPlanObjects.contains(object))
|
|
this.netPlanObjects.add(object);
|
|
|
|
for (INetPlanResource res: object.getResources()){
|
|
addNetPlanResource(res);
|
|
}
|
|
}
|
|
public void removeNetPlanObject(INetPlanObject object){
|
|
this.netPlanObjects.remove(object);
|
|
}
|
|
public INetPlanObject[] getNetPlanObjects(){
|
|
return this.netPlanObjects.toArray(new INetPlanObject[0]);
|
|
}
|
|
|
|
public void addNetPlanResource(INetPlanResource resource){
|
|
if (!this.netPlanResources.contains(resource))
|
|
this.netPlanResources.add(resource);
|
|
}
|
|
public void removeNetPlanResource(INetPlanResource resource){
|
|
this.netPlanResources.remove(resource);
|
|
}
|
|
public INetPlanResource[] getNetPlanResources(){
|
|
return this.netPlanResources.toArray(new INetPlanResource[0]);
|
|
}
|
|
|
|
public Date getFirstDate() {
|
|
return new Date(firstDate);
|
|
}
|
|
public void setFirstDate(Date firstDate) {
|
|
this.firstDate = new Date(firstDate);
|
|
}
|
|
|
|
public int getDays() {
|
|
return days;
|
|
}
|
|
public void setDays(int days) {
|
|
this.days = days;
|
|
}
|
|
|
|
public void setTimeVisible(boolean timeVisible) {
|
|
this.timeVisible = timeVisible;
|
|
}
|
|
public boolean isTimeVisible(){
|
|
return this.timeVisible;
|
|
}
|
|
|
|
/*********
|
|
* Layout variables...
|
|
*/
|
|
|
|
int resourceLines;
|
|
int lineHeight;
|
|
int resourceBaseHeight;
|
|
|
|
int leftColumnWidth;
|
|
|
|
int dayColumnWidth;
|
|
|
|
int minHeight;
|
|
int minWidth;
|
|
|
|
@Override
|
|
public void paint(Graphics _g) {
|
|
Graphics2D g = (Graphics2D)_g;
|
|
|
|
int n;
|
|
|
|
updateSizes();
|
|
|
|
g.setColor(getBackground());
|
|
g.fillRect(0, 0, getWidth(), getHeight());
|
|
|
|
g.setColor(Color.BLACK);
|
|
// g.drawLine(leftColumnWidth, 0, leftColumnWidth, getHeight());
|
|
|
|
Date date = getFirstDate();
|
|
|
|
for (n = 0; n < days; n++){
|
|
int l = ((n) * dayColumnWidth);
|
|
|
|
g.drawLine(((n+1) * dayColumnWidth), 0, ((n+1) * dayColumnWidth), minHeight);
|
|
}
|
|
|
|
|
|
n = 0;
|
|
for (INetPlanResource resource: this.netPlanResources){
|
|
n += resource.getMaxParallelUsage();
|
|
|
|
g.setColor(Color.LIGHT_GRAY);
|
|
|
|
g.drawLine(0, 4 + (n * lineHeight), minWidth, 4 + (n * lineHeight));
|
|
}
|
|
|
|
for (INetPlanObject object: netPlanObjects){
|
|
|
|
|
|
|
|
g.setColor(Color.RED);
|
|
//g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private void updateSizes(){
|
|
if (!relevantChange)
|
|
return;
|
|
|
|
Graphics2D g = (Graphics2D)getGraphics();
|
|
FontMetrics fm = getFontMetrics(getFont());
|
|
|
|
//lineHeight = (int)(getFont().getSize2D()*72) + 4;
|
|
lineHeight = getFont().getSize() + 4;
|
|
|
|
resourceBaseHeight = lineHeight * 3;
|
|
|
|
if (isTimeVisible())
|
|
dayColumnWidth = 96;
|
|
else
|
|
dayColumnWidth = ((int)fm.getStringBounds("WWW", g).getWidth()) + 10;
|
|
|
|
resourceLines = 0;
|
|
leftColumnWidth = 0;
|
|
for (INetPlanResource resource: this.netPlanResources){
|
|
int lw = (int)fm.getStringBounds(resource.getLabel(), g).getWidth();
|
|
|
|
if (lw > leftColumnWidth)
|
|
leftColumnWidth = lw;
|
|
|
|
resourceLines += resource.getMaxParallelUsage();
|
|
}
|
|
|
|
leftColumnWidth += 5;
|
|
|
|
|
|
minHeight = 4 + (lineHeight * resourceLines);
|
|
minWidth = ( dayColumnWidth * days );
|
|
|
|
setPreferredSize(new Dimension(minWidth, minHeight));
|
|
|
|
leftPane.setSize(new Dimension(leftColumnWidth,minHeight));
|
|
topPane.setSize(new Dimension(minWidth, 4 * lineHeight));
|
|
|
|
relevantChange = false;
|
|
}
|
|
|
|
|
|
|
|
|
|
public class TopPane extends JComponent {
|
|
|
|
@Override
|
|
public Dimension getPreferredSize() {
|
|
return new Dimension(minWidth,4 * lineHeight);
|
|
}
|
|
|
|
@Override
|
|
public void paint(Graphics _g) {
|
|
Graphics2D g = (Graphics2D)_g;
|
|
|
|
int n;
|
|
|
|
updateSizes();
|
|
|
|
g.setColor(getBackground());
|
|
g.fillRect(0, 0, getWidth(), getHeight());
|
|
|
|
g.setColor(Color.BLACK);
|
|
// g.drawLine(leftColumnWidth, 0, leftColumnWidth, getHeight());
|
|
|
|
g.drawLine(0, resourceBaseHeight, getWidth(), resourceBaseHeight);
|
|
|
|
Date date = getFirstDate();
|
|
|
|
for (n = 0; n < days; n++){
|
|
int l = ((n) * dayColumnWidth);
|
|
|
|
g.drawLine(((n+1) * dayColumnWidth), resourceBaseHeight - (2 * lineHeight), ((n+1) * dayColumnWidth), minHeight);
|
|
|
|
g.drawString(String.format("%d", date.getDay()), l + 2, resourceBaseHeight - lineHeight - 2);
|
|
g.drawString(String.format("%s", date.weekdays[date.getWeekDay()]), l + 2, resourceBaseHeight - 2);
|
|
|
|
date.addDays(1);
|
|
}
|
|
}
|
|
}
|
|
|
|
public class Leftpane extends JComponent {
|
|
|
|
public Leftpane() {
|
|
}
|
|
|
|
@Override
|
|
public Dimension getPreferredSize() {
|
|
return new Dimension(leftColumnWidth,minHeight);
|
|
}
|
|
|
|
@Override
|
|
public void paint(Graphics _g) {
|
|
Graphics2D g = (Graphics2D)_g;
|
|
|
|
int n;
|
|
|
|
updateSizes();
|
|
|
|
g.setColor(getBackground());
|
|
g.fillRect(0, 0, getWidth(), getHeight());
|
|
|
|
g.setColor(Color.BLACK);
|
|
// g.drawLine(leftColumnWidth, 0, leftColumnWidth, getHeight());
|
|
|
|
// g.drawLine(0, 0, getWidth(), 0);
|
|
|
|
n = 0;
|
|
for (INetPlanResource resource: netPlanResources){
|
|
n += resource.getMaxParallelUsage();
|
|
|
|
g.setColor(Color.BLACK);
|
|
g.drawString(resource.getLabel(), 2, 1 + (n * lineHeight));
|
|
g.setColor(Color.LIGHT_GRAY);
|
|
|
|
g.drawLine(0, 4 + (n * lineHeight), getWidth(), 4 + (n * lineHeight));
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
@Override
|
|
public Dimension getPreferredScrollableViewportSize() {
|
|
return getPreferredSize();
|
|
}
|
|
|
|
@Override
|
|
public int getScrollableBlockIncrement(Rectangle visibleRect,
|
|
int orientation, int direction) {
|
|
if (orientation == SwingConstants.HORIZONTAL)
|
|
return visibleRect.width / 2;
|
|
else
|
|
return visibleRect.height / 2;
|
|
}
|
|
|
|
@Override
|
|
public boolean getScrollableTracksViewportHeight() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean getScrollableTracksViewportWidth() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public int getScrollableUnitIncrement(Rectangle visibleRect,
|
|
int orientation, int direction) {
|
|
if (orientation == SwingConstants.HORIZONTAL)
|
|
return 10;
|
|
else
|
|
return lineHeight;
|
|
}
|
|
*/
|
|
@Override
|
|
public void addNotify() {
|
|
super.addNotify();
|
|
|
|
if (JViewport.class.isInstance(getParent()) && JScrollPane.class.isInstance(getParent().getParent()))
|
|
{
|
|
JScrollPane sp = (JScrollPane)getParent().getParent();
|
|
sp.setColumnHeader(topPane);
|
|
sp.setRowHeader(leftPane);
|
|
}
|
|
|
|
|
|
}
|
|
@Override
|
|
public void removeNotify() {
|
|
super.removeNotify();
|
|
}
|
|
|
|
}
|