puzzled.grid
Class Grid

java.lang.Object
  |
  +--puzzled.grid.Grid
All Implemented Interfaces:
Constants

public class Grid
extends java.lang.Object
implements Constants

The grid class represent the data structure of the problem to be solved. It is not a drawable component itself, but owns a reference to a GridCanvas object, which is the graphical representation of the relationships between the various items. This class also contains an array of category names, as well as a two-dimensional array containing each of the item names.

Version:
1.1 25 Aug 2001
Author:
Frédéric Demers

Field Summary
 AnswerCanvas answerCanvas
           
 GridCanvas gridCanvas
          A canvas to draw the relationship beween the items.
 
Fields inherited from interface puzzled.Constants
ACTION_TOKEN, AND_TOKEN, AND_TOKENS, IS_ACTION, IS_TOKEN, IS_TOKENS, ISNOT_ACTION, JUNCTION_TOKEN, LESS_ACTION, LESS_TOKEN, LESS_TOKENS, MORE_ACTION, MORE_TOKEN, MORE_TOKENS, NEXT_ACTION, NEXT_TOKEN, NEXT_TOKENS, NO_ACTION, NOR_TOKEN, NOT_TOKEN, NOT_TOKENS, NOT_VALID, NOTNEXT_ACTION, NOTNEXT_TOKEN, NUMBER_TOKEN, NUMBER_TOKENS, OR_ACTION, OR_TOKEN, OR_TOKENS, PREVIOUS_CLUE_TOKEN, RESET_TOKEN, THAN_TOKEN, TO_TOKEN, VALUE_NO, VALUE_UNKNOWN, VALUE_YES
 
Constructor Summary
Grid(ProblemSolver parent_arg)
          Constructor
Grid(ProblemSolver parent_arg, java.io.File file_arg)
          Class constructor with filename.
 
Method Summary
 void addClue(Clue newClue)
          Method used to add a clue in the cluesVector.
 boolean addRelation(int value, int cat1, int item1, int cat2, int item2)
          Method used to add a relationship to the squares array, and update the graphical representation by passing the call to the GridCanvas object.
 boolean addRelation(Relation rel)
          Method used to add a relationship to the squares array, and update the graphical representation by passing the call to the GridCanvas object.
 int checkAnswer(java.lang.String testItem, int specialCategory)
          Method used to check if there is a recorded answer between a specific item and a category.
 void generateCanvases()
           
 AnswerCanvas getAnswerCanvas()
           
 java.util.Vector getAnswersVector()
          Method used to obtain a reference to the ayeVector.
 java.lang.String[] getCategories()
           
 java.lang.String getCategory(int index)
          Method used to get the name of a category of items, given its index.
 int getCatIndex(java.lang.String item)
          Method used to obtain the category index of a given item or category name.
 int getCatNumber()
          Method used to obtain the number of categories
 Clue getClue(int clueNumber_arg)
          Method used to obtain the vector containing the special relations.
 java.util.Vector getCluesVector()
           
 java.io.File getCurrentFile()
          Returns a reference to the current File for this Worksheet.
 GridCanvas getGridCanvas()
          Method used to return the canvas, which is the graphic representation of the relationships.
 java.lang.String getItem(int category, int index)
          Method returning the name of an item given its category number and index.
 int getItemIndex(java.lang.String item)
          Method returning the item index of a given item.
 int getItemNumber()
          Method used to obtain the number of items
 java.lang.String[][] getItems()
           
 ProblemSolver getParent()
           
 int getRelation(int cat1, int item1, int cat2, int item2)
          Method used to obtain the relationship between two items, such as set in the square matrix.
 boolean isDirty()
           
 void loadGrid(java.io.File filename)
          Loads a worksheet with the specified file name.
 void refresh()
           
 void regenerateVector()
           
 void reset()
          Method used to reset the squares and redraw the new grid.
 void saveGrid()
          Saves the current Worksheet.
 void saveGrid(java.io.File theFile)
          Saves the current Grid information in the specified File.
 void setCategory(int index, java.lang.String name)
          Method used to set the name of a category given its index.
 void setCatNumber(int number)
          Method called to set the number of categories in the problem.
 void setCurrentFile(java.io.File f)
          Assigns the specified File to this grid.
 void setDirty(boolean dirty_arg)
           
 void setItem(int category, int index, java.lang.String name)
          Method used to set the name of an item, given its category number and index.
 void setItemNumber(int number)
          Method called to set the number of items per category.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

gridCanvas

public GridCanvas gridCanvas
A canvas to draw the relationship beween the items.

answerCanvas

public AnswerCanvas answerCanvas
Constructor Detail

Grid

public Grid(ProblemSolver parent_arg)
Constructor

Grid

public Grid(ProblemSolver parent_arg,
            java.io.File file_arg)
Class constructor with filename.
Parameters:
parent_arg - a reference to the parent which contains this Worksheet.
Method Detail

setCatNumber

public void setCatNumber(int number)
Method called to set the number of categories in the problem. Has to be called before setItemNumber.
Parameters:
number - the number of categories.

setItemNumber

