Исходный текст приложения DirectFile
Исходный текст приложения DirectFile представлен в листинге 2.
Листинг 2. Файл DirectFile.java
import java.awt.*; import java.io.*; import java.util.*;
public class DirectFile { public static void main(String args[]) { MainFrameWnd frame = new MainFrameWnd("MenuApp"); frame.setSize( frame.getInsets().left + frame.getInsets().right + 320, frame.getInsets().top + frame.getInsets().bottom + 240); frame.show(); } }
class MainFrameWnd extends Frame { MenuBar mbMainMenuBar; Menu mnFile; Menu mnHelp; boolean fDBEmpty = true;
public MainFrameWnd(String sTitle) { super(sTitle);
setSize(400, 200); setBackground(Color.yellow); setForeground(Color.black);
setLayout(new FlowLayout());
mbMainMenuBar = new MenuBar(); mnFile = new Menu("File");
mnFile.add("New..."); mnFile.add("View records..."); mnFile.add("-"); mnFile.add("Exit");
mnHelp = new Menu("Help");
mnHelp.add("Content"); mnHelp.add("-"); mnHelp.add("About");
mbMainMenuBar.add(mnFile); mbMainMenuBar.add(mnHelp);
setMenuBar(mbMainMenuBar); }
public void paint(Graphics g) { g.setFont(new Font("Helvetica", Font.PLAIN, 12)); g.drawString("Frame window", 10, 70); super.paint(g); }
public boolean handleEvent(Event evt) { if(evt.id == Event.WINDOW_DESTROY) { setVisible(false); System.exit(0); return true; } else return super.handleEvent(evt); }
public boolean action(Event evt, Object obj) { MenuItem mnItem; if(evt.target instanceof MenuItem) { mnItem = (MenuItem)evt.target;
if(obj.equals("Exit")) { System.exit(0); }
else if(obj.equals("New...")) { if(fDBEmpty) { SimpleDBMS db = new SimpleDBMS( "dbtest.idx", "dbtest.dat");
db.AddRecord("Ivanov", 1000); db.AddRecord("Petrov", 2000); db.AddRecord("Sidoroff", 3000);
db.close();
fDBEmpty = false;
MessageBox mbox; mbox = new MessageBox( "Database created", this, "Information", true); mbox.show(); } }
else if(obj.equals("View records...")) { SimpleDBMS db = new SimpleDBMS( "dbtest.idx", "dbtest.dat"); String szRecords;
szRecords = db.GetRecordByNumber(0) + db.GetRecordByNumber(1) + db.GetRecordByNumber(2);
db.close();
MessageBox mbox; mbox = new MessageBox(szRecords, this, "Database records", true); mbox.show(); }
else if(obj.equals("Content")) { MessageBox mbox; mbox = new MessageBox( "Item Content selected", this, "Dialog from Frame", true); mbox.show(); }
else if(obj.equals("About")) { MessageBox mbox; mbox = new MessageBox( "Item About selected", this, "Dialog from Frame", true); mbox.show(); } else return false; return true; } return false; } }
class MessageBox extends Dialog { Label lbMsg; Button btnOK;
public MessageBox(String sMsg, Frame parent, String sTitle, boolean modal) { super(parent, sTitle, modal); resize(300, 100); setLayout(new GridLayout(2, 1)); lbMsg = new Label(sMsg, Label.CENTER); add(lbMsg); btnOK = new Button("OK"); add(btnOK); }
public boolean handleEvent(Event evt) { if(evt.id == Event.WINDOW_DESTROY) { dispose(); return true; } else return super.handleEvent(evt); }
public boolean action(Event evt, Object obj) { Button btn; if(evt.target instanceof Button) { btn = (Button)evt.target;
if(evt.target.equals(btnOK)) { dispose(); } else return false; return true; } return false; } }
class SimpleDBMS { RandomAccessFile idx; RandomAccessFile dat;
long idxFilePointer = 0;
public SimpleDBMS(String IndexFile, String DataFile) { try { idx = new RandomAccessFile( IndexFile, "rw"); dat = new RandomAccessFile( DataFile, "rw"); } catch(Exception ioe) { System.out.println(ioe.toString()); } }
public void close() { try { idx.close(); dat.close(); } catch(Exception ioe) { System.out.println(ioe.toString()); } }
public void AddRecord(String name, int account) { try { idx.seek(idx.length()); dat.seek(dat.length());
idxFilePointer = dat.getFilePointer();
idx.writeLong(idxFilePointer);
dat.writeBytes(name+ "\r\n"); dat.writeInt(account); } catch(Exception ioe) { System.out.println(ioe.toString()); } }
public String GetRecordByNumber(long nRec) { String sRecord = "<empty>";
try { Integer account; String str = null;
idx.seek(nRec * 8); idxFilePointer = idx.readLong(); dat.seek(idxFilePointer);
str = dat.readLine(); account = new Integer(dat.readInt());
sRecord = new String("> " + account + ", " + str); } catch(Exception ioe) { System.out.println(ioe.toString()); } return sRecord; } }