edu.rice.cs.drjava.ui
Class SingleDisplayModel

java.lang.Object
  |
  +--edu.rice.cs.drjava.model.DefaultGlobalModel
        |
        +--edu.rice.cs.drjava.ui.SingleDisplayModel
All Implemented Interfaces:
GlobalModel, OptionConstants

public class SingleDisplayModel
extends DefaultGlobalModel

A GlobalModel that enforces invariants associated with having one active document at a time. Invariants:

  1. DefaultGlobalModel.getDefinitionsDocuments() will always return an array of at least size 1.
  2. (follows from previous) If there is ever no document in the model, a new one will be created.
  3. There is always exactly one active document, which can be get/set via getActiveDocument() and setActiveDocument(edu.rice.cs.drjava.model.OpenDefinitionsDocument).
Other functions added by this class:
  1. When calling openFile(edu.rice.cs.drjava.model.FileOpenSelector), if there is currently only one open document, and it is untitled and unchanged, it will be closed after the new document is opened. This means that, in one atomic transaction, the model goes from having one totally empty document open to having one document (the requested one) open.

Version:
$Id: SingleDisplayModel.java,v 1.23 2002/09/04 21:02:20 csreis Exp $

Inner Class Summary
private  class SingleDisplayModel.SelectionModelListener
          Listens to the selection model for the open documents.
 
Inner classes inherited from class edu.rice.cs.drjava.model.DefaultGlobalModel
DefaultGlobalModel.DefinitionsDocumentHandler, DefaultGlobalModel.EventNotifier, DefaultGlobalModel.EventPoller, DefaultGlobalModel.ExtraClasspathOptionListener
 
Field Summary
private  OpenDefinitionsDocument _activeDocument
          The active document pointer, which will never be null once the constructor is done.
private  boolean _isClosingAllDocs
          Denotes whether the model is currently trying to close all documents, and thus that a new one should not be created.
private  ListSelectionModel _selectionModel
          Keeps track of the currently selected document in the list model.
 
Fields inherited from class edu.rice.cs.drjava.model.DefaultGlobalModel
_compilerErrorsWithoutFiles, _compilerLock, _consoleDoc, _debugManager, _debugPort, _definitionsDocs, _docBeingTested, _editorKit, _interactionsDoc, _interpreterControl, _interpreterLock, _junitDoc, _listeners, _numErrors, _pageFormat, _waitingForFirstInterpreter, DEBUG_STYLE, INDENTER, INTERACTIONS_ERR_STYLE, SYSTEM_ERR_CONSOLE_STYLE, SYSTEM_ERR_INTERACTIONS_STYLE, SYSTEM_OUT_CONSOLE_STYLE, SYSTEM_OUT_INTERACTIONS_STYLE
 