public void setItemNumber(int number)
Method called to set the number of items per category. Has to be called after setCatNumber. It also creates the names array and the squares array.
Parameters:
number - the number of items per category.

generateCanvases

public void generateCanvases()

setCategory

public void setCategory(int index,
                        java.lang.String name)
Method used to set the name of a category given its index.
Parameters:
index - the index of this category.
name - the name of the category.

getCategory

public java.lang.String getCategory(int index)
Method used to get the name of a category of items, given its index.
Parameters:
index - the index of this category.

getCategories

public java.lang.String[] getCategories()

setItem

public void setItem(int category,
                    int index,
                    java.lang.String name)
Method used to set the name of an item, given its category number and index.
Parameters:
category - the category number between 0 and catNumber-1
index - the index of this item
name - the name of the item

getItem

public java.lang.String getItem(int category,
                                int index)
Method returning the name of an item given its category number and index.
Parameters:
category - the category number between 0 and catNumber-1
index - the index of this item

getItems

public java.lang.String[][] getItems()

getCatIndex

public int getCatIndex(java.lang.String item)
Method used to obtain the category index of a given item or category name. Used when parsing a hint to compare the categories of two or more items. Also needed when obtaining the coordinates of a relationship or of its graphical representation.
Parameters:
item - the item name.
Returns:
the category index of the item

getItemIndex

public int getItemIndex(java.lang.String item)
Method returning the item index of a given item. This is used in order to establish the coordinates of the relationship and of its graphical representation.

addRelation

public boolean addRelation(Relation rel)
Method used to add a relationship to the squares array, and update the graphical representation by passing the call to the GridCanvas object. It returns wether this affectation was successful (true) or if it was already done (false)
Parameters:
rel - the relationship to be added.
Returns:
whether the addition was successful

addRelation

public boolean addRelation(int value,
                           int cat1,
                           int item1,
                           int cat2,
                           int item2)
Method used to add a relationship to the squares array, and update the graphical representation by passing the call to the GridCanvas object. It returns wether this affectation was successful (true) or if it was already done (false). This version will create a new relation from the parameters instead of accepting an existing one.
Parameters:
value -  
cat1 -  
item1 -  
cat2 -  
item2 -  
Returns:
whether the addition was successful

getRelation

public int getRelation(int cat1,
                       int item1,
                       int cat2,
                       int item2)
Method used to obtain the relationship between two items, such as set in the square matrix. The categories can be inversed, this method will compute the proper row and column values needed, also considering the left translation done to improve memory usage.
Parameters:
cat1 - One category index associated with one item
item1 - One item index
cat2 - The other category index associated with the other item
item1 - The other item index
Returns:
The actual value of the relationship such as seen in the Constants interface.

getAnswersVector

public java.util.Vector getAnswersVector()
Method used to obtain a reference to the ayeVector.
Returns:
the ayeVector containing the relations that are a definite match.

checkAnswer

public int checkAnswer(java.lang.String testItem,
                       int specialCategory)
Method used to check if there is a recorded answer between a specific item and a category. Returns the answer (which item in the category it is linked with a VALUE_YES) or -1 if no answer has been discovered yet.
Returns:
the number of categories

getCatNumber

public int getCatNumber()
Method used to obtain the number of categories
Returns:
the number of categories

getItemNumber

public int getItemNumber()
Method used to obtain the number of items
Returns:
the number of items

getClue

public Clue getClue(int clueNumber_arg)
Method used to obtain the vector containing the special relations. It is used by the processor.
Returns:
the special relations vector.

getCluesVector

public java.util.Vector getCluesVector()

addClue

public void addClue(Clue newClue)
Method used to add a clue in the cluesVector. Mainly used by the parser.
Parameters:
clue - the clue to be added in the cluesVector.

reset

public void reset()
Method used to reset the squares and redraw the new grid.

refresh

public void refresh()

getGridCanvas

public GridCanvas getGridCanvas()
Method used to return the canvas, which is the graphic representation of the relationships. It is required when adding the canvas to the scrollpane.
Returns:
the graphical representation of the relationships

getAnswerCanvas

public AnswerCanvas getAnswerCanvas()

getParent

public ProblemSolver getParent()

isDirty

public boolean isDirty()

setDirty

public void setDirty(boolean dirty_arg)

loadGrid

public void loadGrid(java.io.File filename)
              throws java.io.IOException,
                     InvalidGridException
Loads a worksheet with the specified file name.
Parameters:
filename - the name of the file
Returns:
a reference to the saved Worksheet

saveGrid

public void saveGrid()
              throws java.io.IOException
Saves the current Worksheet.

saveGrid

public void saveGrid(java.io.File theFile)
              throws java.io.IOException
Saves the current Grid information in the specified File.
Parameters:
theFile - a File reference obtained through file chooser

setCurrentFile

public void setCurrentFile(java.io.File f)
Assigns the specified File to this grid.
Parameters:
f - the File

getCurrentFile

public java.io.File getCurrentFile()
Returns a reference to the current File for this Worksheet.
Returns:
the current File

regenerateVector

public void regenerateVector()