280 likes | 290 Views
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.
E N D
Lecture 4 • FrameEditorDemo • Packages • System Object • Runtime Object • File Systems
AbstractButtons Edit tools User runs (JVM) Files FrameEditorTester creates reads/writes FrameEditor GameFrame creates creates quitButton interacts with User
quitButton AbstractButtons Edit tools Flow of ActionEvents FrameEditorTester ? FrameEditor GameFrame ?
quitButton AbstractButtons Edit tools Effect of wrapper addActionListener methods FrameEditorTester initial calls to addActionListener() FrameEditor GameFrame relayed calls to addActionListener()
Packages • Hierarchy of classes, independent of inheritance. • Packages group classes related by function. • A package is a namespace • protected/package access specifiers
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.
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.
frameeditor pres trans app Example Package Layout
File Layout ActionFrame.java (unnamed) frameeditor FrameEditor.java pres app trans EditorFrame EditorPanel EditorMenuBar IconSelector HelpFrame EditorToolbar FileSaver EditUndoer TargetHandler EditorPanelHandler EditorMenuHandler
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;
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
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.
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*
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.
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)
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.
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
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.
“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.
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.
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!
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?
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!
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.
AbstractButtons User runs (JVM) WinDohs creates creates Runtime DirectoryFrame invokes creates creates data Process interacts with User
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
Summary • Work on FrameEditor! • Study the Demos • Course Project Specs TBA this week