Fields inherited from interface edu.rice.cs.drjava.config.OptionConstants
COMPILER_ERROR_COLOR, DEBUG_BREAKPOINT_COLOR, DEBUG_SHOW_THREADS, DEBUG_SOURCEPATH, DEBUG_STEP_DRJAVA, DEBUG_STEP_INTERPRETER, DEBUG_STEP_JAVA, DEBUG_THREAD_COLOR, DEFINITIONS_BACKGROUND_COLOR, DEFINITIONS_COMMENT_COLOR, DEFINITIONS_DOUBLE_QUOTED_COLOR, DEFINITIONS_KEYWORD_COLOR, DEFINITIONS_MATCH_COLOR, DEFINITIONS_NORMAL_COLOR, DEFINITIONS_NUMBER_COLOR, DEFINITIONS_SINGLE_QUOTED_COLOR, DEFINITIONS_TYPE_COLOR, EXTRA_CLASSPATH, FONT_DOCLIST, FONT_MAIN, FONT_TOOLBAR, HISTORY_MAX_SIZE, INDENT_LEVEL, INTERACTIONS_EXIT_PROMPT, JAVAC_ALLOW_ASSERT, JAVAC_LOCATION, JSR14_COLLECTIONSPATH, JSR14_LOCATION, KEY_BACKWARD, KEY_BEGIN_DOCUMENT, KEY_BEGIN_LINE, KEY_CLOSE_FILE, KEY_COMPILE, KEY_COMPILE_ALL, KEY_COPY, KEY_CUT, KEY_CUT_LINE, KEY_DEBUG_BREAKPOINT_TOGGLE, KEY_DEBUG_MODE_TOGGLE, KEY_DEBUG_RESUME, KEY_DEBUG_STEP_INTO, KEY_DEBUG_STEP_OUT, KEY_DEBUG_STEP_OVER, KEY_DELETE_NEXT, KEY_DELETE_PREVIOUS, KEY_DOWN, KEY_END_DOCUMENT, KEY_END_LINE, KEY_FIND_NEXT, KEY_FIND_REPLACE, KEY_FORWARD, KEY_GOTO_LINE, KEY_NEW_FILE, KEY_NEXT_DOCUMENT, KEY_NEXT_WORD, KEY_OPEN_FILE, KEY_PAGE_DOWN, KEY_PAGE_UP, KEY_PASTE, KEY_PREVIOUS_DOCUMENT, KEY_PREVIOUS_WORD, KEY_PRINT, KEY_PRINT_PREVIEW, KEY_QUIT, KEY_REDO, KEY_SAVE_FILE, KEY_SAVE_FILE_AS, KEY_SELECT_ALL, KEY_UNDO, KEY_UP, LINEENUM_ENABLED, mask, QUIT_PROMPT, RECENT_FILES, RECENT_FILES_MAX_SIZE, TOOLBAR_ICONS_ENABLED, TOOLBAR_TEXT_ENABLED, WORKING_DIRECTORY
 
Constructor Summary
SingleDisplayModel(DefaultGlobalModel other)
          Creates a SingleDisplayModel using the Interactions JVM from the given model.
SingleDisplayModel(int rmiPort)
          Creates a SingleDisplayModel.
 
Method Summary
private  void _ensureNotEmpty()
          Creates a new document if there are currently no documents open.
private  int _getDocumentIndex(OpenDefinitionsDocument doc)
          Returns the index of the first occurrence of the specified document in the list of open documents, or -1 if it is not found.
private  boolean _hasOneEmptyDocument()
          Returns whether there is currently only one open document which is untitled and unchanged.
private  void _init()
          Initiates this SingleDisplayModel.
private  void _setActiveDoc(int index)
          Actually set the activeDocument field to the document at the given index, and fire an activeDocumentChanged event.
 void aboutToSaveFromSaveAll(OpenDefinitionsDocument doc)
          If the document is untitled, brings it to the top so that the user will know which file she is saving
 void addListener(GlobalModelListener listener)
          Add a listener to this global model.
 boolean closeAllFiles()
          Attempts to close all open documents.
 boolean closeFile(OpenDefinitionsDocument doc)
          Closes an open definitions document, prompting to save if the document has been changed.
 OpenDefinitionsDocument getActiveDocument()
          Returns the currently active document.
 String getDisplayFilename(OpenDefinitionsDocument doc)
          Return the name of the file, or "(untitled)" if no file exists.
 String getDisplayFullPath(int index)
          Return the absolute path of the file with the given index, or "(untitled)" if no file exists.
 String getDisplayFullPath(OpenDefinitionsDocument doc)
          Return the absolute path of the file, or "(untitled)" if no file exists.
 ListSelectionModel getDocumentSelectionModel()
          Returns the selection model for the list of documents.
 boolean isClosingAllFiles()
          Returns whether we are in the process of closing all documents.
 OpenDefinitionsDocument newFile()
          Creates a new document, adds it to the list of open documents, and sets it to be active.
 OpenDefinitionsDocument openFile(FileOpenSelector com)
          Open a file and read it into the definitions.
 OpenDefinitionsDocument openFiles(FileOpenSelector com)
          Open multiple files and read it into the definitions.
 void saveAllFiles(FileSaveSelector com)
          Saves all open files, prompting for names if necessary.
 void setActiveDocument(int index)
          Sets the currently active document by updating the selection model.
 void setActiveDocument(OpenDefinitionsDocument doc)
          Sets the currently active document by updating the selection model.
 void setNextActiveDocument()
          Sets the active document to be the next one in the list.
 void setPreviousActiveDocument()
          Sets the active document to be the previous one in the list.
 
