CubeTwister 2.0alpha142 2012-02-11

ch.randelshofer.cubetwister.doc
Class DocumentModel

java.lang.Object
  extended by javax.swing.tree.DefaultTreeModel
      extended by ch.randelshofer.cubetwister.doc.DocumentModel
All Implemented Interfaces:
MutableTreeModel, Undoable, java.io.Serializable, javax.swing.tree.TreeModel

public class DocumentModel
extends javax.swing.tree.DefaultTreeModel
implements MutableTreeModel, Undoable

Holds a CubeTwister document. A document is a tree of EntityModels. DocumentModels must be self-contained, that is, the the EntityModels in this DocumentModel must not have a reference to an EntityModel in another DocumentModel.

Version:
3.9 2011-01-22 Use DefaultMutableTreeNode instead of DefaultMutableTreeNode.
3.8.1 2010-04-05 Removed dependencies to IBM Unicode normalizer.
3.8 2010-04-05 Increased version number because the colors are interpreted differently by V-Cube 7.
3.7 2010-03-28 Normalize to NFC instead of NFKC.
3.6 2010-01-09 Method createNodeAt tries harder to find a good insertion location.
3.5 2009-07-05 Skip move tokens with unsupported layerList when reading an XML file.
3.4 2009-01-12 Adapted to enhanced syntax support of notations. Normalize notation identifiers and scripts to Unicode NFC when reading and writing a document file.
3.3 2008-12-25 Support reading of input streams into a preferred parent + index.
3.2.1 2008-12-21 XML attribute "scriptType" was not read from XML file.
3.2 2008-04-28 Store twist duration and background colors in XML file.
3.1 2007-06-16 Renamed XML attribute "move" to "twist".
3.0 2007-06-10 Renamed XML element "Construct" to "Statement". Renamed attribute "generator=true/false" to "scriptType=generator/solver". This is now version 3 of the XML file.
2.0 2006-01-08 Reworked to improve startup latency (use nanoxml instead of DOM and parse XML file progressively).
1.4 2004-04-03 New features to notation added. Current version number of XML files is 2.
1.3 2003-08-03 Version check for XML file added. Current version number is 1.
1.2 2003-06-20 Method getNodeActions added. Action translateInto added.
1.1.1 2002-12-21 Minor changes.
1.1 2002-05-18 Decoupled attribute CubeModel.get/set/Shape from XML Cube.kind attribute. ScriptModel has a new attribute "Generator". Uses now property 'removable' from EntityModel to determine if a node is removable. Class EventDispatcher replaced by SequentialDispatcher. Methods isCloseable and close added.
1.0 2001-10-04 Created.
Author:
Werner Randelshofer
See Also:
Serialized Form

Field Summary
static int CUBE_INDEX
           
static int NOTATION_INDEX
           
static java.lang.String PROP_DEFAULT_CUBE
           
static java.lang.String PROP_DEFAULT_NOTATION
           
protected  java.beans.PropertyChangeSupport propertySupport
           
static int SCRIPT_INDEX
           
static int TEXT_INDEX
           
 
Fields inherited from class javax.swing.tree.DefaultTreeModel
asksAllowsChildren, listenerList, root
 
Fields inherited from interface ch.randelshofer.gui.tree.MutableTreeModel
ENABLED_PROPERTY
 
Constructor Summary
DocumentModel()
          Creates a new DocumentModel.
 
