~veger/ganttproject/manual-import

« back to all changes in this revision

Viewing changes to ganttproject/src/net/sourceforge/ganttproject/chart/StyledPainterImpl.java

  • Committer: Maarten Bezemer
  • Date: 2012-01-22 12:20:00 UTC
  • Revision ID: maarten.bezemer@gmail.com-20120122122000-qwyec45rjx86wi7o
Updated till 2fe683a778c3 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* LICENSE: GPL2
2
 
Copyright (C) 2010-2011 Dmitry Barashev
3
 
 
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.
8
 
 
9
 
This program is distributed in the hope that it will be useful,
 
1
/*
 
2
Copyright 2003-2012 Dmitry Barashev, GanttProject Team
 
3
 
 
4
This file is part of GanttProject, an opensource project management tool.
 
5
 
 
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.
 
10
 
 
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.
13
15
 
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/>.
17
18
*/
18
19
package net.sourceforge.ganttproject.chart;
19
20
 
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;
29
 
 
 
32
import java.util.Properties;
 
33
 
 
34
import javax.swing.JLabel;
 
35
 
 
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;
39
50
 
40
51
/**
59
70
    /** List Y coordinates used to draw polygons */
60
71
    private int[] myYPoints = new int[4];
61
72
 
 
73
    private Properties myProperties;
 
74
 
62
75
    /** Default stroke used for the primitives */
63
76
    private final static BasicStroke defaultStroke = new BasicStroke();
64
77
 
108
121
        myStyle2painter.put("load.underload.last", myResourceLoadPainter);
109
122
        myStyle2painter.put("load.underload.first.last", myResourceLoadPainter);
110
123
        myStyle2painter.put("previousStateTask", myPreviousStateTaskRectanglePainter);
 
124
 
 
125
        myProperties = new Properties();
 
126
        PropertiesUtil.loadProperties(myProperties, "/chart.properties");
111
127
    }
112
128
 
113
129
    public void setGraphics(Graphics g) {
115
131
        myTextLengthCalculator.setGraphics(myGraphics);
116
132
    }
117
133
 
 
134
    @Override
118
135
    public void prePaint() {
119
136
        myGraphics.setStroke(defaultStroke);
120
137
        myGraphics.setFont(myConfig.getChartFont());
121
138
    }
122
139
 
 
140
    @Override
123
141
    public void paint(GraphicPrimitiveContainer.Rectangle next) {
124
142
        assert myGraphics != null;
125
143
        RectanglePainter painter = myStyle2painter.get(next.getStyle());
150
168
    }
151
169
 