Methods inherited from class edu.rice.cs.drjava.model.DefaultGlobalModel
_compileFiles, _createDebugger, _createOpenDefinitionsDocument, _deleteSemiColon, _distributeErrors, _docAppend, _docIsOpen, _getConsoleErrStyle, _getDebugStyle, _getInteractionsErrStyle, _getInteractionsOutStyle, _getOpenDocument, _interactionIsOver, _openFile, _resetInteractionsClasspath, _testClassCall, , areAnyModifiedSinceSave, clearCurrentInteraction, clearHistory, compileAll, getActiveCompiler, getAvailableCompilers, getClasspath, getCompilerErrorsWithoutFiles, getConsoleDocument, getDebugManager, getDebugPort, getDefinitionsDocuments, getDocBeingTested, getDocumentForFile, getEditorKit, getHistoryAsString, getInteractionsDocument, getInteractionsFrozenPos, getJUnitDocument, getNumErrors, getPageFormat, getSourceFileFromPaths, getSourceRootSet, interactionsReady, interactionsResetting, interpretCurrentInteraction, loadHistory, nonTestCase, notifyListeners, pollListeners, printDebugMessage, quit, recallNextInteractionInHistory, recallPreviousInteractionInHistory, removeListener, replCalledSystemExit, replReturnedResult, replReturnedVoid, replSystemErrPrint, replSystemOutPrint, replThrewException, resetCompilerErrors, resetConsole, resetInteractions, saveAllBeforeProceeding, saveAllFiles, saveHistory, setActiveCompiler, setDefinitionsIndent, setPageFormat, systemErrPrint, systemOutPrint, testFinished, waitForInterpreter
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

_activeDocument

private OpenDefinitionsDocument _activeDocument
The active document pointer, which will never be null once the constructor is done.

_selectionModel

private ListSelectionModel _selectionModel
Keeps track of the currently selected document in the list model.

_isClosingAllDocs

private boolean _isClosingAllDocs
Denotes whether the model is currently trying to close all documents, and thus that a new one should not be created.
Constructor Detail

SingleDisplayModel

public SingleDisplayModel(int rmiPort)
Creates a SingleDisplayModel.
  1. A new document is created to satisfy the invariant.
  2. The first document in the list is set as the active document.
Parameters:
rmiPort - The port on which to run the RMI registry, or -1 for a unique port.

SingleDisplayModel

public SingleDisplayModel(DefaultGlobalModel other)
Creates a SingleDisplayModel using the Interactions JVM from the given model. (Useful in test cases.)
Parameters:
other - A model with a valid Interactions JVM to use.
Method Detail

_init

private void _init()
Initiates this SingleDisplayModel. Should only be called from the constructor.

getActiveDocument

public OpenDefinitionsDocument getActiveDocument()
Returns the currently active document.

setActiveDocument

public void setActiveDocument(OpenDefinitionsDocument doc)
Sets the currently active document by updating the selection model.
Parameters:
doc - Document to set as active

setActiveDocument

public void setActiveDocument(int index)
Sets the currently active document by updating the selection model. The selection model will trigger our SelectionModelListener to fire an activeDocumentChanged event.
Parameters:
index - Index of active document in the list of documents.

getDocumentSelectionModel

public ListSelectionModel getDocumentSelectionModel()
Returns the selection model for the list of documents.

setNextActiveDocument

public void setNextActiveDocument()
Sets the active document to be the next one in the list.

setPreviousActiveDocument

public void setPreviousActiveDocument()
Sets the active document to be the previous one in the list.

getDisplayFilename

public String getDisplayFilename(OpenDefinitionsDocument doc)
Return the name of the file, or "(untitled)" if no file exists. Does not include the ".java" if it is present.

getDisplayFullPath

public String getDisplayFullPath(OpenDefinitionsDocument doc)
Return the absolute path of the file, or "(untitled)" if no file exists.

getDisplayFullPath

