I need help getting UI to work with my File System
import java.awt.*;
import javax.swing.*;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.*;
class UI extends JFrame implements ActionListener
{
private JButton format = new JButton("Format Disk");
private JButton mount = new JButton("Mount Volume");
private JButton createDIR = new JButton("Create Directory");
private JButton createFile = new JButton("Create File");
private JButton DeleteDIR = new JButton("Delete Directory");
private JButton DeleteFile = new JButton("Delete File");
private JButton editOpen = new JButton("Edit/Open File");
private JButton editSave = new JButton("Edit/Save File");
private JButton setpath = new JButton("Set Path");
private JButton dismount = new JButton("Dismount Volume");
private JButton dumpVolume = new JButton("Dump Volume");
private JLabel path = new JLabel("Path:");
private JLabel fileVolumeLabel = new JLabel("File/Volume:");
private JTextField fileTXTArea = new JTextField(10);
private JTextField errorArea = new JTextField(50);
private JTextArea dumpArea = new JTextArea(35, 80);
private JTextArea pathArea = new JTextArea(2,50);
private JScrollPane dumpScroll = new JScrollPane(dumpArea);
private FileSystem fs = null;
public UI(FileSystem fs)
{
super("File System UI");
setSize(1000,600);
setVisible(true);
dumpArea.setEditable(false);
dumpArea.setWrapStyleWord(true);
dumpArea.setFont(new Font(Font.MONOSPACED,Font.PLAIN,12));
dumpArea.setBackground(Color.PINK);
dumpArea.setForeground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
Panel rightPanel = new Panel();
rightPanel.setLayout(new GridLayout(7,2));
rightPanel.add(fileVolumeLabel);
rightPanel.add(fileTXTArea);
rightPanel.add(format);
rightPanel.add(mount);
rightPanel.add(createDIR);
rightPanel.add(createFile);
rightPanel.add(DeleteDIR);
rightPanel.add(DeleteFile);
rightPanel.add(editOpen);
rightPanel.add(editSave);
rightPanel.add(setpath);
rightPanel.add(dismount);
rightPanel.add(dumpVolume);
add(rightPanel, BorderLayout.EAST);
Panel topPanel = new Panel();
topPanel.add(path);
topPanel.add(pathArea);
add(topPanel, BorderLayout.NORTH);
Panel bottomPanel = new Panel();
JLabel error = new JLabel("Error:");
bottomPanel.add(error);
bottomPanel.add(errorArea);
add(bottomPanel, BorderLayout.SOUTH);
Panel centerpanel = new Panel();
centerpanel.add(dumpScroll);
add(centerpanel, BorderLayout.CENTER);
format.addActionListener(this);
mount.addActionListener(this);
createDIR.addActionListener(this);
createFile.addActionListener(this);
DeleteDIR.addActionListener(this);
DeleteFile.addActionListener(this);
editSave.addActionListener(this);
editOpen.addActionListener(this);
setpath.addActionListener(this);
dismount.addActionListener(this);
dumpVolume.addActionListener(this);
this.fs = fs;
}
public static void main(String[] args)
{
// new UI();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==(format))
{
System.out.println("format");
fs.format(fileTXTArea.getText());//need to check if something typed in format field
}
else if(e.getSource()==(mount))
{
fs.mount(fileTXTArea.getText());
System.out.println("mount");
}
else if(e.getSource()==(createDIR))
{
System.out.println("createDIR");
}
else if(e.getSource()==(createFile))
{
System.out.println("createFile");
}
else if(e.getSource()==(DeleteDIR))
{
System.out.println("DeleteDIR");
}
else if(e.getSource()==(DeleteFile))
{
System.out.println("DeleteFile");
}
else if(e.getSource()==(editSave))
{
System.out.println("editSave");
}
else if(e.getSource()==(editOpen))
{
System.out.println("editOpen");
}
else if(e.getSource()==(setpath))
{
System.out.println("setpath");
}
else if(e.getSource()==(dismount))
{
System.out.println("dismount");
}
else if(e.getSource()==(dumpVolume))
{
String dump = fs.dump();
dumpArea.setText(dump);
}
}
}
whis is my UI
import java.util.StringTokenizer;
public class FileSystem
{
private VirtualDisk vd = null;
private char[] sectorByte = new char[Sector.size];
private BitmapSector bitmap = null;
private IndexSector indexf = null;
private DirectorySector master = null;
private boolean mounted = false;
private String volume = "";
public String status = "";
private DirectorySector pathDIR = null;
private int pathLSN = 4;
public boolean format(String diskName)
{
if(mounted)
{
return false;
}
else
{
bitmap = new BitmapSector();
indexf = new IndexSector();
master = new DirectorySector("MASTER");
pathDIR = master;
pathLSN = 4;
vd = new VirtualDisk("E:/"+diskName+".txt");
vd.write(0,new BootSector());
vd.write(1,new VolumeSector(diskName));
vd.write(2,bitmap);
vd.write(3,indexf);
vd.write(4,pathDIR);
vd.flush();
volume = diskName;
mounted = true;
return true;
}
}
public String dump()
{
flush();
return vd.toString();
}
private void flush()
{
vd.write(2,bitmap);
vd.write(3,indexf);
vd.write(pathLSN,pathDIR);
vd.flush();
}
public boolean setPath(String pathStr)
{
if(pathStr==null)
{
pathLSN = 4;
pathDIR = new DirectorySector(vd.read(pathLSN));
flush();
status="Path reset. Path set to sector "+pathLSN+".";
return true;
}
else if(pathStr.length()==0)
{
pathLSN = 4;
pathDIR = new DirectorySector(vd.read(pathLSN));
flush();
status="Path reset. Path set to sector "+pathLSN+".";
return true;
}
else
{
pathLSN = 4;
pathDIR = new DirectorySector(vd.read(pathLSN));
flush();
StringTokenizer st = new StringTokenizer(pathStr,"/");
while(st.hasMoreTokens())
{
String path = st.nextToken();
System.out.println(path);
if(mounted)
{
int dirEntry = pathDIR.findFile(path,"DIR");
if(dirEntry==-1)
{
status="Path element ["+path+"] not found. Path set to sector "+pathLSN+".";
return false;
}
else
{
int idxEntry = pathDIR.getSector(dirEntry);
int mapEntry = indexf.getSector(idxEntry);
pathLSN = mapEntry;
pathDIR = new DirectorySector(vd.read(pathLSN));
flush();
}
}
else
{
status="Volume not mounted";
return false;
}
}
status="Path set to sector "+pathLSN+".";
return true;
}
}
public boolean mount(String diskName)
{
if(mounted)
{
return false;
}
else
{
vd = new VirtualDisk("C:/Users/gerald/Desktop/"+diskName+".txt");
bitmap = new BitmapSector(vd.read(2));
indexf = new IndexSector(vd.read(3));
master = new DirectorySector(vd.read(4));
pathDIR = master;
pathLSN = 4;
vd.flush();
volume = diskName;
mounted = true;
return true;
}
}
public boolean dismount(String diskName)
{
if(diskName==null) diskName = "";
if(!(mounted && diskName.equals(volume)))
{
return false;
}
else
{
volume = "";
mounted = false;
return true;
}
}
public boolean createDirectory(String directryName)
{
return create(directryName,"DIR");
}
public boolean createFile(String fileName)
{
return create(fileName,"TXT");
}
private boolean create(String name,String type)
{
if(mounted)
{
if(pathDIR.findFile(name,type)>-1)
{
return false;
}
else
{
int dirEntry = pathDIR.getFirstFreeBit();
if(dirEntry>-1)
{
int mapEntry = bitmap.getFirstFreeBit();
if(mapEntry>-1)
{
if(type.equals("DIR"))
{
vd.write(mapEntry, new DirectorySector(name));
}
else
{
vd.write(mapEntry, new Sector(' '));
}
int idxEntry = indexf.getFirstFreeBit();
if(idxEntry>-1)
{
pathDIR.recordFile(dirEntry,name,type,idxEntry);
indexf.recordFile(idxEntry,mapEntry,1);
bitmap.setBit(mapEntry);
indexf.setBit(idxEntry);
flush();
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
}
else
{
return false;
}
}
public boolean deleteFile(String fileName)
{
return delete(fileName,"TXT");
}
public boolean deleteDirectory(String directoryName)
{
return delete(directoryName,"DIR");
}
private boolean delete(String name, String type)
{
if(mounted)
{
int dirEntry = pathDIR.findFile(name,type);
if(dirEntry==-1)
{
status=type+" Not Found";
return false;
}
else
{
int idxEntry = pathDIR.getSector(dirEntry);
int mapEntry = indexf.getSector(idxEntry);
if(!pathDIR.clearBit(dirEntry))
{
status="Directory clear failed from directory";
return false;
}
else if(!indexf.clearBit(idxEntry))
{
status="Directory clear failed indexf";
return false;
}
if(!bitmap.clearBit(mapEntry))
{
status="Directory clear failed indexf";
return false;
}
else
{
flush();
status="Delete was successfull";
return true;
}
}
}
else
{
status="Volume not mounted";
return false;
}
}
public String openFile(String fileName)
{
int dirEntry = pathDIR.findFile(fileName,"TXT");
if(dirEntry==-1)
{
status="File not found";
return null;
}
else
{
int idxEntry = pathDIR.getSector(dirEntry);
if(idxEntry==0)
{
status="No allocation found";
return null;
}
else
{
int sectorNo = indexf.getSector(idxEntry);
if(sectorNo==0)
{
status="Allocation error";
return null;
}
else
{
Sector sector = vd.read(sectorNo);
System.out.println("fs.openFile: sectorNo: "+sectorNo);
System.out.println("fs.openFile: ["+sector.toString()+"]");
status="File opened";
return sector.toString();
}
}
}
}
public boolean saveFile(String fileName, String text)
{
int dirEntry = pathDIR.findFile(fileName,"TXT");
if(dirEntry==-1)
{
status="File not found";
return false;
}
else
{
int idxEntry = pathDIR.getSector(dirEntry);
if(idxEntry==0)
{
status="No allocation found";
return false;
}
else
{
if(text==null)
{
status="Nothing to save";
return false;
}
else
{
int sectorNo = indexf.getSector(idxEntry);
if(sectorNo==0)
{
status="Allocation error";
return false;
}
else
{
vd.write(sectorNo,new Sector(text));
status="File saved";
return true;
}
}
}
}
}
}
this is my FS code