001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the 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, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * ListColumnTransferableHandler.java
028: *
029: * Created on 21 maggio 2003, 18.48
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.dnd;
034:
035: /**
036: *
037: * @author Administrator
038: */
039: import it.businesslogic.ireport.gui.*;
040: import javax.swing.*;
041: import java.awt.*;
042: import java.awt.datatransfer.*;
043:
044: public class ListColumnTransferableHandler extends
045: ColumnTransferableHandler {
046: private int[] indices = null;
047: private int addIndex = -1; //Location where items were added
048: private int addCount = 0; //Number of items added.
049: private JSQLTablesPane jSQLTablePane = null;
050:
051: public void setSQLTablesPane(JSQLTablesPane jSQLTablePane) {
052: this .jSQLTablePane = jSQLTablePane;
053: }
054:
055: public JSQLTablesPane getSQLTablesPane() {
056: return this .jSQLTablePane;
057: }
058:
059: //Bundle up the selected items in the list
060: //as a single string, for export.
061: protected String exportString(JComponent c) {
062: JList list = (JList) c;
063: indices = list.getSelectedIndices();
064: Object[] values = list.getSelectedValues();
065:
066: StringBuffer buff = new StringBuffer();
067:
068: for (int i = 0; i < values.length; i++) {
069: Object val = values[i];
070: buff.append(val == null ? "" : val.toString());
071: if (i != values.length - 1) {
072: buff.append("\n");
073: }
074: }
075:
076: return buff.toString();
077: }
078:
079: //Take the incoming string and wherever there is a
080: //newline, break it into a separate item in the list.
081: protected void importString(JComponent c, String str) {
082: JList target = (JList) c;
083: DefaultListModel listModel = (DefaultListModel) target
084: .getModel();
085: int index = target.getSelectedIndex();
086:
087: //Prevent the user from dropping data back on itself.
088: //For example, if the user is moving items #4,#5,#6 and #7 and
089: //attempts to insert the items after item #5, this would
090: //be problematic when removing the original items.
091: //So this is not allowed.
092: /*
093: if (indices != null && index >= indices[0] - 1 &&
094: index <= indices[indices.length - 1]) {
095: indices = null;
096: return;
097: }
098:
099: int max = listModel.getSize();
100: if (index < 0) {
101: index = max;
102: } else {
103: index++;
104: if (index > max) {
105: index = max;
106: }
107: }
108: **/
109: addIndex = index;
110: String[] values = str.split("\n");
111: if (values.length > 0) {
112: System.out.println("Link source." + values[0]
113: + " <-> destination." + target.getSelectedValue());
114: }
115: /*
116: addCount = values.length;
117: for (int i = 0; i < values.length; i++) {
118: listModel.add(index++, values[i]);
119: }
120: */
121:
122: }
123:
124: //If the remove argument is true, the drop has been
125: //successful and it's time to remove the selected items
126: //from the list. If the remove argument is false, it
127: //was a Copy operation and the original list is left
128: //intact.
129: protected void cleanup(JComponent c, boolean remove) {
130: if (remove && indices != null) {
131: JList source = (JList) c;
132: DefaultListModel model = (DefaultListModel) source
133: .getModel();
134: //If we are moving items around in the same list, we
135: //need to adjust the indices accordingly, since those
136: //after the insertion point have moved.
137: /*
138: if (addCount > 0) {
139: for (int i = 0; i < indices.length; i++) {
140: if (indices[i] > addIndex) {
141: indices[i] += addCount;
142: }
143: }
144: }
145: for (int i = indices.length - 1; i >= 0; i--) {
146: model.remove(indices[i]);
147: }
148: ***/
149: }
150:
151: indices = null;
152: addCount = 0;
153: addIndex = -1;
154:
155: }
156: }
|