www.pudn.com > htmlsaver.rar > SetFileNameOfURL.java


import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.*; 
 
public class SetFileNameOfURL 
{ 
  protected String url; 
  protected GetNameFromURL get; 
   
  protected JList oldWords; 
  protected JList newWords; 
  protected DefaultListModel newWordsModel; 
   
  protected JTextField newValue; 
  protected JButton modify; 
   
  protected JList parts; 
  protected DefaultListModel partsModel; 
  protected JTextField name; 
  protected JRadioButton isString; 
  protected JRadioButton isWord; 
  protected JTextField text; 
   
  protected JButton append; 
  protected JButton insert; 
  protected JButton delete; 
   
  protected JDialog dialog; 
 
  protected JButton ok; 
  protected JButton cancel; 
   
  protected boolean isFinish; 
   
  SetFileNameOfURL(GetNameFromURL get, JDialog owner) 
  { 
    this.get = get; 
    this.url = get.getURL(); 
    get.clear(); 
     
    Vector words = getWords(); 
    oldWords = new JList(words); 
    newWordsModel = new DefaultListModel(); 
    int i; 
    for (i = 0; i < words.size(); i++) 
      newWordsModel.addElement(words.elementAt(i)); 
    newWords = new JList(newWordsModel); 
     
    isFinish = false; 
     
    dialog = new JDialog(owner, "Set File Name Of URL", true); 
 
    initDialog(); 
  } 
   
  public String getNameOf(String url) 
  { 
    if (isFinish) 
      return get.getNameOf(url); 
    else 
      return null; 
  } 
   
  public boolean isFinished() 
  { 
    return isFinish; 
  } 
   
  Vector getWords() 
  { 
    Vector words = new Vector(); 
    Vector wordsOfURL = get.getWords(); 
     
    int i; 
    for (i = 0; i < wordsOfURL.size(); i++) 
    { 
      WordOfURL w = (WordOfURL)wordsOfURL.elementAt(i); 
      words.add(w.getValue()); 
    } 
    return words; 
  } 
   
  void initDialog() 
  { 
    dialog.getContentPane().setLayout(new BorderLayout()); 
     
    dialog.getContentPane().add(new JLabel("URL: " + url), BorderLayout.NORTH); 
     
    JPanel cenPanel = new JPanel(); 
    cenPanel.setLayout(new GridLayout(1, 2, 5, 5)); 
     
    JPanel leftPanel = new JPanel(); 
    leftPanel.setLayout(new BorderLayout()); 
     
    JPanel panel = new JPanel(); 
    panel.setLayout(new GridLayout(1, 2)); 
    panel.add(initList(oldWords, "Old Words")); 
    panel.add(initList(newWords, "New Words")); 
    oldWords.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    newWords.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     
    leftPanel.add(panel, BorderLayout.CENTER); 
     
    panel = new JPanel(); 
    panel.setLayout(new BorderLayout()); 
    panel.add(new JLabel("New value:"), BorderLayout.WEST); 
    newValue = new JTextField("", 10); 
    panel.add(newValue, BorderLayout.CENTER); 
    modify = new JButton("Modify"); 
    modify.addActionListener(new WordAction()); 
    panel.add(modify, BorderLayout.EAST); 
     
    leftPanel.add(panel, BorderLayout.SOUTH); 
     
    cenPanel.add(leftPanel); 
     
    JPanel rightPanel = new JPanel(); 
    rightPanel.setLayout(new BorderLayout()); 
     
    JPanel sidePanel = new JPanel(); 
    sidePanel.setLayout(new GridLayout(1, 2)); 
     
    panel = new JPanel(); 
    //panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
    panel.setLayout(new GridLayout(6, 1)); 
     
    isString = new JRadioButton("Const String"); 
    panel.add(isString); 
    text = new JTextField("const string"); 
    panel.add(text); 
    isWord = new JRadioButton("From Word"); 
    panel.add(isWord); 
     
    ButtonGroup g = new ButtonGroup(); 
    g.add(isString); 
    g.add(isWord); 
     
    append = new JButton("Append"); 
    panel.add(append); 
    insert = new JButton("Insert"); 
    panel.add(insert); 
    delete = new JButton("Delete"); 
    panel.add(delete); 
     
    sidePanel.add(panel); 
     
    append.addActionListener(new PartAction()); 
    insert.addActionListener(new PartAction()); 
    delete.addActionListener(new PartAction()); 
     
    partsModel = new DefaultListModel(); 
    parts = new JList(partsModel); 
    sidePanel.add(initList(parts, "Part of Name")); 
    parts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     
    rightPanel.add(sidePanel, BorderLayout.CENTER); 
     
    panel = new JPanel(); 
    panel.setLayout(new BorderLayout()); 
    panel.add(new JLabel("File Name: "), BorderLayout.WEST); 
    name = new JTextField(""); 
    name.setEditable(false); 
    panel.add(name, BorderLayout.CENTER); 
     
    rightPanel.add(panel, BorderLayout.SOUTH); 
     
    cenPanel.add(rightPanel); 
     
    dialog.getContentPane().add(cenPanel, BorderLayout.CENTER); 
     
    panel = new JPanel(); 
    panel.setLayout(new FlowLayout()); 
    ok = new JButton("OK"); 
    panel.add(ok); 
    cancel = new JButton("Cancel"); 
    panel.add(cancel); 
     
    ok.addActionListener(new ExitAction()); 
    cancel.addActionListener(new ExitAction()); 
     
    dialog.getContentPane().add(panel, BorderLayout.SOUTH); 
     
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
    dialog.setSize(500, 270); 
    dialog.show(); 
  } 
   
