org.htmlparser.parserapplications.filterbuilder

Class FilterBuilder

Implemented Interfaces:
ActionListener, ClipboardOwner, DragGestureListener, DragSourceListener, DropTargetListener, MouseListener, MouseMotionListener, WindowListener

public class FilterBuilder
extends JFrame
implements WindowListener, ActionListener, MouseListener, MouseMotionListener, DragGestureListener, DragSourceListener, DropTargetListener, ClipboardOwner

The main program for the FilterBuilder programming system.

ToDo:

Field Summary

protected Point
mBasePoint
The relative position of the mouse while dragging.
protected Component
mCurrentComponent
Kludge: Used by actionPerformed/filterAction to remember the filter menu item.
protected DragSource
mDragSource
Enables this component to be a Drag Source.
protected DropTarget
mDropTarget
This component is a drop target.
protected JPanel
mMainPanel
The main panel GUI component.
protected JScrollPane
mMainScroller
The main panel scrolling GUI component.
protected boolean
mMoved
If true selection moved.
protected JDesktopPane
mOutput
The output panel GUI component.
protected Vector
mSelection
Selected commands.
protected JTextField
mURLField
The URL input GUI component.

Constructor Summary

FilterBuilder()
Create an FilterBuilder programming environment.
FilterBuilder(String title)
Creates a new instance of an FilterBuilder environment with the given title.

Method Summary

protected void
aboutAction()
The action to take when "About" menu or button pressed.
void
actionPerformed(ActionEvent event)
Handles menu and toolbar item choices.
void
addFilter(JMenu menu, JToolBar toolbar, String class_name)
Add a filter to the GUI.
protected void
addSelection(Filter filter)
Add a filter to the current selection set.
protected String
classFromFile(String file)
Extracts a java class name from a file name.
protected void
clearSelection()
Clear (empty) the current selection set.
void
collapseAction()
The action to take when "Collapse" menu chosen.
void
collapseAllAction()
The action to take when "Collapse" menu chosen.
protected void
copyAction()
The action to take when "Copy" menu or button pressed.
protected void
cutAction()
The action to take when "Cut" menu or button pressed.
protected void
deleteAction()
The action to take when "Delete" menu or button pressed.
void
deleteSelection()
Delete the current selection set from the filters in the GUI.
void
dragDropEnd(DragSourceDropEvent event)
This message goes to DragSourceListener, informing it that the dragging has ended.
void
dragEnter(DragSourceDragEvent event)
This message goes to DragSourceListener, informing it that the dragging has entered the DropSite.
void
dragEnter(DropTargetDragEvent event)
This is invoked when you are dragging over the DropSite.
void
dragExit(DragSourceEvent event)
This message goes to DragSourceListener, informing it that the dragging has exited the DropSite.
void
dragExit(DropTargetEvent event)
Thi ss invoked when you are exit the DropSite without dropping.
void
dragGestureRecognized(DragGestureEvent event)
A DragGestureRecognizer has detected a platform-dependent drag initiating gesture.
void
dragOver(DragSourceDragEvent event)
This message goes to DragSourceListener, informing it that the dragging is currently ocurring over the DropSite.
void
dragOver(DropTargetDragEvent event)
This is invoked when a drag operation is going on.
void
drop(DropTargetDropEvent event)
This is invoked when a drop has occurred.
void
dropActionChanged(DragSourceDragEvent event)
This is invoked when the user changes the dropAction.
void
dropActionChanged(DropTargetDragEvent event)
This is invoked if the user modifies the current drop gesture.
protected void
executeAction()
The action to take when "Execute" menu or button pressed.
protected void
exitAction()
The action to take when "Exit" menu or button pressed.
void
expandAction()
The action to take when "Expand" menu chosen.
void
expandAllAction()
The action to take when "Expand All" menu chosen.
protected void
fetchAction()
The action to take when "Fetch" menu pressed.
protected void
filterAction()
The action to take when a filter menu or button pressed.
protected void
filteringAction()
The action to take when "Filtering" menu pressed.
protected Point
getBasePoint()
Gets the current base point of the mouse pointer.
protected SubFilterList
getEnclosed(Component component)
Get the enclosed sub filter list if any.
protected SubFilterList
getEnclosing(Component component)
Get the enclosing sub filter list if any.
Filter[]
getFilters()
Retrieve the top level filters in the main window.
protected Filter[]
getSelection()
Return the current selection set as an array.
protected URL
getURL(String spec)
Get a url for the given resource specification.
protected void
hintsAction()
The action to take when "Hints" menu pressed.
protected void
insertFilters(Filter[] filters, Point point, SubFilterList list)
Adds a set of filters to the main panel or a sublist.
protected void
instructionsAction()
The action to take when "Instructions" menu pressed.
boolean
isSingleSelection()
Check if there is more than one filter selected.
protected Filter
lastSelected()
Return the last filter added to the selection set.
void
lostOwnership(Clipboard clipboard, Transferable contents)
Notifies this object that it is no longer the owner of the contents of the clipboard.
static void
main(args[] )
The entry point for this application.
JButton
makeFilterButton(String class_name)
Creates a new button for the given class.
protected void
makeMenuButton(String name, String description, String text, int mnemonic, KeyStroke key, JToolBar toolbar, JMenu menu)
Makes menu and toolbar items for commands.
protected void
makeProgram(String name, StringBuffer out, FilterBean bean)
Makes a program like:
 // Generated by FilterBuilder.
