java.lang.Object
javax.swing.TransferHandler
org.bzdev.swing.ExtObjListTransferHandler
- All Implemented Interfaces:
Serializable
Transfer handler for JLists of objects reference by URLs or file names.
This transfer handler supports objects represented externally by
A file name or URL. Subclasses must implement the method
insertByURL
. This is used when objects are loaded
from an external source. If the list is being reordered, the
objects in the list are not modified and insertByURL
is not
called.
In one use case, the objects will be subclasses of ImageIcon, which a JList can display graphically, with each object containing additional methods that the application needs. For example, the ImageIcon might contain a scaled "thumbnail" image of a larger image file, and store the original file's URL for later processing.
The following code is an example of how to set up a JList that supports reordering and displays icon images obtained from a URL or file name (the class OurImageIcon is a subclass of ImageIcon that provides an icon and some additional data):
DefaultListModel<OurImageIcon> listModel = new DefaultListModel<OurImageIcon>(); JList<OurImageIcon> list = new JList<OurImageIcon>(listModel); JScrollPane scrollPane = new JScrollPane(list); File dir = ...; TransferHandler th = new ExtObjListTransferHandler(list, dir) { public void insertByURL(URL url, DefaultListModel model, int index) { // Create OurIconImage OurIconImage image = ... ; // Make sure the image is fully loaded. ... // Add the image to the model at the specified index, or at // the end if no index was specified. if (index == -1) { model.addElement(image); } else { model.addElement(index, image); } } }; list.setTranferHandler(th); list.setDragEnabled(true); list.setDropMode(DropMode.INSERT); DefaultListCellRenderer lcr = (DefaultListCellRenderer) list.getCellRenderer(); lcr.setHorizontalAlignment(SwingConstants.CENTER); Dimension d = scrollPane.getHorizontalScrollBar().getPreferredSize(); int h = d.height; d = scrollPane.getVerticalScrollBar().getPreferredSize(); int w = d.width; int iconWidth = ...; int iconHeight = ...; list.setFixedCellHeight(iconHeight + h); rlist.setFixedCellWidth(iconWidth + w); rlist.setLayoutOrientation(JList.VERTICAL); rlist.setVisibleRowCount(5);
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.TransferHandler
TransferHandler.DropLocation, TransferHandler.TransferSupport
-
Field Summary
Fields inherited from class javax.swing.TransferHandler
COPY, COPY_OR_MOVE, LINK, MOVE, NONE
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
canImport
(TransferHandler.TransferSupport support) protected Transferable
protected void
exportDone
(JComponent src, Transferable data, int action) int
boolean
importData
(JComponent comp, Transferable t) boolean
protected abstract void
insertByURL
(URL url, DefaultListModel model, int index) Insert an object given its URL.void
setMultiEntryMode
(boolean mode) Set multi-entry mode.void
setMultiEntryMode
(boolean mode, boolean cmode) Set multi-selection mode and replacement option.Methods inherited from class javax.swing.TransferHandler
canImport, exportAsDrag, exportToClipboard, getCopyAction, getCutAction, getDragImage, getDragImageOffset, getPasteAction, getVisualRepresentation, setDragImage, setDragImageOffset
-
Constructor Details
-
ExtObjListTransferHandler
Constructor.- Parameters:
jlist
- the JList that will be the drop target.currentDir
- the current working directory to use for relative URLs or file names
-
-
Method Details
-
insertByURL
Insert an object given its URL. The implementation is responsible for creating the object given its URL, and the object must be one that the JList can display. The default JList implementation can handle objects of type String and ImageIcon. After an object OBJ is created, the implementation must call model.addElement(OBJ) when index is -1; otherwise it must call model.add(index, OBJ). Some implementations will create an object that must later be modified to be displayed correctly (e.g., a 'blank' image until the actual image has been loaded), with the object modified after it was added to the list model. The modification would typically be done in a separate thread. In this case, as a final step, the implementation may call model.indexOf(OBJ) to get the actual index INDEX and then call model.setElementAt(INDEX, OBJ), which will automatically force the JList to be repainted so that the modified object is displayed correctly.- Parameters:
url
- the image's URLmodel
- the list model for the JList in which the image will appearindex
- the index in the list model before which the image will be inserted; -1 if the entry is to be added to the end of the list- Throws:
Exception
- an error occurred
-
setMultiEntryMode
public void setMultiEntryMode(boolean mode) Set multi-entry mode. Equivalent to setMultiEntryMode(mode, false). When this class is initialized, its state is equivalent to callingsetMultiEntryMode(true)
.- Parameters:
mode
- true if multiple entries are allowed; false otherwise- See Also:
-
setMultiEntryMode
public void setMultiEntryMode(boolean mode, boolean cmode) Set multi-selection mode and replacement option. The behavior depends on the combination of arguments provided:mode cmode Description true (ignored) A drag & drop operation may insert multiple values, with new entries added to the target false true A drag & drop operation may insert a single value, even if multiple values were selected, and previous values will be cleared from the JList. false false A drag & drop operation may insert multiple values and previous values will be cleared from the JList. - Parameters:
mode
- true if images from multiple D&D entries are kept; false if they are replacedcmode
- true if only one value is to be inserted in a D&D operation when mode has the value false; false if multiple values can be inserted in a D&D operation when mode has the value false; ignored if mode has the value true
-
createTransferable
- Overrides:
createTransferable
in classTransferHandler
-
getSourceActions
- Overrides:
getSourceActions
in classTransferHandler
-
importData
- Overrides:
importData
in classTransferHandler
-
canImport
- Overrides:
canImport
in classTransferHandler
-
importData
- Overrides:
importData
in classTransferHandler
-
exportDone
- Overrides:
exportDone
in classTransferHandler
-