import java.awt.*; import java.io.*; import java.awt.event.*; import java.applet.Applet; import java.util.StringTokenizer; import java.awt.MenuBar.*; import java.awt.datatransfer.*; public class Redaktor extends Frame implements ActionListener, TextListener{ int arv, arv1; TextArea ta=new TextArea(); TextField tu=new TextField(); TextField tu1=new TextField(); TextArea tf=new TextArea(10,20); Label loe=new Label("Lugemis väli"); Label salvesta=new Label("Salvestamis väli"); Panel nupupaneel2=new Panel(new GridLayout(2,1)); Panel nupupaneel=new Panel(new GridLayout(1,2)); Panel nupupaneel1=new Panel(new GridLayout(2,2)); public Redaktor(){ MenuBar mb = new MenuBar(); mb.add(looMenyy()); mb.add(looMenyy2()); setMenuBar(mb); setLayout(new BorderLayout()); nupupaneel1.add(loe); nupupaneel1.add(salvesta); nupupaneel.add(nupupaneel1); nupupaneel.add(nupupaneel2); nupupaneel2.add(tu); nupupaneel2.add(tu1); add(ta, BorderLayout.CENTER); add(nupupaneel, BorderLayout.SOUTH); add(tf, BorderLayout.EAST); ta.addTextListener(this); } void arvutaStatistika(){ arv=0; arv1=0; String s=ta.getText(); tf.setText(""); tf.append("Pikkus: "+s.length()+"\n"); StringTokenizer st=new StringTokenizer(s, " \n"); while(st.hasMoreTokens()){ String sona=st.nextToken(); arv=arv+1; } tf.append("Sõnu: "+arv+"\n"); tf.append("Ridu: "+new StringTokenizer(s,"\n").countTokens()+"\n"); tf.append("Lauseid: "+new StringTokenizer(s,".?!").countTokens()+"\n"); } public void textValueChanged(TextEvent e){ arvutaStatistika(); } Menu looMenyy() { Menu m = new Menu("Fail"); MenuItem mi = new MenuItem("Uus"); mi.addActionListener(this); m.add(mi); mi = new MenuItem("Ava"); mi.addActionListener(this); m.add(mi); mi = new MenuItem("Salvesta"); mi.addActionListener(this); m.add(mi); m.addSeparator(); mi = new MenuItem("Sulge"); mi.addActionListener(this); m.add(mi); return m; } Menu looMenyy2(){ Menu m2 = new Menu("Redigeeri"); MenuItem mi2 = new MenuItem("Kopeeri"); mi2.addActionListener(this); m2.add(mi2); mi2 = new MenuItem("Kleebi"); mi2.addActionListener(this); m2.add(mi2); mi2 = new MenuItem("Selekteeri kõik"); mi2.addActionListener(this); m2.add(mi2); return m2; } public void actionPerformed(ActionEvent e){ Clipboard malu = getToolkit().getSystemClipboard(); String kask = e.getActionCommand(); if(kask.equals("Salvesta")){ try{ setTitle("Vihik "+tu1.getText()+".txt"); PrintWriter faili=new PrintWriter( new FileWriter(tu1.getText()+".txt") ); faili.println(ta.getText()); faili.close(); }catch(Exception viga){ System.out.println("Probleem kirjutamisel"); } } if(kask.equals("Ava")){ try{ BufferedReader failist=new BufferedReader( new FileReader(tu.getText()+".txt") ); setTitle("Vihik "+tu.getText()+".txt"); String rida=failist.readLine(); while(rida!=null){ ta.append(rida+"\n"); rida=failist.readLine(); } failist.close(); } catch(Exception viga){ System.out.println("Probleem avamisel"); } } if(kask.equals("Sulge")){ try{System.exit(0); }catch(Exception viga){ System.out.println("Probleem sulgemisel"); } } if(kask.equals("Uus")){ try{ ta.setText(""); tu.setText(""); tu1.setText(""); setTitle("Vihik Nimetu.txt"); }catch(Exception viga){ System.out.println("Probleem uuendamisel"); } } if(kask.equals("Kopeeri")){ try{ StringSelection data = new StringSelection(ta.getSelectedText()); malu.setContents(data, data); }catch(Exception viga){ System.out.println("Probleem selekteerimisel"); } } if (kask.equals("Kleebi")) { Transferable andmed = malu.getContents(this); String s; String t=ta.getText(); try { s = (String)(andmed.getTransferData(DataFlavor.stringFlavor)); ta.setText(t+s); } catch (Exception viga) { s = viga.getMessage(); } } if(kask.equals("Selekteeri kõik")){ try{ ta.selectAll(); }catch (Exception viga){ System.out.println("Viga selekteerimisel"); } } } }