blob: 22eaeefe01ae7ca39eb402fb0c64afdc271b3dbf [file] [log] [blame]
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2006 IBM Corporation and others.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
// IBM Corporation - initial implementation
//------------------------------------------------------------------------------
package org.eclipse.epf.authoring.ui.editors;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EventObject;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandStackListener;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.epf.authoring.ui.AuthoringUIPlugin;
import org.eclipse.epf.authoring.ui.AuthoringUIResources;
import org.eclipse.epf.authoring.ui.forms.CategoryGuidancePage;
import org.eclipse.epf.authoring.ui.forms.ChecklistItemsPage;
import org.eclipse.epf.authoring.ui.forms.ContentPackageDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.CustomCategoryAssignPage;
import org.eclipse.epf.authoring.ui.forms.CustomCategoryDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.DisciplineDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.DisciplineGroupingDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.DisciplineGroupingDisciplinesPage;
import org.eclipse.epf.authoring.ui.forms.DisciplineReferenceWorkflowPage;
import org.eclipse.epf.authoring.ui.forms.DisciplineTasksPage;
import org.eclipse.epf.authoring.ui.forms.DomainDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.DomainWorkProductsPage;
import org.eclipse.epf.authoring.ui.forms.GuidanceDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.IRefreshable;
import org.eclipse.epf.authoring.ui.forms.MethodLibraryDescriptionFormPage;
import org.eclipse.epf.authoring.ui.forms.MethodPluginDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.PracticeDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.PracticeReferencesPage;
import org.eclipse.epf.authoring.ui.forms.RoleCategoriesPage;
import org.eclipse.epf.authoring.ui.forms.RoleDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.RoleGuidancePage;
import org.eclipse.epf.authoring.ui.forms.RoleSetDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.RoleSetGroupingDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.RoleSetRolesPage;
import org.eclipse.epf.authoring.ui.forms.RoleWorkProductsPage;
import org.eclipse.epf.authoring.ui.forms.TaskCategoriesPage;
import org.eclipse.epf.authoring.ui.forms.TaskDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.TaskGuidancePage;
import org.eclipse.epf.authoring.ui.forms.TaskRolesPage;
import org.eclipse.epf.authoring.ui.forms.TaskStepsPage;
import org.eclipse.epf.authoring.ui.forms.TaskWorkProductsPage;
import org.eclipse.epf.authoring.ui.forms.TemplateDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.ToolDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.ToolToolMentorsPage;
import org.eclipse.epf.authoring.ui.forms.WorkProductCategoriesPage;
import org.eclipse.epf.authoring.ui.forms.WorkProductDeliverablePartsPage;
import org.eclipse.epf.authoring.ui.forms.WorkProductDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.WorkProductGuidancePage;
import org.eclipse.epf.authoring.ui.forms.WorkProductTypeDescriptionPage;
import org.eclipse.epf.authoring.ui.forms.WorkProductTypeWorkProductsPage;
import org.eclipse.epf.authoring.ui.richtext.IMethodRichText;
import org.eclipse.epf.authoring.ui.util.RoleSetGroupingRoleSets;
import org.eclipse.epf.authoring.ui.views.ElementHTMLViewer;
import org.eclipse.epf.authoring.ui.views.ViewHelper;
import org.eclipse.epf.common.utils.StrUtil;
import org.eclipse.epf.library.edit.command.ActionManager;
import org.eclipse.epf.library.edit.command.FullyRevertibleCommandStack;
import org.eclipse.epf.library.edit.command.IActionManager;
import org.eclipse.epf.library.edit.command.IResourceAwareCommand;
import org.eclipse.epf.library.edit.ui.UserInteractionHelper;
import org.eclipse.epf.library.edit.util.TngUtil;
import org.eclipse.epf.library.services.LibraryProcessor;
import org.eclipse.epf.richtext.IRichText;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import com.ibm.uma.Checklist;
import com.ibm.uma.ContentPackage;
import com.ibm.uma.CustomCategory;
import com.ibm.uma.Deliverable;
import com.ibm.uma.Discipline;
import com.ibm.uma.DisciplineGrouping;
import com.ibm.uma.Domain;
import com.ibm.uma.Guidance;
import com.ibm.uma.MethodElement;
import com.ibm.uma.MethodLibrary;
import com.ibm.uma.MethodPlugin;
import com.ibm.uma.Practice;
import com.ibm.uma.Role;
import com.ibm.uma.RoleSet;
import com.ibm.uma.RoleSetGrouping;
import com.ibm.uma.Task;
import com.ibm.uma.Template;
import com.ibm.uma.Tool;
import com.ibm.uma.UmaPackage;
import com.ibm.uma.WorkProduct;
import com.ibm.uma.WorkProductType;
import com.ibm.uma.util.ContentDescriptionFactory;
import com.ibm.uma.util.IMethodLibraryPersister;
import com.ibm.uma.util.IMethodLibraryPersister.FailSafeMethodLibraryPersister;
/**
* The Method Element editor.
*
* @author Shilpa Toraskar
* @author Phong Nguyen Le
* @author Kelvin Low
* @since 1.0
*/
public class MethodElementEditor extends AbstractBaseFormEditor implements
ISelectionProvider {
/**
* The editor ID.
*/
public static final String EDITOR_ID = MethodElementEditor.class.getName();
protected int preview_page_index = -1;
protected ElementHTMLViewer previewer = null;
protected MethodElement elementObj = null;
// The rich text control or editor whose content was last modified before
// the Save or Save All button key is selected.
protected IMethodRichText modifiedRichText = null;
protected Adapter nameChangedListener = new AdapterImpl() {
public void notifyChanged(org.eclipse.emf.common.notify.Notification msg) {
switch (msg.getFeatureID(MethodElement.class)) {
case UmaPackage.METHOD_ELEMENT__NAME:
setPartName();
if (pages != null) {
for (Iterator iter = pages.iterator(); iter.hasNext();) {
Object page = iter.next();
if(page instanceof IRefreshable) {
((IRefreshable)page).refreshName(elementObj.getName());
}
}
}
break;
}
}
};
protected Collection selectionChangedListeners = new ArrayList();
protected ISelection currentSelection = StructuredSelection.EMPTY;
protected ActionManager actionMgr;
private boolean dirty;
private FailSafeMethodLibraryPersister persister;
private Collection resourcesToAdjustLocation = new HashSet();
private Collection widgetsToRestoreValue = new HashSet();
private long changeTime = -1;
public Object widgetToCheck;
private Object currentFeatureEditor;
private EStructuralFeature currentEditedFeature;
/**
* Creates a new instance.
*/
public MethodElementEditor() {
super();
actionMgr = new ActionManager() {
protected FullyRevertibleCommandStack createCommandStack() {
return new FullyRevertibleCommandStack(this) {
public boolean doExecute(Command command) {
// Check modify first.
if (command instanceof IResourceAwareCommand) {
Collection modifiedResources = ((IResourceAwareCommand) command)
.getModifiedResources();
if (modifiedResources != null
&& !(modifiedResources.isEmpty())) {
IStatus status = UserInteractionHelper
.checkModify(modifiedResources,
getSite().getShell());
if (!status.isOK()) {
AuthoringUIPlugin
.getDefault()
.getMsgDialog()
.display(
AuthoringUIResources
.getString("AuthoringUI.errorDialog.title"), //$NON-NLS-1$
AuthoringUIResources
.getString("AuthoringUI.editDialog.msgCannotEdit"), //$NON-NLS-1$
status);
return false;
}
}
} else {
EObject owner = TngUtil.getOwner(command);
if (owner != null) {
IStatus status = TngUtil.checkEdit(owner,
getSite().getShell());
if (!status.isOK()) {
AuthoringUIPlugin
.getDefault()
.getMsgDialog()
.display(
AuthoringUIResources
.getString("AuthoringUI.errorDialog.title"), //$NON-NLS-1$
AuthoringUIResources
.getString("AuthoringUI.editDialog.msgCannotEdit"), //$NON-NLS-1$
status);
return false;
}
}
}
if(changeTime == -1) {
changeTime = System.currentTimeMillis();
}
boolean ret = super.doExecute(command);
if(!ret && changeTime != -1) {
changeTime = -1;
}
return ret;
}
};
}
public boolean doAction(int actionType, EObject object,
org.eclipse.emf.ecore.EStructuralFeature feature,
Object value, int index) {
IStatus status = TngUtil
.checkEdit(object, getSite().getShell());
if (status.isOK()) {
return super.doAction(actionType, object, feature, value,
index);
} else {
AuthoringUIPlugin
.getDefault()
.getMsgDialog()
.displayError(
AuthoringUIResources
.getString("AuthoringUI.editDialog.title"), //$NON-NLS-1$
AuthoringUIResources
.getString("AuthoringUI.editDialog.msgCannotEdit"), //$NON-NLS-1$
status);
return false;
}
}
protected void save(Resource resource) {
//
File file = new File(resource.getURI().toFileString());
try {
if(file.lastModified() > changeTime) {
IMethodLibraryPersister.FailSafeMethodLibraryPersister persister = getPersister();
try {
persister.save(resource);
persister.commit();
} catch (Exception e) {
AuthoringUIPlugin.getDefault().getLogger().logError(e);
try {
persister.rollback();
} catch (Exception ex) {
ViewHelper
.reloadCurrentLibaryOnRollbackError(getEditorSite()
.getShell());
}
}
}
}
finally {
changeTime = -1;
}
}
};
actionMgr.getCommandStack().addCommandStackListener(
new CommandStackListener() {
public void commandStackChanged(EventObject event) {
firePropertyChange(IEditorPart.PROP_DIRTY);
}
});
LibraryProcessor.getInstance().listenTo(actionMgr.getCommandStack());
}
public IActionManager getActionManager() {
return actionMgr;
}
/**
* @see org.eclipse.ui.forms.editor.FormEditor#init(IEditorSite,
* IEditorInput)
*/
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
setSite(site);
setInput(input);
site.setSelectionProvider(this);
// Get method element object from Editor input.
MethodElementEditorInput methodElementInput = (MethodElementEditorInput) input;
elementObj = methodElementInput.getMethodElement();
setPartName();
elementObj.eAdapters().add(nameChangedListener);
}
public void setPartName() {
String partName = elementObj.getName();
if (partName == null) {
partName = ""; //$NON-NLS-1$
}
setPartName(partName);
}
public void addSelectionChangedListener(ISelectionChangedListener listener) {
selectionChangedListeners.add(listener);
}
public void removeSelectionChangedListener(
ISelectionChangedListener listener) {
selectionChangedListeners.remove(listener);
}
public ISelection getSelection() {
return currentSelection;
}
public void setSelection(ISelection selection) {
currentSelection = selection;
for (Iterator listeners = new ArrayList(selectionChangedListeners)
.iterator(); listeners.hasNext();) {
ISelectionChangedListener listener = (ISelectionChangedListener) listeners
.next();
listener
.selectionChanged(new SelectionChangedEvent(this, selection));
}
}
/**
* @see org.eclipse.ui.forms.editor.FormEditor#addPages()
*/
protected void addPages() {
try {
if (elementObj instanceof MethodLibrary) {
addPage(new MethodLibraryDescriptionFormPage(this));
} else if (elementObj instanceof MethodPlugin) {
addPage(new MethodPluginDescriptionPage(this));
} else if (elementObj instanceof ContentPackage) {
addPage(new ContentPackageDescriptionPage(this));
} else if (elementObj instanceof Role) {
addPage(new RoleDescriptionPage(this));
addPage(new RoleWorkProductsPage(this));
addPage(new RoleGuidancePage(this));
addPage(new RoleCategoriesPage(this));
} else if (elementObj instanceof Task) {
addPage(new TaskDescriptionPage(this));
addPage(new TaskStepsPage(this));
addPage(new TaskRolesPage(this));
addPage(new TaskWorkProductsPage(this));
addPage(new TaskGuidancePage(this));
addPage(new TaskCategoriesPage(this));
} else if (elementObj instanceof WorkProduct) {
addPage(new WorkProductDescriptionPage(this));
if (elementObj instanceof Deliverable) {
addPage(new WorkProductDeliverablePartsPage(this));
}
addPage(new WorkProductGuidancePage(this));
addPage(new WorkProductCategoriesPage(this));
} else if (elementObj instanceof Guidance) {
if (elementObj instanceof Template) {
addPage(new TemplateDescriptionPage(this));
} else if (elementObj instanceof Practice) {
addPage(new PracticeDescriptionPage(this));
addPage(new PracticeReferencesPage(this));
} else if (elementObj instanceof Checklist) {
addPage(new GuidanceDescriptionPage(this));
addPage(new ChecklistItemsPage(this));
} else {
addPage(new GuidanceDescriptionPage(this));
}
if (!(elementObj instanceof Practice))
addPage(new CategoryGuidancePage(this));
} else if (elementObj instanceof Discipline) {
addPage(new DisciplineDescriptionPage(this));
addPage(new DisciplineTasksPage(this));
addPage(new DisciplineReferenceWorkflowPage(this));
addPage(new CategoryGuidancePage(this));
} else if (elementObj instanceof DisciplineGrouping) {
addPage(new DisciplineGroupingDescriptionPage(this));
addPage(new DisciplineGroupingDisciplinesPage(this));
} else if (elementObj instanceof Domain) {
addPage(new DomainDescriptionPage(this));
addPage(new DomainWorkProductsPage(this));
addPage(new CategoryGuidancePage(this));
} else if (elementObj instanceof WorkProductType) {
addPage(new WorkProductTypeDescriptionPage(this));
addPage(new WorkProductTypeWorkProductsPage(this));
addPage(new CategoryGuidancePage(this));
} else if (elementObj instanceof RoleSet) {
addPage(new RoleSetDescriptionPage(this));
addPage(new RoleSetRolesPage(this));
addPage(new CategoryGuidancePage(this));
} else if (elementObj instanceof RoleSetGrouping) {
addPage(new RoleSetGroupingDescriptionPage(this));
addPage(new RoleSetGroupingRoleSets(this));
} else if (elementObj instanceof Tool) {
addPage(new ToolDescriptionPage(this));
addPage(new ToolToolMentorsPage(this));
addPage(new CategoryGuidancePage(this));
} else if (elementObj instanceof CustomCategory) {
addPage(new CustomCategoryDescriptionPage(this));
addPage(new CustomCategoryAssignPage(this));
}
setPartName(elementObj.getName());
ILabelProvider labelProvider = new AdapterFactoryLabelProvider(
TngUtil.umaItemProviderAdapterFactory);
Image titleImage = labelProvider.getImage(elementObj);
labelProvider.dispose();
setTitleImage(titleImage);
if (!(elementObj instanceof ContentPackage || elementObj instanceof MethodPlugin))
createPreviewPage();
} catch (Throwable t) {
t.printStackTrace();
dispose();
throw (SWTError) t;
}
}
public void dispose() {
try {
modifiedRichText = null;
LibraryProcessor.getInstance().stopListeningTo(
actionMgr.getCommandStack());
if (isDirty()) {
actionMgr.undoAll();
}
actionMgr.dispose();
elementObj.eAdapters().remove(nameChangedListener);
} finally {
super.dispose();
}
}
/**
* @see org.eclipse.epf.authoring.ui.editors.AbstractBaseFormEditor#isDirty()
*/
public boolean isDirty() {
if (dirty)
return true;
dirty = actionMgr.isSaveNeeded();
return dirty;
}
public FailSafeMethodLibraryPersister getPersister() {
if (persister == null) {
persister = ContentDescriptionFactory.getMethodLibraryPersister()
.getFailSafePersister();
}
return persister;
}
protected Collection getModifiedResources() {
return actionMgr.getModifiedResources();
}
public void setModifiedRichText(IMethodRichText modifiedRichText) {
this.modifiedRichText = modifiedRichText;
}
public void saveModifiedRichText(IMethodRichText richText) {
if (richText != null && !richText.isDisposed()
&& richText.getModified()) {
EObject modalObject = richText.getModalObject();
EStructuralFeature modalObjectFeature = richText
.getModalObjectFeature();
if (modalObject != null && modalObjectFeature != null) {
Object oldContent = modalObject.eGet(modalObjectFeature);
if (!mustRestoreValue(richText, oldContent)) {
Object newContent = richText.getText();
if (!newContent.equals(oldContent)) {
actionMgr.doAction(IActionManager.SET, modalObject,
modalObjectFeature, newContent, -1);
}
}
}
}
}
public void saveModifiedRichText() {
saveModifiedRichText(modifiedRichText);
}
public void doSave(IProgressMonitor monitor) {
if (debug) {
System.out
.println("MethodElementEditor.doSave: saving changes to method library"); //$NON-NLS-1$
}
saveModifiedRichText(modifiedRichText);
getPersister();
try {
// Save.
Collection modifiedResources = getModifiedResources();
for (Iterator iter = modifiedResources.iterator(); iter.hasNext();) {
Resource resource = (Resource) iter.next();
try {
persister.save(resource);
} catch (Exception e) {
String title = AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.saveErrorTitle"); //$NON-NLS-1$
String msg = AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.saveErrorMessage1"); //$NON-NLS-1$
String reason = e.getMessage() == null ? AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.saveErrorReason1") : e.getMessage(); //$NON-NLS-1$
AuthoringUIPlugin.getDefault().getMsgDialog().displayError(
title, msg, reason, e);
throw e;
}
}
try {
persister.commit();
dirty = false;
actionMgr.saveIsDone();
changeTime = -1;
} catch (Exception e) {
String reason = e.getMessage();
if (StrUtil.isBlank(reason)) {
reason = AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.saveErrorReason2"); //$NON-NLS-1$
}
StringWriter details = new StringWriter();
e.printStackTrace(new PrintWriter(details));
String title = AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.saveErrorTitle"); //$NON-NLS-1$
String message = AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.saveErrorMessage2"); //$NON-NLS-1$
AuthoringUIPlugin.getDefault().getMsgDialog().displayError(
title, message, reason, details.toString(), e);
throw e;
}
// Rename.
if (!resourcesToAdjustLocation.isEmpty()) {
try {
persister.adjustLocation(resourcesToAdjustLocation);
} catch (Exception e) {
String title = AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.renameErrorTitle"); //$NON-NLS-1$
String template = AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.renameErrorMessage1"); //$NON-NLS-1$
StringBuffer fileList = new StringBuffer();
for (Iterator iter = resourcesToAdjustLocation.iterator(); iter
.hasNext();) {
Resource resource = (Resource) iter.next();
fileList.append(resource.getURI().toFileString())
.append(", "); //$NON-NLS-1$
}
String msg = MessageFormat.format(template,
new Object[] { fileList });
String reason = e.getMessage() == null ? AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.saveErrorReason1") : e.getMessage(); //$NON-NLS-1$
AuthoringUIPlugin.getDefault().getMsgDialog().displayError(
title, msg, reason, e);
throw e;
}
try {
persister.commit();
} catch (Exception e) {
String reason = e.getMessage();
if (StrUtil.isBlank(reason)) {
reason = AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.saveErrorReason2"); //$NON-NLS-1$
}
StringWriter details = new StringWriter();
e.printStackTrace(new PrintWriter(details));
String title = AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.renameErrorTitle"); //$NON-NLS-1$
String message = AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.saveErrorMessage2"); //$NON-NLS-1$
AuthoringUIPlugin.getDefault().getMsgDialog().displayError(
title, message, reason, details.toString(), e);
throw e;
}
resourcesToAdjustLocation.clear();
}
} catch (Exception e) {
try {
persister.rollback();
} catch (Exception ex) {
AuthoringUIPlugin.getDefault().getLogger().logError(ex);
ViewHelper.reloadCurrentLibaryOnRollbackError(getSite()
.getShell());
}
}
}
protected void saveResource(Resource resource) {
if (resource != null && resource.isModified()) {
try {
getPersister().save(resource);
} catch (Exception e) {
String title = AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.saveErrorTitle"); //$NON-NLS-1$
String template = AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.saveErrorMessage1"); //$NON-NLS-1$
String msg = MessageFormat.format(template,
new Object[] { resource.getURI().toFileString() });
String reason = e.getMessage() == null ? AuthoringUIResources
.getString("AuthoringUI.editors.MethodElementEditor.saveErrorReason1") : e.getMessage(); //$NON-NLS-1$
AuthoringUIPlugin.getDefault().getMsgDialog().displayError(
title, msg, reason, e);
}
}
}
protected void createPreviewPage() {
Composite parent = getContainer();
Composite previewComposite = new Composite(parent, SWT.NONE);
previewComposite.setLayout(new GridLayout());
PreviewPage page = new PreviewPage(previewComposite);
previewer = page.getPreviewViewer();
preview_page_index = addPage(previewComposite);
setPageText(preview_page_index, AuthoringUIResources
.getString("AuthoringUI.previewPage.title")); //$NON-NLS-1$
}
/**
* Override this method to listen to the tab change.
*/
protected void pageChange(int newPageIndex) {
super.pageChange(newPageIndex);
if (preview_page_index == newPageIndex && previewer != null) {
// Use the default layout manager.
previewer.setLayoutManager(null);
previewer.showElementContent(elementObj);
}
}
private void setDirty() {
dirty = true;
firePropertyChange(PROP_DIRTY);
}
public boolean mustRestoreValue(Object control, Object value) {
if(widgetToCheck == control) {
// the control is currently being checked for editable, but there is still focus lost
// event is being sent out even the check is not completed yet.
// return true so the focusLost() will not make any change to the control.
//
return true;
}
Object editControl = control;
if (editControl instanceof MethodRichTextEditor) {
editControl = ((MethodRichTextEditor) control).getRichTextControl();
}
boolean restore = widgetsToRestoreValue.contains(editControl);
if (!restore) {
synchronized (widgetsToRestoreValue) {
restore = widgetsToRestoreValue.contains(editControl);
}
}
if (restore) {
if (editControl instanceof Text) {
Text text = ((Text) editControl);
text.setText((String) value);
} else if (editControl instanceof IMethodRichText) {
IMethodRichText richText = (IMethodRichText) editControl;
richText.setText((String) value);
}
widgetsToRestoreValue.remove(editControl);
}
return restore;
}
public class ModifyListener implements
org.eclipse.swt.events.ModifyListener {
private EObject element;
private boolean checkContainerResource;
private boolean disabled = false;
/**
* Creates a new instance.
*/
public ModifyListener(EObject element, boolean checkContainer) {
super();
checkContainerResource = checkContainer;
this.element = element;
}
private void restoreText(Object control, String txt) {
boolean old = disabled;
try {
disabled = true;
Object editControl = control;
if (editControl instanceof MethodRichTextEditor) {
editControl = ((MethodRichTextEditor) control).getRichTextControl();
}
if (editControl instanceof Text) {
Text text = ((Text) editControl);
text.setText(txt);
} else if (editControl instanceof IMethodRichText) {
IMethodRichText richText = (IMethodRichText) editControl;
richText.setText(txt);
}
}
finally {
disabled = old;
}
}
private boolean checkEdit(EObject element, Object control,
boolean checkContainerResource) {
// keep a reference to the current widget so mustRestoreValue() can use it to check
// whether a focus lost event is triggered during a checkEdit. mustRestoreValue() then
// returns true so focusLost() will not try to make change to the value bound to the widget
//
widgetToCheck = control;
try {
if (debug) {
System.out
.println("MethodElementEditor.checkEdit: enter, control=" + control); //$NON-NLS-1$
}
IStatus status = null;
if (widgetsToRestoreValue.contains(control)) {
if (debug) {
System.out
.println("MethodElementEditor.checkEdit: widget found in widgetsToRestoreValue, exit"); //$NON-NLS-1$
}
return false;
}
synchronized (widgetsToRestoreValue) {
if (widgetsToRestoreValue.contains(control)) {
if (debug) {
System.out
.println("MethodElementEditor.checkEdit: widget found in widgetsToRestoreValue, exit"); //$NON-NLS-1$
}
return false;
}
status = TngUtil.checkEdit(element, getSite().getShell());
if (!status.isOK()) {
if (control instanceof IRichText) {
((IRichText) control).restoreText();
} else {
if(control == currentFeatureEditor) {
restoreText(control, (String) element.eGet(currentEditedFeature));
}
else {
// Add the control to the list of widgets whose value needs
// to be restored to the original one..
widgetsToRestoreValue.add(control);
if (debug) {
System.out
.println("MethodElementEditor.checkEdit: added widget to widgetsToRestoreValue"); //$NON-NLS-1$
}
}
}
} else if (checkContainerResource) {
if (element.eContainer() != null
&& element.eContainer().eResource() != element
.eResource()) {
status = TngUtil.checkEdit(element.eContainer(), getSite()
.getShell());
if (!status.isOK()) {
// // Add the control to the list of widgets whose value
// // needs to be restored to the original one.
// if (debug) {
// System.out
// .println("MethodElementEditor.checkEdit: added widget to widgetsToRestoreValue"); //$NON-NLS-1$
// }
if (control instanceof IRichText) {
((IRichText) control).restoreText();
} else {
if(control == currentFeatureEditor) {
restoreText(control, (String) element.eGet(currentEditedFeature));
}
else {
widgetsToRestoreValue.add(control);
}
}
}
}
}
}
if (status != null && !status.isOK()) {
AuthoringUIPlugin.getDefault().getMsgDialog().displayError(
AuthoringUIResources
.getString("AuthoringUI.editDialog.title"), //$NON-NLS-1$
AuthoringUIResources
.getString("AuthoringUI.editDialog.msgCannotEdit"), //$NON-NLS-1$
status);
return false;
}
return true;
}
finally {
// clear the reference when the check is done
//
widgetToCheck = null;
}
}
/**
* @see org.eclipse.swt.events.ModifyListener#modifyText(ModifyEvent)
*/
public void modifyText(ModifyEvent e) {
if (debug) {
System.out
.println("MethodElementEditor.ModifyListener.modifyText: enter, disabled=" + disabled); //$NON-NLS-1$
}
if (!disabled && element != null) {
if (e.widget == null) {
return;
}
Object widget = e.widget;
if (widget instanceof Browser) {
widget = ((Browser) widget)
.getData(IMethodRichText.PROPERTY_NAME);
if (widget == null) {
widget = e.widget;
}
}
if (debug) {
System.out
.println("MethodElementEditor.ModifyListener.modifyText: widget=" + widget); //$NON-NLS-1$
}
if (widgetsToRestoreValue.contains(widget)) {
if (debug) {
System.out
.println("MethodElementEditor.ModifyListener.modifyText: widget found in widgetsToRestoreValue, exit"); //$NON-NLS-1$
}
return;
}
if (!checkEdit(element, widget, checkContainerResource)) {
if (debug) {
System.out
.println("MethodElementEditor.ModifyListener.modifyText: checkEdit failed, exit"); //$NON-NLS-1$
}
return;
}
if (widget instanceof IMethodRichText) {
IMethodRichText richText = (IMethodRichText) widget;
setModifiedRichText(richText);
if (debug) {
System.out
.println("MethodElementEditor.ModifyListener.modifyText: adding to modifiedRichTexts list"); //$NON-NLS-1$
}
}
if (debug) {
System.out
.println("MethodElementEditor.ModifyListener.modifyText: marking editor as dirty"); //$NON-NLS-1$
}
setDirty();
}
if (debug) {
System.out
.println("MethodElementEditor.ModifyListener.modifyText: exit"); //$NON-NLS-1$
}
}
/**
* Sets the underlying model element.
*
* @param element
* A method element.
*/
public void setElement(EObject element) {
this.element = element;
}
/**
* Sets the disable flag.
*
* @param disabled
* If <code>true</code>, ignore the modify events.
*/
public void setDisable(boolean disabled) {
this.disabled = disabled;
}
}
public ModifyListener createModifyListener(EObject eObj) {
return createModifyListener(eObj, false);
}
public ModifyListener createModifyListener(EObject eObj,
boolean checkContainer) {
return new ModifyListener(eObj, checkContainer);
}
/**
* Sets the specified control to be the current editor of the specified feature.
*
* @param control
* @param feature
*/
public void setCurrentFeatureEditor(Object control, EStructuralFeature feature) {
currentFeatureEditor = control;
currentEditedFeature = feature;
}
protected void monitorChange() {
IExtensionRegistry registry = Platform.getExtensionRegistry();
IExtensionPoint point = registry
.getExtensionPoint("org.eclipse.core.resources.refreshProviders"); //$NON-NLS-1$
if (point == null)
return;
IExtension[] extensions = point.getExtensions();
for (int i = 0; i < extensions.length; i++) {
IExtension extension = extensions[i];
if (debug) {
System.out
.println("extension: UID=" + extension.getUniqueIdentifier() + ", " + extension); //$NON-NLS-1$ //$NON-NLS-2$
}
}
}
public void addResourceToAdjustLocation(Resource resource) {
resourcesToAdjustLocation.add(resource);
}
}