~veger/ganttproject/manual-import

« back to all changes in this revision

Viewing changes to ganttproject/src/net/sourceforge/ganttproject/action/task/TaskActionBase.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
1
/*
2
2
GanttProject is an opensource project management tool.
3
 
Copyright (C) 2002-2010 Dmitry Barashev
 
3
Copyright (C) 2002-2011 Dmitry Barashev, GanttProject Team
4
4
 
5
5
This program is free software; you can redistribute it and/or
6
6
modify it under the terms of the GNU General Public License
7
 
as published by the Free Software Foundation; either version 2
 
7
as published by the Free Software Foundation; either version 3
8
8
of the License, or (at your option) any later version.
9
9
 
10
10
This program is distributed in the hope that it will be useful,
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.
18
 
 */
 
18
*/
19
19
package net.sourceforge.ganttproject.action.task;
20
20
 
21
21
import java.awt.event.ActionEvent;
22
22
import java.util.ArrayList;
23
23
import java.util.List;
24
24
 
 
25
import net.sourceforge.ganttproject.GanttTree2;
 
26
import net.sourceforge.ganttproject.action.ActionDelegate;
 
27
import net.sourceforge.ganttproject.action.ActionStateChangedListener;
25
28
import net.sourceforge.ganttproject.action.GPAction;
26
29
import net.sourceforge.ganttproject.gui.UIFacade;
27
30
import net.sourceforge.ganttproject.task.Task;
28
31
import net.sourceforge.ganttproject.task.TaskManager;
29
32
import net.sourceforge.ganttproject.task.TaskSelectionManager;
30
 
import net.sourceforge.ganttproject.task.TaskSelectionManager.Listener;
 
33
import net.sourceforge.ganttproject.task.dependency.TaskDependencyException;
31
34
 
32
 
abstract class TaskActionBase extends GPAction implements Listener {
 
35
public abstract class TaskActionBase extends GPAction implements TaskSelectionManager.Listener, ActionDelegate {
 
36
    private final List<ActionStateChangedListener> myListeners = new ArrayList<ActionStateChangedListener>();
33
37
    private final TaskManager myTaskManager;
 
38
    private final UIFacade myUIFacade;
 
39
    private final TaskSelectionManager mySelectionManager;
 
40
    private final GanttTree2 myTree;
34
41
    private List<Task> mySelection;
35
 
    private final UIFacade myUIFacade;
36
 
    private TaskSelectionManager mySelectionManager;
37
42
 
38
 
    protected TaskActionBase(String name, TaskManager taskManager, TaskSelectionManager selectionManager, UIFacade uiFacade) {
39
 
        super(name);
 
43
    protected TaskActionBase(String name, TaskManager taskManager, TaskSelectionManager selectionManager,
 
44
            UIFacade uiFacade, GanttTree2 tree) {
 
45
        this(name, taskManager, selectionManager, uiFacade, tree, IconSize.MENU);
 
46
    }
 
47
    protected TaskActionBase(String name, TaskManager taskManager, TaskSelectionManager selectionManager,
 
48
            UIFacade uiFacade, GanttTree2 tree, IconSize size) {
 
49
        super(name, size);
40
50
        myTaskManager = taskManager;
41
51
        mySelectionManager = selectionManager;
 
52
        myUIFacade = uiFacade;
 
53
        myTree = tree;
42
54
        selectionManager.addSelectionListener(this);
43
55
        selectionChanged(selectionManager.getSelectedTasks());
44
 
        myUIFacade = uiFacade;
45
 
    }
46
 
 
 
56
    }
 
57
 
 
58
    @Override
 
59
    public void addStateChangedListener(ActionStateChangedListener l) {
 
60
        myListeners.add(l);
 
61
    }
 
62
 
 
63
    @Override
47
64
    public void actionPerformed(ActionEvent e) {
48
65
        final List<Task> selection = new ArrayList<Task>(mySelection);
49
 
        myUIFacade.getUndoManager().undoableEdit(getLocalizedName(), new Runnable() {
50
 
            public void run() {
51
 
                try {
52
 
                    TaskActionBase.this.run(selection);
53
 
                } catch (Exception e) {
54
 
                    getUIFacade().showErrorDialog(e);
 
66
        if(isEnabled() && askUserPermission(selection)) {
 
67
            myUIFacade.getUndoManager().undoableEdit(getLocalizedDescription(), new Runnable() {
 
68
                @Override
 
69
                public void run() {
 
70
                    try {
 
71
                        TaskActionBase.this.run(selection);
 
72
                    } catch (Exception e) {
 
73
                        getUIFacade().showErrorDialog(e);
 
74
                    }
55
75
                }
56
 
            }
57
 
        });
58
 
    }
59
 
 
 
76
            });
 
77
        }
 
78
    }
 
79
 
 
80
    /**
 
81
     * @param selection of tasks for which permission is required
 
82
     * @return true if the operation is accepted by the user
 
83
     */
 
84
    protected boolean askUserPermission(List<Task> selection) {
 
85
        // Accept operation by default
 
86
        return true;
 
87
    }
 
88
 
 
89
    @Override
60
90
    public void selectionChanged(List<Task> currentSelection) {
61
91
        setEnabled(isEnabled(currentSelection));
62
92
        mySelection = currentSelection;
63
93
    }
64
94
 
 
95
    @Override
 
96
    public void setEnabled(boolean newValue) {
 
97
        super.setEnabled(newValue);
 
98
        for(ActionStateChangedListener l: myListeners) {
 
99
            l.actionStateChanged();
 
100
        }
 
101
    }
 
102
 
 
103
    @Override
65
104
    public void userInputConsumerChanged(Object newConsumer) {
66
105
    }
67
106
 
77
116
        return myUIFacade;
78
117
    }
79
118
 
 
119
    protected GanttTree2 getTree() {
 
120
        return myTree;
 
121
    }
 
122
 
 
123
    protected void forwardScheduling() throws TaskDependencyException {
 
124
        // TODO 07 Sep 2011: It does seem necessary to reset() the charts: remove if this indeed is the case
 
125
//        // TODO Find out which chart is opened and only reset that one (maybe add a resetChart to UIFacade?)
 
126
//        myUIFacade.getGanttChart().reset();
 
127
//        myUIFacade.getResourceChart().reset();
 
128
        myTaskManager.getAlgorithmCollection().getRecalculateTaskScheduleAlgorithm().run();
 
129
        getUIFacade().getTaskTree().getTreeComponent().repaint();
 
130
    }
 
131
 
80
132
    protected abstract boolean isEnabled(List<Task> selection);
81
133
    protected abstract void run(List<Task> selection) throws Exception ;
82
134
}