001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Mikael MARCHE, Fayçal SOUGRATI, Vincent PAUTRET
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.ihm.models;
025:
026: import java.awt.datatransfer.Clipboard;
027: import java.awt.datatransfer.DataFlavor;
028: import java.awt.datatransfer.Transferable;
029: import java.awt.datatransfer.UnsupportedFlavorException;
030: import java.io.IOException;
031: import java.util.ArrayList;
032:
033: import javax.swing.JComponent;
034: import javax.swing.JTable;
035: import javax.swing.TransferHandler;
036:
037: /**
038: * @author teaml039
039: * @version : 0.1
040: */
041: public class RowTransferHandler extends TransferHandler {
042:
043: DataFlavor stringFlavor = DataFlavor.stringFlavor;
044: JTable sourcePic;
045: boolean shouldRemove;
046:
047: public boolean importData(JComponent c, Transferable t) {
048:
049: if (canImport(c, t.getTransferDataFlavors())) {
050: ArrayList data;
051: JTable table = (JTable) c;
052: //Don't drop on myself.
053: if (sourcePic == table) {
054: shouldRemove = false;
055: return true;
056: }
057: try {
058: data = (ArrayList) t.getTransferData(stringFlavor);
059: //Set the component to the new picture.
060: ((MyTableModel) table.getModel()).addRow(data);
061: return true;
062: } catch (UnsupportedFlavorException ufe) {
063: //System.out.println("importData: unsupported data flavor");
064: } catch (IOException ioe) {
065: //System.out.println("importData: I/O exception");
066: }
067: }
068: return false;
069: }
070:
071: protected Transferable createTransferable(JComponent c) {
072: sourcePic = (JTable) c;
073: shouldRemove = true;
074: //int selectedRow = sourcePic.getSelectedRow();
075: return new PictureTransferable(((MyTableModel) sourcePic
076: .getModel()).getData());
077: }
078:
079: public int getSourceActions(JComponent c) {
080: return COPY_OR_MOVE;
081: }
082:
083: protected void exportDone(JComponent c, Transferable data,
084: int action) {
085: if (shouldRemove && (action == MOVE)) {
086: sourcePic.remove(sourcePic.getSelectedRow());
087: }
088: sourcePic = null;
089: }
090:
091: public boolean canImport(JComponent c, DataFlavor[] flavors) {
092: for (int i = 0; i < flavors.length; i++) {
093: if (stringFlavor.equals(flavors[i])) {
094: return true;
095: }
096: }
097: return false;
098: }
099:
100: class PictureTransferable implements Transferable {
101: private ArrayList data;
102:
103: PictureTransferable(ArrayList list) {
104: data = list;
105: }
106:
107: public Object getTransferData(DataFlavor flavor)
108: throws UnsupportedFlavorException {
109: if (!isDataFlavorSupported(flavor)) {
110: throw new UnsupportedFlavorException(flavor);
111: }
112: return data;
113: }
114:
115: public DataFlavor[] getTransferDataFlavors() {
116: return new DataFlavor[] { stringFlavor };
117: }
118:
119: public boolean isDataFlavorSupported(DataFlavor flavor) {
120: return stringFlavor.equals(flavor);
121: }
122: }
123:
124: public void exportToClipboard(JComponent comp, Clipboard clip,
125: int action) {
126: boolean exportSuccess = false;
127: Transferable t = null;
128:
129: int clipboardAction = getSourceActions(comp) & action;
130: if (clipboardAction != NONE) {
131: t = createTransferable(comp);
132: if (t != null) {
133: clip.setContents(t, null);
134: exportSuccess = true;
135: }
136: }
137:
138: if (exportSuccess) {
139: exportDone(comp, t, clipboardAction);
140: } else {
141: exportDone(comp, null, NONE);
142: }
143: }
144:
145: } // Fin de la classe RowTransferHandler
|