152
170
    private final RectanglePainter myCalendarHolidayPainter = new RectanglePainter() {
 
171
        @Override
153
172
        public void paint(Rectangle next) {
154
173
            Color c = next.getBackgroundColor();
155
174
            myGraphics.setColor(c);
158
177
    };
159
178
 
160
179
    private class TaskRectanglePainter implements RectanglePainter {
 
180
        @Override
161
181
        public void paint(GraphicPrimitiveContainer.Rectangle next) {
162
182
            Object modelObject = next.getModelObject();
163
183
            if (modelObject instanceof TaskActivity == false) {
226
246
        float myAlphaValue = 0;
227
247
        Composite myAlphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, myAlphaValue);
228
248
 
 
249
        @Override
229
250
        public void paint(GraphicPrimitiveContainer.Rectangle next) {
230
251
            if (myAlphaValue != myConfig.getWeekendAlphaValue()) {
231
252
                myAlphaValue = myConfig.getWeekendAlphaValue();
250
271
    };
251
272
 
252
273
    private final RectanglePainter myTaskSupertaskRectanglePainter = new RectanglePainter() {
 
274
        @Override
253
275
        public void paint(Rectangle next) {
254
276
            Color c = next.getBackgroundColor();
255
277
            if (c == null) {
271
293
    };
272
294
 
273
295
    private final RectanglePainter mySupertaskStartPainter = new RectanglePainter() {
 
296
        @Override
274
297
        public void paint(Rectangle next) {
275
298
            int bottomy = next.getBottomY() - 3;
276
299
            myXPoints[0] = next.myLeftX;
285
308
    };
286
309
 
287
310
    private final RectanglePainter mySupertaskEndPainter = new RectanglePainter() {
 
311
        @Override
288
312
        public void paint(Rectangle next) {
289
313
            int bottomy = next.getBottomY() - 3;
290
314
            int rightx = next.getRightX();
300
324
    };
301
325
 
302
326
    private final RectanglePainter myTaskProjectTaskRectanglePainter = new RectanglePainter() {
 
327
        @Override
303
328
        public void paint(Rectangle next) {
304
329
            Color c = getDefaultColor();
305
330
            if (myConfig.isCriticalPathOn()
318
343
    };
319
344
 
320
345
    private final RectanglePainter myProjectTaskStartPainter = new RectanglePainter() {
 
346
        @Override
321
347
        public void paint(Rectangle next) {
322
348
            int bottomy = next.getBottomY() - 3;
323
349
            myXPoints[0] = next.myLeftX;
332
358
    };
333
359
 
334
360
    private final RectanglePainter myProjectTaskEndPainter = new RectanglePainter() {
 
361
        @Override
335
362
        public void paint(Rectangle next) {
336
363
            int bottomy = next.getBottomY() - 3;
337
364
            int rightx = next.getRightX();
347
374
    };
348
375
 
349
376
    private final RectanglePainter myMilestonePainter = new RectanglePainter() {
 
377
        @Override
350
378
        public void paint(Rectangle next) {
351
379
            Object modelObject = next.getModelObject();
352
380
            if (modelObject instanceof TaskActivity == false) {
377
405
    };
378
406
 
379
407
    private final RectanglePainter myArrowDownPainter = new RectanglePainter() {
 
408
        @Override
380
409
        public void paint(Rectangle next) {
381
410
            myXPoints[0] = next.myLeftX;
382
411
            myXPoints[1] = next.getRightX();
390
419
    };
391
420
 
392
421
    private final RectanglePainter myArrowUpPainter = new RectanglePainter() {
 
422
        @Override
393
423
        public void paint(Rectangle next) {
394
424
            myXPoints[0] = next.myLeftX;
395
425
            myXPoints[1] = next.getRightX();
403
433
    };
404
434
 
405
435
    private final RectanglePainter myArrowLeftPainter = new RectanglePainter() {
 
436
        @Override
406
437
        public void paint(Rectangle next) {
407
438
            Graphics g = myGraphics;
408
439
            g.setColor(Color.BLACK);
417
448
    };
418
449
 
419
450
    private final RectanglePainter myArrowRightPainter = new RectanglePainter() {
 
451
        @Override
420
452
        public void paint(Rectangle next) {
421
453
            myXPoints[0] = next.myLeftX;
422
454
            myXPoints[1] = next.getRightX();
430
462
    };
431
463
 
432
464
    private final RectanglePainter myDayOffPainter = new RectanglePainter() {
 
465
        @Override
433
466
        public void paint(Rectangle next) {
434
467
            int margin = StyledPainterImpl.this.margin - 3;
435
468
            Color c = myConfig.getDayOffColor();
450
483
    };
451
484
 
452
485
    private final RectanglePainter myResourceLoadPainter = new RectanglePainter() {
 
486
        @Override
453
487
        public void paint(Rectangle next) {
454
488
            String style = next.getStyle();
455
489
            Color c;
501
535
        private int[] myXPoints = new int[4];
502
536
        private int[] myYPoints = new int[4];
503
537
 
 
538
        @Override
504
539
        public void paint(GraphicPrimitiveContainer.Rectangle next) {
505
540
            Graphics g = myGraphics;
506
541
            final Color c;
558
593
            myColor = color;
559
594
        }
560
595
 
 
596
        @Override
561
597
        public void paint(Rectangle next) {
562
598
            myGraphics.setColor(myColor);
563
599
            myGraphics.fillRect(next.myLeftX, next.myTopY, next.myWidth, next.myHeight);
564
600
        }
565
601
    }
566
602
 
 
603
    @Override
567
604
    public void paint(Line line) {
568
605
        Color foreColor = line.getForegroundColor();
569
606
        if (foreColor == null) {
582
619
        }
583
620
    }
584
621
 
 
622
    @Override
585
623
    public void paint(Text next) {
586
624
        Font graphicFont = null;
587
625
        Color foreColor = next.getForegroundColor();
595
633
            myGraphics.setFont(next.getFont());
596
634
        }
597
635
 
598
 
        String nextTextString = next.getText();
599
 
 
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...)
603
 
            // Get TimeUnit text
604
 
            TimeUnitText nextText = (TimeUnitText) next.getModelObject();
605
 
            nextTextString = nextText.getText(requestedMaxLength,
606
 
                    myTextLengthCalculator);
607
 
        }
608
 
 
609
 
        int actualLength = myTextLengthCalculator.getTextLength(nextTextString);
610
 
        if (requestedMaxLength >= 0 && actualLength > requestedMaxLength) {
611
 
            return; // Text is too large
612
 
        }
 
636
        Label[] labels = next.getLabels(myTextLengthCalculator);
 
637
        if (labels.length == 0) {
 
638
            return;
 
639
        }
 
640
//        int actualLength = myTextLengthCalculator.getTextLength(nextTextString);
 
641
//        if (requestedMaxLength >= 0 && actualLength > requestedMaxLength) {
 
642
//            return; // Text is too large
 
643
//        }
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
616
 
        }
617
 
 
618
 
        int xleft, ybottom;
619
 
        switch (next.getHAlignment()) {
620
 
        case CENTER:
621
 
            xleft = next.getLeftX() - actualLength / 2;
622
 
            break;
623
 
        case RIGHT:
624
 
            xleft = next.getLeftX() - actualLength;
625
 
            break;
626
 
        default:
627
 
            xleft = next.getLeftX();
628
 
        }
629
 
        switch (next.getVAlignment()) {
630
 
        case CENTER:
631
 
            ybottom = next.getBottomY() + myGraphics.getFont().getSize() / 2;
632
 
            break;
633
 
        case TOP:
634
 
            ybottom = next.getBottomY() + myGraphics.getFont().getSize();
635
 
            break;
636
 
        default:
637
 
            ybottom = next.getBottomY();
638
 
        }
639
 
 
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
 
647
//        }
 
648
        Label label = labels[0];
 
649
        if (label == null) {
 
650
            return;
 
651
        }
 
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);
644
656
        }
645
657
    }
 
658
 
 
659
    private void paint(int xleft, int ybottom, HAlignment alignHor, VAlignment alignVer, Label label) {
 
660
        switch (alignHor) {
 
661
        case CENTER:
 
662
            xleft = xleft - label.lengthPx / 2;
 
663
            break;
 
664
        case RIGHT:
 
665
            xleft = xleft - label.lengthPx;
 
666
            break;
 
667
        }
 
668
        switch (alignVer) {
 
669
        case CENTER:
 
670
            ybottom = ybottom + myGraphics.getFont().getSize() / 2;
 
671
            break;
 
672
        case TOP:
 
673
            ybottom = ybottom + myGraphics.getFont().getSize();
 
674
            break;
 
675
        }
 
676
        myGraphics.drawString(label.text, xleft, ybottom);
 
677
    }
 
678
 
 
679
    @Override
 
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);
 
686
        }
 
687
    }
 
688
 
 
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();
 
693
 
 
694
        myGraphics.setFont(textGroup.getFont(lineNum));
 
695
        myGraphics.setColor(textGroup.getColor(lineNum));
 
696
 
 
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);
 
702
            if (maxIndex == 0) {
 
703
                return;
 
704
            }
 
705
            labelList.add(labels);
 
706
        }
 
707
 
 
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);
 
712
        }
 
713
 
 
714
        myGraphics.setFont(savedFont);
 
715
        myGraphics.setColor(savedColor);
 
716
    }
646
717
}