/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.data.filetable;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Observable;
import java.util.Vector;
import pdfdb.settings.ColumnSettingsManager;
/** Provides access to the properties of columns (or ColumnState) as stored by
* the SettingsManager. They should be considered user requested preferences
* and not absolute values. The state held in this class is not persisted
* until the save method is called.
*
* This class allows access to visible columns and invisible columns. Invisible
* columns are typically accessed using a visibleOnly argument set to false.
*
* The getters with a single string argument (key) directly access the
* settings manager and will not take into account state held in the
* ColumnData but not the settings manager, until the ColumnData is saved.
*
* @author ug22cmg */
public class ColumnData extends Observable
{
private int columnCount = -1;
private List<ColumnState> states = null;
private List<ColumnState> visibleStates = null;
private static ColumnData instance = null;
/** Initializes and reloads the column states from the settings manager */
private ColumnData()
{
reload();
}
/** Gets the singleton instance.
* @return The instance. */
public synchronized static ColumnData getInstance()
{
if (instance == null)
instance = new ColumnData();
return instance;
}
/** Gets the list of states of the columns as held by the settings manager.
* Includes visible and non visible column states. Side-effect: The
* settings manager's cache is cleared and re-reads the settings file
* during this method.
* @return A list of column states. */
private List<ColumnState> getStates()
{
ColumnSettingsManager manager = ColumnSettingsManager.getInstance();
List<ColumnState> stateList = new Vector<ColumnState>();
for (String key : manager.keySet())
{
ColumnState state = manager.get(key);
stateList.add(state);
}
Collections.sort(stateList);
return stateList;
}
/** Gets the list of column states that only include visible items.
* Note: requires getStates() to have been called directly before it. */
private List<ColumnState> getVisibleStates()
{
List<ColumnState> visibleStateList = new Vector<ColumnState>();
for (ColumnState state : states)
{
if (state.isVisible()) visibleStateList.add(state);
}
return visibleStateList;
}
/** Gets the list of keys for the specified column type.
* @param visibleOnly If only visible column keys should be returned.
* @return A list of column keys.*/
public synchronized List<String> getKeys(boolean visibleOnly)
{
List<String> list = new Vector<String>();
for (ColumnState state : visibleOnly ? visibleStates : states)
{
list.add(state.getKey());
}
return list;
}
/** Sets the visibility of the column specified by the column key. The
* new visibility will be equal to the visible argument.
* @param key The key to alter.
* @param visible The new visibility. */
public synchronized void setVisibility(String key, boolean visible)
{
ColumnSettingsManager manager = ColumnSettingsManager.getInstance();
ColumnState state = manager.get(key);
state.setIsVisible(visible);
}
/** Sets the requested width of the specified column.
* @param key The key that defines the column to be altered.
* @param width The new width. */
public synchronized void setWidth(String key, int width)
{
ColumnSettingsManager manager = ColumnSettingsManager.getInstance();
ColumnState state = manager.get(key);
state.setWidth(width);
}
/** Reinserts a column from oldIndex into newIndex. Indexes of the other
* columns are altered to ensure against duplications.
* @param newIndex index to move to.
* @param oldIndex index to move from */
public synchronized void changeOrder(int newIndex, int oldIndex)
{
ColumnSettingsManager manager = ColumnSettingsManager.getInstance();
ColumnState target = states.get(oldIndex);
states.remove(oldIndex);
states.add(newIndex, target);
for (int i = 0; i < states.size(); i++)
{
ColumnState tmp = states.get(i);
ColumnState required = manager.get(tmp.getKey());
if (!required.getKey().equals(target.getKey()))
{
required.setIndex(i);
tmp.setIndex(i);
}
}
}
/** Ensures the specified column has the last index in the collection.
* @param key The key identfying the column. */
public synchronized void ensureLast(String key)
{
ColumnSettingsManager manager = ColumnSettingsManager.getInstance();
ColumnState state = manager.get(key);
if (visibleStates.contains(state))
state.setIndex(columnCount - 1);
else state.setIndex(columnCount);
}
/** Gets the specified column. Must be visible.
* @param localIndex The index of the column.
* @return The ColumnState instance representing the state of the
* column preferences. */
private ColumnState getColumnWhereVisible(int localIndex)
{
return visibleStates.get(localIndex);
}
/** Gets the width of the specified key as held by the settings manager.
* Additional state held by this ColumnData instance is not considered.
* @param key The key of the column.
* @return The requested width of the column. */
public synchronized int getWidth(String key)
{
ColumnSettingsManager manager = ColumnSettingsManager.getInstance();
return manager.get(key).getWidth();
}
/** Gets the index of the specified key as held by the settings manager.
* Additional state held by this ColumnData instance is not considered.
* @param key The key of the column.
* @return The requested index of the column. */
public synchronized int getIndex(String key)
{
ColumnSettingsManager manager = ColumnSettingsManager.getInstance();
return manager.get(key).getIndex();
}
/** Gets whether the settings manager considers the specified key to
* be visible or not. Settings held by this ColumnData instance are
* not considered.
* @param key The key to check.
* @return Whether the column is visible. */
public synchronized boolean isVisible(String key)
{
ColumnSettingsManager manager = ColumnSettingsManager.getInstance();
return manager.get(key).isVisible();
}
/** Gets the column label. Note: Special switch allowing the possibility
* of accessing invisible columns. To do this, the visibleOnly argument
* must be false.
* @param index The index of the column in either global or visible column
* space.
* @param visibleOnly whether to check only visible column space or check
* global column space.
* @return The column label */
public synchronized String getColumnLabel(int index, boolean visibleOnly)
{
if (!visibleOnly)
{
return states.get(index).getLabel();
}
else
{
return getColumnLabel(index);
}
}
/** Gets the index of the key in the specified subset of columns.
* @param key The key of the column.
* @param visibleOnly Whether the column exists in the visible items
* or only in the collection of all columns.
* @return Position or -1. */
public synchronized int indexOf(String key, boolean visibleOnly)
{
ColumnSettingsManager manager = ColumnSettingsManager.getInstance();
if (visibleOnly)
{
return visibleStates.indexOf(manager.get(key));
}
else
{
return states.indexOf(manager.get(key));
}
}
/** Gets the unique column key - can only retrieve visible columns.
* @param visibleIndex The index of the column as specified in visible
* column space.
* @return The column key. */
public synchronized String getColumnKey(int visibleIndex)
{
ColumnState state = getColumnWhereVisible(visibleIndex);
if (state == null) throw new IllegalArgumentException();
else
return state.getKey();
}
/** Gets the friendly label of the column - can only retrieve visible
* columns.
* @param visibleIndex The column index as specified in visible column
* space.
* @return The column label. */
public synchronized String getColumnLabel(int visibleIndex)
{
ColumnState state = getColumnWhereVisible(visibleIndex);
if (state == null) throw new IllegalArgumentException();
else return state.getLabel();
}
/** Gets the number of visible columns in the ColumnData (note: not
* nessecarily the same as settings file)
* @return The number of columns held by the ColumnData */
public synchronized int getVisibleColumnCount()
{
return columnCount;
}
/** Reloads the state of the column data to be exactly the same as what
* is stored in the configuration file. */
public synchronized void reload()
{
states = getStates();
visibleStates = getVisibleStates();
columnCount = visibleStates.size();
}
/** Saves state of the ColumnData to the underlying manager.
* @throws java.io.IOException If the save fails. */
public synchronized void save() throws IOException
{
ColumnSettingsManager manager = ColumnSettingsManager.getInstance();
manager.save();
}
}