lecture 6 adding html to jbuttons and jlabels. getting the right cursor data transfer cut and paste,...

38
Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop with Unix A desktop icon class

Post on 19-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Lecture 6

Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop with Unix A desktop icon class

Page 2: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

HTML-aware components

Can use HTML text mark-up with many Swing components now. Examples: multi-line labels, more fonts, formatting and styles. Warning: bad HTML will cause exceptions. Feature still almost undocumented (Sun’s web page). Believed to work with JLabel, JButton, JMenuItem, JMenu, JCheckBoxMenuItem, JRadioButtonMenuItem, JTabbedPane, JToolTip. JOptionPane too.

Page 3: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Uses

Can split large labels into several lines. Documentation/Help options. Can make HTML docs, and also view them from within the program. Hyperlinks are displayed but not active. Unfinished feature. You still have to insert your own line-breaks, and implement your own hypertext support.

Page 4: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Pointing devices

User normally thinks of pointer as hand-held tool, or even an extension of his/her body. Usually, but not always, controlled by mouse, which moves wherever user moves hand. Used to allow user to specify a Location and a desired Action. “gestures” such as drag, double-click.

Page 5: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

The right cursor

“Cursor” refers to the on-screen image used to help user position pointer. Cursor appearance should reflect pointer usage. This may change frequently. Java provides 14 system-independent cursors. OS may provide more. Can also build your own.

Page 6: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Cursors in AWT

Each component can choose its cursor (Component.setCursor), or inherit from its parent. Cursor appearance changes when pointer enters component. Cursor consists of an Image and a “hotspot” (relative pointer location).

Page 7: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Built-in cursorsCustom cursors

Page 8: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Setting component cursor

// Create a new JButton with a custom cursor// “myCursor”.JButton b=new JButton(text);Image im=new ImageIcon(“myFile.gif”).getImage();// Note: the above is the shortest way to load an// image from file. Many other methods exist. Point hotspot=new Point(0,15);// This sets hotspot at center of left edge// (assuming a 32x32 image)String name=“myCursor”; b.setCursor(b.getToolkit().createCustomCursor(im,

hotspot,name));

Page 9: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Transparency

With most image formats, it is possible to mark at least one color as “transparent.” One use: although images are stored as rectangles, can make them appear to be other shapes. For cursors, this is the norm. Can also make transparent buttons.

Page 10: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Translucency

There is an even more advanced technique called alpha-compositing, in which every pixel of an image is assigned a number representing its translucency. This is likely to become more common in the future. I’ll come back to this when I discuss Java2D, another day.

Page 11: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Data Transfer

Internal: between Objects Between JVM’s: (rather easy with Serialization or RMI) via the OS: to/from Clipboard via the OS: Drag and Drop

Page 12: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Data Flavors

Data comes in many formats. Format unknown data unusable Makes sense to keep a label on data describing the format. MIME types are the de-facto internet standard. Java’s DataFlavor class is a slight extension to MIME.

Page 13: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

MIME

Multipurpose Internet Mail Extensions format. e-mail, http “Content-type” “type/subtype” A proposed standard: RFC 2912, 2913. Examples: text/plain, image/jpeg, image/gif, audio/basic, video/mpeg, application/x-java-serialized-object, model/vrml, text/rtf, multipart/signed

Page 14: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

What’s in a flavor?

Hi, I am Mysterio, an instance of a class implementing the

Transferable interface.Hi, I’m Jo Java. I want to

get cool stuff from Mysterio. And I want it

now, now, now!

Meet the cast:

A drama in 6 slides

Page 15: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

What’s in a flavor?Mysterio, what cool stuff do you

have?

Haha, not so fast my dear!I’ll only tell you if you

invoke the right methods (wink, wink)!

Scum! Give me what I want now!

mysterio.getTransferDataFlavors();

Page 16: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

What’s in a flavor?

Ouch, you win! I have cool stuff in the

following flavors: “text/html”,

“application/powerpoint”,

“text/rtf”, “image/jpeg”Quick, give me the “image/jpeg”.

Um, I mean:mysterio.getTransferData(jpegFlavor);

Page 17: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

What’s in a flavor?

OK! Here’s an Object “representing” the

data you requested. Bye!

???

Page 18: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

What’s in a flavor?

Oh, no! Not a “black box!”

This thing is supposed to represent a JPEG?

Object

How am I supposed to put this on screen?

Page 19: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

What’s in a flavor?

Object

Wait, the DataFlavor for “image/jpeg” tells me the class of

this Object. jpegFlavor.getRepresentationClass();

It’s an InputStream. How quaint. Now I can read the JPEG file from this stream, and try to display it.

Page 20: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Object

Epilogue

Usually, when Transferables come from within Java programs, they include a DataFlavor obtained by

