Link to home
Start Free TrialLog in
Avatar of Zorro032798
Zorro032798

asked on

Need applet 2: edit text file

I need an applet to easily build the text file mentioned in the other "need applet"-question. Anyway, because it is a separate procedure I wanted to put it in another question.

The text file to edit is a two column file with item names in the first column and url's in the second. The items are sorted aphabetically. Something like this:

java  java.htm
jerk  \somedir\jerk.htm
pole  pole.htm

When reading a few html pages on my hard disk (not on the web; the application runs only locally) I want to highlight a word and then click on a button in the left frame of the page to put that word in my text file together with the url of the page I'm reading. Suppose I'm reading the word "jerba" in the "tunisia.htm" file; I highlight it and click the applet button in the left frame; "jerba" is inserted in my text file, between "java" and "jerk" (alphabetical sort); in the second column "tunesia.htm" is written.
Furthermore it has to check if the item already exists in the file and ask the user if he wants to override the url or not (if item and url are the same, there can be a simple message to tell that it already exists).

As I mentioned in my first question: I'm too new to Java to implement this myself and it's so darn urgent that I'm humbly :-) asking one of you to do this for me. If you think you can do it, lock the question and send the zipped source files directly to me.
If something is not clear, please ask.
Thanx.
ASKER CERTIFIED SOLUTION
Avatar of diakov
diakov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zorro032798
Zorro032798

ASKER

See other question.
I assume this will be an applet in local mode.

I have several questions:

>>I highlight it and click the applet button in the left frame; "jerba" is ...

