KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > userinterface > DragSourcePanel


1 package com.calipso.reportgenerator.userinterface;
2
3 import com.calipso.reportgenerator.common.InfoException;
4
5 import javax.swing.*;
6 import java.awt.*;
7 import java.awt.datatransfer.StringSelection JavaDoc;
8 import java.awt.dnd.*;
9 import java.util.Vector JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Set JavaDoc;
12 import com.calipso.reportgenerator.common.IReportManager;
13 import com.calipso.reportgenerator.common.ShowExceptionMessageDialog;
14
15 /**
16  * Clese utilizada para crear un componente con propiedades de Drag
17  */

18 public class DragSourcePanel extends JPanel implements DragGestureListener, DragSourceListener {
19
20   private JLabel label;
21   private JButton button;
22   private DimensionColumnValue framColVal;
23   private Vector JavaDoc colVecJCeck;
24   private PivotTable pivote;
25   private String JavaDoc location;
26   private IReportManager reportManager;
27   private int reportHandle;
28
29   /**
30    * Retorna el JLabel utilizado para colocar el nombre de la dimension
31    * @return
32    */

33   public JLabel getLabel() {
34     return label;
35   }
36
37   /**
38    * Retorna el botón con los valores de las dimensiones
39    * @return
40    */

41   public JButton getButton() {
42     return button;
43   }
44
45   /**
46    * Setea al componente como drageable
47    * @param name
48    */

49   public DragSourcePanel(String JavaDoc name, PivotTable pivote, String JavaDoc description, String JavaDoc location, IReportManager reportManager,int reportHandle) {
50     this.pivote = pivote;
51     this.location = location;
52     this.reportManager = reportManager;
53     this.reportHandle = reportHandle;
54     label = new JLabel(description + " ");
55     label.setBackground(Color.BLACK);
56     label.setForeground(Color.WHITE);
57     Font font = new Font("Arial", Font.BOLD, 10);
58     label.setFont(font);
59     setName(name);
60     setLayout(new BorderLayout());
61     button = new JButton(" ");
62     button.setPreferredSize(new Dimension(10, 10));
63     add(button, BorderLayout.EAST);
64     button.addActionListener(new java.awt.event.ActionListener JavaDoc() {
65       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
66         try {
67           jButton1ActionPerformed();
68         }
69         catch (Exception JavaDoc e) {
70           ShowExceptionMessageDialog.initExceptionDialogMessage(com.calipso.reportgenerator.common.LanguageTraslator.traslate("89"),e);
71         }
72       }
73     });
74     add(label, BorderLayout.CENTER);
75     DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
76   }
77
78   /**
79    * Retorna un Objeto DimensionColumnValue
80    * @return
81    */

82   public DimensionColumnValue getFramColVal() {
83     return framColVal;
84   }
85
86   /**
87    * Dispara el evento en caso de realizar un ActionEvent sobre el botón con los valores de las dimensiones
88    *
89    */

90   private void jButton1ActionPerformed() throws InfoException {
91     Set JavaDoc set = getReportManager().getDimensionValues(getReportHandle(),getName());
92     int maxWidthLabel = Math.max((int) (getPreferredSize().getWidth()), getMaxWidthLabel(storeCheckButtonLabel(set)));
93     this.framColVal = new DimensionColumnValue(maxWidthLabel, getLocationOnScreen(), pivote, colVecJCeck, location, getName());
94
95   }
96
97   /**
98    * Retorna el size máximo del label
99    * @param vector
100    * @return
101    */

102   private int getMaxWidthLabel(Vector JavaDoc vector) {
103     Font font = new Font("Arial", Font.BOLD, 10);
104     int width = 0;
105     int max = -1;
106     JTextField aux = null;
107     for (int index = 0; index < vector.size(); index++) {
108       Object JavaDoc vec[] = (Object JavaDoc[]) vector.get(index);
109       String JavaDoc str = (vec[0]).toString();
110       width = getFontMetrics(font).stringWidth(str);
111       if (width > max) {
112         aux = new JTextField(str.trim());
113         max = (int) aux.getPreferredSize().getWidth() + str.length();
114       }
115     }
116     return max;
117   }
118
119   /**
120    * Carga los valores de las dimensiones para cada componente
121    * @return
122    */

123   private Vector JavaDoc storeCheckButtonLabel(Set JavaDoc set) {
124     ColumnProperties properties = pivote.getTableProperties().getRowColumn(getName());
125     Set JavaDoc unchecked = properties.getExcludeValue();
126     colVecJCeck = new Vector JavaDoc();
127       Iterator JavaDoc iterator = set.iterator();
128       while (iterator.hasNext()) {
129         Object JavaDoc value = iterator.next();
130         Object JavaDoc[] btnArray = new Object JavaDoc[2];
131         btnArray[0] = value;
132         btnArray[1] = new Boolean JavaDoc(unchecked == null || !unchecked.contains(value));
133         colVecJCeck.add(btnArray);
134       }
135     return colVecJCeck;
136   }
137
138   public String JavaDoc getLocationOfComponent() {
139     return location;
140   }
141
142   /**
143    * Implementación de la interface DragGestureListener
144    * @param dge
145    */

146   public void dragGestureRecognized(DragGestureEvent dge) {
147     dge.startDrag(DragSource.DefaultCopyNoDrop, new StringSelection JavaDoc(this.getName()), this);
148   }
149
150   /**
151    * Implementación de la interface DragSourceListener
152    * @param dsde
153    */

154   public void dragEnter(DragSourceDragEvent dsde) {
155     dsde.getDragSourceContext().setCursor(DragSource.DefaultCopyDrop);
156   }
157
158   /**
159    * Implementación de la interface DragSourceListener
160    * @param dsde
161    */

162   public void dragOver(DragSourceDragEvent dsde) {
163   }
164
165   /**
166    * Implementación de la interface DragSourceListener
167    * @param dsde
168    */

169   public void dropActionChanged(DragSourceDragEvent dsde) {
170   }
171
172   /**
173    * Implementación de la interface DragSourceListener
174    * @param dse
175    */

176   public void dragExit(DragSourceEvent dse) {
177     dse.getDragSourceContext().setCursor(DragSource.DefaultCopyNoDrop);
178   }
179
180   /**
181    * Implementación de la interface DragSourceListener
182    * @param dsde
183    */

184   public void dragDropEnd(DragSourceDropEvent dsde) {
185   }
186
187   public IReportManager getReportManager() {
188     return reportManager;
189   }
190
191   public int getReportHandle() {
192     return reportHandle;
193   }
194 }
195
Popular Tags