CS2007: Undo - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

CS2007: Undo

Description:

Example: Currency converter. Input: poundsField ... MS Word vs CS2007 Currency Convert. Dr. Ehud Reiter, Computing Science, ... for currency convertor ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 29
Provided by: computin7
Category:

less

Transcript and Presenter's Notes

Title: CS2007: Undo


1
CS2007 Undo
  • Assessment 1
  • What is Undo?
  • GUI State
  • Stacks
  • Reading Malik, first part of chap 17 (Stacks)

2
Programming Aside
  • String comparison
  • assignment, not equality test
  • useless (do not use this)
  • equals() tests for equal strings
  • equalsIgnoreCase() ignores case when testing
  • text will be regarded as equal to TEXT
  • java.text.Collator most flexible, can be set to
    ignore accents.
  • text will be regarded as equal to tèxt
  • Similar comparison methods (eg, compareTo)
  • is String A alphabetically before String B?

3
Undo
  • Interfaces should allow users to reverse actions
  • In other words, undo!
  • Error recovery
  • Gives user confidence to experiment
  • Essential for high usability

4
Undo Functionality
  • Granularity (character? word?)
  • Depth (1 undo? 10 undos?)
  • Redo (undo undo)?
  • User interface
  • Compare Notepad, Word, NetBeans

5
Whats best?
  • Opinions
  • Granularity, depth, redo, interface

6
Whats best?
  • Should determine by studying user needs and
    expectations
  • CS2506

7
Programming Undo
  • Must remember previous GUI state
  • Must be able to restore GUI to a previous state
  • (if depthgt1) must keep track of many previous
    states, in right order

8
Example Currency converter
  • Input poundsField
  • Output eurosField

9
GUI State
  • Need to save state of GUI
  • Inputs (eg, poundsField), outputs (eg,
    eurosField)
  • Not just inputs (poundsField)
  • Probably as Strings
  • Could also save as doubles (JTextField?)
  • But Strings easier, more general

10
Option 1 Class Variables
  • Declare class variables to hold old values
  • String oldPoundsText, oldEurosText
  • .
  • at beginning of Convert or saveState method
  • oldPoundsText poundsField.getText()
  • oldEurosText eurosField.getText()

11
Example
  • Add Save button as above
  • Add Restore button with code
  • poundsField.setText(oldPoundsText)
  • eurosField.setText(oldEurosText)

12
Option 2 State Class
  • Define a new class that holds GUI state
  • Class variables hold information
  • Constructor creates this class from the GUI
  • Method to set GUI to this state
  • Can have other methods as well
  • Read/write from file
  • Compare to other state

13
Example
  • public class GUIState
  • String poundsText, eurosText
  • GUIState (GUI g) // construct from GUI
  • poundsText g.poundsField.getText()
  • eurosText g.eurosField.getText()
  • void setGUI (GUI g) // set GUI to this state
  • g.poundsField.setText(poundsText)
  • g.eurosField.setText(eurosText)

14
NetBeans Note
  • Only works if poundsField, etc accessible in
    guiState
  • Not accessible by default in NB (private)
  • For each widget
  • Select widget in GUI designer
  • select code properties
  • Change Variable modifiers from private to
    ltdefaultgt
  • In theory, change tools, options, misc, GUI
    Builder, variable modifier to ltdefaultgt

15
Algorithm Design Choice 1
  • How should state be stored?
  • Class variables?
  • State class?
  • Opinions? Rationale?

16
State Class is Better
  • Much better programming practice!
  • Especially for more complex GUIs (more state to
    remember)
  • Especially if other things done with state (eg,
    saving to a file)

17
Just record changes
  • What if GUI state is very large?
  • Eg, entire Document for MS Word
  • Just record changes between old state and current
    state
  • Eg, if only poundsBox has changed (because
    Convert not pressed), just record its old value,
    dont bother with eurosBox
  • Alternative undo algorithm

18
Algorithm Design Choice 2
  • Record
  • Full state
  • Change in state
  • Advantages of recording full state
  • Easier to program
  • Quicker (less computation needed)
  • Advantages of recording changes
  • Needs much less memory

19
Changes vs Full State
  • Which is better?
  • Depends on circumstances
  • Size of GUI state
  • Computer speed, memory
  • Cost of programmer time
  • MS Word vs CS2007 Currency Convert

20
Algorithm Design
  • Store state in variables vs class
  • Class is almost always better!
  • Store full state vs changes
  • Depends on circumstances
  • Art of algorithmic problem solving Designing
    (choosing) appropriate algorithms for particular
    circumstances

21
Keeping Track of States
  • If undo depth is 1, just save GUI status in a
    class variable
  • GUIState oldState
  • .
  • oldState new GUIState(this)
  • Exercise single Undo for currency convertor

22
Multiple states
  • If depthgt1, we must keep track of many previous
    states
  • GUI before most recent change
  • GUI before second most recent change
  • GUI before third most recent change
  • etc

23
Stacks
  • Use a Stack data structure
  • Last-in, first-out collection
  • Elements are added and removed only from one end
    (the top of the stack).
  • Very common data structure in computer
    programming

24
Stack concepts (2)
  • Illustration (stack of books)

25
Stacks in Java
  • Stack class in java.util
  • Usually we add a type in ltgt
  • StackltStringgt stringStack
  • StackltGUIStategt guiStack

26
Stack methods
  • push (element) -- add element to the top of the
    stack
  • peek() -- return object at top of stack (dont
    change stack)
  • pop() return object at top of stack (and take
    it off the stack)
  • size() number of things in stack
  • empty() is stack empty?

27
Stack example
  • StackltStringgt stringStack new StackltStringgt()
  • stringStack.push(Joe)
  • stringStack.push(Susan)
  • System.out.println(stringStack.peek()) //
    prints Susan
  • System.out.println(stringStack.pop()) // prints
    Susan
  • System.out.println(stringStack.pop()) // prints
    Joe

28
Stack in Undo
  • Main idea
  • Whenever GUI changes, push new state onto a stack
  • When undo pressed, pop state off the stack, and
    set GUI to it
  • Many details and caveats, to-be-discussed
Write a Comment
User Comments (0)
About PowerShow.com