Method Summary
 void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
          Adds a PropertyChangeListener.
 void addPropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
           
 void addSerializedNode(java.io.InputStream in)
          Adds the contents of the input stream to the DocumentModel.
 void addTo(javax.swing.tree.DefaultMutableTreeNode newChild, javax.swing.tree.TreeNode parent)
          Invoked this to add newChild to parents children.
 void addUndoableEditListener(javax.swing.event.UndoableEditListener l)
          Adds an UndoableEditListener.
 void addXMLNode(nanoxml.XMLElement doc)
           
 void close()
          Shuts down all background processes.
 javax.swing.tree.TreePath createNodeAt(java.lang.Object newNodeType, javax.swing.tree.DefaultMutableTreeNode parent, int index)
          Invoke this to insert a new child at location index in parents children.
 void dispatch(java.lang.Runnable runner)
          Dispatches a runnable on the worker thread of the document model.
 void dispatchSolver(java.lang.Runnable runner)
          Dispatches a runnable on the global thread pool of the virtual machine.
 void dispose()
           
 java.awt.datatransfer.Transferable exportTransferable(javax.swing.tree.DefaultMutableTreeNode[] nodes)
          Copies the indicated nodes to the clipboard.
 void fireUndoableEdit(javax.swing.undo.UndoableEdit edit)
          Notify all listeners that have registered interest for notification on this event type.
 java.lang.Object getCreatableNodeType(java.lang.Object parent)
          Returns the default type of children that can be created at the specified node.
 java.lang.Object[] getCreatableNodeTypes(java.lang.Object parent)
          Returns the types of children that may be created at this node.
 EntityModel getCubes()
           
 CubeModel getDefaultCube()
          Gets the default cube.
 NotationModel getDefaultNotation(int layerCount)
          Gets the default notation.
 javax.swing.Action[] getNodeActions(javax.swing.tree.DefaultMutableTreeNode[] nodes)
          Gets actions for the specified nodes.
 EntityModel getNotations()
           
 EntityModel getScripts()
           
 EntityModel getTexts()
           
 java.util.List<javax.swing.tree.TreePath> importTransferable(java.awt.datatransfer.Transferable transfer, int action, javax.swing.tree.DefaultMutableTreeNode parent, int index)
          Causes a transfer to the model from a clipboard or a DND drop operation.
 void insertNodeInto(javax.swing.tree.MutableTreeNode newChild, javax.swing.tree.MutableTreeNode parent, int index)
          Invoked this to add newChild to parents children.
protected  void insertNodeIntoQuiet(javax.swing.tree.DefaultMutableTreeNode newChild, javax.swing.tree.TreeNode parent, int index)
           
 void insertSerializedNodeInto(java.io.InputStream in, javax.swing.tree.DefaultMutableTreeNode parent, int index)
          Adds the contents of the input stream to the DocumentModel.
 void insertSerializedNodeInto(java.io.Reader r, javax.swing.tree.DefaultMutableTreeNode parent, int index)
          Adds the contents of the input stream to the DocumentModel.
 void insertXMLNodeInto(nanoxml.XMLElement doc, javax.swing.tree.DefaultMutableTreeNode parent, int index)
           
 boolean isCloseable()
          Returns true, if no background processes are working on the model.
 boolean isImportable(java.awt.datatransfer.DataFlavor[] flavors, int action, javax.swing.tree.DefaultMutableTreeNode parent, int index)
          Indicates whether the model would accept an import of the given set of data flavors prior to actually attempting to import it.
 boolean isLeaf(java.lang.Object node)
           
 boolean isNodeAddable(javax.swing.tree.DefaultMutableTreeNode parent, int index)
          Returns wether the specified node may be inserted.
 boolean isNodeEditable(javax.swing.tree.DefaultMutableTreeNode node)
          Returns wether the specified node may be renamed.
 boolean isNodeRemovable(javax.swing.tree.DefaultMutableTreeNode node)
          Returns wether the specified node may be removed.
 boolean isWriteMember(EntityModel m)
           
 void removeNodeFromParent(javax.swing.tree.DefaultMutableTreeNode node)
          Message this to remove node from its parent.
protected  void removeNodeFromParentQuiet(javax.swing.tree.DefaultMutableTreeNode node)
           
 void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
          Removes a PropertyChangeListener.
 void removePropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
           
 void removeUndoableEditListener(javax.swing.event.UndoableEditListener l)
          Removes an UndoableEditListener.
 void setDefaultCube(CubeModel newValue)
          Sets the default cube.
 void setDefaultNotation(NotationModel value)
          Sets the default notation.
 void setDispatcher(SequentialDispatcher d)
           
 void writeXML(java.io.OutputStream out, java.lang.Object[] entities)
          Writes the indicated entities of the DocumentModel into the output stream.
 void writeXML(java.io.PrintWriter out)
          Writes the contents of the DocumentModel into the output stream.
 