void
mouseClicked(MouseEvent event)
Invoked when the mouse has been clicked on a component.
void
mouseDragged(MouseEvent event)
Mouse drag notification.
void
mouseEntered(MouseEvent event)
Invoked when the mouse enters a component.
void
mouseExited(MouseEvent event)
Invoked when the mouse exits a component.
void
mouseMoved(MouseEvent event)
Mouse move notification.
void
mousePressed(MouseEvent event)
Invoked when a mouse button has been pressed on a component.
void
mouseReleased(MouseEvent event)
Invoked when a mouse button has been released on a component.
protected void
moveSelection(Point translation)
Move the current selection set as a group.
protected void
newAction()
The action to take when "New" menu or button pressed.
void
open(String name)
Read a workspace from file.
protected void
openAction()
The action to take when "Open" menu or button pressed.
protected void
pasteAction()
The action to take when "Paste" menu or button pressed.
void
relayout()
Redo the layout.
protected void
removeSelection(Filter filter)
Remove a filter from the current selection set.
void
save(String name)
Save the workspace contents to file.
protected void
saveAction()
The action to take when "Save" menu or button pressed.
protected void
saveasAction()
The action to take when "Save As" menu or button pressed.
protected void
selectSelection(boolean select)
Select(highlight)/deselect the current selection set.
protected boolean
selectionContains(Filter filter)
Check if the current selection set contains the given filter.
String
serializeSelection()
Serialize the current selection set.
protected void
setBasePoint(Point point)
Sets the position of the mouse in the component.
void
setExpanded(Filter[] filters, boolean expanded, boolean recursive)
Expand or collapse filters, possibly recursively.
void
setupDropTargets(Filter[] filters)
Set up drop targets.
void
setupMouseListeners(Filter[] filters)
Set up mouse listeners.
void
showContextMenu(MouseEvent event)
Show a pop up context menu.
protected void
tutorialAction()
The action to take when "Tutorial" menu pressed.
void
windowActivated(WindowEvent event)
Invoked when the window is set to be the user's active window, which means the window (or one of its subcomponents) will receive keyboard events.
void
windowClosed(WindowEvent event)
Invoked when a window has been closed as the result of calling dispose on the window.
void
windowClosing(WindowEvent event)
Handles window closing event.
void
windowDeactivated(WindowEvent event)
Invoked when a window is no longer the user's active window, which means that keyboard events will no longer be delivered to the window or its subcomponents.
void
windowDeiconified(WindowEvent event)
Invoked when a window is changed from a minimized to a normal state.
void
windowIconified(WindowEvent event)
Invoked when a window is changed from a normal to a minimized state.
void
windowOpened(WindowEvent event)
Invoked the first time a window is made visible.