public String getDisplayFullPath(int index)
Return the absolute path of the file with the given index, or "(untitled)" if no file exists.

addListener

public void addListener(GlobalModelListener listener)
Add a listener to this global model.
Overrides:
addListener in class DefaultGlobalModel
Parameters:
listener - a listener that reacts on events generated by the GlobalModel

newFile

public OpenDefinitionsDocument newFile()
Creates a new document, adds it to the list of open documents, and sets it to be active.
Overrides:
newFile in class DefaultGlobalModel
Returns:
The new open document

openFile

public OpenDefinitionsDocument openFile(FileOpenSelector com)
                                 throws IOException,
                                        OperationCanceledException,
                                        AlreadyOpenException
Open a file and read it into the definitions. The provided file selector chooses a file, and on a successful open, the fileOpened() event is fired. This also checks if there was previously a single, unchanged, untitled document open, and if so, closes it after a successful opening.
Overrides:
openFile in class DefaultGlobalModel
Parameters:
com - a command pattern command that selects what file to open
Returns:
The open document, or null if unsuccessful
Throws:
IOException -  
OperationCanceledException - if the open was canceled
AlreadyOpenException - if the file is already open

openFiles

public OpenDefinitionsDocument openFiles(FileOpenSelector com)
                                  throws IOException,
                                         OperationCanceledException,
                                         AlreadyOpenException
Open multiple files and read it into the definitions. The provided file selector chooses a file, and on a successful open, the fileOpened() event is fired. This also checks if there was previously a single, unchanged, untitled document open, and if so, closes it after a successful opening.
Overrides:
openFiles in class DefaultGlobalModel
Parameters:
com - a command pattern command that selects what file to open
Returns:
The open document, or null if unsuccessful
Throws:
IOException -  
OperationCanceledException - if the open was canceled
AlreadyOpenException - if the file is already open

saveAllFiles

public void saveAllFiles(FileSaveSelector com)
                  throws IOException
Saves all open files, prompting for names if necessary. When prompting (ie, untitled document), set that document as active.
Overrides:
saveAllFiles in class DefaultGlobalModel
Parameters:
com - a FileSaveSelector
Throws:
IOException -  

aboutToSaveFromSaveAll

public void aboutToSaveFromSaveAll(OpenDefinitionsDocument doc)
If the document is untitled, brings it to the top so that the user will know which file she is saving
Overrides:
aboutToSaveFromSaveAll in class DefaultGlobalModel
Following copied from class: edu.rice.cs.drjava.model.DefaultGlobalModel
Parameters:
doc - the document which is about to be saved by a save all command

closeFile

public boolean closeFile(OpenDefinitionsDocument doc)
Closes an open definitions document, prompting to save if the document has been changed. Returns whether the file was successfully closed. Also ensures the invariant that there is always at least one open document holds by creating a new file if necessary.
Overrides:
closeFile in class DefaultGlobalModel
Returns:
true if the document was closed

closeAllFiles

public boolean closeAllFiles()
Attempts to close all open documents. Also ensures the invariant that there is always at least one open document holds by creating a new file if necessary.
Overrides:
closeAllFiles in class DefaultGlobalModel
Returns:
true if all documents were closed

isClosingAllFiles

public boolean isClosingAllFiles()
Returns whether we are in the process of closing all documents. (Don't want to prompt the user to revert files that have become modified on disk if we're just closing everything.)

_getDocumentIndex

private int _getDocumentIndex(OpenDefinitionsDocument doc)
Returns the index of the first occurrence of the specified document in the list of open documents, or -1 if it is not found.

_hasOneEmptyDocument

private boolean _hasOneEmptyDocument()
Returns whether there is currently only one open document which is untitled and unchanged.

_ensureNotEmpty

private void _ensureNotEmpty()
Creates a new document if there are currently no documents open.

_setActiveDoc

private void _setActiveDoc(int index)
Actually set the activeDocument field to the document at the given index, and fire an activeDocumentChanged event. This should usually be called from the SelectionModelListener, but must also be called in setActiveDocument in the case where the selection index does not change, and thus the SelectionModel does not fire a valueChanged event.