1 / 28

Lecture 4: Frame Editor Demo, Packages, and File Systems

This lecture covers the Frame Editor demo, packages in Java, and file systems. It explains the hierarchy of classes, creating packages, using packages, and the basics of file systems.

nigelt
Download Presentation

Lecture 4: Frame Editor Demo, Packages, and File Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 4 • FrameEditorDemo • Packages • System Object • Runtime Object • File Systems

  2. AbstractButtons Edit tools User runs (JVM) Files FrameEditorTester creates reads/writes FrameEditor GameFrame creates creates quitButton interacts with User

  3. quitButton AbstractButtons Edit tools Flow of ActionEvents FrameEditorTester ? FrameEditor GameFrame ?

  4. quitButton AbstractButtons Edit tools Effect of wrapper addActionListener methods FrameEditorTester initial calls to addActionListener() FrameEditor GameFrame relayed calls to addActionListener()

  5. Packages • Hierarchy of classes, independent of inheritance. • Packages group classes related by function. • A package is a namespace • protected/package access specifiers

  6. Creating Packages • First line of each file is: package packagename; • To access a class: import packagename.classname; • To access all classes from a package: import packagename.*; • Each package has its own directory, named packagename.

  7. Using Packages • By default, files are in the “unnamed package.” This is only ok for very small projects. • Can have hierarchies of packages. Ex: com.company.region.package. This corresponds to the directory $CLASSPATH/com/company/region/package. There are a few naming conventions. • Separate import statements for each package, even subpackages.

  8. frameeditor pres trans app Example Package Layout

  9. File Layout ActionFrame.java (unnamed) frameeditor FrameEditor.java pres app trans EditorFrame EditorPanel EditorMenuBar IconSelector HelpFrame EditorToolbar FileSaver EditUndoer TargetHandler EditorPanelHandler EditorMenuHandler

  10. Package code frameeditor.FrameEditor: package frameeditor; import ActionFrame; import frameeditor.pres.*; import frameeditor.app.*; import frameeditor.trans.*; frameeditor.pres.EditorFrame: package frameeditor.pres; import ActionFrame; import frameeditor.FrameEditor; frameeditor.trans.EditorMenuHandler: package frameeditor.trans; import ActionFrame; import frameeditor.FrameEditor; import frameeditor.app.*; import frameeditor.pres.EditorMenuBar;

  11. Package Details • Matters where you compile from. Java and Javac look in 2 places for packages: $CLASSPATH, and the current directory. • Java, Javac can find classes and directories stored in ZIP and JAR archives. • May have to disambiguate names, e.g. javax.swing.Timer, java.util.Timer and com.thomasphayes.silly.Timer

  12. File Systems • A standardized, high-level mechanism to allow a user or users to access organized data on a medium or media. • Usually a rooted tree at heart (Unix), sometimes a rooted forest (Win*), may be a directed graph (both fake this). • Files, directories, paths. • Permissions, owners, locks.

  13. Paths • Steps from one node to another. • May be absolute (starts at root), canonical (absolute, plus no redundancies or shortcuts), or relative (starts somewhere else). • . = current, .. = parent, / or \ = file-separator, : or ; = path-separator. • Root: / in Unix. L:\ in Win*

  14. Warning: File systems • Notably, the pathnames for Win* and Unix are different. • Windows: C:\home\hayest\fname • Unix: /home/hayest/fname • File location conventions also differ. • JRE seems to support Unix symlinks but not Windows Shortcuts.

  15. Solutions • java.io.File.separator Do not hard-code pathnames. • Environment variables. $HOME, $PATH, $CLASSPATH, $FRAMEEDITORSAVEPATH, etc. Your installer can easily set up more. • Make user locate files (JFileChooser)

  16. Example: Fixing filenames • “images/starters/icon1.gif” or “images\\starters\\icon1.gif” should be replaced by “images” + file.separator + “starters” + file.separator + “icon1.gif” • Avoid symbolic links and absolute paths. • e.g. “images” needs to be replaced by “..” + file.sep + “..” + file.sep + “images” + file.sep + “starters” in a number of the demos.

  17. Environment variables • Can get a few of these with System.getProperties() • Can get all from shell, but code will be highly system dependent. Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(“set”); //…code to grab output from process… • Demo code

  18. RunTime and System • System provides a system-independent way of accessing system resources, but is very limited. • Runtime lets you execute OS commands, and fiddle with the Java Interpreter and Java Virtual Machine. You should minimize and encapsulate this code to retain some portability.

  19. The Runtime Object

  20. “System calls” • Get the Runtime instance (there can be only one). Runtime rt = Runtime.getRuntime(); • Call an exec method. This returns a Process object and may throw an IOException. • Monitor the returned Process.

  21. Runtime Example • Runtime.getRuntime() gets the Runtime object. • exec() creates a Process object. • Wrap the InputStream produced by the Process object, then use read*() methods to read the input. • waitFor(), destroy() methods available.

  22. Shells • A tool for low-level OS control. • High-level conveniences, such as variables, job control, automatic completion, scripting. • Downside: steep learning curve, ugly. • Perfect candidate for a GUI!

  23. Shell --> WinDohs 101 • Goal: Ability to open and close directories, and to view files and their properties. • Shell normally lets you into only one directory at a time; this doesn’t make sense for our GUI. Each window can display the contents of a directory. • When to refresh?

  24. Oh no! • We must be experts at using the File, System, and Runtime objects. • We must be experts on the shell. • We must be experts on the OS. • We must be experts on GUI’s. • We must be crazy!

  25. Don’t Panic! • Basically, our job is translation. • The shell will do all the really hard work. • Plenty of help out there for the asking. • We have already learned most of what we need.

  26. AbstractButtons User runs (JVM) WinDohs creates creates Runtime DirectoryFrame invokes creates creates data Process interacts with User

  27. Start create desktop bring up FolderSelector click on directory name check permissions open and refresh a new window if ok FolderSelector Cancel click on refresh button or refresh timer event. check permissions exec(“ls”) display output if ok QuitDialog Quit Done

  28. Summary • Work on FrameEditor! • Study the Demos • Course Project Specs TBA this week

More Related