Field Details

mBasePoint

protected Point mBasePoint
The relative position of the mouse while dragging.

mCurrentComponent

protected Component mCurrentComponent
Kludge: Used by actionPerformed/filterAction to remember the filter menu item.

mDragSource

protected DragSource mDragSource
Enables this component to be a Drag Source.

mDropTarget

protected DropTarget mDropTarget
This component is a drop target.

mMainPanel

protected JPanel mMainPanel
The main panel GUI component.

mMainScroller

protected JScrollPane mMainScroller
The main panel scrolling GUI component.

mMoved

protected boolean mMoved
If true selection moved.

mOutput

protected JDesktopPane mOutput
The output panel GUI component.

mSelection

protected Vector mSelection
Selected commands.

mURLField

protected JTextField mURLField
The URL input GUI component.

Constructor Details

FilterBuilder

public FilterBuilder()
Create an FilterBuilder programming environment.

FilterBuilder

public FilterBuilder(String title)
Creates a new instance of an FilterBuilder environment with the given title.
Parameters:
title - the title for the new frame.

Method Details

aboutAction

protected void aboutAction()
The action to take when "About" menu or button pressed.

actionPerformed

public void actionPerformed(ActionEvent event)
Handles menu and toolbar item choices.
Parameters:
event - The action even that triggers this function.

addFilter

public void addFilter(JMenu menu,
                      JToolBar toolbar,
                      String class_name)
Add a filter to the GUI. Adds the filter specified by class_name to the menu, toolbar and starts listening for actions.
Parameters:
menu - The menu to add the filter to.
toolbar - The toolbar to add the filter to.
class_name - The class name for the filter wrapper. From the wrapper, the NodeFilter, description and icon can be obtained.

addSelection

protected void addSelection(Filter filter)
Add a filter to the current selection set.
Parameters:
filter - The filter to add.

classFromFile

protected String classFromFile(String file)
Extracts a java class name from a file name. ToDo: make this package-smart somehow.
Parameters:
file - The name of the file.
Returns:
The name of the class.

clearSelection

protected void clearSelection()
Clear (empty) the current selection set.

collapseAction

public void collapseAction()
The action to take when "Collapse" menu chosen.

collapseAllAction

public void collapseAllAction()
The action to take when "Collapse" menu chosen.

copyAction

protected void copyAction()
The action to take when "Copy" menu or button pressed.

cutAction

protected void cutAction()
The action to take when "Cut" menu or button pressed.

deleteAction

protected void deleteAction()
The action to take when "Delete" menu or button pressed.

deleteSelection

public void deleteSelection()
Delete the current selection set from the filters in the GUI.

dragDropEnd

public void dragDropEnd(DragSourceDropEvent event)
This message goes to DragSourceListener, informing it that the dragging has ended.
Parameters:
event - Details about the drop event.

dragEnter

public void dragEnter(DragSourceDragEvent event)
This message goes to DragSourceListener, informing it that the dragging has entered the DropSite.
Parameters:
event - Details about the drag event.

dragEnter

public void dragEnter(DropTargetDragEvent event)
This is invoked when you are dragging over the DropSite.
Parameters:
event - Details about the drag event.

dragExit

public void dragExit(DragSourceEvent event)
This message goes to DragSourceListener, informing it that the dragging has exited the DropSite.
Parameters:
event - Details about the drag event.

dragExit

public void dragExit(DropTargetEvent event)
Thi ss invoked when you are exit the DropSite without dropping.
Parameters:
event - Details about the drag event.

dragGestureRecognized

public void dragGestureRecognized(DragGestureEvent event)
A DragGestureRecognizer has detected a platform-dependent drag initiating gesture. It is notifying this listener in order for it to initiate the action for the user.
Parameters:
event - The DragGestureEvent describing the gesture that has just occurred.