  JPanel initList(JList list, String s) 
  { 
    JPanel panel = new JPanel(); 
    panel.setLayout(new BorderLayout()); 
    panel.add(new JLabel(s), BorderLayout.NORTH); 
    panel.add(new JScrollPane(list)); 
     
    return panel; 
  } 
   
  void appendPart() 
  { 
    if (isString.isSelected()) 
    { 
      String s = text.getText(); 
      partsModel.addElement(s); 
      get.addString(s); 
    } 
    else if (isWord.isSelected()) 
    { 
      int i = newWords.getSelectedIndex(); 
      if (i < 0) 
        return; 
         
      partsModel.addElement("Word: " + Integer.toString(i)); 
      get.addWord(i); 
    } 
    name.setText(get.getName()); 
  } 
   
  void insertPart() 
  { 
    int j = parts.getSelectedIndex(); 
    if (j < 0) 
      return; 
     
    if (isString.isSelected()) 
    { 
      String s = text.getText(); 
      partsModel.insertElementAt(s, j); 
      get.insertString(s, j); 
    } 
    else if (isWord.isSelected()) 
    { 
      int i = newWords.getSelectedIndex(); 
      if (i < 0) 
        return; 
      
      partsModel.insertElementAt("Word: " + Integer.toString(i), j); 
      get.insertWord(i, j); 
    } 
    name.setText(get.getName()); 
  } 
   
  void deletePart() 
  { 
    int j = parts.getSelectedIndex(); 
    if (j < 0) 
      return; 
     
    partsModel.remove(j); 
    get.remove(j); 
    name.setText(get.getName()); 
  } 
   
  void modifyWord() 
  { 
    int i = oldWords.getSelectedIndex(); 
    if (i < 0) 
      return; 
    int oldval = Integer.parseInt((String)(oldWords.getModel().getElementAt(i))); 
    int newval = Integer.parseInt(newValue.getText()); 
    DefaultListModel model = (DefaultListModel)newWords.getModel(); 
    model.setElementAt(Integer.toString(newval), i); 
    DigitDistance dis = new DigitDistance(oldval, newval); 
    get.setChangeWord(dis, i); 
    name.setText(get.getName()); 
  } 
   
  class ExitAction implements ActionListener 
  { 
    public void actionPerformed(ActionEvent e) 
    { 
      if (e.getSource() == ok) 
      { 
        isFinish = true; 
      } 
      else if (e.getSource() == cancel) 
      { 
        if (JOptionPane.showConfirmDialog(null, "Really Cancel?", "Quit",  
          JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION) 
        { 
          return; 
        } 
        else 
        { 
          get.clear(); 
        } 
      } 
      dialog.hide(); 
    } 
  } 
   
  class PartAction implements ActionListener 
  { 
    public void actionPerformed(ActionEvent e) 
    { 
      if (e.getSource() == append) 
      { 
        appendPart(); 
      } 
      else if (e.getSource() == insert) 
      { 
        insertPart(); 
      } 
      else if (e.getSource() == delete) 
      { 
        deletePart(); 
      } 
    } 
  } 
   
  class WordAction implements ActionListener 
  { 
    public void actionPerformed(ActionEvent e) 
    { 
      modifyWord(); 
    } 
  } 
   
  public static void main(String[] args) 
  { 
    GetNameFromURL get = new GetNameFromURL(new SystemIn().readLine()); 
    SetFileNameOfURL set = new SetFileNameOfURL(get, new JDialog()); 
  } 
}