15
15
You should have received a copy of the GNU General Public License
16
16
along with this program; if not, write to the Free Software
17
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
package net.sourceforge.ganttproject;
21
import java.awt.Color;
22
import java.awt.Component;
23
import java.awt.Cursor;
21
24
import java.awt.Dimension;
22
25
import java.awt.Graphics;
26
import java.awt.Graphics2D;
27
import java.awt.Image;
23
28
import java.awt.Point;
24
29
import java.awt.event.MouseEvent;
25
30
import java.awt.image.BufferedImage;
26
31
import java.awt.image.RenderedImage;
32
import java.util.ArrayList;
33
import java.util.Collections;
27
34
import java.util.Date;
28
35
import java.util.Iterator;
29
36
import java.util.LinkedHashSet;
37
import java.util.List;
30
38
import java.util.Set;
32
import javax.swing.Icon;
33
40
import javax.swing.ImageIcon;
34
41
import javax.swing.JComponent;
35
42
import javax.swing.JTable;
43
50
import net.sourceforge.ganttproject.chart.ChartSelectionListener;
44
51
import net.sourceforge.ganttproject.chart.ChartUIConfiguration;
45
52
import net.sourceforge.ganttproject.chart.TimelineChart;
53
import net.sourceforge.ganttproject.chart.export.ChartDimensions;
54
import net.sourceforge.ganttproject.chart.export.ChartImageBuilder;
55
import net.sourceforge.ganttproject.chart.export.ChartImageVisitor;
56
import net.sourceforge.ganttproject.chart.export.RenderedChartImage;
46
57
import net.sourceforge.ganttproject.chart.mouse.MouseInteraction;
58
import net.sourceforge.ganttproject.gui.UIFacade;
47
59
import net.sourceforge.ganttproject.gui.options.model.GPOptionGroup;
48
60
import net.sourceforge.ganttproject.gui.zoom.ZoomEvent;
49
61
import net.sourceforge.ganttproject.gui.zoom.ZoomListener;
62
import net.sourceforge.ganttproject.resource.HumanResource;
63
import net.sourceforge.ganttproject.task.Task;
50
64
import net.sourceforge.ganttproject.task.TaskLength;
51
import net.sourceforge.ganttproject.task.TaskManager;
52
import net.sourceforge.ganttproject.time.TimeFrame;
65
import net.sourceforge.ganttproject.task.TaskSelectionManager;
53
66
import net.sourceforge.ganttproject.time.TimeUnit;
54
import net.sourceforge.ganttproject.time.TimeUnitStack;
56
68
import org.eclipse.core.runtime.IStatus;
69
import org.eclipse.core.runtime.Status;
58
71
public class AbstractChartImplementation implements TimelineChart, ZoomListener {
59
72
public static final ImageIcon LOGO = new ImageIcon(AbstractChartImplementation.class.getResource("/icons/big.png"));
63
76
private final IGanttProject myProject;
64
77
private Set<ChartSelectionListener> mySelectionListeners= new LinkedHashSet<ChartSelectionListener>();
65
78
private final ChartComponentBase myChartComponent;
79
private MouseInteraction myActiveInteraction;
80
private final UIFacade myUiFacade;
67
public AbstractChartImplementation(IGanttProject project, ChartModelBase chartModel, ChartComponentBase chartComponent) {
82
public AbstractChartImplementation(IGanttProject project, UIFacade uiFacade, ChartModelBase chartModel, ChartComponentBase chartComponent) {
68
83
assert chartModel != null;
84
myUiFacade = uiFacade;
69
85
myChartModel = chartModel;
70
86
myProject = project;
71
87
myChartComponent = chartComponent;
88
uiFacade.getTaskSelectionManager().addSelectionListener(new TaskSelectionManager.Listener() {
90
public void userInputConsumerChanged(Object newConsumer) {
91
fireSelectionChanged();
95
public void selectionChanged(List<Task> currentSelection) {
96
fireSelectionChanged();
102
public void init(IGanttProject project) {
103
// Skip as we already have a project instance.
106
protected void setCursor(Cursor cursor) {
107
myChartComponent.setCursor(cursor);
110
protected UIFacade getUIFacade() {
74
115
public IGanttProject getProject() {
98
139
return myActiveInteraction;
101
143
public void zoomChanged(ZoomEvent e) {
102
144
myChartComponent.invalidate();
103
145
myChartComponent.repaint();
107
148
public void paintChart(Graphics g) {
108
149
getChartModel().paint(g);
110
private MouseInteraction myActiveInteraction;
111
private TimeFrame myFirstTimeFrame;
113
private ChartModelBase getChartModel() {
152
protected ChartModelBase getChartModel() {
114
153
return myChartModel;
117
156
/////////////////////////////////////////////////////////////
118
157
// interface Chart
159
public void buildImage(GanttExportSettings settings, ChartImageVisitor imageVisitor) {
160
ChartModelBase modelCopy = getChartModel().createCopy();
161
modelCopy.setBounds(myChartComponent.getSize());
162
if (settings.getStartDate() == null) {
163
settings.setStartDate(modelCopy.getStartDate());
165
if (settings.getEndDate() == null) {
166
settings.setEndDate(modelCopy.getEndDate());
168
if (settings.isCommandLineMode()) {
169
myChartComponent.getTreeTable().getTable().getTableHeader().setVisible(true);
170
myChartComponent.getTreeTable().doLayout();
171
myChartComponent.getTreeTable().getTable().setRowHeight(modelCopy.calculateRowHeight());
172
myChartComponent.getTreeTable().autoFitColumns();
174
ChartImageBuilder builder = new ChartImageBuilder(settings, modelCopy, myChartComponent.getTreeTable());
175
builder.buildImage(imageVisitor);
119
179
public RenderedImage getRenderedImage(GanttExportSettings settings) {
120
// TODO Auto-generated method stub
123
public BufferedImage getChart(GanttExportSettings settings) {
124
// TODO Auto-generated method stub
180
class ChartImageVisitorImpl implements ChartImageVisitor {
181
private RenderedChartImage myRenderedImage;
182
private Graphics2D myGraphics;
183
private BufferedImage myTreeImage;
186
public void acceptLogo(ChartDimensions d, Image logo) {
187
Graphics2D g = getGraphics(d);
188
g.setBackground(Color.WHITE);
189
g.clearRect(0, 0, d.getTreeWidth(), d.getLogoHeight());
190
// Hack: by adding 35, the left part of the logo becomes visible, otherwise it gets chopped off
191
g.drawImage(logo, 35, 0, null);
195
public void acceptTable(ChartDimensions d, Component header, Component table) {
196
Graphics2D g = getGraphics(d);
197
g.translate(0, d.getLogoHeight());
200
g.translate(0, d.getTableHeaderHeight());
205
public void acceptChart(ChartDimensions d, ChartModel model) {
206
myRenderedImage = new RenderedChartImage(model, myTreeImage, d.getChartWidth(), d.getChartHeight()
207
+ d.getLogoHeight(), d.getLogoHeight());
210
private Graphics2D getGraphics(ChartDimensions d) {
211
if (myGraphics == null) {
212
myTreeImage = new BufferedImage(d.getTreeWidth(), d.getChartHeight() + d.getLogoHeight(), BufferedImage.TYPE_INT_RGB);
213
myGraphics = myTreeImage.createGraphics();
218
ChartImageVisitorImpl visitor = new ChartImageVisitorImpl();
219
buildImage(settings, visitor);
220
return visitor.myRenderedImage;
128
224
public Date getStartDate() {
129
225
return getChartModel().getStartDate();
132
229
public void setStartDate(Date startDate) {
133
getChartModel().setStartDate(startDate);
134
myFirstTimeFrame = scrollTimeFrame(startDate);
135
startDate = myFirstTimeFrame.getStartDate();
230
startDate = getBottomTimeUnit().adjustLeft(startDate);
136
231
getChartModel().setStartDate(startDate);
139
235
public void scrollBy(TaskLength duration) {
140
236
setStartDate(getChartModel().getTaskManager().shift(getStartDate(), duration));
145
241
getChartModel().setHorizontalOffset(pixels);
148
private TimeFrame scrollTimeFrame(Date scrolledDate) {
149
TimeFrame result = null;
150
if (getTopTimeUnit().isConstructedFrom(getBottomTimeUnit())) {
151
result = getTimeUnitStack().createTimeFrame(scrolledDate,
152
getTopTimeUnit(), getBottomTimeUnit());
154
result = getTimeUnitStack().createTimeFrame(scrolledDate,
155
getBottomTimeUnit(), getBottomTimeUnit());
160
private TimeUnit getTopTimeUnit() {
161
return getChartModel().getTopUnit();
164
244
private TimeUnit getBottomTimeUnit() {
165
245
return getChartModel().getBottomUnit();
168
private TimeUnitStack getTimeUnitStack() {
169
return myProject.getTimeUnitStack();
172
249
public Date getEndDate() {
173
250
return getChartModel().getEndDate();
176
254
public void setDimensions(int height, int width) {
177
255
Dimension bounds = new Dimension(width, height);
178
256
getChartModel().setBounds(bounds);
181
260
public void setBottomUnit(TimeUnit bottomUnit) {
182
261
getChartModel().setBottomTimeUnit(bottomUnit);
185
265
public void setTopUnit(TimeUnit topUnit) {
186
266
getChartModel().setTopTimeUnit(topUnit);
189
270
public void setBottomUnitWidth(int width) {
190
271
getChartModel().setBottomUnitWidth(width);
193
275
public String getName() {
194
276
return myChartComponent.getName();
197
public void setTaskManager(TaskManager taskManager) {
198
throw new UnsupportedOperationException();
201
280
public void reset() {
202
throw new UnsupportedOperationException();
205
public Icon getIcon() {
281
myChartComponent.reset();
209
285
public GPOptionGroup[] getOptionGroups() {
210
286
return getChartModel().getChartOptionGroups();
213
290
public Chart createCopy() {
214
return new AbstractChartImplementation(myProject, getChartModel().createCopy(), myChartComponent);
291
return new AbstractChartImplementation(myProject, myUiFacade, getChartModel().createCopy(), myChartComponent);
217
295
public Object getAdapter(Class arg0) {
221
300
public ChartSelection getSelection() {
222
301
throw new UnsupportedOperationException();
225
305
public IStatus canPaste(ChartSelection selection) {
226
306
throw new UnsupportedOperationException();
229
310
public void paste(ChartSelection selection) {
230
311
throw new UnsupportedOperationException();
233
315
public void addSelectionListener(ChartSelectionListener listener) {
234
316
mySelectionListeners.add(listener);
237
320
public void removeSelectionListener(ChartSelectionListener listener) {
238
321
mySelectionListeners.remove(listener);
269
355
return headerLocation.y - treeLocation.y + tableHeader.getHeight() + HEADER_OFFSET;
359
protected static class ChartSelectionImpl implements ChartSelection {
360
private List<Task> myTasks = new ArrayList<Task>();
361
private List<Task> myTasksRO = Collections.unmodifiableList(myTasks);
362
private List<HumanResource> myHumanResources = new ArrayList<HumanResource>();
363
private List<HumanResource> myHumanResourceRO = Collections.unmodifiableList(myHumanResources);
364
private boolean isTransactionRunning;
367
public boolean isEmpty() {
368
return myTasks.isEmpty() && myHumanResources.isEmpty();
372
public List<Task> getTasks() {
377
public List<HumanResource> getHumanResources() {
378
return myHumanResourceRO;
382
public IStatus isDeletable() {
383
return Status.OK_STATUS;
387
public void startCopyClipboardTransaction() {
388
if (isTransactionRunning) {
389
throw new IllegalStateException("Transaction is already running");
391
isTransactionRunning = true;
394
public void startMoveClipboardTransaction() {
395
if (isTransactionRunning) {
396
throw new IllegalStateException("Transaction is already running");
398
isTransactionRunning = true;
401
public void cancelClipboardTransaction() {
402
isTransactionRunning = false;
405
public void commitClipboardTransaction() {
406
isTransactionRunning = false;
b'\\ No newline at end of file'