001: /*
002: * Copyright (C) 2004 Giuseppe MANNA
003: *
004: * This file is part of FreeReportBuilder
005: *
006: * FreeReportBuilder is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * 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, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package it.frb;
023:
024: import java.awt.*;
025: import java.awt.Container;
026: import java.awt.Point;
027: import java.awt.event.MouseEvent;
028: import java.util.*;
029: import javax.swing.*;
030: import javax.swing.JComponent;
031: import javax.swing.border.*;
032:
033: public class Dragger extends java.awt.event.MouseAdapter implements
034: java.awt.event.MouseMotionListener, java.io.Serializable,
035: java.awt.event.KeyListener {
036: private DataPanel dataPanel = null;
037:
038: boolean isPossibleCancel;
039: boolean dragging = false;
040: public boolean ctrlPress = false;
041: public boolean shiftPress = false;
042:
043: public Border border;
044: public Container panel;
045: public Vector vectorComponent = new Vector();
046:
047: public Container dadContainer;
048: private Hashtable hashInitialPosition = new Hashtable();
049:
050: Point press = new Point();
051:
052: public int getCountVectorComponentSelected() {
053: return vectorComponent.size();
054: }
055:
056: public Vector getVectorComponentSelected() {
057: return vectorComponent;
058: }
059:
060: public void setDataPanel(DataPanel dataPanel) {
061: this .dataPanel = dataPanel;
062: }
063:
064: public void setCancelMode(boolean cancel) {
065: this .isPossibleCancel = cancel;
066: }
067:
068: public boolean getCancelMode() {
069: return isPossibleCancel;
070: }
071:
072: public void setJPanel(Container panel) {
073: this .panel = panel;
074: }
075:
076: public void addXValueToAllSelectJComponent(int x) {
077: if (this .vectorComponent.size() > 0) {
078: for (int i = 0; i < this .vectorComponent.size(); i++) {
079: JComponent comp = comp = (JComponent) this .vectorComponent
080: .get(i);
081: comp.setLocation(comp.getLocation().x + x, comp
082: .getLocation().y);
083: }
084: }
085: }
086:
087: public void addYValueToAllSelectJComponent(int y) {
088: if (this .vectorComponent.size() > 0) {
089: for (int i = 0; i < this .vectorComponent.size(); i++) {
090: JComponent comp = comp = (JComponent) this .vectorComponent
091: .get(i);
092: comp.setLocation(comp.getLocation().x, comp
093: .getLocation().y
094: + y);
095: }
096: }
097: }
098:
099: public void selectJComponent(JComponent obj) {
100: if (!vectorComponent.contains(obj)) {
101: obj.setEnabled(false);
102:
103: int x = obj.getLocation().x;
104:
105: /***** nuova implementazione **********/
106: Container sourceContainer = (Container) obj.getParent();
107:
108: //Servira' per sapere il primo oggetto da quale pannello
109: //arrivava
110: dadContainer = sourceContainer;
111:
112: Container container = sourceContainer;
113: Container etest = null;
114:
115: while (!(container instanceof javax.swing.JLayeredPane)) {
116: container = (Container) container.getParent();
117: }
118:
119: obj.setLocation(obj.getLocationOnScreen().x
120: - javax.swing.JLayeredPane.getLayeredPaneAbove(
121: dataPanel).getLocationOnScreen().x, obj
122: .getLocationOnScreen().y
123: - javax.swing.JLayeredPane.getLayeredPaneAbove(
124: dataPanel).getLocationOnScreen().y);
125:
126: sourceContainer.remove(obj);
127:
128: ((javax.swing.JLayeredPane) container).add(obj,
129: javax.swing.JLayeredPane.DRAG_LAYER);
130: container.repaint();
131:
132: int y = obj.getLocation().y;
133:
134: hashInitialPosition.put(obj, new Point(x, y));
135: /***** nuova implementazione **********/
136:
137: vectorComponent.add(obj);
138: }
139: }
140:
141: public void deselectJcomponent(JComponent obj, MouseEvent e) {
142: int x;
143: int y;
144:
145: obj.setEnabled(true);
146: obj.setBorder(BorderFactory.createLineBorder(Color.gray));
147:
148: try {
149: x = obj.getLocationOnScreen().x;
150: y = obj.getLocationOnScreen().y;
151:
152: } catch (java.awt.IllegalComponentStateException illExc) {
153: x = e.getX()
154: + ((JComponent) e.getSource())
155: .getLocationOnScreen().x;
156: y = e.getY()
157: + ((JComponent) e.getSource())
158: .getLocationOnScreen().y;
159: }
160:
161: Component container = dataPanel.getComponentAt(0, y
162: - dataPanel.getLocationOnScreen().y);
163:
164: JPanel searchPanel = null;
165: boolean exit = false;
166: int sumDividerPosition = 0;
167: int lastsumDividerPosition = 0;
168: int scrollH = 0;
169: int scrollV = 0;
170: int scrollHorHeader = 0;
171: int scrollVerRow = 0;
172:
173: while ((container != null) && (!exit)) {
174: if (container instanceof JPanel) {
175: if (!(((JPanel) container).getBorder() instanceof javax.swing.border.TitledBorder)
176: || ((JPanel) container).getBorder() == null) {
177: container = (Component) container.getComponentAt(0,
178: y - dataPanel.getLocationOnScreen().y);
179: } else {
180: exit = true;
181: }
182: } else if (container instanceof JSplitPane) {
183: if (lastsumDividerPosition == 0) {
184: lastsumDividerPosition = dataPanel
185: .getLocationOnScreen().y;
186: } else {
187: lastsumDividerPosition = sumDividerPosition
188: + new Integer(UIManager.getDefaults().get(
189: "SplitPane.dividerSize").toString())
190: .intValue();
191: }
192:
193: sumDividerPosition = ((JSplitPane) container)
194: .getDividerLocation()
195: + lastsumDividerPosition;
196:
197: if (sumDividerPosition > y + scrollV) {
198: container = ((JSplitPane) container)
199: .getLeftComponent();
200: } else {
201: container = ((JSplitPane) container)
202: .getRightComponent();
203: }
204: } else if (container instanceof JScrollPane) {
205: javax.swing.JScrollBar jscrollHorz = ((JScrollPane) container)
206: .getHorizontalScrollBar();
207: javax.swing.JScrollBar jscrollVert = ((JScrollPane) container)
208: .getVerticalScrollBar();
209:
210: scrollH = jscrollHorz.getValue();
211: scrollV = jscrollVert.getValue();
212:
213: javax.swing.JViewport jscrollHeader = ((JScrollPane) container)
214: .getColumnHeader();
215: javax.swing.JViewport jscrollRow = ((JScrollPane) container)
216: .getRowHeader();
217:
218: if (jscrollHeader != null) {
219: scrollHorHeader = jscrollHeader.getHeight();
220: }
221:
222: if (jscrollRow != null) {
223: scrollVerRow = jscrollRow.getWidth();
224: }
225:
226: container = ((JScrollPane) container).getViewport();
227: } else {
228: container = (Component) container.getComponentAt(0, y
229: - dataPanel.getLocationOnScreen().y
230: + sumDividerPosition);
231:
232: if (container instanceof javax.swing.JTextPane) {
233: container = null;
234: exit = true;
235: }
236: }
237: }
238:
239: obj.setLocation(x + scrollH - 1
240: - dataPanel.getLocationOnScreen().x - scrollVerRow, y
241: + scrollV - 1 - lastsumDividerPosition
242: - scrollHorHeader);
243:
244: if ((container != null)
245: && !(container instanceof javax.swing.JTextArea)) {
246: if ((Container) obj.getParent() != null) {
247: ((Container) obj.getParent()).setCursor(Cursor
248: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
249: ((Container) obj.getParent()).remove(obj);
250: }
251:
252: if (obj.getClass() == JPanel.class) {
253: ((JPanel) container).add(obj);
254: } else {
255: ((JPanel) container).add(obj, 0);
256: }
257: ((JPanel) container).repaint();
258:
259: this .vectorComponent.remove(obj);
260: } else {
261: int newX = 0;
262: int newY = 0;
263:
264: if (!hashInitialPosition.isEmpty()) {
265: newX = ((Point) hashInitialPosition.get(obj)).x;
266: newY = ((Point) hashInitialPosition.get(obj)).y;
267: }
268:
269: obj.setLocation(newX, newY);
270: this .deselectJcomponent(obj, e);
271: obj.setLocation(newX, obj.getLocation().y);
272:
273: this .dataPanel.repaint();
274:
275: this .hashInitialPosition.remove(obj);
276: //i=i-1;
277: }
278: }
279:
280: public void deselectAllJcomponent(MouseEvent e) {
281: if (this .vectorComponent.size() > 0) {
282: for (int i = 0; i < this .vectorComponent.size(); i++) {
283: int x;
284: int y;
285:
286: JComponent comp = comp = (JComponent) this .vectorComponent
287: .get(i);
288:
289: try {
290: x = comp.getLocationOnScreen().x;
291: y = comp.getLocationOnScreen().y;
292:
293: } catch (java.awt.IllegalComponentStateException illExc) {
294: x = e.getX()
295: + ((JComponent) e.getSource())
296: .getLocationOnScreen().x;
297: y = e.getY()
298: + ((JComponent) e.getSource())
299: .getLocationOnScreen().y;
300: }
301:
302: Component container = container = dataPanel
303: .getComponentAt(0, y
304: - dataPanel.getLocationOnScreen().y);
305:
306: JPanel searchPanel = null;
307: boolean exit = false;
308: int sumDividerPosition = 0;
309: int lastsumDividerPosition = 0;
310: int scrollH = 0;
311: int scrollV = 0;
312: int scrollHorHeader = 0;
313: int scrollVerRow = 0;
314:
315: while ((container != null) && (!exit)) {
316: if (container instanceof JPanel) {
317: if (!(((JPanel) container).getBorder() instanceof javax.swing.border.TitledBorder)
318: || ((JPanel) container).getBorder() == null) {
319: container = (Component) container
320: .getComponentAt(
321: 0,
322: y
323: - dataPanel
324: .getLocationOnScreen().y);
325: } else {
326: exit = true;
327: }
328: } else if (container instanceof JSplitPane) {
329: if (lastsumDividerPosition == 0) {
330: lastsumDividerPosition = dataPanel
331: .getLocationOnScreen().y;
332: } else {
333: lastsumDividerPosition = sumDividerPosition
334: + new Integer(
335: UIManager
336: .getDefaults()
337: .get(
338: "SplitPane.dividerSize")
339: .toString())
340: .intValue();
341: }
342:
343: sumDividerPosition = ((JSplitPane) container)
344: .getDividerLocation()
345: + lastsumDividerPosition;
346:
347: if (sumDividerPosition > y + scrollV) {
348: container = ((JSplitPane) container)
349: .getLeftComponent();
350: } else {
351: container = ((JSplitPane) container)
352: .getRightComponent();
353: }
354: } else if (container instanceof JScrollPane) {
355: javax.swing.JScrollBar jscrollHorz = ((JScrollPane) container)
356: .getHorizontalScrollBar();
357: javax.swing.JScrollBar jscrollVert = ((JScrollPane) container)
358: .getVerticalScrollBar();
359:
360: scrollH = jscrollHorz.getValue();
361: scrollV = jscrollVert.getValue();
362:
363: javax.swing.JViewport jscrollHeader = ((JScrollPane) container)
364: .getColumnHeader();
365: javax.swing.JViewport jscrollRow = ((JScrollPane) container)
366: .getRowHeader();
367:
368: if (jscrollHeader != null) {
369: scrollHorHeader = jscrollHeader.getHeight();
370: }
371:
372: if (jscrollRow != null) {
373: scrollVerRow = jscrollRow.getWidth();
374: }
375:
376: container = ((JScrollPane) container)
377: .getViewport();
378: } else {
379: container = (Component) container
380: .getComponentAt(
381: 0,
382: y
383: - dataPanel
384: .getLocationOnScreen().y
385: + sumDividerPosition);
386:
387: if (container instanceof javax.swing.JTextPane) {
388: container = null;
389: exit = true;
390: }
391: }
392: }
393:
394: if ((container != null)
395: && !(container instanceof javax.swing.JTextArea)) {
396: int newX = x + scrollH - 1
397: - dataPanel.getLocationOnScreen().x
398: - scrollVerRow;
399: int newY = y + scrollV - 1 - lastsumDividerPosition
400: - scrollHorHeader;
401:
402: if (dataPanel.getLocationOnScreen().x > newX) {
403: if (!hashInitialPosition.isEmpty()) {
404: newX = ((Point) hashInitialPosition
405: .get(comp)).x;
406: newY = ((Point) hashInitialPosition
407: .get(comp)).y;
408: }
409:
410: comp.setLocation(newX, newY);
411: this .deselectJcomponent(comp, e);
412:
413: } else {
414: if ((Container) comp.getParent() != null) {
415: ((Container) comp.getParent()).remove(comp);
416: }
417:
418: comp.setLocation(newX, newY);
419: ((JPanel) container).add(comp);
420: ((JPanel) container).repaint();
421:
422: comp.setEnabled(true);
423: comp.setBorder(BorderFactory
424: .createLineBorder(Color.gray));
425:
426: this .vectorComponent.remove(comp);
427: }
428:
429: this .hashInitialPosition.remove(comp);
430: i = i - 1;
431: } else {
432: int newX = 0;
433: int newY = 0;
434:
435: if (!hashInitialPosition.isEmpty()) {
436: newX = ((Point) hashInitialPosition.get(comp)).x;
437: newY = ((Point) hashInitialPosition.get(comp)).y;
438: }
439:
440: comp.setLocation(newX, newY);
441: this .deselectJcomponent(comp, e);
442: comp.setLocation(newX, comp.getLocation().y);
443:
444: this .hashInitialPosition.remove(comp);
445: i = i - 1;
446: }
447: }
448: }
449: this .ctrlPress = false;
450: }
451:
452: public void mousePressed(MouseEvent e) {
453: if (!ctrlPress) {
454: if (vectorComponent.size() == 1) {
455: this .deselectAllJcomponent(e);
456: }
457:
458: press.x = e.getX();
459: press.y = e.getY();
460:
461: dragging = true;
462:
463: ((JComponent) e.getSource())
464: .setBorder(javax.swing.BorderFactory
465: .createLineBorder(java.awt.Color.red));
466: this .selectJComponent((JComponent) e.getSource());
467: } else {
468: ((JComponent) e.getSource())
469: .setBorder(javax.swing.BorderFactory
470: .createLineBorder(java.awt.Color.red));
471: }
472: }
473:
474: public boolean isDragging() {
475: return dragging;
476: }
477:
478: public void mouseReleased(MouseEvent e) {
479: if (!ctrlPress) {
480: if (vectorComponent != null && vectorComponent.size() == 1) {
481: JComponent comp = (JComponent) e.getSource();
482: this .deselectJcomponent(comp, e);
483: }
484: dragging = false;
485: }
486: }
487:
488: public void mouseClicked(MouseEvent e) {
489: if (!ctrlPress) {
490: if (e.getClickCount() == 1) {
491: dragging = false;
492: JComponent obj = (JComponent) e.getSource();
493:
494: if (obj != null && !obj.isEnabled()) {
495: if (vectorComponent.size() == 1) {
496: this .deselectAllJcomponent(e);
497: ((JComponent) obj)
498: .setBorder(javax.swing.BorderFactory
499: .createLineBorder(java.awt.Color.red));
500: this .selectJComponent(obj);
501: } else {
502: this .selectJComponent(obj);
503: }
504: }
505: } else if (e.getClickCount() == 2) {
506: JComponent obj = (JComponent) e.getSource();
507:
508: if (vectorComponent.contains(obj)) {
509: this .deselectJcomponent(obj, e);
510: } else {
511: this .selectJComponent(obj);
512: ResizeBorder rsb = new ResizeBorder();
513: ((JComponent) obj).setBorder(rsb);
514: }
515: }
516: } else {
517: JComponent obj = (JComponent) e.getSource();
518:
519: if (vectorComponent.contains(obj)) {
520: this .deselectJcomponent(obj, e);
521: } else {
522: if (vectorComponent != null
523: && vectorComponent.size() == 1) {
524: if (((JComponent) vectorComponent.get(0))
525: .getBorder().getClass() == ResizeBorder.class) {
526: this .deselectJcomponent(
527: (JComponent) vectorComponent.get(0), e);
528: }
529: }
530: this .selectJComponent(obj);
531: }
532: }
533: }
534:
535: public void mouseMoved(MouseEvent e) {
536: }
537:
538: public void mouseDragged(MouseEvent e) {
539: JComponent c = (JComponent) e.getSource();
540:
541: if (dragging) {
542: Point loc = c.getLocation();
543: Point pt = new Point();
544:
545: pt.x = e.getX() + loc.x - press.x;
546: pt.y = e.getY() + loc.y - press.y;
547:
548: Container container = dataPanel;
549: int ySQL = dataPanel.getFooterPanel().getLocationOnScreen().y
550: + dataPanel.getFooterPanel().getSize().height;
551:
552: //if (container.contains(pt.x , pt.y) && container.contains(pt.x + c.getWidth() , pt.y + c.getHeight()) && (ySQL>pt.y + c.getHeight() + 15))
553: if (container.contains(pt.x, pt.y)
554: && (ySQL > pt.y + c.getHeight() + 15)) {
555: if (vectorComponent.size() > 0) {
556: for (int i = 0; i < vectorComponent.size(); i++) {
557: JComponent comp = (JComponent) vectorComponent
558: .get(i);
559:
560: if (comp != c) {
561: comp
562: .setLocation(comp.getLocation().x
563: + pt.x - loc.x, comp
564: .getLocation().y
565: + pt.y - loc.y);
566: } else {
567: comp.setLocation(pt.x, pt.y);
568: }
569: }
570: } else {
571: c.setLocation(pt.x, pt.y);
572: c.getParent().repaint();
573: }
574: } else {
575: container = (Container) c.getParent();
576:
577: while (!(container instanceof javax.swing.JScrollPane)
578: && (container != null)) {
579: if (container instanceof DataPanel) {
580: container = (Container) dataPanel
581: .getComponentAt(pt);
582: } else {
583: container = (Container) container.getParent();
584: }
585: }
586:
587: if (container != null) {
588: javax.swing.JScrollBar jscrollHorz = ((javax.swing.JScrollPane) container)
589: .getHorizontalScrollBar();
590: javax.swing.JScrollBar jscrollVert = ((javax.swing.JScrollPane) container)
591: .getVerticalScrollBar();
592:
593: if (loc.x > pt.x) {
594: jscrollHorz.setValue(pt.x);
595: } else {
596: jscrollHorz.setValue(loc.x);
597: }
598: }
599: /*if (loc.y < pt.y)
600: {
601: jscrollVert.setValue(pt.y);
602: }else
603: {
604: jscrollVert.setValue(loc.y);
605: }*/
606: }
607: }
608: }
609:
610: public void keyPressed(java.awt.event.KeyEvent keyEvent) {
611: if (keyEvent.CTRL_MASK == keyEvent.getModifiers()) {
612: ctrlPress = true;
613: }
614: }
615:
616: public void keyReleased(java.awt.event.KeyEvent keyEvent) {
617: if (keyEvent.CTRL_MASK != keyEvent.getModifiers()) {
618: ctrlPress = false;
619: }
620: }
621:
622: public void keyTyped(java.awt.event.KeyEvent keyEvent) {
623: }
624: }
|