~veger/ganttproject/manual-import

« back to all changes in this revision

Viewing changes to ganttproject/src/net/sourceforge/ganttproject/action/DeleteHumanAction.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
 
/*
2
 
GanttProject is an opensource project management tool.
3
 
Copyright (C) 2011 GanttProject Team
4
 
 
5
 
This program is free software; you can redistribute it and/or
6
 
modify it under the terms of the GNU General Public License
7
 
as published by the Free Software Foundation; either version 2
8
 
of the License, or (at your option) any later version.
9
 
 
10
 
This program is distributed in the hope that it will be useful,
11
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
GNU General Public License for more details.
14
 
 
15
 
You should have received a copy of the GNU General Public License
16
 
along with this program; if not, write to the Free Software
17
 
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
 
 */
19
 
package net.sourceforge.ganttproject.action;
20
 
 
21
 
import java.awt.Toolkit;
22
 
import java.awt.event.ActionEvent;
23
 
import java.awt.event.KeyEvent;
24
 
import java.net.URL;
25
 
 
26
 
import javax.swing.AbstractAction;
27
 
import javax.swing.Action;
28
 
import javax.swing.ImageIcon;
29
 
import javax.swing.KeyStroke;
30
 
 
31
 
import net.sourceforge.ganttproject.GanttProject;
32
 
import net.sourceforge.ganttproject.gui.UIFacade;
33
 
import net.sourceforge.ganttproject.gui.UIFacade.Choice;
34
 
import net.sourceforge.ganttproject.language.GanttLanguage;
35
 
import net.sourceforge.ganttproject.resource.HumanResource;
36
 
import net.sourceforge.ganttproject.resource.HumanResourceManager;
37
 
import net.sourceforge.ganttproject.resource.ResourceContext;
38
 
 
39
 
/**
40
 
 * Action connected to the menu item for delete a resource
41
 
 */
42
 
public class DeleteHumanAction extends ResourceAction {
43
 
    private final UIFacade myUIFacade;
44
 
 
45
 
    private final ResourceContext myContext;
46
 
 
47
 
    private static final String ICON_URL = "icons/delete_16.gif";
48
 
 
49
 
    private final int MENU_MASK = Toolkit.getDefaultToolkit()
50
 
            .getMenuShortcutKeyMask();
51
 
 
52
 
    private GanttProject myProjectFrame;
53
 
 
54
 
    public DeleteHumanAction(HumanResourceManager hrManager,
55
 
            ResourceContext context, GanttProject projectFrame, UIFacade uiFacade) {
56
 
        super(hrManager);
57
 
        myUIFacade = uiFacade;
58
 
        myProjectFrame = projectFrame;
59
 
        this.putValue(AbstractAction.NAME, GanttLanguage.getInstance()
60
 
                .correctLabel(getLanguage().getText("deleteHuman")));
61
 
        this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(
62
 
                KeyEvent.VK_J, MENU_MASK));
63
 
        URL iconUrl = this.getClass().getClassLoader().getResource(ICON_URL);
64
 
        if (iconUrl != null) {
65
 
            this.putValue(Action.SMALL_ICON, new ImageIcon(iconUrl));
66
 
        }
67
 
        myContext = context;
68
 
    }
69
 
 
70
 
    public void actionPerformed(ActionEvent event) {
71
 
        final HumanResource[] context = getContext().getResources();
72
 
        if (context.length > 0) {
73
 
            final String message = getLanguage().getText("msg6") + " "
74
 
                    + getDisplayName(context) + "?";
75
 
            final String title = getLanguage().getText("question");
76
 
            Choice choice = myUIFacade.showConfirmationDialog(message, title);
77
 
            if (choice == Choice.YES) {
78
 
                myUIFacade.getUndoManager().undoableEdit("Resource removed",
79
 
                        new Runnable() {
80
 
                            public void run() {
81
 
                                deleteResources(context);
82
 
                                getProjectFrame().repaint2();
83
 
                            }
84
 
                        });
85
 
            }
86
 
        }
87
 
    }
88
 
 
89
 
    private GanttProject getProjectFrame() {
90
 
        return myProjectFrame;
91
 
    }
92
 
 
93
 
    private void deleteResources(HumanResource[] resources) {
94
 
        for (HumanResource resource : resources) {
95
 
            resource.delete();
96
 
        }
97
 
    }
98
 
 
99
 
    private String getDisplayName(HumanResource[] resources) {
100
 
        if (resources.length == 1) {
101
 
            return resources[0].toString();
102
 
        }
103
 
        StringBuffer result = new StringBuffer();
104
 
        for (int i = 0; i < resources.length; i++) {
105
 
            result.append(resources[i].toString());
106
 
            if (i < resources.length - 1) {
107
 
                result.append(", ");
108
 
            }
109
 
        }
110
 
        return result.toString();
111
 
    }
112
 
 
113
 
    private ResourceContext getContext() {
114
 
        return myContext;
115
 
    }
116
 
}