001: /*
002: * SwingML Copyright (C) 2002 SwingML Team
003: *
004: * This library is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU Lesser General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option) any
007: * later version.
008: *
009: * This library is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with this library; if not, write to the Free Software Foundation, Inc.,
016: * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: * Authors: Ezequiel Cuellar <ecuellar@crosslogic.com> Robert Morris
019: * <robertj@morris.net>
020: */
021: package org.swingml.component;
022:
023: import java.awt.datatransfer.*;
024: import java.awt.dnd.*;
025: import java.awt.event.*;
026: import java.io.*;
027:
028: import javax.swing.*;
029: import javax.swing.event.*;
030:
031: import org.swingml.*;
032: import org.swingml.event.*;
033: import org.swingml.model.*;
034: import org.swingml.system.*;
035:
036: public class JListComponent extends JList implements
037: ListSelectionListener, XMLTranslatable, DropTargetListener,
038: DragSourceListener, DragGestureListener, MouseListener {
039:
040: private DragSource dragSource = null;
041: // private DropTarget dropTarget = null;
042: private EventHandler eventHandler = EventHandler.getInstance();
043: private JListModel model = null;
044: private Object selected = null;
045:
046: public JListComponent(JListModel theModel) {
047: this .model = theModel;
048: super .setListData(model.getChildren().toArray());
049: super .setName(model.getName());
050: super .setToolTipText(model.getTooltip());
051: super .setSelectedIndices(model.getSelectedIndexes());
052: super .setSelectionMode(model.getMode());
053: super .setVisibleRowCount(model.getVisibleRowCount());
054: super .addListSelectionListener(this );
055: super .addMouseListener(this );
056: if (theModel.isDndEnabled()) {
057: // this.dropTarget = new DropTarget(this, this);
058: new DropTarget(this , this );
059: this .dragSource = new DragSource();
060: this .dragSource.createDefaultDragGestureRecognizer(this ,
061: DnDConstants.ACTION_MOVE, this );
062: }
063: }
064:
065: public void addElement(Object anElement) {
066: ItemModel theNewItem = new ItemModel();
067: theNewItem.setText(anElement.toString());
068: JListModel theListModel = this .getListModel();
069: theListModel.addChild(theNewItem);
070: super .setListData(theListModel.getChildren().toArray());
071: }
072:
073: // -- DragSourceListener realization.
074: public void dragDropEnd(DragSourceDropEvent aEvt) {
075: if (aEvt.getDropSuccess()) {
076: removeElement();
077: }
078: }
079:
080: public void dragEnter(DragSourceDragEvent aEvt) {
081: }
082:
083: public void dragEnter(DropTargetDragEvent aEvt) {
084: aEvt.acceptDrag(DnDConstants.ACTION_MOVE);
085: }
086:
087: public void dragExit(DragSourceEvent aEvt) {
088: }
089:
090: public void dragExit(DropTargetEvent aEvt) {
091: }
092:
093: // -- DragGestureListener realization.
094: public void dragGestureRecognized(DragGestureEvent aEvt) {
095: this .selected = getSelectedValue();
096: if (selected != null) {
097: StringSelection theText = new StringSelection(selected
098: .toString());
099: dragSource.startDrag(aEvt, DragSource.DefaultMoveDrop,
100: theText, this );
101: }
102: }
103:
104: public void dragOver(DragSourceDragEvent aEvt) {
105: }
106:
107: public void dragOver(DropTargetDragEvent aEvt) {
108: }
109:
110: // -- DropTargetListener realization.
111: public void drop(DropTargetDropEvent aEvt) {
112: try {
113: Transferable theTransferable = aEvt.getTransferable();
114: if (theTransferable
115: .isDataFlavorSupported(DataFlavor.stringFlavor)) {
116: aEvt.acceptDrop(DnDConstants.ACTION_MOVE);
117: String theText = (String) theTransferable
118: .getTransferData(DataFlavor.stringFlavor);
119: addElement(theText);
120: aEvt.getDropTargetContext().dropComplete(true);
121: } else {
122: aEvt.rejectDrop();
123: }
124: } catch (IOException e) {
125: SwingMLLogger.getInstance().log(e);
126: aEvt.rejectDrop();
127: } catch (UnsupportedFlavorException e) {
128: SwingMLLogger.getInstance().log(e);
129: aEvt.rejectDrop();
130: }
131: }
132:
133: public void dropActionChanged(DragSourceDragEvent aEvt) {
134: }
135:
136: public void dropActionChanged(DropTargetDragEvent aEvt) {
137: }
138:
139: // -- Utility methods.
140: public JListModel getListModel() {
141: return this .model;
142: }
143:
144: // -- XMLTranslatable realization.
145: public String getXMLValue() {
146: Object[] theValues = null;
147: StringBuffer theResult = new StringBuffer();
148: if (this .getListModel().getPostStyle().equalsIgnoreCase(
149: Constants.POST_ALL)) {
150: theValues = this .getListModel().getChildren().toArray();
151: } else {
152: // Constants.POST_SELECTED
153: theValues = this .getSelectedValues();
154: }
155: if (theValues != null && theValues.length > 0) {
156: for (int i = 0; i < theValues.length; i++) {
157: // Determine whether or not the original SwingML document
158: // indicated a VALUE attribute
159: // for the ITEM tag. If so, return that value rather than the
160: // value of the TEXT attribute.
161: if (theValues[i] instanceof ItemModel
162: && ((ItemModel) theValues[i]).getValue() != null) {
163: theResult.append(((ItemModel) theValues[i])
164: .getValue());
165: } else {
166: theResult.append(theValues[i].toString());
167: }
168: if (i < theValues.length - 1) {
169: theResult.append(",");
170: }
171: }
172: }
173: return theResult.toString();
174: }
175:
176: // MouseListener Realization.
177: /**
178: * @see java.awt.event.MouseListener#mouseClicked(MouseEvent)
179: */
180: public void mouseClicked(MouseEvent aEvt) {
181: final int DOUBLE_CLICK_COUNT = 2;
182: final int SINGLE_CLICK_COUNT = 1;
183: String theEventType = null;
184: switch (aEvt.getClickCount()) {
185: case DOUBLE_CLICK_COUNT:
186: theEventType = Constants.MOUSE_DOUBLE_CLICKED;
187: break;
188: case SINGLE_CLICK_COUNT:
189: theEventType = Constants.MOUSE_SINGLE_CLICKED;
190: break;
191: default:
192: theEventType = null;
193: break;
194: }
195: if (theEventType != null && theEventType.length() > 0) {
196: this .eventHandler.handleEvent(this .model, theEventType,
197: this );
198: }
199: }
200:
201: /**
202: * @see java.awt.event.MouseListener#mouseEntered(MouseEvent)
203: */
204: public void mouseEntered(MouseEvent aEvt) {
205: }
206:
207: /**
208: * @see java.awt.event.MouseListener#mouseExited(MouseEvent)
209: */
210: public void mouseExited(MouseEvent aEvt) {
211: }
212:
213: /**
214: * @see java.awt.event.MouseListener#mousePressed(MouseEvent)
215: */
216: public void mousePressed(MouseEvent aEvt) {
217: }
218:
219: /**
220: * @see java.awt.event.MouseListener#mouseReleased(MouseEvent)
221: */
222: public void mouseReleased(MouseEvent aEvt) {
223: }
224:
225: public void removeElement() {
226: JListModel theListModel = this .getListModel();
227: theListModel.getChildren().remove(selected);
228: super .setListData(theListModel.getChildren().toArray());
229: }
230:
231: public void setListModel(JListModel aListModel) {
232: this .model = aListModel;
233: super .setListData(aListModel.getChildren().toArray());
234: }
235:
236: // -- ListSelectionListener realization.
237: public void valueChanged(ListSelectionEvent aEvt) {
238: this.eventHandler.handleEvent(this.model,
239: Constants.LIST_VALUE_CHANGED, this);
240: }
241: }
|