// HTMLParser Library $Name: v1_6 $ - A java-based parser for HTML
// http://sourceforge.org/projects/htmlparser
// Copyright (C) 2005 Derrick Oswald
//
// Revision Control Information
//
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/parserapplications/filterbuilder/FilterBuilder.java,v $
// $Author: derrickoswald $
// $Date: 2005/04/12 11:27:42 $
// $Revision: 1.5 $
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
package org.jh.htmlparser.parserapplications.filterbuilder;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetContext;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
//import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
import org.jh.htmlparser.beans.FilterBean;
import org.jh.htmlparser.parserapplications.filterbuilder.layouts.NullLayoutManager;
import org.jh.htmlparser.util.EncodingChangeException;
import org.jh.htmlparser.util.NodeIterator;
import org.jh.htmlparser.util.NodeList;
import org.jh.htmlparser.util.ParserException;
import org.jh.htmlparser.Parser;
/**
* The main program for the FilterBuilder programming system.
* <p>ToDo:
* <ul>
* <li>thread the attribute fetching</li>
* <li>CSS selector filter</li>
* <li>table row filter</li>
* <li>table column filter</li>
* <li>trigger filter</li>
* <li>undo</li>
* <li>handle bad URLs</li>
* <li>StringBean type secondary text output</li>
* <li>context sensitive menus</li>
* </ul>
*/
public class FilterBuilder
extends
JFrame
implements
WindowListener,
ActionListener,
MouseListener,
MouseMotionListener,
DragGestureListener,
DragSourceListener,
DropTargetListener,
ClipboardOwner
{
static final String TITLE = "HTML Parser FilterBuilder";
static final URL mDocumentBase;
static
{
String p;
char ps;
URL base;
p = System.getProperty ("user.dir");
// if the system file separator isn't the URL file separator convert it.
try
{
ps = (System.getProperty ("file.separator")).charAt(0);
if ('/' != ps)
p.replace (ps, '/');
}
catch (StringIndexOutOfBoundsException e)
{
}
try
{
base = new URL ("file:///" + p + "/");
}
catch (MalformedURLException murle)
{
base = null;
}
mDocumentBase = base;
}
static String mHomeDir;
static
{
String dir;
File file;
dir = System.getProperty ("user.home")
+ System.getProperty ("file.separator")
+ ".htmlparser";
file = new File (dir);
if (!file.exists ())
if (!file.mkdirs ()) // make the directory if it doesn't exist
throw new RuntimeException (
"cannot create directory "
+ file.getAbsolutePath ());
mHomeDir = file.getAbsolutePath ();
}
/**
* The relative position of the mouse while dragging.
*/
protected Point mBasePoint;
/**
* Selected commands.
*/
protected Vector mSelection;
/**
* If true selection moved.
*/
protected boolean mMoved;
/**
* This component is a drop target.
*/
protected DropTarget mDropTarget;
/**
* Enables this component to be a Drag Source.
*/
protected DragSource mDragSource;
/**
* Kludge: Used by actionPerformed/filterAction to remember the filter menu item.
*/
protected Component mCurrentComponent;
/**
* The main panel GUI component.
*/
protected JPanel mMainPanel;
/**
* The main panel scrolling GUI component.
*/
protected JScrollPane mMainScroller;
/**
* The URL input GUI component.
*/
protected JTextField mURLField;
/**
* The output panel GUI component.
*/
protected JDesktopPane mOutput;
/**
* Create an FilterBuilder programming environment.
*/
public FilterBuilder ()
{
JMenuBar menubar;
JToolBar toolbar;
JMenu menu;
JPanel panel;
JScrollPane pane;
JSplitPane split;
JMenuItem item;
// drag and drop support
mMainPanel = new JPanel ();
mDropTarget = new DropTarget (mMainPanel, this);
mDragSource = new DragSource ();
// menu and toolbar
menubar = new JMenuBar();
toolbar = new JToolBar ();
toolbar.setAlignmentY (0.222222F);
// file menu
menu = new JMenu ();
menu.setText ("File");
menu.setActionCommand ("File");
menu.setMnemonic ((int)'F');
makeMenuButton ("New", "Create a new document", "New", 'N', KeyStroke.getKeyStroke (KeyEvent.VK_N, Event.CTRL_MASK), toolbar, menu);
makeMenuButton ("Open", "Open an existing document",