KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > dnd > ListColumnTransferableHandler


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * ListColumnTransferableHandler.java
28  *
29  * Created on 21 maggio 2003, 18.48
30  *
31  */

32
33 package it.businesslogic.ireport.gui.dnd;
34
35 /**
36  *
37  * @author Administrator
38  */

39 import it.businesslogic.ireport.gui.*;
40 import javax.swing.*;
41 import java.awt.*;
42 import java.awt.datatransfer.*;
43
44 public class ListColumnTransferableHandler extends ColumnTransferableHandler {
45     private int[] indices = null;
46     private int addIndex = -1; //Location where items were added
47
private int addCount = 0; //Number of items added.
48
private JSQLTablesPane jSQLTablePane=null;
49     public void setSQLTablesPane(JSQLTablesPane jSQLTablePane)
50     {
51         this.jSQLTablePane = jSQLTablePane;
52     }
53     public JSQLTablesPane getSQLTablesPane()
54     {
55         return this.jSQLTablePane;
56     }
57             
58     //Bundle up the selected items in the list
59
//as a single string, for export.
60
protected String JavaDoc exportString(JComponent c) {
61         JList list = (JList)c;
62         indices = list.getSelectedIndices();
63         Object JavaDoc[] values = list.getSelectedValues();
64         
65         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
66
67         for (int i = 0; i < values.length; i++) {
68             Object JavaDoc val = values[i];
69             buff.append(val == null ? "" : val.toString());
70             if (i != values.length - 1) {
71                 buff.append("\n");
72             }
73         }
74         
75         return buff.toString();
76     }
77
78     //Take the incoming string and wherever there is a
79
//newline, break it into a separate item in the list.
80
protected void importString(JComponent c, String JavaDoc str) {
81         JList target = (JList)c;
82         DefaultListModel listModel = (DefaultListModel)target.getModel();
83         int index = target.getSelectedIndex();
84
85         //Prevent the user from dropping data back on itself.
86
//For example, if the user is moving items #4,#5,#6 and #7 and
87
//attempts to insert the items after item #5, this would
88
//be problematic when removing the original items.
89
//So this is not allowed.
90
/*
91         if (indices != null && index >= indices[0] - 1 &&
92               index <= indices[indices.length - 1]) {
93             indices = null;
94             return;
95         }
96
97         int max = listModel.getSize();
98         if (index < 0) {
99             index = max;
100         } else {
101             index++;
102             if (index > max) {
103                 index = max;
104             }
105         }
106          **/

107         addIndex = index;
108         String JavaDoc[] values = str.split("\n");
109         if (values.length > 0)
110         {
111             System.out.println("Link source."+values[0]+" <-> destination."+ target.getSelectedValue());
112         }
113         /*
114         addCount = values.length;
115         for (int i = 0; i < values.length; i++) {
116             listModel.add(index++, values[i]);
117         }
118          */

119         
120         
121     }
122
123     //If the remove argument is true, the drop has been
124
//successful and it's time to remove the selected items
125
//from the list. If the remove argument is false, it
126
//was a Copy operation and the original list is left
127
//intact.
128
protected void cleanup(JComponent c, boolean remove) {
129         if (remove && indices != null) {
130             JList source = (JList)c;
131             DefaultListModel model = (DefaultListModel)source.getModel();
132             //If we are moving items around in the same list, we
133
//need to adjust the indices accordingly, since those
134
//after the insertion point have moved.
135
/*
136             if (addCount > 0) {
137                 for (int i = 0; i < indices.length; i++) {
138                     if (indices[i] > addIndex) {
139                         indices[i] += addCount;
140                     }
141                 }
142             }
143             for (int i = indices.length - 1; i >= 0; i--) {
144                 model.remove(indices[i]);
145             }
146              ***/

147         }
148              
149         indices = null;
150         addCount = 0;
151         addIndex = -1;
152              
153     }
154 }
155
Popular Tags