import java.io.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.lang.*; import javax.swing.*; public class Redaktor extends JApplet implements ActionListener,TextListener{ String failinimi; int akendeArv=1; TextArea tekstivali1=new TextArea(); TextArea tekstivali2=new TextArea(); TextArea tekstivali3=new TextArea(); TextArea tekstivali4=new TextArea(); TextArea tekstivali5=new TextArea(); TextArea tekstivali6=new TextArea(); JButton nupp2=new JButton("Uus"); JButton nupp=new JButton("Salvesta"); JButton nupp1=new JButton("Ava aknas"); JButton nupp3=new JButton("Sulge"); TextField tf1=new TextField(""); JTabbedPane paneel2=new JTabbedPane(); JPanel p1=new JPanel(new GridLayout(1,3)); JSplitPane paneel=new JSplitPane(JSplitPane.VERTICAL_SPLIT,p1,paneel2); TextArea valjad[]=new TextArea[]{tekstivali1, tekstivali2,tekstivali3,tekstivali4,tekstivali5,tekstivali6}; public Redaktor(){ //konstruktor nupp.addActionListener(this); nupp1.addActionListener(this); nupp2.addActionListener(this); nupp3.addActionListener(this); tf1.addTextListener(this); tekstivali1.addTextListener(this); tekstivali2.addTextListener(this); tekstivali3.addTextListener(this); tekstivali4.addTextListener(this); tekstivali5.addTextListener(this); tekstivali6.addTextListener(this); nupp.setEnabled(false); nupp1.setEnabled(false); p1.add(nupp2); p1.add(nupp); // p1.add(nupp3); // p1.add(nupp1);// p1.add(tf1); getContentPane().add(paneel); } public void avaAken(String failinimi, int aken){ if (failinimi.length()>0){ paneel2.add(failinimi,valjad[aken]); // } } public void sulgeAken(int aken){ paneel2.remove(aken); tf1.setText(""); } public void avaFail(String failinimi, String tegevus, int aken){ try{ if (tegevus=="loe"){ //System.out.println("avaFail-loe , aken "+aken+" "); BufferedReader sisse=new BufferedReader(new FileReader(failinimi)); paneel2.setTitleAt(aken,failinimi); valjad[aken].setText(""); //TextArea tyhjendamine - String rida=sisse.readLine(); while (rida!=null){ valjad[aken].append(rida+"\n"); rida=sisse.readLine(); } } } catch (Exception viga){System.out.println("Viga:"+viga);} try{ if (tegevus=="salvesta"){ //System.out.println("avaFail-salvesta aken " +aken); PrintWriter valja=new PrintWriter(new FileWriter(failinimi)); paneel2.setTitleAt(aken,failinimi); valja.println(valjad[aken].getText()); // valja.close(); } } catch (Exception viga){System.out.println("Viga:"+viga);} } public void actionPerformed(ActionEvent e){ if ((e.getSource()==nupp2) && (tf1.getText().endsWith(".txt"))){ //uus aken avaAken(tf1.getText(), (paneel2.getTabCount())); // tyhja akna avamisel vaja teada olemasolevaid aknaid //System.out.println("Akna indeks"+paneel2.getSelectedIndex()); tf1.setText(""); if (paneel2.getTabCount()==6){ nupp2.setEnabled(false); } } if (e.getSource()==nupp){ avaFail(tf1.getText(), "salvesta",paneel2.getSelectedIndex()); } if (e.getSource()==nupp1){ if (paneel2.getTabCount()==0){ //kas on yldse avatud aknaid ; siin oli getSelectedIndex avaAken(tf1.getText(), 0); // tyhja akna avamisel (0 indeks) vaja teada olemasolevaid aknaid } avaFail(tf1.getText(), "loe",paneel2.getSelectedIndex()); } if (e.getSource()==nupp3){ //sulgemine if (paneel2.getTabCount()>0){ //kas on yldse avatud aknaid sulgeAken(paneel2.getSelectedIndex()); } } } public void textValueChanged(TextEvent m){ if (m.getSource()==tf1){ nupp1.setEnabled(true); if(paneel2.getTabCount()>0){ nupp.setEnabled(true); } if(!tf1.getText().endsWith(".txt") || (tf1.getText().length() <4)){ nupp.setEnabled(false); nupp1.setEnabled(false); } } if ((m.getSource()==tekstivali1) || (m.getSource()==tekstivali2) || (m.getSource()==tekstivali3)|| (m.getSource()==tekstivali4)|| (m.getSource()==tekstivali5)||(m.getSource()==tekstivali6)){ tf1.setText(paneel2.getTitleAt(paneel2.getSelectedIndex())); nupp.setEnabled(true); } } public static void main(String argumendid[]){ JFrame f=new JFrame("Redaktor"); f.getContentPane().add(new Redaktor()); f.setSize(500, 300); f.setVisible(true); } }