dragOver

public void dragOver(DragSourceDragEvent event)
This message goes to DragSourceListener, informing it that the dragging is currently ocurring over the DropSite.
Parameters:
event - Details about the drag event.

dragOver

public void dragOver(DropTargetDragEvent event)
This is invoked when a drag operation is going on.
Parameters:
event - Details about the drag event.

drop

public void drop(DropTargetDropEvent event)
This is invoked when a drop has occurred.
Parameters:
event - The drop event.

dropActionChanged

public void dropActionChanged(DragSourceDragEvent event)
This is invoked when the user changes the dropAction.
Parameters:
event - Details about the drop action event.

dropActionChanged

public void dropActionChanged(DropTargetDragEvent event)
This is invoked if the user modifies the current drop gesture.
Parameters:
event - Details about the drop action change event.

executeAction

protected void executeAction()
The action to take when "Execute" menu or button pressed.

exitAction

protected void exitAction()
The action to take when "Exit" menu or button pressed.

expandAction

public void expandAction()
The action to take when "Expand" menu chosen.

expandAllAction

public void expandAllAction()
The action to take when "Expand All" menu chosen.

fetchAction

protected void fetchAction()
The action to take when "Fetch" menu pressed.

filterAction

protected void filterAction()
The action to take when a filter menu or button pressed.

filteringAction

protected void filteringAction()
The action to take when "Filtering" menu pressed.

getBasePoint

protected Point getBasePoint()
Gets the current base point of the mouse pointer. This value is used to offset the drag position to maintain the mouse position at the same relative position within the card while dragging.
Returns:
The current base point of the mouse pointer.

getEnclosed

protected SubFilterList getEnclosed(Component component)
Get the enclosed sub filter list if any.
Parameters:
component - The component that's supposedly enclosing the list.
Returns:
The enclosed component or null otherwise.

getEnclosing

protected SubFilterList getEnclosing(Component component)
Get the enclosing sub filter list if any.
Parameters:
component - The component that's supposedly enclosed.
Returns:
The enclosing component or null otherwise.

getFilters

public Filter[] getFilters()
Retrieve the top level filters in the main window.
Returns:
The top level filters.

getSelection

protected Filter[] getSelection()
Return the current selection set as an array.
Returns:
The array of selected filters.

getURL

protected URL getURL(String spec)
            throws MalformedURLException
Get a url for the given resource specification.
Parameters:
spec - The name of the resource.
Returns:
The fully formed URL.

hintsAction

protected void hintsAction()
The action to take when "Hints" menu pressed.

insertFilters

protected void insertFilters(Filter[] filters,
                             Point point,
                             SubFilterList list)
Adds a set of filters to the main panel or a sublist. Sets up the GUI components as drop targets and mouse listeners, and performs a relayout to display them.
Parameters:
filters - The filter wrappers to add.
point - The point at which to start adding (list == null).
list - The list to add to (point not used), or null for the main panel.

instructionsAction

protected void instructionsAction()
The action to take when "Instructions" menu pressed.

isSingleSelection

public boolean isSingleSelection()
Check if there is more than one filter selected.
Returns:
true if only one filter is selected, false otherwise.

lastSelected

protected Filter lastSelected()
Return the last filter added to the selection set.
Returns:
The last filter added or null if the current selection set is empty.

lostOwnership

public void lostOwnership(Clipboard clipboard,
                          Transferable contents)
Notifies this object that it is no longer the owner of the contents of the clipboard.
Parameters:
clipboard - The clipboard that is no longer owned.
contents - The contents which this owner had placed on the clipboard.

main

public static void main(args[] )
The entry point for this application. Creates a new FilterBuilder and makes it visible.
Parameters:

makeFilterButton

public JButton makeFilterButton(String class_name)
Creates a new button for the given class.
Parameters:
class_name - The name of the Filter class.
Returns:
A fully functional button with name, tool tip, icon and drag recognizer.

