2
Copyright (C) 2010-2011 Dmitry Barashev
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License
6
as published by the Free Software Foundation; either version 2
7
of the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
2
Copyright 2003-2012 Dmitry Barashev, GanttProject Team
4
This file is part of GanttProject, an opensource project management tool.
6
GanttProject is free software: you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation, either version 3 of the License, or
9
(at your option) any later version.
11
GanttProject is distributed in the hope that it will be useful,
10
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
14
GNU General Public License for more details.
14
16
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
along with GanttProject. If not, see <http://www.gnu.org/licenses/>.
18
19
package net.sourceforge.ganttproject.chart;
24
25
import java.awt.Font;
25
26
import java.awt.Graphics;
26
27
import java.awt.Graphics2D;
28
import java.util.ArrayList;
27
29
import java.util.HashMap;
30
import java.util.List;
28
31
import java.util.Map;
32
import java.util.Properties;
34
import javax.swing.JLabel;
36
import net.sourceforge.ganttproject.chart.GraphicPrimitiveContainer.HAlignment;
37
import net.sourceforge.ganttproject.chart.GraphicPrimitiveContainer.Label;
30
38
import net.sourceforge.ganttproject.chart.GraphicPrimitiveContainer.Line;
31
39
import net.sourceforge.ganttproject.chart.GraphicPrimitiveContainer.Rectangle;
32
40
import net.sourceforge.ganttproject.chart.GraphicPrimitiveContainer.Text;
41
import net.sourceforge.ganttproject.chart.GraphicPrimitiveContainer.TextGroup;
42
import net.sourceforge.ganttproject.chart.GraphicPrimitiveContainer.VAlignment;
33
43
import net.sourceforge.ganttproject.shape.ShapeConstants;
34
44
import net.sourceforge.ganttproject.shape.ShapePaint;
35
45
import net.sourceforge.ganttproject.task.Task;
36
46
import net.sourceforge.ganttproject.task.TaskActivity;
37
47
import net.sourceforge.ganttproject.time.TimeUnitText;
48
import net.sourceforge.ganttproject.util.PropertiesUtil;
38
49
import net.sourceforge.ganttproject.util.TextLengthCalculatorImpl;
595
633
myGraphics.setFont(next.getFont());
598
String nextTextString = next.getText();
600
int requestedMaxLength = next.getMaxLength();
601
if (next.getModelObject() != null) {
602
// TODO Maybe add this functionality to GraphicsPrimitiveContainer.Text?? (And make it more safe, as modelObject could be something else than TimeUnitText...)
604
TimeUnitText nextText = (TimeUnitText) next.getModelObject();
605
nextTextString = nextText.getText(requestedMaxLength,
606
myTextLengthCalculator);
609
int actualLength = myTextLengthCalculator.getTextLength(nextTextString);
610
if (requestedMaxLength >= 0 && actualLength > requestedMaxLength) {
611
return; // Text is too large
636
Label[] labels = next.getLabels(myTextLengthCalculator);
637
if (labels.length == 0) {
640
// int actualLength = myTextLengthCalculator.getTextLength(nextTextString);
641
// if (requestedMaxLength >= 0 && actualLength > requestedMaxLength) {
642
// return; // Text is too large
613
644
//FIXME This check if not 100% working (when scrolling to the right the text seems to disappear too soon...)
614
if (next.getLeftX() + actualLength < 0) {
615
return; // Text is not visible: too far to the left for current view
619
switch (next.getHAlignment()) {
621
xleft = next.getLeftX() - actualLength / 2;
624
xleft = next.getLeftX() - actualLength;
627
xleft = next.getLeftX();
629
switch (next.getVAlignment()) {
631
ybottom = next.getBottomY() + myGraphics.getFont().getSize() / 2;
634
ybottom = next.getBottomY() + myGraphics.getFont().getSize();
637
ybottom = next.getBottomY();
640
myGraphics.drawString(nextTextString, xleft, ybottom);
645
// if (next.getLeftX() + actualLength < 0) {
646
// return; // Text is not visible: too far to the left for current view
648
Label label = labels[0];
652
paint(next.getLeftX(), next.getBottomY(), next.getHAlignment(), next.getVAlignment(), label);
641
653
if (graphicFont != null) {
642
654
// Set Font back to original font
643
655
myGraphics.setFont(graphicFont);
659
private void paint(int xleft, int ybottom, HAlignment alignHor, VAlignment alignVer, Label label) {
662
xleft = xleft - label.lengthPx / 2;
665
xleft = xleft - label.lengthPx;
670
ybottom = ybottom + myGraphics.getFont().getSize() / 2;
673
ybottom = ybottom + myGraphics.getFont().getSize();
676
myGraphics.drawString(label.text, xleft, ybottom);
680
public void paint(TextGroup textGroup) {
681
TextLengthCalculatorImpl calculator = new TextLengthCalculatorImpl((Graphics2D)myGraphics.create());
682
FontChooser fontChooser = new FontChooser(myProperties, calculator);
683
textGroup.setFonts(fontChooser);
684
for (int i = 0; i < textGroup.getLineCount(); i++) {
685
paintTextLine(textGroup, i);
689
private void paintTextLine(TextGroup textGroup, int lineNum) {
690
List<Text> line = textGroup.getLine(lineNum);
691
Font savedFont = myGraphics.getFont();
692
Color savedColor = myGraphics.getColor();
694
myGraphics.setFont(textGroup.getFont(lineNum));
695
myGraphics.setColor(textGroup.getColor(lineNum));
697
List<Label[]> labelList = new ArrayList<Label[]>();
698
int maxIndex = Integer.MAX_VALUE;
699
for (Text t : line) {
700
Label[] labels = t.getLabels(myTextLengthCalculator);
701
maxIndex = Math.min(maxIndex, labels.length);
705
labelList.add(labels);
708
for (int i = 0; i < labelList.size(); i++) {
709
Label longest = labelList.get(i)[maxIndex - 1];
710
Text t = line.get(i);
711
paint(textGroup.getLeftX() + t.getLeftX(), textGroup.getBottomY(lineNum), t.getHAlignment(), t.getVAlignment(), longest);
714
myGraphics.setFont(savedFont);
715
myGraphics.setColor(savedColor);