Methods inherited from class javax.swing.tree.DefaultTreeModel
addTreeModelListener, asksAllowsChildren, fireTreeNodesChanged, fireTreeNodesInserted, fireTreeNodesRemoved, fireTreeStructureChanged, getChild, getChildCount, getIndexOfChild, getListeners, getPathToRoot, getPathToRoot, getRoot, getTreeModelListeners, nodeChanged, nodesChanged, nodeStructureChanged, nodesWereInserted, nodesWereRemoved, reload, reload, removeNodeFromParent, removeTreeModelListener, setAsksAllowsChildren, setRoot, valueForPathChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.tree.TreeModel
addTreeModelListener, getChild, getChildCount, getIndexOfChild, getRoot, removeTreeModelListener, valueForPathChanged
 

Field Detail

CUBE_INDEX

public static final int CUBE_INDEX
See Also:
Constant Field Values

NOTATION_INDEX

public static final int NOTATION_INDEX
See Also:
Constant Field Values

SCRIPT_INDEX

public static final int SCRIPT_INDEX
See Also:
Constant Field Values

TEXT_INDEX

public static final int TEXT_INDEX
See Also:
Constant Field Values

PROP_DEFAULT_CUBE

public static final java.lang.String PROP_DEFAULT_CUBE
See Also:
Constant Field Values

PROP_DEFAULT_NOTATION

public static final java.lang.String PROP_DEFAULT_NOTATION
See Also:
Constant Field Values

propertySupport

protected java.beans.PropertyChangeSupport propertySupport
Constructor Detail

DocumentModel

public DocumentModel()
Creates a new DocumentModel.

Method Detail

setDispatcher

public void setDispatcher(SequentialDispatcher d)

dispatch

public void dispatch(java.lang.Runnable runner)
Dispatches a runnable on the worker thread of the document model.

Note that the worker thread executes the runnables sequentially.

Use this method to dispatch tasks which block the user interface until the task has finished. i.e. Loading and Saving a document from a file.


dispatchSolver

public void dispatchSolver(java.lang.Runnable runner)
Dispatches a runnable on the global thread pool of the virtual machine. The size of the thread pool is determined by the number of available processors for the virtual machine.

Note that the worker thread executes the runnables concurrently.

Use this method to dispatch tasks which do not block the user interface until the task has finished. i.e. Solving a scrambled cube.


getScripts

public EntityModel getScripts()

getTexts

public EntityModel getTexts()

getNotations

public EntityModel getNotations()

getCubes

public EntityModel getCubes()

getDefaultCube

public CubeModel getDefaultCube()
Gets the default cube. The default cube is used by the scripts unless they override it with their own definition.


setDefaultCube

public void setDefaultCube(CubeModel newValue)
Sets the default cube. The default cube is used by the scripts unless they override it with their own definition.


getDefaultNotation

public NotationModel getDefaultNotation(int layerCount)
Gets the default notation. The default notation is used by the scripts unless they override it with their own definition.


setDefaultNotation

public void setDefaultNotation(NotationModel value)
Sets the default notation. The default notation is used by the scripts unless they override it with their own definition.


writeXML

public void writeXML(java.io.OutputStream out,
                     java.lang.Object[] entities)
              throws java.io.IOException
Writes the indicated entities of the DocumentModel into the output stream. For peak performance, the output stream should be buffered.

Throws:
java.io.IOException

isWriteMember

public boolean isWriteMember(EntityModel m)

writeXML

public void writeXML(java.io.PrintWriter out)
              throws java.io.IOException
Writes the contents of the DocumentModel into the output stream. For peak performance, the output stream should be buffered.

Throws:
java.io.IOException

addSerializedNode

public void addSerializedNode(java.io.InputStream in)
                       throws java.io.IOException
Adds the contents of the input stream to the DocumentModel. For peak performance, the input stream should be buffered.

Throws:
java.io.IOException

insertSerializedNodeInto

public void insertSerializedNodeInto(java.io.InputStream in,
                                     javax.swing.tree.DefaultMutableTreeNode parent,
                                     int index)
                              throws java.io.IOException
Adds the contents of the input stream to the DocumentModel. For peak performance, the input stream should be buffered.

Throws:
java.io.IOException

insertSerializedNodeInto

