Виктор Кон,   vkBook, Java-Second

Класс fileFrame



import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class fileFrame extends JInternalFrame implements ActionListener {
 String txt,tefi,font; int i,k,n,ok,ifs,ifk;
 short bt; char[] c; byte[] ba; String[] lab; String[] tt;
 JMenu m; JMenuItem subm; String[] name; int[] mne; int[] acc; String[] act;
 Container pcont; JTextArea txtAr; File fs; Frame parent = MyPro.parent();

 public fileFrame(String fn, int oks, int j, int[] ia){
  fs = new File(fn);
  if( fs.exists() ){ 
   setTitle(fn); setResizable(true); setSize(ia[2],ia[3]); setLocation(ia[0],ia[1]);
   setJMenuBar(createMenuBar()); ok=oks; tefi="";
   ifs=MyPro.fs; ifk=MyPro.fk;
   if( ifk < 1 || ifk > 5 ){ifk=1;} if( ifs < 8 || ifs > 20 ){ifs=14;}
   switch(ifk){
    case 1: font="Monospaced"; break;
    case 2: font="Serif"; break;
    case 3: font="SansSerif"; break;
    case 4: font="Dialog"; break;
    case 5: font="DialogInput"; break;
   }
   pcont = getContentPane(); txtAr = new JTextArea();
   txtAr.setFont(new java.awt.Font(font, java.awt.Font.PLAIN, ifs));
   n=(int)fs.length(); ba = new byte[n]; c = new char[n]; i=0; 
   try{ 
    DataInputStream din = new DataInputStream(new BufferedInputStream(new FileInputStream(fs)));
    din.readFully(ba,0,n); din.close();
   }catch(IOException ex){ex.printStackTrace();} 
   for( k=0; k < n; k++ ){bt=ba[k]; if(bt != 13){if(bt < 0){bt += 1104;} c[i]=(char)bt; i++;}}
   txt = new String(c,0,i); txtAr.setText(txt);
   txtAr.setCaretPosition(j); txtAr.setEditable(true);
   JScrollPane scrollPane = new JScrollPane(txtAr);
   pcont.add(scrollPane, BorderLayout.CENTER);
  }else{
   JOptionPane.showMessageDialog(this,"File is absent !\n"+fn,"Warning",-1);
 }}

 protected JMenuBar createMenuBar(){
  JMenuBar menub = new JMenuBar();
  //
  m = new JMenu("Edit"); m.setMnemonic(KeyEvent.VK_D);
  name = new String[]{"Find", "Font", "Exit"};
  mne = new int[]{KeyEvent.VK_F, KeyEvent.VK_O, KeyEvent.VK_X};
  acc = new int[]{KeyEvent.VK_F, KeyEvent.VK_O, KeyEvent.VK_X};
  act = new String[]{"find", "font", "exit"};
  createSubMenu(); menub.add(m);
  return menub;
 }

 protected void createSubMenu(){
  for( i=0; i < act.length; i++ ){
   subm = new JMenuItem(name[i]);  subm.setMnemonic(mne[i]);
   subm.setAccelerator(KeyStroke.getKeyStroke(acc[i], ActionEvent.ALT_MASK));
   subm.setActionCommand(act[i]); subm.addActionListener(this);
   m.add(subm);
  }
 }

 public void actionPerformed(ActionEvent e) {
       if("find".equals(e.getActionCommand())){findop();}
  else if("font".equals(e.getActionCommand())){fontop();}
  else if("exit".equals(e.getActionCommand())){saveFile();}
 }

 private void findop(){
  int nc; String s,ss;
  lab = new String[1]; tt = new String[1];
  lab[0]="Find what:"; tt[0]=tefi;
  inpForm f = new inpForm(parent,"Find",lab,tt,1,50,0); f.show(); f.dispose();
  tt = MyPro.tt; tefi=tt[0]; if( tefi == null || !MyPro.ok ){return;}
  nc = txtAr.getCaretPosition(); 
  s = txtAr.getText();  ss=s.substring(nc);
  i=ss.indexOf(tefi); if( i > -1 ){ nc += i; }
  txtAr.setCaretPosition(nc);  
 }

 private void fontop(){
  lab = new String[1]; tt = new String[1]; int[] ia = new int[2];
  lab[0]="Select kind (1-5) and size (8-20) of font:"; tt[0]=" "+ifk+"  "+ifs;
  inpForm f = new inpForm(parent,"Font",lab,tt,1,50,0); f.show(); f.dispose();
  if( !MyPro.ok ){return;} tt=MyPro.tt; ia = MyPro.ttiarr(tt[0],2);
  ifk=ia[0]; ifs=ia[1]; MyPro.fk=ifk; MyPro.fs=ifs;
  if( ifk < 1 || ifk > 5 ){ifk=1;} if( ifs < 8 || ifs > 20 ){ifs=14;}
  switch(ifk){
   case 1: font="Monospaced"; break;
   case 2: font="Serif"; break;
   case 3: font="SansSerif"; break;
   case 4: font="Dialog"; break;
   case 5: font="DialogInput"; break;
  }
  txtAr.setFont(new java.awt.Font(font, java.awt.Font.PLAIN, ifs));
 }

 public void saveFile(){
  if( ok == 1 ){ 
   txt = txtAr.getText(); c = txt.toCharArray(); n=c.length; ba = new byte[n];
   try{
    BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(fs));
    DataOutputStream dout = new DataOutputStream(bout); 
    for( i=0; i < n; i++ ){ bt=(short)c[i];
     if( bt < 1104 && bt > 1039){bt -= 1104;} ba[i]=(byte)bt; 
    } dout.write(ba,0,n); bout.flush(); dout.close(); 
   }catch(IOException ex){ex.printStackTrace();}
  } try{ this.setClosed(true); }catch(java.beans.PropertyVetoException e){}
 }
}

Hosted by uCoz