use of edu.cmu.cs.hcii.cogtool.ui.Interaction in project cogtool by cogtool.
the class Controller method open.
/**
* Prompts for and opens a file.
*
* @throws RcvrIOLoadException if the open fails
*/
protected boolean open(DoublePoint loc, File[] openLocs) {
try {
// If the open dialog was canceled, do nothing.
if (openLocs != null) {
Interaction interaction = getUI().getStandardInteraction();
// Otherwise, open all of the selected projects.
for (int i = 0; i < openLocs.length; i++) {
String statusMsg = null;
if (!openLocs[i].exists()) {
interaction.protestFileNotFound(openLocs[i].getName());
continue;
}
Project proj = null;
try {
proj = (Project) persist.isLoaded(openLocs[i]);
if (proj != null) {
if (UndoManager.isAtSavePoint(proj)) {
statusMsg = L10N.get("AC.OpenNotModified", "Project already open and not modified");
} else {
boolean revert = interaction.askRevertBeforeOpen(proj.getName());
if (revert) {
closeProject(proj, false);
} else {
continue;
}
}
}
proj = (Project) persist.load(openLocs[i]);
} catch (RuntimeException e) {
// format
throw new RcvrIOLoadException(("Error loading project: " + e.getMessage()), e);
}
CogToolPref.setRecent(openLocs[i].getCanonicalPath());
// Reset the project name in case the file was renamed
String name = openLocs[i].getName();
// Remove the .cgt, if it is there.
int periodIndex = name.lastIndexOf('.');
if (periodIndex > 0) {
name = name.substring(0, periodIndex);
}
proj.setName(name);
// Create a window; the project is not yet registered
// and is unmodified.
ProjectController c = ProjectController.openController(proj, false, true);
UI ui = getUI();
if ((loc != null) && (ui != null)) {
c.getUI().setLocation(loc);
}
if (statusMsg != null) {
c.interaction.setStatusMessage(statusMsg);
}
}
return true;
}
return false;
} catch (IOException e) {
throw new RcvrIOLoadException("Error loading project: " + e.getMessage(), e);
} catch (GraphicsUtil.ImageException e) {
throw new RcvrImageException("Error loading image from project: " + e.getMessage(), e);
}
}
use of edu.cmu.cs.hcii.cogtool.ui.Interaction in project cogtool by cogtool.
the class DefaultController method closeWindow.
/**
* Close the window associated with this controller.
* <p>
* If the associated <code>Project</code> instance or any of its
* "children" have been modified, we may want to try to save the changes.
* <p>
* Two policies have been implemented: (1) if this is the last window
* open for the associated project, then check to save; OR (2) if this
* is the project's window (that is, a <code>ProjectController</code>),
* then check to save. This policy is controlled by the
* <code>PROJECT_MANAGES_OTHERS</code> flag in <code>CogTool</code>;
* if true, then policy (2) is used, otherwise (1) is used.
* <p>
* Note: Policy (2) assumes that all other open editor windows managing
* objects belonging to the project are closed when its window is closed
* (see <code>ProjectController.dispose()</code>).
* <p>
* If saving, the user is asked whether to save the associated project.
* One response is to abort/cancel the window closing.
* <p>
* Sometimes, a window may be closed in the process of opening another
* window (such as when the Frame Chooser window is closed to open the
* corresponding Script Demonstration window). In this case, we don't
* want to check to save -- thus, the parameter.
*
* @param checkToSave whether to check to save the associated project
* @return true if and only if the close was approved and successful
* @throws RcvrIOSaveException if the save operation fails
* TODO: Change the exception to one that should be caught and managed!
* @author mlh
*/
protected boolean closeWindow(boolean checkToSave) {
// project (regardless of whether it is the ProjectController).
if (checkToSave && ((CogTool.projectManagesOthers && // this: ProjectController
(project == getModelObject())) || (CogTool.controllerNexus.getControllerCount(project) == 1))) {
boolean needToAsk;
try {
needToAsk = !UndoManager.isAtSavePoint(project);
} catch (IllegalStateException ex) {
System.err.println("Ignoring that isAtSavePoint failed.");
needToAsk = true;
}
if (needToAsk) {
Interaction interaction = getUI().getStandardInteraction();
// ask to save.
switch(interaction.askSaveBeforeClose(project.getName())) {
case Interaction.SAVE:
{
// Try to save; user may get the chance to abort/cancel
if (!forceSave()) {
// close aborted/canceled
return false;
}
// ok to close
break;
}
case Interaction.NO_SAVE:
{
// ok to close
break;
}
case Interaction.CANCEL:
{
// aborted/canceled
return false;
}
}
}
// Close window and recover resources.
dispose();
recoverManagers(project);
} else {
// No need to attempt to save (perhaps not modified).
// Close window and recover resources.
dispose();
}
return true;
}
use of edu.cmu.cs.hcii.cogtool.ui.Interaction in project cogtool by cogtool.
the class DefaultController method assignActions.
/**
* Registers the set of <code>IListenerAction</code> instances
* that implement the semantic actions that are possible.
* <p>
* For this class, this consists of the actions that all CogTool
* model editing windows support.
*
* @author mlh
*/
@Override
protected void assignActions() {
super.assignActions();
UI ui = getUI();
final Interaction interaction = getUI().getStandardInteraction();
if (ui != null) {
// Set "save as" action
ui.setAction(CogToolLID.SaveProjectAs, new AListenerAction() {
public boolean performAction(Object prms) {
return saveAs();
}
});
// Set "save" action
ui.setAction(CogToolLID.SaveProject, new AListenerAction() {
public boolean performAction(Object prms) {
return forceSave();
}
});
ui.setAction(CogToolLID.CloseWindow, createCloseWindowAction());
ui.setAction(CogToolLID.CloseProject, new AListenerAction() {
public boolean performAction(Object prms) {
return closeProject(project, true);
}
});
ui.setAction(CogToolLID.Properties, new AListenerAction() {
public boolean performAction(Object prms) {
return showProperties();
}
});
ui.setAction(CogToolLID.SetAttribute, new IListenerAction() {
public Class<?> getParameterClass() {
return UI.SetAttributeParameters.class;
}
public boolean performAction(Object prms) {
UI.SetAttributeParameters p = (UI.SetAttributeParameters) prms;
return DefaultCmd.setAttribute(p.target, null, p.attrName, p.value, interaction, undoMgr);
}
});
}
}
use of edu.cmu.cs.hcii.cogtool.ui.Interaction in project cogtool by cogtool.
the class DefaultController method showProperties.
protected boolean showProperties() {
Interaction interaction = getUI().getStandardInteraction();
interaction.showProjectProperties(project);
return true;
}
use of edu.cmu.cs.hcii.cogtool.ui.Interaction in project cogtool by cogtool.
the class DefaultController method saveAs.
/**
* Perform a Save As... operation, prompting for a save location.
*
* @return true if file was saved, false otherwise
* @throws RcvrIOException if the save operation fails
*/
protected boolean saveAs() {
Interaction stdInteraction = getUI().getStandardInteraction();
try {
boolean nameIsInUse;
File saveLoc;
do {
// Request a new file name, using the project's current name
// as the default.
// TODO: It is "evil" that the test for existing files is
// hidden within this call; separate at some point (mlh)
saveLoc = stdInteraction.selectFileDest(project.getName());
// If the save dialog was canceled, do nothing.
if (saveLoc == null) {
return false;
}
// If saveLoc is already open, refuse to save there.
nameIsInUse = persist.isRegistered(saveLoc, project);
if (nameIsInUse) {
switch(stdInteraction.protestBeingEdited(saveLoc)) {
case Interaction.SAVE:
{
nameIsInUse = false;
break;
}
case Interaction.NO_SAVE:
{
// Simply loop to ask for a new name
break;
}
case Interaction.CANCEL:
{
return false;
}
}
}
} while (nameIsInUse);
CogToolPref.setRecent(saveLoc.getCanonicalPath());
// Set the title BEFORE saving the project.
String name = saveLoc.getName();
// Remove the .cgt, if it is there.
int periodIndex = name.lastIndexOf('.');
if (periodIndex > 0) {
name = name.substring(0, periodIndex);
}
project.setName(name);
// Save to the selected location.
project.setBuildVersion(CogTool.getVersion());
persist.save(project, saveLoc);
// Tell undo manager(s) that a save has just occurred
try {
UndoManager.markSavePoint(project);
} catch (IllegalStateException ex) {
throw new RcvrCannotUndoRedoException("Marking save point", ex);
}
return true;
} catch (IOException e) {
throw new RcvrIOSaveException("Error persisting project: " + e.getMessage(), e);
}
}
Aggregations