001: package com.calipso.reportgenerator.userinterface.dinamicchart;
002:
003: import com.calipso.reportgenerator.reportdefinitions.types.DimensionDefinitionLocationType;
004:
005: import javax.swing.*;
006: import java.awt.*;
007: import java.awt.datatransfer.DataFlavor;
008: import java.awt.dnd.*;
009: import java.util.Vector;
010: import java.util.Set;
011: import java.util.HashSet;
012:
013: /**
014: *
015: * User: soliveri
016: * Date: Jul 23, 2003
017: * Time: 4:46:13 PM
018: *
019: */
020:
021: public class DropPanel extends JPanel implements DropTargetListener {
022:
023: private String id;
024: private Vector dimensions;
025: private ChartPivotTableManager manager = ChartPivotTableManager
026: .getManager();
027:
028: public DropPanel(String id, int layoutFormat) {
029: this .id = id;
030: setLayout(new BoxLayout(this , layoutFormat));
031: setBackground(Color.white);
032: setSize(new Dimension(0, 0));
033: setBorder(BorderFactory.createEtchedBorder());
034: setDropTarget(new DropTarget(this , this ));
035: }
036:
037: public void addDimension(String name, String text,
038: HashSet excludeDimensionValues, Set dimensionValues) {
039: SourceDimension current = null;
040: if (DimensionDefinitionLocationType.ROW.toString().equals(id)) {
041: current = new SourceDimension(name, text,
042: excludeDimensionValues, dimensionValues);
043: current.setSize(new Dimension(getSize().width, 15));
044: current
045: .setPreferredSize(new Dimension(getSize().width, 15));
046: current.setMinimumSize(new Dimension(getSize().width, 15));
047: current.setMaximumSize(new Dimension(getSize().width, 15));
048: getDimensions().add(current);
049: add(current);
050: } else {
051: current = new SourceDimension(name, text,
052: excludeDimensionValues, dimensionValues);
053: getDimensions().add(current);
054: add(current);
055: }
056: }
057:
058: public void addDimensionToPanel(String name) {
059: SourceDimension current = getDimensionFromName(name);
060: if (DimensionDefinitionLocationType.ROW.toString().equals(id)) {
061: current.setSize(new Dimension(getSize().width, 15));
062: current
063: .setPreferredSize(new Dimension(getSize().width, 15));
064: current.setMinimumSize(new Dimension(getSize().width, 15));
065: current.setMaximumSize(new Dimension(getSize().width, 15));
066: add(current);
067: } else {
068: current.setSize(new Dimension(current.getOriginalWidth(),
069: 15));
070: current.setPreferredSize(new Dimension(current
071: .getOriginalWidth(), 15));
072: current.setMinimumSize(new Dimension(current
073: .getOriginalWidth(), 15));
074: current.setMaximumSize(new Dimension(current
075: .getOriginalWidth(), 15));
076: add(current);
077: }
078:
079: }
080:
081: private SourceDimension getDimensionFromName(String name) {
082: SourceDimension sourceDimension = null;
083: for (int i = 0; i < getDimensions().size(); i++) {
084: if (((SourceDimension) getDimensions().elementAt(i))
085: .getName().equals(name)) {
086: sourceDimension = (SourceDimension) getDimensions()
087: .elementAt(i);
088: }
089: }
090: return sourceDimension;
091: }
092:
093: public boolean contains(String element) {
094: for (int i = 0; i < getDimensions().size(); i++) {
095: if (((SourceDimension) getDimensions().elementAt(i))
096: .getName().equals(element)) {
097: return true;
098: }
099: }
100: return false;
101: }
102:
103: public void addIncomingDimension(DropPanel source,
104: String dimension, Point point) {
105: int index;
106: if (DimensionDefinitionLocationType.PAGE.toString().equals(id)
107: || DimensionDefinitionLocationType.COLUMN.toString()
108: .equals(id)) {
109: index = getVectorDimensionLocationOnLenght(point);
110: if (this == source && getDimensionIndex(dimension) < index) {
111: getDimensions().insertElementAt(
112: source.getDimension(dimension), index - 1);
113: } else {
114: getDimensions().insertElementAt(
115: source.getDimension(dimension), index);
116: }
117: } else {
118: index = getVectorDimensionLocationOnHeight(point);
119: if (this == source && getDimensionIndex(dimension) < index) {
120: getDimensions().insertElementAt(
121: source.getDimension(dimension), index - 1);
122: } else {
123: getDimensions().insertElementAt(
124: source.getDimension(dimension), index);
125: }
126: }
127: }
128:
129: public int getDimensionIndex(String dimension) {
130: for (int i = 0; i < getDimensions().size(); i++) {
131: if (((SourceDimension) getDimensions().elementAt(i))
132: .getName().equals(dimension)) {
133: return i;
134: }
135: }
136: return 0;
137: }
138:
139: public SourceDimension getDimension(String dimension) {
140: SourceDimension sourceDimension = null;
141: for (int i = 0; i < getDimensions().size(); i++) {
142: if (((SourceDimension) getDimensions().elementAt(i))
143: .getName().equals(dimension)) {
144: sourceDimension = (SourceDimension) getDimensions()
145: .elementAt(i);
146: getDimensions().remove(i);
147: break;
148: }
149: }
150: return sourceDimension;
151: }
152:
153: private int getVectorDimensionLocationOnLenght(Point point) {
154: int acc = 0;
155: for (int i = 0; i < getDimensions().size(); i++) {
156: SourceDimension dragDimension = (SourceDimension) getDimensions()
157: .elementAt(i);
158: acc = acc + dragDimension.getSize().width;
159: if (point.x <= acc
160: && point.x > (acc - dragDimension.getSize().width)) {
161: return i;
162: }
163: }
164: return getDimensions().size();
165: }
166:
167: private int getVectorDimensionLocationOnHeight(Point point) {
168: int acc = 0;
169: for (int i = 0; i < getDimensions().size(); i++) {
170: SourceDimension dragDimension = (SourceDimension) getDimensions()
171: .elementAt(i);
172: acc = acc + dragDimension.getSize().height;
173: if (point.y <= acc
174: && point.y > (acc - dragDimension.getSize().height)) {
175: return i;
176: }
177: }
178: return getDimensions().size();
179: }
180:
181: public String getDimensionNameAt(int index) {
182: return ((SourceDimension) getDimensions().elementAt(index))
183: .getName();
184: }
185:
186: public int getDimensionsCount() {
187: return getDimensions().size();
188: }
189:
190: public String getId() {
191: return id;
192: }
193:
194: public Vector getDimensions() {
195: if (dimensions == null) {
196: dimensions = new Vector();
197: }
198: return dimensions;
199: }
200:
201: public void dragEnter(DropTargetDragEvent dtde) {
202: }
203:
204: public void dragOver(DropTargetDragEvent dtde) {
205: }
206:
207: public void dropActionChanged(DropTargetDragEvent dtde) {
208: }
209:
210: public void dragExit(DropTargetEvent dte) {
211: }
212:
213: public void drop(DropTargetDropEvent dtde) {
214: DataFlavor stringFlavor = DataFlavor.stringFlavor;
215: String incoming = null;
216: try {
217: incoming = dtde.getTransferable().getTransferData(
218: stringFlavor).toString();
219: Point point = dtde.getLocation();
220: System.out.println(incoming);
221: System.out.println(point.y);
222: manager.dropJustHappened(this , incoming, point);
223: } catch (Exception e) {
224: e.printStackTrace();
225: }
226: dtde.dropComplete(true);
227: }
228: }
|