public void insertSerializedNodeInto(java.io.Reader r,
                                     javax.swing.tree.DefaultMutableTreeNode parent,
                                     int index)
                              throws java.io.IOException
Adds the contents of the input stream to the DocumentModel. For peak performance, the input stream should be buffered.

Throws:
java.io.IOException

addXMLNode

public void addXMLNode(nanoxml.XMLElement doc)
                throws java.io.IOException
Throws:
java.io.IOException

insertXMLNodeInto

public void insertXMLNodeInto(nanoxml.XMLElement doc,
                              javax.swing.tree.DefaultMutableTreeNode parent,
                              int index)
                       throws java.io.IOException
Throws:
java.io.IOException

addTo

public void addTo(javax.swing.tree.DefaultMutableTreeNode newChild,
                  javax.swing.tree.TreeNode parent)
Invoked this to add newChild to parents children. This will then message nodesWereInserted to create the appropriate event. This is the preferred way to add children as it will create the appropriate event.


insertNodeInto

public void insertNodeInto(javax.swing.tree.MutableTreeNode newChild,
                           javax.swing.tree.MutableTreeNode parent,
                           int index)
Invoked this to add newChild to parents children. This will then message nodesWereInserted to create the appropriate event. This is the preferred way to add children as it will create the appropriate event.

Overrides:
insertNodeInto in class javax.swing.tree.DefaultTreeModel

insertNodeIntoQuiet

protected void insertNodeIntoQuiet(javax.swing.tree.DefaultMutableTreeNode newChild,
                                   javax.swing.tree.TreeNode parent,
                                   int index)

removeNodeFromParentQuiet

protected void removeNodeFromParentQuiet(javax.swing.tree.DefaultMutableTreeNode node)

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Description copied from interface: MutableTreeModel
Adds a PropertyChangeListener.

Specified by:
addPropertyChangeListener in interface MutableTreeModel

addPropertyChangeListener

public void addPropertyChangeListener(java.lang.String propertyName,
                                      java.beans.PropertyChangeListener listener)

removePropertyChangeListener

public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Description copied from interface: MutableTreeModel
Removes a PropertyChangeListener.

Specified by:
removePropertyChangeListener in interface MutableTreeModel

removePropertyChangeListener

public void removePropertyChangeListener(java.lang.String propertyName,
                                         java.beans.PropertyChangeListener listener)

isNodeRemovable

public boolean isNodeRemovable(javax.swing.tree.DefaultMutableTreeNode node)
Returns wether the specified node may be removed.

Specified by:
isNodeRemovable in interface MutableTreeModel
Parameters:
node - a node from the tree, obtained from this data source.

removeNodeFromParent

public void removeNodeFromParent(javax.swing.tree.DefaultMutableTreeNode node)
Message this to remove node from its parent.

Specified by:
removeNodeFromParent in interface MutableTreeModel
Parameters:
node - a node from the tree, obtained from this data source.
Throws:
java.lang.IllegalStateException - if the node is not removable.

createNodeAt

public javax.swing.tree.TreePath createNodeAt(java.lang.Object newNodeType,
                                              javax.swing.tree.DefaultMutableTreeNode parent,
                                              int index)
                                       throws java.lang.IllegalStateException
Invoke this to insert a new child at location index in parents children. This will then message nodesWereInserted to create the appropriate event. This is the preferred way to add children as it will create the appropriate event.

Specified by:
createNodeAt in interface MutableTreeModel
Parameters:
newNodeType - the type of the new child to be created, obtained from getCreatableChildren
parent - a node from the tree, obtained from this data source.
index - index of the child.
Returns:
The path pointing to the newly created node.
Throws:
java.lang.IllegalStateException - if the parent node does not allow children.

isNodeEditable

public boolean isNodeEditable(javax.swing.tree.DefaultMutableTreeNode node)
Returns wether the specified node may be renamed.

Specified by:
isNodeEditable in interface MutableTreeModel
Parameters:
node - a node from the tree, obtained from this data source.

getCreatableNodeTypes

public java.lang.Object[] getCreatableNodeTypes(java.lang.Object parent)
Returns the types of children that may be created at this node.

