Lecture 3 Chat Assignments 2 and 3 Undo

  • Slides: 22
Download presentation
Lecture 3 Chat: Assignments 2 and 3 Undo Capability Fine-tuning appearance Adding Customizability Save/Restore

Lecture 3 Chat: Assignments 2 and 3 Undo Capability Fine-tuning appearance Adding Customizability Save/Restore Assignment 4

Undo/Redo Expected by user Apply consistently n n n Second undo = redo? More

Undo/Redo Expected by user Apply consistently n n n Second undo = redo? More depth is nice What counts as an event? Easily and non-easily reversible events Warn user when taking irreversible actions Persistency: can undo vector be saved?

1 -step Undo After each reversible action, save state (backend) If Undo command issued:

1 -step Undo After each reversible action, save state (backend) If Undo command issued: w Restore state. (backend, possibly frontend). w Save undone state in case of redo. w Disable undo, enable redo.

Multi-step undo/redo Keep stack of recent states. Undo pops back to previous state. New

Multi-step undo/redo Keep stack of recent states. Undo pops back to previous state. New actions pushed onto the top. Immediately after a series of undos, can redo actions. Enhanced stack. More complex behavior possible n Emacs’ tree undo

Resource issues with undo Saving/restoring state may cost too much. n n Can store

Resource issues with undo Saving/restoring state may cost too much. n n Can store difference between before/after states. Can store command itself. Need special undo code for each command. May only want to support limited undo depth.

Supporting undo in Java Vectors to store states/commands. Dynamically sized and untyped. Need one

Supporting undo in Java Vectors to store states/commands. Dynamically sized and untyped. Need one vector for undoable events and one for redoable events. Can just store Action. Events generated by your UI, rather than state, if you are able to reverse them. Allows highly reusable code.

Images “A picture is worth a thousand words, and uses less screen real estate.

Images “A picture is worth a thousand words, and uses less screen real estate. ” Attach to a JComponent: Image. Icon ii = new Image. Icon(“images/icon 1. gif”); JButton b = new JButton(ii); JLabel l = new JLabel(“Hello”, ii); JMenu. Item mi = new JMenu. Item(“test”); mi. set. Icon(b. get. Icon()); Manipulate using Graphics or Graphics 2 D object

What can I do in 1 line? Can add images to any Abstract. Button

What can I do in 1 line? Can add images to any Abstract. Button (button, checkbox, menuitem), JLabel, JList, JCombo. Box Only Icons (fixed-size images) supported so far. Use Image. Icon. Dubious support for animated gifs, but these distract even when they do work.

Other Easy Graphic Effects Buttons/Menus can have several icons to indicate status. Change background

Other Easy Graphic Effects Buttons/Menus can have several icons to indicate status. Change background color of any JComponent using set. Background. set. Foreground too. Use JLayered. Pane to superimpose text, components, on top of image. Can also set. Font().

Example: JButton icons Image. Icon ic 1 = new Image. Icon(filename. Or. URL); //

Example: JButton icons Image. Icon ic 1 = new Image. Icon(filename. Or. URL); // … Get icons ic 2 through ic 6. JButton but = new Jbutton(“text”); but. set. Icon(ic 1); but. set. Rollover. Icon(ic 2); but. set. Pressed. Icon(ic 3); but. set. Selected. Icon(ic 4); but. set. Disabled. Icon(ic 5); but. set. Disabled. Selected. Icon(ic 6); // etc.

Customization “A happy user is a productive user. ” What should we allow the

Customization “A happy user is a productive user. ” What should we allow the user to change? n n Almost everything Don’t let the user trap himself. no “hide Edit Preferences”, “disable Quit”. Session vs. Permanent prefs no e. g.

Easy part first: Cosmetic customization, such as colors, background screen, menu shortcuts, all easy.

Easy part first: Cosmetic customization, such as colors, background screen, menu shortcuts, all easy. Font also easy, except font size may cause problems. Same goes for Icons. Examples

Hard parts (sort of) Permanent prefs. Need to save everything relevant in a file.

Hard parts (sort of) Permanent prefs. Need to save everything relevant in a file. Prioritized prefs. “I like blue text, except in buttons with icons, except for the Back button. ” Multiple preference settings. Resizing/rearranging GUI components.

Save/Restore A quick overview of stream I/O in Java. n Note: easy to write

Save/Restore A quick overview of stream I/O in Java. n Note: easy to write OS specific code here, because of path name conventions Serialization

Streams Input. Stream Output. Stream

Streams Input. Stream Output. Stream

Wrapping A technique for “borrowing methods” Vector. add() Object. Input. Stream int Object Filter.

Wrapping A technique for “borrowing methods” Vector. add() Object. Input. Stream int Object Filter. Input. Stream File. Input. Stream Integer int A wrapper class Stream wrapping

Example (Save. Restore. Demo. java) // In action. Performed(Action. Event e) method. // code

Example (Save. Restore. Demo. java) // In action. Performed(Action. Event e) method. // code handles “Save” menuitem by storing a // serialized object to file. if (e. get. Action. Command(). equals("Save")) { try { File. Output. Stream out = new File. Output. Stream(fname); Object. Output. Stream s = new Object. Output. Stream(out); s. write. Object(my. Serializable. Object); s. flush(); } catch (Exception ex) { System. out. println("Save exception. " + ex); }

Object Serialization Uniform way of converting objects to strings, for RMI and saving/restoring from

Object Serialization Uniform way of converting objects to strings, for RMI and saving/restoring from files. Thread-safe. Object cannot be modified during serialization. Objects can only be serialized once by a given serializer. Further references are by handle. This is efficient, but somewhat limiting. Compatibility issues.

Using Serialization Use classes Object. Output. Stream, Object. Input. Stream. Methods write. Object(ob), read.

Using Serialization Use classes Object. Output. Stream, Object. Input. Stream. Methods write. Object(ob), read. Object() convert any Serializable object to a data stream. Serializable interface is empty. n n Only purpose is security. If you make a class Serializable, its private fields can be accessed. Can override this by providing custom write. Object and read. Object methods in Serializable class, or by declaring variables transient

Custom Serialization Default serialization is inefficient May want to serialize part of a field,

Custom Serialization Default serialization is inefficient May want to serialize part of a field, but protect another part. May wish to switch between allowing and disallowing serialization, or change methods.

Save/Restore 1 -step save: serialize everything, write it to file. Wasteful. Better: save only

Save/Restore 1 -step save: serialize everything, write it to file. Wasteful. Better: save only as much as necessary, but serialize where handy. Restore: Read from file in reverse order data was written.

Assignment 4 Lots of work, start early. Get totally familiar with Save. Restore. Demo

Assignment 4 Lots of work, start early. Get totally familiar with Save. Restore. Demo before starting. The new functionality is mostly here.