KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > userinterface > dinamicchart > DropPanel


1 package com.calipso.reportgenerator.userinterface.dinamicchart;
2
3 import com.calipso.reportgenerator.reportdefinitions.types.DimensionDefinitionLocationType;
4
5 import javax.swing.*;
6 import java.awt.*;
7 import java.awt.datatransfer.DataFlavor JavaDoc;
8 import java.awt.dnd.*;
9 import java.util.Vector JavaDoc;
10 import java.util.Set JavaDoc;
11 import java.util.HashSet JavaDoc;
12
13 /**
14  *
15  * User: soliveri
16  * Date: Jul 23, 2003
17  * Time: 4:46:13 PM
18  *
19  */

20
21 public class DropPanel extends JPanel implements DropTargetListener{
22
23   private String JavaDoc id;
24   private Vector JavaDoc dimensions;
25   private ChartPivotTableManager manager = ChartPivotTableManager.getManager();
26
27   public DropPanel(String JavaDoc id, int layoutFormat) {
28     this.id = id;
29     setLayout(new BoxLayout(this, layoutFormat));
30     setBackground(Color.white);
31     setSize(new Dimension(0, 0));
32     setBorder(BorderFactory.createEtchedBorder());
33     setDropTarget(new DropTarget(this, this));
34   }
35
36   public void addDimension(String JavaDoc name, String JavaDoc text, HashSet JavaDoc excludeDimensionValues, Set JavaDoc dimensionValues) {
37     SourceDimension current = null;
38     if(DimensionDefinitionLocationType.ROW.toString().equals(id)) {
39       current = new SourceDimension(name, text, excludeDimensionValues, dimensionValues);
40       current.setSize(new Dimension(getSize().width, 15));
41       current.setPreferredSize(new Dimension(getSize().width, 15));
42       current.setMinimumSize(new Dimension(getSize().width, 15));
43       current.setMaximumSize(new Dimension(getSize().width, 15));
44       getDimensions().add(current);
45       add(current);
46     } else {
47       current = new SourceDimension(name, text, excludeDimensionValues, dimensionValues);
48       getDimensions().add(current);
49       add(current);
50     }
51   }
52
53   public void addDimensionToPanel(String JavaDoc name) {
54     SourceDimension current = getDimensionFromName(name);
55     if(DimensionDefinitionLocationType.ROW.toString().equals(id)) {
56       current.setSize(new Dimension(getSize().width, 15));
57       current.setPreferredSize(new Dimension(getSize().width, 15));
58       current.setMinimumSize(new Dimension(getSize().width, 15));
59       current.setMaximumSize(new Dimension(getSize().width, 15));
60       add(current);
61     } else{
62       current.setSize(new Dimension(current.getOriginalWidth(), 15));
63       current.setPreferredSize(new Dimension(current.getOriginalWidth(), 15));
64       current.setMinimumSize(new Dimension(current.getOriginalWidth(), 15));
65       current.setMaximumSize(new Dimension(current.getOriginalWidth(), 15));
66       add(current);
67     }
68
69   }
70
71   private SourceDimension getDimensionFromName(String JavaDoc name) {
72     SourceDimension sourceDimension = null;
73     for(int i = 0 ; i < getDimensions().size() ; i++) {
74       if(((SourceDimension)getDimensions().elementAt(i)).getName().equals(name)) {
75         sourceDimension = (SourceDimension)getDimensions().elementAt(i);
76       }
77     }
78     return sourceDimension;
79   }
80
81   public boolean contains(String JavaDoc element) {
82     for(int i = 0 ; i < getDimensions().size() ; i++) {
83       if(((SourceDimension)getDimensions().elementAt(i)).getName().equals(element)) {
84         return true;
85       }
86     }
87     return false;
88   }
89
90   public void addIncomingDimension(DropPanel source, String JavaDoc dimension, Point point) {
91     int index;
92     if(DimensionDefinitionLocationType.PAGE.toString().equals(id) || DimensionDefinitionLocationType.COLUMN.toString().equals(id)) {
93       index = getVectorDimensionLocationOnLenght(point);
94       if(this == source && getDimensionIndex(dimension) < index) {
95         getDimensions().insertElementAt(source.getDimension(dimension), index - 1);
96       } else {
97         getDimensions().insertElementAt(source.getDimension(dimension), index);
98       }
99     } else {
100       index = getVectorDimensionLocationOnHeight(point);
101       if(this == source && getDimensionIndex(dimension) < index) {
102         getDimensions().insertElementAt(source.getDimension(dimension), index - 1);
103       } else {
104         getDimensions().insertElementAt(source.getDimension(dimension), index);
105       }
106     }
107   }
108
109   public int getDimensionIndex(String JavaDoc dimension) {
110     for(int i = 0 ; i < getDimensions().size() ; i++) {
111       if(((SourceDimension)getDimensions().elementAt(i)).getName().equals(dimension)) {
112         return i;
113       }
114     }
115     return 0;
116   }
117
118   public SourceDimension getDimension(String JavaDoc dimension) {
119     SourceDimension sourceDimension = null;
120     for(int i = 0 ; i < getDimensions().size() ; i++) {
121       if(((SourceDimension)getDimensions().elementAt(i)).getName().equals(dimension)) {
122         sourceDimension = (SourceDimension) getDimensions().elementAt(i);
123         getDimensions().remove(i);
124         break;
125       }
126     }
127     return sourceDimension;
128   }
129
130   private int getVectorDimensionLocationOnLenght(Point point) {
131     int acc = 0;
132     for(int i = 0 ; i < getDimensions().size() ; i++) {
133       SourceDimension dragDimension = (SourceDimension) getDimensions().elementAt(i);
134       acc = acc + dragDimension.getSize().width;
135       if(point.x <= acc && point.x > (acc - dragDimension.getSize().width)){
136          return i;
137       }
138     }
139     return getDimensions().size();
140   }
141
142   private int getVectorDimensionLocationOnHeight(Point point) {
143     int acc = 0;
144     for(int i = 0 ; i < getDimensions().size() ; i++) {
145       SourceDimension dragDimension = (SourceDimension) getDimensions().elementAt(i);
146       acc = acc + dragDimension.getSize().height;
147       if(point.y <= acc && point.y > (acc - dragDimension.getSize().height)){
148         return i;
149       }
150     }
151     return getDimensions().size();
152   }
153
154   public String JavaDoc getDimensionNameAt(int index) {
155     return ((SourceDimension) getDimensions().elementAt(index)).getName();
156   }
157
158   public int getDimensionsCount() {
159     return getDimensions().size();
160   }
161
162   public String JavaDoc getId() {
163     return id;
164   }
165
166   public Vector JavaDoc getDimensions() {
167     if(dimensions == null) {
168       dimensions = new Vector JavaDoc();
169     }
170     return dimensions;
171   }
172
173   public void dragEnter(DropTargetDragEvent dtde) {
174   }
175
176   public void dragOver(DropTargetDragEvent dtde) {
177   }
178
179   public void dropActionChanged(DropTargetDragEvent dtde) {
180   }
181
182   public void dragExit(DropTargetEvent dte) {
183   }
184
185   public void drop(DropTargetDropEvent dtde) {
186     DataFlavor JavaDoc stringFlavor = DataFlavor.stringFlavor;
187     String JavaDoc incoming = null;
188     try {
189       incoming = dtde.getTransferable().getTransferData(stringFlavor).toString();
190       Point point = dtde.getLocation();
191       System.out.println(incoming);
192       System.out.println(point.y);
193       manager.dropJustHappened(this, incoming, point);
194     }
195     catch (Exception JavaDoc e) {
196       e.printStackTrace();
197     }
198     dtde.dropComplete(true);
199   }
200 }
201
Popular Tags