Specified by:
getCreatableNodeTypes in interface MutableTreeModel
Parameters:
parent - a node from the tree, obtained from this data source.
Returns:
an array of objects that specify a child type that may be added to the node. Returns an empty array for nodes that cannot have additional children.

getCreatableNodeType

public java.lang.Object getCreatableNodeType(java.lang.Object parent)
Description copied from interface: MutableTreeModel
Returns the default type of children that can be created at the specified node.

Specified by:
getCreatableNodeType in interface MutableTreeModel
Parameters:
parent - a node from the tree, obtained from this data source.
Returns:
an Object that specifies the default child type that can be inserted at the insertion point. Returns null if no children can be inserted here. The value must be one of the types returned by operation getCreatableNodeTypes.

isNodeAddable

public boolean isNodeAddable(javax.swing.tree.DefaultMutableTreeNode parent,
                             int index)
Returns wether the specified node may be inserted.

Specified by:
isNodeAddable in interface MutableTreeModel
Parameters:
parent - a node from the tree, obtained from this data source.
index - the insertion index.

isLeaf

public boolean isLeaf(java.lang.Object node)
Specified by:
isLeaf in interface javax.swing.tree.TreeModel
Overrides:
isLeaf in class javax.swing.tree.DefaultTreeModel

removeUndoableEditListener

public void removeUndoableEditListener(javax.swing.event.UndoableEditListener l)
Removes an UndoableEditListener.

Specified by:
removeUndoableEditListener in interface MutableTreeModel
Specified by:
removeUndoableEditListener in interface Undoable

addUndoableEditListener

public void addUndoableEditListener(javax.swing.event.UndoableEditListener l)
Adds an UndoableEditListener.

Specified by:
addUndoableEditListener in interface MutableTreeModel
Specified by:
addUndoableEditListener in interface Undoable

fireUndoableEdit

public void fireUndoableEdit(javax.swing.undo.UndoableEdit edit)
Notify all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Specified by:
fireUndoableEdit in interface MutableTreeModel

exportTransferable

public java.awt.datatransfer.Transferable exportTransferable(javax.swing.tree.DefaultMutableTreeNode[] nodes)
Copies the indicated nodes to the clipboard.

Specified by:
exportTransferable in interface MutableTreeModel
Parameters:
nodes - The nodes to be copied.
Throws:
java.lang.IllegalStateException - if the nodes can not be removed.

importTransferable

public java.util.List<javax.swing.tree.TreePath> importTransferable(java.awt.datatransfer.Transferable transfer,
                                                                    int action,
                                                                    javax.swing.tree.DefaultMutableTreeNode parent,
                                                                    int index)
                                                             throws java.awt.datatransfer.UnsupportedFlavorException,
                                                                    java.io.IOException
Description copied from interface: MutableTreeModel
Causes a transfer to the model from a clipboard or a DND drop operation.

Specified by:
importTransferable in interface MutableTreeModel
Parameters:
transfer - The transfer data.
parent - a node from the tree, obtained from this data source.
index - The insertion point.
Returns:
The paths of the imported nodes.
Throws:
java.awt.datatransfer.UnsupportedFlavorException
java.io.IOException

isImportable

public boolean isImportable(java.awt.datatransfer.DataFlavor[] flavors,
                            int action,
                            javax.swing.tree.DefaultMutableTreeNode parent,
                            int index)
Description copied from interface: MutableTreeModel
Indicates whether the model would accept an import of the given set of data flavors prior to actually attempting to import it.

Specified by:
isImportable in interface MutableTreeModel
Parameters:
flavors - the data formats available
action - the action, this is either COPY, MOVE or LINK.
parent - a node from the tree, obtained from this data source.
index - The insertion point.
Returns:
true if the data can be inserted into the component, false otherwise

isCloseable

public boolean isCloseable()
Returns true, if no background processes are working on the model.


close

public void close()
Shuts down all background processes.


getNodeActions

public javax.swing.Action[] getNodeActions(javax.swing.tree.DefaultMutableTreeNode[] nodes)
Description copied from interface: MutableTreeModel
Gets actions for the specified nodes.

Specified by:
getNodeActions in interface MutableTreeModel
Parameters:
nodes - The nodes.

dispose

public void dispose()

(c) Werner Randelshofer.
All rights reserved.