001    /**
002     * planningtool - A Planning Tool with Critiquing Support.
003     * 
004     * Copyright (C) 2006 olale
005    
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License
008     * as published by the Free Software Foundation; either version 2
009     * of the License, or (at your option) any later version.
010    
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015    
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019    
020     * Contact information: 
021     * E-mail: olale@ida.liu.se
022     *         olale@lysator.liu.se
023     */
024    package se.liu.ida.critiquer.gui;
025    
026    import java.awt.datatransfer.DataFlavor;
027    import java.awt.datatransfer.Transferable;
028    import java.awt.datatransfer.UnsupportedFlavorException;
029    import java.io.IOException;
030    import java.util.ArrayList;
031    import java.util.Arrays;
032    import java.util.Vector;
033    
034    import javax.swing.Icon;
035    import javax.swing.JComponent;
036    import javax.swing.TransferHandler;
037    
038    import org.tigris.gef.base.Selection;
039    import org.tigris.gef.graph.presentation.JGraph;
040    
041    import se.liu.ida.critiquer.activities.Activity;
042    import se.liu.ida.critiquer.activities.ActivityUtils;
043    import se.liu.ida.critiquer.gui.graph.ActivityFigNode;
044    import se.liu.ida.critiquer.mics.ReferenceHolder;
045    import se.liu.ida.critiquer.resources.Agent;
046    
047    public class AgentTransferHandler extends TransferHandler {
048    
049            /**
050             * 
051             */
052            private static final long                                               serialVersionUID        = 1L;
053    
054            private transient ArrayList<TransferListener>     listeners                       = new ArrayList<TransferListener>();
055    
056            private static AgentTransferHandler                             handler                         = new AgentTransferHandler();
057    
058            private AgentTransferHandler() {
059                    super();
060            }
061    
062            public static AgentTransferHandler getAgentTransferHandler() {
063                    return handler;
064            }
065    
066            public void addTransferListener(TransferListener l) {
067                    listeners.add(l);
068            }
069    
070            /*
071             * (non-Javadoc)
072             * 
073             * @see javax.swing.TransferHandler#getVisualRepresentation(java.awt.datatransfer.Transferable)
074             */
075            @Override
076            public Icon getVisualRepresentation(Transferable t) {
077                    // TODO Auto-generated method stub
078                    return super.getVisualRepresentation(t);
079            }
080    
081            /*
082             * (non-Javadoc)
083             * 
084             * @see javax.swing.TransferHandler#canImport(javax.swing.JComponent,
085             *      java.awt.datatransfer.DataFlavor[])
086             */
087            @Override
088            public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
089                    boolean canImportFlavors = (comp instanceof TrashLabel || comp instanceof JGraph) && Arrays.asList(transferFlavors).contains(AgentSelection.agentFlavor);
090                    return canImportFlavors;
091            }
092    
093            /*
094             * (non-Javadoc)
095             * 
096             * @see javax.swing.TransferHandler#getSourceActions(javax.swing.JComponent)
097             */
098            @Override
099            public int getSourceActions(JComponent c) {
100                    return TransferHandler.COPY;
101            }
102    
103            /*
104             * This is to be used when an agent is dragged from an agent label
105             * 
106             * @see javax.swing.TransferHandler#createTransferable(javax.swing.JComponent)
107             */
108            @Override
109            protected Transferable createTransferable(JComponent c) {
110                    Transferable t = null;
111                    if (c instanceof AgentLabel) {
112                            AgentLabel agentLabel = (AgentLabel) c;
113                            t = new AgentSelection(agentLabel.getAgent(), agentLabel.getAgentParam());
114                    }
115                    for (TransferListener listener : listeners) {
116                            listener.transferInitialized();
117                    }
118    
119                    return t;
120            }
121    
122            /**
123             * <p>
124             * Select which agents are available for a commander when a drop is
125             * performed in the graph of the resource view. Actually we have activities
126             * in the graph view, but we can only select activities that have an
127             * assigned commander and all activities with the same commander are
128             * selected if one is, so as to highlight which activities are affected
129             * </p>
130             * 
131             * @see javax.swing.TransferHandler#importData(javax.swing.JComponent,
132             *      java.awt.datatransfer.Transferable)
133             */
134            @SuppressWarnings("unchecked")
135            @Override
136            public boolean importData(JComponent comp, Transferable t) {
137                    boolean success = false;
138                    
139                    if (comp instanceof JGraph) {
140                            JGraph graph = (JGraph) comp;
141                            Vector<Selection> selections = graph.getEditor().getSelectionManager().selections();
142                            for (Selection selection : selections) {
143                                    if (selection.getContent() instanceof ActivityFigNode) {
144                                            ActivityFigNode activityFigNode = (ActivityFigNode) selection.getContent();
145                                            Activity selectedActivity = activityFigNode.getActivityNode().getActivity();
146                                            if (selectedActivity.isAgentAssignable()) {
147                                                    try {
148                                                            success = ActivityUtils .getMainAgentParameter(selectedActivity)
149                                                                                            .selectAgent((Agent) t.getTransferData(AgentSelection.agentFlavor));
150                                                            
151                                                    } catch (UnsupportedFlavorException e) {
152                                                            // TODO Auto-generated catch block
153                                                            e.printStackTrace();
154                                                    } catch (IOException e) {
155                                                            // TODO Auto-generated catch block
156                                                            e.printStackTrace();
157                                                    }
158    
159                                            }
160                                    }
161                            }
162    
163                    } else if (comp instanceof TrashLabel) {
164                            
165                            try {
166                                    Vector<Selection> selections = ReferenceHolder.resourceView       .getGraph()
167                                                                                                                                                            .getEditor()
168                                                                                                                                                            .getSelectionManager()
169                                                                                                                                                            .selections();
170                                    for (Selection selection : selections) {
171                                            if (selection.getContent() instanceof ActivityFigNode) {
172                                                    ActivityFigNode activityFigNode = (ActivityFigNode) selection.getContent();
173                                                    Activity selectedActivity = activityFigNode.getActivityNode().getActivity();
174                                                    
175                                                    /**
176                                                     * Remove an activity from an agent
177                                                     */
178                                                            try {
179                                                                    ActivityUtils   .getMainAgentParameter(selectedActivity)
180                                                                                                    .deSelectAgent((Agent) t.getTransferData(AgentSelection.agentFlavor),selectedActivity);
181                                                                    success = true;
182                                                            } catch (UnsupportedFlavorException e) {
183                                                                    // TODO Auto-generated catch block
184                                                                    e.printStackTrace();
185                                                            } catch (IOException e) {
186                                                                    // TODO Auto-generated catch block
187                                                                    e.printStackTrace();
188                                                            }
189    
190                                                    
191                                            }
192                                    }
193                            } catch (NullPointerException e) {
194                                    e.printStackTrace();
195                            }
196                            
197                            
198                            
199                    }
200                    if (success) {
201                            for (TransferListener listener : listeners) {
202                                    listener.transferComplete();
203                            }
204                    }
205                    return success;
206            }
207    }