. You have to copy it into the system clipboard (ctrl-c, shift-ins, or whatever it is on the OS/browser combination you're using). Then this is available to the applet.

>>.... Suppose I'm reading the word "jerba" in the "tunisia.htm" file; I highlight it and click the applet b...

. the Applet can call Javascript to get the location, however this is browser dependent. The other way around, Javascript to applet is browser independend. what I can do is to put a Javascript in the HTML of the left frame that often (each second) looks at the URL of the right frame and sets it in the applet. this way the applet will know almost instantly which URL you're brosing. The URL will be the exact one from the location bar, so this might be a problem if you're making a CD-rom. The drive leters will be there, so you have to edit the text file after words, to make them relative or other.

. Consider this: how are you going to change the URL of the left frame? I suggest you put an initial index of the whole tree you're going to browse and then browse into it, clicking occasionally on the applet.

Cheers,
  Nik
Nik, your questions are not very clear to me.
Concerning those url's: the url of the left frame doesn't change, only the right one.
Implement it in a single-frame document, if this is easier to accomplish. CU.
What I understood is that you want to browse in the right frame while occasionally storing links in the left. Is this so?

Then my previous comments address exactly issues related to that solution.

The easiest is to have one frame document with an applet embeded in it, that has two fields, for URL and keyword, two listboxes, for URLs and keywords and you paste/type the keyword and paste the link, and the it is put in the list boxes and at the end of the session written to a text file. Do you want it this way?

Cheers,
  Nik
This is the basic file, Editor.java.

I understood that after our discussion you decide not to use an editor applet, but rather an application. You have to decide what is more appropriate.
----------------------------

/*
      A basic extension of the java.applet.Applet class
 */

import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;

public class Editor extends Applet
{
  public String file_name = "";
  public String delimiter = "";
  public boolean local_mode = true;
 
  public boolean running_applet_mode = false;
 
      public void init()
      {
        running_applet_mode = true;
        
        String t = getParameter("FILE_NAME");
        System.out.println("Using FILE_NAME: " + t);
        if ((t != null) && (t.length() > 0))
          file_name = new String(t);
        t = getParameter("DELIMITER");
        System.out.println("Using DELIMITER: " + t);
        if ((t != null) && (t.length() > 0))
          delimiter = new String(t);
            // Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
            //symantec.itools.lang.Context.setApplet(this);
      
            // This code is automatically generated by Visual Cafe when you add
            // components to the visual environment. It instantiates and initializes
            // the components. To modify the code, only use code syntax that matches
            // what Visual Cafe can generate, or Visual Cafe may be unable to back
            // parse your Java file into its visual environment.
            //{{INIT_CONTROLS
            GridBagLayout gridBagLayout;
            gridBagLayout = new GridBagLayout();
            setLayout(gridBagLayout);
            setSize(539,373);
            label1 = new java.awt.Label("File name");
            label1.setBounds(0,0,538,23);
            GridBagConstraints gbc;
            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 2;
            gbc.weightx = 1.0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0,0,0,0);
            ((GridBagLayout)getLayout()).setConstraints(label1, gbc);
            add(label1);
            filename = new java.awt.TextField();
            filename.setBounds(0,23,538,23);
            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.gridwidth = 2;
            gbc.weightx = 1.0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0,0,0,0);
            ((GridBagLayout)getLayout()).setConstraints(filename, gbc);
            add(filename);
            label2 = new java.awt.Label("Item keyword");
            label2.setBounds(0,69,269,23);
            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.weightx = 1.0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0,0,0,0);
            ((GridBagLayout)getLayout()).setConstraints(label2, gbc);
            add(label2);
            label3 = new java.awt.Label("Item link");
            label3.setBounds(269,69,269,23);
            gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 3;
            gbc.weightx = 1.0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0,0,0,0);
            ((GridBagLayout)getLayout()).setConstraints(label3, gbc);
            add(label3);
            keyword = new java.awt.TextField();
            keyword.setBounds(0,92,269,23);
            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 5;
            gbc.weightx = 1.0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0,0,0,0);
            ((GridBagLayout)getLayout()).setConstraints(keyword, gbc);
            add(keyword);
            link = new java.awt.TextField();
            link.setBounds(269,92,269,23);
            gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 5;
            gbc.weightx = 1.0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0,0,0,0);
            ((GridBagLayout)getLayout()).setConstraints(link, gbc);
            add(link);
            keyword_list = new java.awt.List(4);
            keyword_list.setBounds(0,138,269,235);
            gbc = new GridBagConstraints();
            gbc.gridy = 7;
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.insets = new Insets(0,0,0,0);
            ((GridBagLayout)getLayout()).setConstraints(keyword_list, gbc);
            add(keyword_list);
            link_list = new java.awt.List(4);
            link_list.setBounds(269,138,269,235);
            gbc = new GridBagConstraints();
            gbc.gridy = 7;
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.insets = new Insets(0,0,0,0);
            ((GridBagLayout)getLayout()).setConstraints(link_list, gbc);
            add(link_list);
            save = new java.awt.Button();
            save.setLabel("Show result");
            save.setBounds(269,46,269,23);
            save.setBackground(new Color(12632256));
            gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 2;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0,0,0,0);
            ((GridBagLayout)getLayout()).setConstraints(save, gbc);
            add(save);
            open = new java.awt.Button();
            open.setLabel("Open/Create file");
            open.setBounds(0,46,269,23);
            open.setBackground(new Color(12632256));
            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0,0,0,0);
            ((GridBagLayout)getLayout()).setConstraints(open, gbc);
            add(open);
            add_item = new java.awt.Button();
            add_item.setLabel("Add item");
            add_item.setBounds(0,115,269,23);
            add_item.setBackground(new Color(12632256));
            gbc = new GridBagConstraints();
            gbc.gridy = 6;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0,0,0,0);
            ((GridBagLayout)getLayout()).setConstraints(add_item, gbc);
            add(add_item);
            delete_item = new java.awt.Button();
            delete_item.setLabel("Delete Item");
            delete_item.setBounds(269,115,269,23);
            delete_item.setBackground(new Color(12632256));
            gbc = new GridBagConstraints();
            gbc.gridy = 6;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0,0,0,0);
            ((GridBagLayout)getLayout()).setConstraints(delete_item, gbc);
            add(delete_item);
            //}}
      
            //{{REGISTER_LISTENERS
            SymAction lSymAction = new SymAction();
            open.addActionListener(lSymAction);
            save.addActionListener(lSymAction);
            add_item.addActionListener(lSymAction);
            delete_item.addActionListener(lSymAction);
            SymMouse aSymMouse = new SymMouse();
            keyword_list.addMouseListener(aSymMouse);
            link_list.addMouseListener(aSymMouse);
            SymMouseMotion aSymMouseMotion = new SymMouseMotion();
            keyword_list.addMouseMotionListener(aSymMouseMotion);
            link_list.addMouseMotionListener(aSymMouseMotion);
            //}}
            
            try
            {
              URL cb = getCodeBase();
              String host = cb.getHost();
              local_mode = ((host == null) || ((host != null) && (host.length() == 0)));
              
              System.out.println("Host: " + cb.getHost());
              System.out.println("Port: " + Integer.toString(cb.getPort(), 10));
              System.out.println("File: " + cb.getFile());
              
              if (local_mode)
                System.out.println("Running in local mode...");
              else
                System.out.println("Running in remote mode...");
        }
        catch (Exception e)
        {
          e.printStackTrace();  
        }
            filename.setText(file_name);
            
            if (local_mode)
            {
              open_file();
            }
            else
            {
              filename.setText("Remote mode not supported!");
              disable_all();
            }
      }
      
      //{{DECLARE_CONTROLS
      java.awt.Label label1;
      java.awt.TextField filename;
      java.awt.Label label2;
      java.awt.Label label3;
      java.awt.TextField keyword;
      java.awt.TextField link;
      java.awt.List keyword_list;
      java.awt.List link_list;
      java.awt.Button save;
      java.awt.Button open;
      java.awt.Button add_item;
      java.awt.Button delete_item;
      //}}

      class SymAction implements java.awt.event.ActionListener
      {
            public void actionPerformed(java.awt.event.ActionEvent event)
            {
                  Object object = event.getSource();
                  if (object == open)
                        open_ActionPerformed(event);
                  else if (object == save)
                        save_ActionPerformed(event);
                  else if (object == add_item)
                        addItem_ActionPerformed(event);
                  else if (object == delete_item)
                        deleteItem_ActionPerformed(event);
            }
      }

      void open_ActionPerformed(java.awt.event.ActionEvent event)
      {
            // to do: code goes here.
                  
            //{{CONNECTION
            // Set the text for TextField...
            if (!open_file())
            {
              message("File could not be opened!");
              disable_all();
            }
            //}}
      }

      void save_ActionPerformed(java.awt.event.ActionEvent event)
      {
            // to do: code goes here.
                  
            //{{CONNECTION
            // Set the text for TextField...
            if (save_file())
              ;
            //}}
      }

      void addItem_ActionPerformed(java.awt.event.ActionEvent event)
      {
            // to do: code goes here.
                  
            //{{CONNECTION
            // Set the text for TextField...
            add_item(keyword.getText(), link.getText());
            //}}
      }

      void deleteItem_ActionPerformed(java.awt.event.ActionEvent event)
      {
            // to do: code goes here.
                  
            //{{CONNECTION
            // Set the text for TextField...
            delete_item(keyword.getText());
            //}}
      }
      public boolean open_file()
      {
        String url_string = "";
        file_name = filename.getText();
    String protocol = "";
    String file;

        try
        {        
          URL cb = getCodeBase();

          if (local_mode) //& Windows
          {
            protocol = "file://";
                file = cb.getFile();
                url_string = protocol + file + file_name;
          }

          keyword_list.removeAll();
          link_list.removeAll();
          
          URL a;
          URLConnection conn;
      a = new URL(url_string);
      conn = a.openConnection();
      conn.setUseCaches(false);
      BufferedReader dis = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String inputLine = "";
      while ((inputLine = dis.readLine())!= null)
      {
        int delimiter_pos = inputLine.indexOf(delimiter);
        add_item(inputLine.substring(0, delimiter_pos), inputLine.substring(delimiter_pos + 1));
      }
      dis.close();
    }
            catch (Exception e)
            {
              e.printStackTrace();
                  return false;
            }
            return true;
      }
      public boolean save_file()
      {
    try
    {
          String file, url_string;
          URL cb = getCodeBase();
          file_name = filename.getText();

          if (local_mode) //& Windows
          {
                file = cb.getFile();
                file = file.substring(1);
/*        File rec = new File(file + file_name);

        FileOutputStream fos = new FileOutputStream(rec);
*/        
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
       
        //now preparing the content
        String[] klist = keyword_list.getItems();
        String[] llist = link_list.getItems();
       
        for (int i = 0; i < klist.length; i++)
          pw.println(klist[i] + delimiter + llist[i]);
        pw.flush();
        pw.close();
        String content = sw.toString();
        ResultFrame rf = new ResultFrame(content);
        rf.setVisible(true);
/*
        try
        {
          Clipboard clip = this.getToolkit().getSystemClipboard();

          if (clip == null)
            return false;

          StringSelection ss = new StringSelection(content);
          clip.setContents(ss, this);
        }
        catch(Exception e)
        {
        //do smthng.    
        }
  */      
          }
          else
          {
            return false;
          }
    }
    catch (Exception e)
    {
      e.printStackTrace();
      return false;
    }
    return true;
      }
      public void add_item(String key, String l)
      {
        boolean found = false;

        String[] items = keyword_list.getItems();
        int left = 0, right = items.length - 1, mid = 0;
        int res = 0;
        while (left <= right) //simple binary search
        {
          mid = left + (int)((right - left) / 2);

      res = key.compareTo(items[mid]);
      if (res == 0)
      {
        found = true;
        break;
      }
      else
      {
        if (res > 0)
        {
          left = mid + 1;  
        }
        else
        {
          right = mid - 1;
        }
          }
        }
        if (found)
        {
          link_list.replaceItem(l, mid);
        }
        else
        {
          if ((res > 0) && (mid == items.length - 1))
            mid++;
          keyword_list.addItem(key, mid);
          link_list.addItem(l, mid);
        }
      }
      public void delete_item(String key)
      {
        int sk = keyword_list.getSelectedIndex();
        if (sk >= 0)
        {
          keyword_list.remove(sk);
          link_list.remove(sk);
          keyword.setText("");
          link.setText("");
        }
      }
      public void disable_all()
      {
        Component[] clist = getComponents();  
        for (int i = 0; i < clist.length; i++)
          clist[i].disable();
      }
      public void enable_all()
      {
        Component[] clist = getComponents();  
        for (int i = 0; i < clist.length; i++)
          clist[i].enable();
      }
      public void message(String msg)
      {
    try
    {
      MsgFrame frm = new MsgFrame("Information", msg);
      frm.setVisible(true);
    }
    catch (Exception e)
    {
       
    }
      }

      class SymMouse extends java.awt.event.MouseAdapter
      {
            public void mouseClicked(java.awt.event.MouseEvent event)
            {
                  Object object = event.getSource();
                  if (object == keyword_list)
                        keywordList_MouseClicked(event);
                  else if (object == link_list)
                        linkList_MouseClicked(event);
            }
      }

      void keywordList_MouseClicked(java.awt.event.MouseEvent event)
      {
            // to do: code goes here.
            int ksel = keyword_list.getSelectedIndex();
            if (ksel >= 0)
            {
              link_list.select(ksel);
              link_list.makeVisible(ksel);
        }
      }

      void linkList_MouseClicked(java.awt.event.MouseEvent event)
      {
            // to do: code goes here.
            int lsel = link_list.getSelectedIndex();
            if (lsel >= 0)
            {
              keyword_list.select(lsel);
              keyword_list.makeVisible(lsel);
        }
      }


      class SymMouseMotion extends java.awt.event.MouseMotionAdapter
      {
            public void mouseDragged(java.awt.event.MouseEvent event)
            {
                  Object object = event.getSource();
                  if (object == keyword_list)
                        keywordList_MouseDragged(event);
                  else if (object == link_list)
                        linkList_MouseDragged(event);
            }
      }

      void keywordList_MouseDragged(java.awt.event.MouseEvent event)
      {
            // to do: code goes here.
            int ksel = keyword_list.getSelectedIndex();
            if (ksel >= 0)
            {
              link_list.select(ksel);
              link_list.makeVisible(ksel);
        }
      }

      void linkList_MouseDragged(java.awt.event.MouseEvent event)
      {
            // to do: code goes here.
            int lsel = link_list.getSelectedIndex();
            if (lsel >= 0)
            {
              keyword_list.select(lsel);
              keyword_list.makeVisible(lsel);
        }
      }
  public static void main(String[] args)
  {
    //this can be implemented later
  }
}

----------------------------------------


Cheers,
  Nik
Thanks for the good work, Nik.