import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.applet.Applet; import java.util.*; class TekstiRedaktor extends JApplet implements ActionListener, KeyListener { JMenuItem menuOpen, menuSave, menuSaveAs, menuExit, menuBlack, menuBlue, menuRed; JTextArea editArea, textField4; JTextField textField1, textField2; String fileName=""; TekstiRedaktor() { // siin on ülemine osa - menubar JMenuBar menubar=new JMenuBar(); setJMenuBar(menubar); JMenu menuFile=new JMenu("File", false); // File menüü menubar.add(menuFile); menuOpen=new JMenuItem("Open"); menuSave=new JMenuItem("Save"); menuSaveAs=new JMenuItem("Save As"); menuExit=new JMenuItem("Exit"); menuFile.add(menuOpen); menuFile.add(menuSave); menuFile.add(menuSaveAs); menuFile.addSeparator(); menuFile.add(menuExit); JMenu menuFont=new JMenu("Font", false); // Font menüü menubar.add(menuFont); menuBlack=new JMenuItem("Black"); menuBlue=new JMenuItem("Blue"); menuRed=new JMenuItem("Red"); menuFont.add(menuBlack); menuFont.add(menuBlue); menuFont.add(menuRed); // siin algab keskmine osa ehk tekstiredaktori väli, kuhu saab oma teksti kirjutada JPanel panel=new JPanel(); panel.setLayout(new BorderLayout()); editArea=new JTextArea(); getContentPane().add(panel, "Center"); editArea.setFont(new Font("Sans Serif", Font.PLAIN, 14)); editArea.setForeground(Color.black); editArea.setLineWrap(true); editArea.setWrapStyleWord(true); editArea.setEditable(true); JScrollPane areaScrollPanel=new JScrollPane(editArea); areaScrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPanel.setPreferredSize(new Dimension(600, 400)); areaScrollPanel.setBorder(BorderFactory.createTitledBorder("Print here")); panel.add(areaScrollPanel); // siin algab nö. 'statusbar' - kus on info tekstifaili kohta textField1=new JTextField(4); // sümbolite arv textField2=new JTextField(4); // sõnade arv textField4=new JTextArea(""); // lahti oleva tekstifaili nimi textField1.setBackground(Color.white); textField4.setBackground(new Color(204,204,204)); JPanel panel2=new JPanel(); panel2.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 0)); panel2.add(textField1); panel2.add(textField2); panel2.add(textField4); panel.add(panel2, BorderLayout.SOUTH); menuOpen.addActionListener(this); menuSave.addActionListener(this); menuSaveAs.addActionListener(this); menuExit.addActionListener(this); menuBlack.addActionListener(this); menuBlue.addActionListener(this); menuRed.addActionListener(this); textField1.addActionListener(this); textField2.addActionListener(this); editArea.addKeyListener(this); } // end of TekstiRedaktor public void actionPerformed(ActionEvent event) { Object source=event.getSource(); String returned=null; FileInputStream in=null; FileOutputStream out=null; String selectedValue; // faili avamine if(source == menuOpen){ // dialoogiakna algus try{ selectedValue=JOptionPane.showInputDialog(null, "Please write the file name!"); fileName=selectedValue.toString(); } catch(NullPointerException npe) {} // dialoogiakna lõpp try{ File inputFile=new File(fileName); in=new FileInputStream(inputFile); byte bt[]=new byte[(int)inputFile.length()]; in.read(bt); String s=new String(bt); editArea.setText(s); textField4.setText(fileName); } catch(IOException ez){ JOptionPane.showMessageDialog(null, "Cannot find or read this file!"); } if(in!=null){ try{ in.close(); } catch(IOException ew){ } } } // endif menuOpen // faili jooksev salvestamine if(source==menuSave) { returned=editArea.getText(); if(returned!=null) { try { byte b[]=returned.getBytes(); out=new FileOutputStream(fileName); out.write(b); textField4.setText(fileName); } catch(IOException ex) {} if(out!=null) { try { out.close(); } catch(IOException ey) {} } } // endif returned! else { JOptionPane.showMessageDialog(null, "There is no text to save!"); } // endif else returned! } // endif menuSave // uue faili salvestamine if(source==menuSaveAs) { returned=editArea.getText(); if(returned!=null) { // dialoogiakna algus try{ selectedValue=JOptionPane.showInputDialog(null, "Please write the file name!"); fileName=selectedValue.toString(); } catch(NullPointerException npe) {} // dialoogiakna lõpp try { byte b[]=returned.getBytes(); out=new FileOutputStream(fileName); out.write(b); textField4.setText(fileName); } catch(IOException ex) {} if(out!=null) { try { out.close(); } catch(IOException ey) {} } } // endif returned! else { JOptionPane.showMessageDialog(null, "There is no text to save!"); } // endif else returned! } // endif menuSaveAs // tekstiredakorist väljumine if(source == menuExit){ int replay=JOptionPane.showConfirmDialog(null, "Are You sure, You want to exit?"); switch(replay) { case JOptionPane.YES_OPTION: System.exit(0); case JOptionPane.NO_OPTION: break; case JOptionPane.CANCEL_OPTION: break; } // end switch } // endif menuExit // teksti värvide vahetamine if(source == menuBlack) editArea.setForeground(Color.black); if(source == menuBlue) editArea.setForeground(Color.blue); if(source == menuRed) editArea.setForeground(Color.red); } // end of void actionPerformed // tähtede ja sõnade kokkulugemine public void keyPressed(KeyEvent e){} public void keyReleased(KeyEvent e){ int charCount=0; charCount = editArea.getDocument().getLength(); String rida=editArea.getText(); StringTokenizer st=new StringTokenizer(rida, " "); int wordCount=0; while(st.hasMoreTokens()){ String sona=st.nextToken(); wordCount=wordCount+1; } try { textField1.setText(String.valueOf(charCount)); textField2.setText(String.valueOf(wordCount)); } catch (NullPointerException npe) { System.out.println("keyReleased:npe"); } } public void keyTyped(KeyEvent e){} // tähtede kokkulugemise lõpp public static void main(String[] args) { JFrame frame= new JFrame("H2T tekstiredaktor"); frame.getContentPane().add(new TekstiRedaktor()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } // end of public static void } // end of class TekstiRedaktor