Download presentation
Presentation is loading. Please wait.
1
CS 190 Lecture Notes: My Editor Design
Position Position(int line, int charIndex) Position(Position other) public int line; public int charIndex; boolean equals(Position other) int compareTo(Position other) void set(int line, int charIndex) void set(Position other) String toString() CS 190 Lecture Notes: My Editor Design
2
CS 190 Lecture Notes: My Editor Design
Text Text() Text(String fileName) throws FileNotFoundException, IOException String getFileName() void writeFile(String fileName) void adjustPosition(Position p) Position advancePosition(Position p, int numChars) void insert(Position pos, String newText) void delete(Position start, Position end) CharSequence getRange(Position start, Position end) String getAll() int getTotalLines() void addTracker(Tracker tracker) void removeTracker(Tracker tracker) CS 190 Lecture Notes: My Editor Design
3
CS 190 Lecture Notes: My Editor Design
Text.Tracker public interface Tracker { void afterInsertion(Position start, Position end); void beforeDeletion(Position start, Position end); } CS 190 Lecture Notes: My Editor Design
4
CS 190 Lecture Notes: My Editor Design
TextPanel Knows everything about displaying text Handles scrolling, window resizes Knows nothing about other events TextPanel(JFrame frame, Text text, Font font) Position getCharUnderPoint(int x, int y) Position getInsertPosition() void setInsertPosition(Position p) Position getSelectionStart() Position getSelectionEnd() String getSelection() void setSelection(Position start, Position end) void makeVisible(Position position) CS 190 Lecture Notes: My Editor Design
5
CS 190 Lecture Notes: My Editor Design
EventHandler Defines the application’s user interface No public methods (except event handlers called by Swing/AWT) EventHandler(Context context, TextPanel panel, JFrame frame) CS 190 Lecture Notes: My Editor Design
6
CS 190 Lecture Notes: My Editor Design
Context Represents a file being edited (and all of its windows) Top-level state shared among windows Also, creates top-level windows public String fileName; public Text text; public Undo undo; public UndoText undoText; public TextPanel focusPanel; Context(String fileName) public void newWindow() CS 190 Lecture Notes: My Editor Design
7
CS 190 Lecture Notes: My Editor Design
TrackedPosition Subclass of Position Implements the Tracker interface: automatically updates when text is modified TrackedPosition(Text text, int line, int charIndex); CS 190 Lecture Notes: My Editor Design
8
Using Trackers for Displaying
TextPanel insert selStart selEnd Position Position Position TrackedPositions Tracker Tracker Tracker Tracker Text CS 190 Lecture Notes: My Editor Design
9
CS 190 Lecture Notes: My Editor Design
Undo public class Undo { public interface Action { public void redo(); public void undo(); } Undo() void addAction(Action action) void addFence() void undo() void redo() CS 190 Lecture Notes: My Editor Design
10
CS 190 Lecture Notes: My Editor Design
UndoText No methods, just constructor: new UndoText(Text text, Undo undo); insert/delete text EventHandler undo/redo UndoText Undo Text Tracker UndoableDelete UndoableInsert CS 190 Lecture Notes: My Editor Design
11
Undoing Cursor and Selection
Before insertions and deletions: context.undo.addAction(new UndoCursorSelection(context)); ... probably should have been: new UndoCursorSelection(context); ... or, even better? UndoCursorSelection.record(context); CS 190 Lecture Notes: My Editor Design
12
CS 190 Lecture Notes: My Editor Design
Undo Grouping When to add fences? When mouse clicked Before and after insertions > 1 character But, doesn’t handle single-character paste ops correctly... Before and after deleting selection Advantage: can understand each fence (pair?) in isolation Disadvantage: may not scale well (future ops will require fences before and after?) CS 190 Lecture Notes: My Editor Design
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.