makeMenuButton

protected void makeMenuButton(String name,
                              String description,
                              String text,
                              int mnemonic,
                              KeyStroke key,
                              JToolBar toolbar,
                              JMenu menu)
Makes menu and toolbar items for commands.
Parameters:
name - The name of the command.
description - A description for the tooltip.
text - The text for the menu.
mnemonic - The navigation mnemonic.
key - Accelerator key.
toolbar - The toolbar to add the button to.
menu - The menu to add the menu item to.

makeProgram

protected void makeProgram(String name,
                           StringBuffer out,
                           FilterBean bean)
Makes a program like:
 // Generated by FilterBuilder. http://htmlparser.org
 // [aced0005737200206f72672e68746d6c7061727365722e66696c746572732e416e6446696c74657224c30516b2b7b2120200015b000b6d5072656469636174657374001c5b4c6f72672f68746d6c7061727365722f4e6f646546696c7465723b78707572001c5b4c6f72672e68746d6c7061727365722e4e6f646546696c7465723b8f17479b1d5f7992020000787000000002737200246f72672e68746d6c7061727365722e66696c746572732e5461674e616d6546696c746572b28b2601a614890f0200014c00056d4e616d657400124c6a6176612f6c616e672f537472696e673b78707400044d455441737200296f72672e68746d6c7061727365722e66696c746572732e48617341747472696275746546696c74657296abdfb3b0714cda0200024c000a6d41747472696275746571007e00064c00066d56616c756571007e000678707400046e616d6570]
                                                                                                                                                         
 import org.htmlparser.*;
 import org.htmlparser.filters.*;
 import org.htmlparser.beans.*;
 import org.htmlparser.util.*;
                                                                                                                                                         
 public class Test
 {
     public static void main (String args[])
     {
         TagNameFilter filter0 = new TagNameFilter ();
         filter0.setName ("META");
         HasAttributeFilter filter1 = new HasAttributeFilter ();
         filter1.setAttributeName ("name");
         NodeFilter[] array0 = new NodeFilter[2];
         array0[0] = filter0;
         array0[1] = filter1;
         AndFilter filter2 = new AndFilter ();
         filter2.setPredicates (array0);
         NodeFilter[] array1 = new NodeFilter[1];
         array1[0] = filter2;
         FilterBean bean = new FilterBean ();
         bean.setFilters (array1);
         if (0 != args.length)
         {
             bean.setURL (args[0]);
             System.out.println (bean.getNodes ().toHtml ());
         }
         else
             System.out.println ("Usage: java -classpath .:htmlparser.jar Test ");
     }
 }
 
Parameters:
name - The name of the class.
out - The buffer to append to.
bean - The bean to extract the filters from to make the program.

mouseClicked

public void mouseClicked(MouseEvent event)
Invoked when the mouse has been clicked on a component.
Parameters:
event - The mouse clicked event.

mouseDragged

public void mouseDragged(MouseEvent event)
Mouse drag notification. Invoked when a mouse button is pressed on a component and then dragged. Mouse drag events will continue to be delivered to the component where the first originated until the mouse button is released (regardless of whether the mouse position is within the bounds of the component).
Parameters:
event - The mouse drag event.

mouseEntered

public void mouseEntered(MouseEvent event)
Invoked when the mouse enters a component.
Parameters:
event - The mouse entered event.

mouseExited

public void mouseExited(MouseEvent event)
Invoked when the mouse exits a component.
Parameters:
event - The mouse exited event.

mouseMoved

public void mouseMoved(MouseEvent event)
Mouse move notification. Invoked when the mouse button has been moved on a component (with no buttons no down).
Parameters:
event - The mouse moved event.

mousePressed

public void mousePressed(MouseEvent event)
Invoked when a mouse button has been pressed on a component.
Parameters:
event - The mouse pressed event.

mouseReleased

public void mouseReleased(MouseEvent event)
Invoked when a mouse button has been released on a component.
Parameters:
event - The mouse released event.

