Package net.sf.jpluck.apps.jpluckx.ui

Source Code of net.sf.jpluck.apps.jpluckx.ui.PatternPanel$EditAction

package net.sf.jpluck.apps.jpluckx.ui;

import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.jgoodies.forms.builder.ButtonStackBuilder;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.FormLayout;


public class PatternPanel extends JPanel {
  private EditAction editAction = new EditAction();
  private JDialog owner;
  private List patternList = new ArrayList();
  private PatternListModel model = new PatternListModel();
  private JList list = new JList(model);
  private RemoveAction removeAction = new RemoveAction();
  private AddAction addAction = new AddAction();

  public PatternPanel(JDialog owner) {
    this.owner = owner;
    initComponents();
  }

  private void initComponents() {
    list.addListSelectionListener(editAction);
    list.addListSelectionListener(removeAction);
    list.setVisibleRowCount(8)
   
    ButtonStackBuilder buttonBuilder = new ButtonStackBuilder();
    buttonBuilder.addGridded(new JButton(addAction));
    buttonBuilder.addRelatedGap();
    buttonBuilder.addGridded(new JButton(editAction));
    buttonBuilder.addGlue();
    buttonBuilder.addGridded(new JButton(removeAction));

    FormLayout formLayout = new FormLayout("pref:grow, 4dlu, pref", "fill:pref:grow");
    PanelBuilder panelBuilder = new PanelBuilder(this, formLayout);
    panelBuilder.add(new JScrollPane(list), "1,1");
    panelBuilder.add(buttonBuilder.getPanel(), "3,1");
   
    getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("INSERT"), "add");
    getActionMap().put("add", new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        addAction.actionPerformed(e);
      }
    });
    list.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e) && editAction.isEnabled()) {
          editAction.actionPerformed(null);
        }
      }
    });
    /*getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ENTER"), "edit");
    getActionMap().put("edit", new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        editAction.actionPerformed(e);
      }
    });*/
    getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("DELETE"), "remove");
    getActionMap().put("remove", new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        removeAction.actionPerformed(e);
      }
    });

  }
 
  private class AddAction extends AbstractAction {
    AddAction() {
      super("Add...");
    }

    public void actionPerformed(ActionEvent e) {
      String pattern = PatternDialog.addPattern(owner, list);
      if (pattern != null) {
        if (patternList.contains(pattern)) {
          list.setSelectedIndex(patternList.indexOf(pattern));
        } else {
          model.add(pattern);
          list.setSelectedIndex(patternList.size()-1);
        }
      }
    }
  }

  private class EditAction extends AbstractAction implements ListSelectionListener {
    EditAction() {
      super("Edit...");
      setEnabled(false);
    }

    public void actionPerformed(ActionEvent e) {
      String pattern = PatternDialog.editPattern(owner, list, (String) list.getSelectedValue());
      if (pattern != null) {
        int idx = list.getSelectedIndex();
        patternList.set(idx, pattern);
        model.edited(idx);
      }
    }

    public void valueChanged(ListSelectionEvent e) {
      setEnabled(list.getSelectedIndices().length==1);
    }
  }

  private class PatternListModel extends AbstractListModel {
    public Object getElementAt(int index) {
      return patternList.get(index);
    }

    public int getSize() {
      return patternList.size();
    }

    void add(String pattern) {
      patternList.add(pattern);
      fireIntervalAdded(this, patternList.size() - 1, patternList.size() - 1);
    }

    void edited(int index) {
      fireContentsChanged(this, index, index);
    }

    void remove(int index) {
      patternList.remove(index);
      fireIntervalRemoved(this, index, index);
    }
   
    void removed() {
      fireContentsChanged(this, 0, patternList.size()-1);
    }
  }

  private class RemoveAction extends AbstractAction implements ListSelectionListener {
    RemoveAction() {
      super("Remove...");
      setEnabled(false);
    }

    public void actionPerformed(ActionEvent e) {
      int result = JOptionPane.showConfirmDialog(list, "Remove selected pattern(s)?",
                             "Remove URL Pattern(s)", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
      if (result == JOptionPane.YES_OPTION) {
        patternList.removeAll(Arrays.asList(list.getSelectedValues()));
        model.removed();
        list.clearSelection();
      }
    }

    public void valueChanged(ListSelectionEvent e) {
      setEnabled(list.getSelectedIndices().length>0);
    }
  }
 
  public void addPattern(String pattern) {
    model.add(pattern);
  }
 
  public String[] getPatterns() {
    return (String[])patternList.toArray(new String[patternList.size()]);
  }
}
TOP

Related Classes of net.sf.jpluck.apps.jpluckx.ui.PatternPanel$EditAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.