new DataFlavor(Class.forName(String),String);

These are represented by a ready-to-use Java object

But Transferables from other applications are usually represented by

a java.io.InputStream These take more work.

Page 21: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Flavors ready for scooping:

DataFlavor.stringFlavor

DataFlavor.getTextPlainUnicodeFlavor()

DataFlavor.javaSerializedObjectMimeType

DataFlavor.javaFileListFlavornew DataFlavor(Class.forName(“mypackage.MyClass”),

“My Neat-O Class”)

new DataFlavor(MIMETypeString, HumanReadableString)

Page 22: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Using the Clipboard

Pass Transferables to and from system Clipboard. The clipboard must be obtained with: Toolkit.getSystemClipboard()

Pasting: use theClipboard.getContents() to get a Transferable object. Then extract the DataFlavor you want as previously described.

Page 23: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Clipboard: Cutting

When cutting, we are promising the system that we will make the clipboard material available until it is replaced by the user. The ClipboardOwner interface allows the OS to notify us when we can forget the clipboard contents. Clipboard.setContents() method cuts. Must implement Transferable for this.

Page 24: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Clipboard notes

Swing text components already support cut-and-paste to system clipboard. Often this is enough. Can also have local clipboards. Useful if you want more persistence and don’t wish to allow data exchange with other programs.

Page 25: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Gestures

A gesture is a high-level input event comprising a series or combination of several low-level mouse events. Can convey complicated information more easily than low-level mouse events.

Page 26: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Gestures are:

information-rich natural to the user subject to misinterpretation/misinput common to many tasks (even a language in themselves) not necessarily natural to the UI culture-specific

Page 27: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Gestures in Java

Java does not have a “Gesture” class. Does have an extremely powerful mechanism for handling “drag gestures” Supports drag-and-drop between

components, objects, even non-Java applications.

Useful for moving, copying, linking, etc.

Page 28: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Package java.awt.dnd

On drag end: must set up DragSource, DragGestureListener, DragSourceListener

On drop end: must set up DropTarget, DropTargetListener

A successful drop gives the DropTargetListener a reference to a Transferable Object. See java.awt.datatransfer

Page 29: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Initialization

DragSourceDragSourceListener

DragGestureListener

Source Component Target Component

DropTargetListener DropTarget

DragSourceContext

DragSourceContextPeer

The AWT Implementation(System specific)

Page 30: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Flow of Events

DragSourceDragSourceListener

DragGestureListener

Source Component Target Component

DropTargetListener DropTarget

DragSourceContext

DragSourceContextPeer

The AWT Implementation(System specific)

Page 31: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

DND within Java

Page 32: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

DND recipe

1. Obtain a reference to a drag source with DragSource.getDefaultDragSource() or by instantiating a new one -- for example, new DragSource() .

2. Create a drag gesture recognizer with the drag source from step 1 by invoking DragSource.createDefaultDragGestureRecognizer(). The method is passed the component where the drag originates.

3. Wrap the data to be dragged in a transferable.

Page 33: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

DND Recipe part II

4. Initiate a drag when the drag gesture recognizer from step 2 is notified by invoking DragSource.startDrag() for the drag source from step 1. The transferable from step 4 is passed to the startDrag() method.

5. Handle the drop by implementing the DropTargetListener interface.

6. Implement the DragSource interface (often with empty methods).

Page 34: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Click gestures

Instead of associating a single command to a button, can associate two or more. For instance, left-click, right-click, double-click, shift-click, alt-click, left-and-right-click, etc. However, Java doesn’t support this well. Can recognize double-clicks using MouseEvent.getClickCount() method.

Page 35: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Windows 2000 clicks

Click type Default Action

left-click select

right-click select + options

left-and-right-click

select + options

double-(left)-click

open/raise

ctrl-click add to selection

shift-click select rectangle

alt-double-click show properties

Page 36: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Windows 2000 drags

Click type Default Action

left-drag move

ctrl-left-drag copy

alt-left-drag link

shft-ctrl-left-drag

link

right-drag pop-up options

Page 37: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

DND Reading

DND Fundamentals (Geary, Sun) Intro, part 1 (Javaworld) Intro, part 2 (Javaworld) Sun’s DND trail (minimal) DND Links (esus) JFC in a Nutshell, Chapter 6. Data Transfer API (Javaworld)

Page 38: Lecture 6 Adding HTML to JButtons and JLabels. Getting the right cursor Data Transfer Cut and Paste, System Clipboard Gestures Drag and Drop Drag ‘n’ Drop

Creating a DesktopIcon class

Desktop icons represent files. Their transferable content is most naturally represented internally by the File class. Externally by DataFlavor.javaFileListFlavor.

Set up DND. Ordinary files have dragsources. Directories have dragsources and dragtargets for MOVE and COPY.