moveSelection

protected void moveSelection(Point translation)
Move the current selection set as a group.
Parameters:
translation - The displacement to move them all by.

newAction

protected void newAction()
The action to take when "New" menu or button pressed.

open

public void open(String name)
Read a workspace from file. The current contents are erased.
Parameters:
name - The name of the file to open.

openAction

protected void openAction()
The action to take when "Open" menu or button pressed.

pasteAction

protected void pasteAction()
The action to take when "Paste" menu or button pressed.

relayout

public void relayout()
Redo the layout.

removeSelection

protected void removeSelection(Filter filter)
Remove a filter from the current selection set.
Parameters:
filter - The filter to remove.

save

public void save(String name)
Save the workspace contents to file.
Parameters:
name - The name of the file to save it under.

saveAction

protected void saveAction()
The action to take when "Save" menu or button pressed.

saveasAction

protected void saveasAction()
The action to take when "Save As" menu or button pressed.

selectSelection

protected void selectSelection(boolean select)
Select(highlight)/deselect the current selection set.
Parameters:
select - If true turn on highlighting, turn it off otherwise.

selectionContains

protected boolean selectionContains(Filter filter)
Check if the current selection set contains the given filter.
Parameters:
filter - The filter to check.
Returns:
true if the filter is a member, false otherwise.

serializeSelection

public String serializeSelection()
Serialize the current selection set.
Returns:
The serialized form of the set of filters.

setBasePoint

protected void setBasePoint(Point point)
Sets the position of the mouse in the component.
Parameters:
point - The point where the mouse position is.

setExpanded

public void setExpanded(Filter[] filters,
                        boolean expanded,
                        boolean recursive)
Expand or collapse filters, possibly recursively.
Parameters:
filters - The list of filters to expand or collapse.
expanded - If true the filters are expanded, otherwise they are collapsed.
recursive - If true the filters are processed recursively.

setupDropTargets

public void setupDropTargets(Filter[] filters)
Set up drop targets. Recursively descends the filter tree and sets up the filter lists as drop targets.
Parameters:
filters - The container with filters in it.

setupMouseListeners

public void setupMouseListeners(Filter[] filters)
Set up mouse listeners. Sets this up to listen to each command in the list as a MouseListener. Recursively descends the tree adding to all contained elements also.
Parameters:
filters - The container with commands in it.

showContextMenu

public void showContextMenu(MouseEvent event)
Show a pop up context menu. Shows a context sensitive popup menu at the location of the mouse event.
Parameters:
event - The mouse event that initiates the popup.

tutorialAction

protected void tutorialAction()
The action to take when "Tutorial" menu pressed.

windowActivated

public void windowActivated(WindowEvent event)
Invoked when the window is set to be the user's active window, which means the window (or one of its subcomponents) will receive keyboard events. Not used.
Parameters:
event - The window event.

windowClosed

public void windowClosed(WindowEvent event)
Invoked when a window has been closed as the result of calling dispose on the window. Not used.
Parameters:
event - The window event.

windowClosing

public void windowClosing(WindowEvent event)
Handles window closing event. Performs function exitApplication().
Parameters:
event - The window event.

windowDeactivated

public void windowDeactivated(WindowEvent event)
Invoked when a window is no longer the user's active window, which means that keyboard events will no longer be delivered to the window or its subcomponents. Not used.
Parameters:
event - The window event.

windowDeiconified

public void windowDeiconified(WindowEvent event)
Invoked when a window is changed from a minimized to a normal state. Not used.
Parameters:
event - The window event.

windowIconified

public void windowIconified(WindowEvent event)
Invoked when a window is changed from a normal to a minimized state. For many platforms, a minimized window is displayed as the icon specified in the window's iconImage property. Not used.
Parameters:
event - The window event.

windowOpened

public void windowOpened(WindowEvent event)
Invoked the first time a window is made visible. Not used.
Parameters:
event - The window event.

HTML Parser is an open source library released under LGPL. SourceForge.net