001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui.access;
019:
020: import java.awt.datatransfer.Transferable;
021: import java.awt.datatransfer.UnsupportedFlavorException;
022: import java.awt.dnd.DnDConstants;
023: import java.awt.dnd.DragGestureEvent;
024: import java.awt.dnd.DragGestureListener;
025: import java.awt.dnd.DragSource;
026: import java.awt.dnd.DragSourceDragEvent;
027: import java.awt.dnd.DragSourceDropEvent;
028: import java.awt.dnd.DragSourceEvent;
029: import java.awt.dnd.DragSourceListener;
030: import java.awt.dnd.DropTarget;
031: import java.awt.dnd.DropTargetDragEvent;
032: import java.awt.dnd.DropTargetDropEvent;
033: import java.awt.dnd.DropTargetEvent;
034: import java.awt.dnd.DropTargetListener;
035: import java.io.IOException;
036:
037: import javax.swing.JLabel;
038: import javax.swing.JTable;
039: import javax.swing.ListSelectionModel;
040: import javax.swing.table.TableCellRenderer;
041:
042: import de.finix.contelligent.client.base.ContelligentComponent;
043: import de.finix.contelligent.client.base.Type;
044: import de.finix.contelligent.client.security.Principal;
045: import de.finix.contelligent.client.util.dnd.SingleComponentTransferable;
046:
047: public class OwnerTable extends JTable implements DropTargetListener,
048: DragSourceListener, DragGestureListener {
049:
050: private boolean editable;
051:
052: public OwnerTable(OwnerTableModel ownerTableModel) {
053: super (ownerTableModel);
054: setDefaultRenderer(Principal.class, new PrincipalRenderer());
055: DropTarget dropTarget = new DropTarget(this , this );
056: DragSource dragSource = new DragSource();
057: dragSource.createDefaultDragGestureRecognizer(this ,
058: DnDConstants.ACTION_MOVE, this );
059: getSelectionModel().setSelectionMode(
060: ListSelectionModel.SINGLE_SELECTION);
061: }
062:
063: static class PrincipalRenderer implements TableCellRenderer {
064: public java.awt.Component getTableCellRendererComponent(
065: JTable table, Object object, boolean isSelected,
066: boolean hasFocus, int row, int column) {
067: JLabel cellRenderer = new JLabel();
068: cellRenderer.setOpaque(true);
069: if (isSelected) {
070: cellRenderer.setBackground(table
071: .getSelectionBackground());
072: cellRenderer.setForeground(table
073: .getSelectionForeground());
074: } else {
075: cellRenderer.setBackground(table.getBackground());
076: }
077: Principal principal = (Principal) object;
078: cellRenderer.setFont(table.getFont());
079: cellRenderer.setText(principal.getDisplayName());
080: return cellRenderer;
081: }
082:
083: private java.awt.Component renderingComponent = null;
084: }
085:
086: /**
087: * is invoked when you are dragging over the DropSite
088: *
089: */
090: public void dragEnter(DropTargetDragEvent event) {
091: // debug messages for diagnostics
092: event.acceptDrag(DnDConstants.ACTION_MOVE);
093: }
094:
095: /**
096: * is invoked when you are exit the DropSite without dropping
097: *
098: */
099: public void dragExit(DropTargetEvent event) {
100: }
101:
102: /**
103: * is invoked when a drag operation is going on
104: *
105: */
106: public void dragOver(DropTargetDragEvent event) {
107: }
108:
109: /**
110: * a drop has occurred
111: *
112: */
113: public void drop(DropTargetDropEvent event) {
114: // TODO: This seems to be code for the old, old user system. See if we can remove this.
115: if (editable) {
116: try {
117: Transferable transferable = event.getTransferable();
118:
119: // we accept ContelligentComponent and Strings
120: if (transferable
121: .isDataFlavorSupported(SingleComponentTransferable.componentFlavor)) {
122: event.acceptDrop(DnDConstants.ACTION_MOVE);
123: ContelligentComponent droppedComponent = (ContelligentComponent) transferable
124: .getTransferData(SingleComponentTransferable.componentFlavor);
125: if (droppedComponent.instanceOf(Type.ROLE)
126: || droppedComponent.instanceOf(Type.USER)) {
127: String principalString = droppedComponent
128: .getPropertyValue(ContelligentComponent.PRINCIPAL_STRING);
129: String principalGroupId = principalString
130: .substring(0, principalString
131: .indexOf(":"));
132: ;
133: String principalId = principalString
134: .substring(principalString.indexOf(":") + 1);
135: ;
136: ((OwnerTableModel) getModel())
137: .addOwner(new Principal(
138: principalGroupId, principalId));
139: }
140: event.getDropTargetContext().dropComplete(true);
141: } else {
142: event.rejectDrop();
143: }
144: } catch (IOException exception) {
145: exception.printStackTrace();
146: event.rejectDrop();
147: } catch (UnsupportedFlavorException ufException) {
148: ufException.printStackTrace();
149: event.rejectDrop();
150: }
151: }
152: }
153:
154: /**
155: * is invoked if the use modifies the current drop gesture
156: *
157: */
158: public void dropActionChanged(DropTargetDragEvent event) {
159: }
160:
161: /**
162: * a drag gesture has been initiated
163: *
164: */
165: public void dragGestureRecognized(DragGestureEvent event) {
166: }
167:
168: /**
169: * this message goes to DragSourceListener, informing it that the dragging
170: * has ended
171: *
172: */
173: public void dragDropEnd(DragSourceDropEvent event) {
174: if (event.getDropSuccess()) {
175: // removeElement((ContelligentComponent)event.getDragSourceContext().getTransferable());
176: }
177: }
178:
179: /**
180: * this message goes to DragSourceListener, informing it that the dragging
181: * has entered the DropSite
182: *
183: */
184: public void dragEnter(DragSourceDragEvent event) {
185: }
186:
187: /**
188: * this message goes to DragSourceListener, informing it that the dragging
189: * has exited the DropSite
190: *
191: */
192: public void dragExit(DragSourceEvent event) {
193: }
194:
195: /**
196: * this message goes to DragSourceListener, informing it that the dragging
197: * is currently ocurring over the DropSite
198: *
199: */
200: public void dragOver(DragSourceDragEvent event) {
201: }
202:
203: /**
204: * is invoked when the user changes the dropAction
205: *
206: */
207: public void dropActionChanged(DragSourceDragEvent event) {
208: }
209:
210: public void setEditable(boolean editable) {
211: this .editable = editable;
212: }
213:
214: public boolean isEditable() {
215: return editable;
216: }
217: }
|