📄 dbdesignerdocumentprovider.java
字号:
package dbdesigner.diagram.part;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.MultiRule;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.emf.transaction.NotificationFilter;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
import org.eclipse.gmf.runtime.diagram.core.DiagramEditingDomainFactory;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.AbstractDocumentProvider;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.DiagramDocument;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocumentProvider;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.internal.EditorStatusCodes;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.internal.util.DiagramIOUtil;
import org.eclipse.gmf.runtime.emf.core.resources.GMFResourceFactory;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.part.FileEditorInput;
/**
* @generated
*/
public class DbdesignerDocumentProvider extends AbstractDocumentProvider
implements IDiagramDocumentProvider {
/**
* @generated
*/
protected ElementInfo createElementInfo(Object element)
throws CoreException {
if (false == element instanceof FileEditorInput
&& false == element instanceof URIEditorInput) {
throw new CoreException(
new Status(
IStatus.ERROR,
DbdesignerDiagramEditorPlugin.ID,
0,
NLS
.bind(
Messages.DbdesignerDocumentProvider_IncorrectInputError,
new Object[] {
element,
"org.eclipse.ui.part.FileEditorInput",
"org.eclipse.emf.common.ui.URIEditorInput" }),
null));
}
IEditorInput editorInput = (IEditorInput) element;
IDiagramDocument document = (IDiagramDocument) createDocument(editorInput);
ResourceSetInfo info = new ResourceSetInfo(document, editorInput);
info.setModificationStamp(computeModificationStamp(info));
info.fStatus = null;
return info;
}
/**
* @generated
*/
protected IDocument createDocument(Object element) throws CoreException {
if (false == element instanceof FileEditorInput
&& false == element instanceof URIEditorInput) {
throw new CoreException(
new Status(
IStatus.ERROR,
DbdesignerDiagramEditorPlugin.ID,
0,
NLS
.bind(
Messages.DbdesignerDocumentProvider_IncorrectInputError,
new Object[] {
element,
"org.eclipse.ui.part.FileEditorInput",
"org.eclipse.emf.common.ui.URIEditorInput" }),
null));
}
IDocument document = createEmptyDocument();
setDocumentContent(document, (IEditorInput) element);
setupDocument(element, document);
return document;
}
/**
* Sets up the given document as it would be provided for the given element. The
* content of the document is not changed. This default implementation is empty.
* Subclasses may reimplement.
*
* @param element the blue-print element
* @param document the document to set up
* @generated
*/
protected void setupDocument(Object element, IDocument document) {
// for subclasses
}
/**
* @generated
*/
private long computeModificationStamp(ResourceSetInfo info) {
int result = 0;
for (Iterator it = info.getResourceSet().getResources().iterator(); it
.hasNext();) {
Resource nextResource = (Resource) it.next();
IFile file = WorkspaceSynchronizer.getFile(nextResource);
if (file != null) {
if (file.getLocation() != null) {
result += file.getLocation().toFile().lastModified();
} else {
result += file.getModificationStamp();
}
}
}
return result;
}
/**
* @generated
*/
protected IDocument createEmptyDocument() {
DiagramDocument document = new DiagramDocument();
document.setEditingDomain(createEditingDomain());
return document;
}
/**
* @generated
*/
private TransactionalEditingDomain createEditingDomain() {
TransactionalEditingDomain editingDomain = DiagramEditingDomainFactory
.getInstance().createEditingDomain();
editingDomain.setID("com.my.dbdesigner.diagram.EditingDomain"); //$NON-NLS-1$
final NotificationFilter diagramResourceModifiedFilter = NotificationFilter
.createNotifierFilter(editingDomain.getResourceSet()).and(
NotificationFilter
.createEventTypeFilter(Notification.ADD)).and(
NotificationFilter.createFeatureFilter(
ResourceSet.class,
ResourceSet.RESOURCE_SET__RESOURCES));
editingDomain.getResourceSet().eAdapters().add(new Adapter() {
private Notifier myTarger;
public Notifier getTarget() {
return myTarger;
}
public boolean isAdapterForType(Object type) {
return false;
}
public void notifyChanged(Notification notification) {
if (diagramResourceModifiedFilter.matches(notification)) {
Object value = notification.getNewValue();
if (value instanceof Resource) {
((Resource) value).setTrackingModification(true);
}
}
}
public void setTarget(Notifier newTarget) {
myTarger = newTarget;
}
});
return editingDomain;
}
/**
* @generated
*/
protected void setDocumentContent(IDocument document, IEditorInput element)
throws CoreException {
IDiagramDocument diagramDocument = (IDiagramDocument) document;
TransactionalEditingDomain domain = diagramDocument.getEditingDomain();
if (element instanceof FileEditorInput) {
IStorage storage = ((FileEditorInput) element).getStorage();
Diagram diagram = DiagramIOUtil.load(domain, storage, true,
getProgressMonitor());
document.setContent(diagram);
} else if (element instanceof URIEditorInput) {
URI uri = ((URIEditorInput) element).getURI();
Resource resource = null;
try {
resource = domain.getResourceSet().getResource(
uri.trimFragment(), false);
if (resource == null) {
resource = domain.getResourceSet().createResource(
uri.trimFragment());
}
if (!resource.isLoaded()) {
try {
Map options = new HashMap(GMFResourceFactory
.getDefaultLoadOptions());
// @see 171060
// options.put(org.eclipse.emf.ecore.xmi.XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE);
resource.load(options);
} catch (IOException e) {
resource.unload();
throw e;
}
}
if (uri.fragment() != null) {
EObject rootElement = resource.getEObject(uri.fragment());
if (rootElement instanceof Diagram) {
document.setContent((Diagram) rootElement);
return;
}
} else {
for (Iterator it = resource.getContents().iterator(); it
.hasNext();) {
Object rootElement = it.next();
if (rootElement instanceof Diagram) {
document.setContent((Diagram) rootElement);
return;
}
}
}
throw new RuntimeException(
Messages.DbdesignerDocumentProvider_NoDiagramInResourceError);
} catch (Exception e) {
CoreException thrownExcp = null;
if (e instanceof CoreException) {
thrownExcp = (CoreException) e;
} else {
String msg = e.getLocalizedMessage();
thrownExcp = new CoreException(
new Status(
IStatus.ERROR,
DbdesignerDiagramEditorPlugin.ID,
0,
msg != null ? msg
: Messages.DbdesignerDocumentProvider_DiagramLoadingError,
e));
}
throw thrownExcp;
}
} else {
throw new CoreException(
new Status(
IStatus.ERROR,
DbdesignerDiagramEditorPlugin.ID,
0,
NLS
.bind(
Messages.DbdesignerDocumentProvider_IncorrectInputError,
new Object[] {
element,
"org.eclipse.ui.part.FileEditorInput",
"org.eclipse.emf.common.ui.URIEditorInput" }),
null));
}
}
/**
* @generated
*/
public long getModificationStamp(Object element) {
ResourceSetInfo info = getResourceSetInfo(element);
if (info != null) {
return computeModificationStamp(info);
}
return super.getModificationStamp(element);
}
/**
* @generated
*/
public boolean isDeleted(Object element) {
IDiagramDocument document = getDiagramDocument(element);
if (document != null) {
Resource diagramResource = document.getDiagram().eResource();
if (diagramResource != null) {
IFile file = WorkspaceSynchronizer.getFile(diagramResource);
return file == null || file.getLocation() == null
|| !file.getLocation().toFile().exists();
}
}
return super.isDeleted(element);
}
/**
* @generated
*/
public ResourceSetInfo getResourceSetInfo(Object editorInput) {
return (ResourceSetInfo) super.getElementInfo(editorInput);
}
/**
* @generated
*/
protected void disposeElementInfo(Object element, ElementInfo info) {
if (info instanceof ResourceSetInfo) {
ResourceSetInfo resourceSetInfo = (ResourceSetInfo) info;
resourceSetInfo.dispose();
}
super.disposeElementInfo(element, info);
}
/**
* @generated
*/
protected void doValidateState(Object element, Object computationContext)
throws CoreException {
ResourceSetInfo info = getResourceSetInfo(element);
if (info != null) {
Collection files2Validate = new ArrayList();
for (Iterator it = info.getResourceSet().getResources().iterator(); it
.hasNext();) {
Resource nextResource = (Resource) it.next();
IFile file = WorkspaceSynchronizer.getFile(nextResource);
if (file != null && file.isReadOnly()) {
files2Validate.add(file);
}
}
ResourcesPlugin.getWorkspace().validateEdit(
(IFile[]) files2Validate.toArray(new IFile[files2Validate
.size()]), computationContext);
}
super.doValidateState(element, computationContext);
}
/**
* @generated
*/
public boolean isReadOnly(Object element) {
ResourceSetInfo info = getResourceSetInfo(element);
if (info != null) {
if (info.isUpdateCache()) {
try {
updateCache(element);
} catch (CoreException ex) {
DbdesignerDiagramEditorPlugin.getInstance().logError(
Messages.DbdesignerDocumentProvider_isModifiable,
ex);
// Error message to log was initially taken from org.eclipse.gmf.runtime.diagram.ui.resources.editor.ide.internal.l10n.EditorMessages.StorageDocumentProvider_isModifiable
}
}
return info.isReadOnly();
}
return super.isReadOnly(element);
}
/**
* @generated
*/
public boolean isModifiable(Object element) {
if (!isStateValidated(element)) {
if (element instanceof FileEditorInput
|| element instanceof URIEditorInput) {
return true;
}
}
ResourceSetInfo info = getResourceSetInfo(element);
if (info != null) {
if (info.isUpdateCache()) {
try {
updateCache(element);
} catch (CoreException ex) {
DbdesignerDiagramEditorPlugin.getInstance().logError(
Messages.DbdesignerDocumentProvider_isModifiable,
ex);
// Error message to log was initially taken from org.eclipse.gmf.runtime.diagram.ui.resources.editor.ide.internal.l10n.EditorMessages.StorageDocumentProvider_isModifiable
}
}
return info.isModifiable();
}
return super.isModifiable(element);
}
/**
* @generated
*/
protected void updateCache(Object element) throws CoreException {
ResourceSetInfo info = getResourceSetInfo(element);
if (info != null) {
for (Iterator it = info.getResourceSet().getResources().iterator(); it
.hasNext();) {
Resource nextResource = (Resource) it.next();
IFile file = WorkspaceSynchronizer.getFile(nextResource);
if (file != null && file.isReadOnly()) {
info.setReadOnly(true);
info.setModifiable(false);
return;
}
}
info.setReadOnly(false);
info.setModifiable(true);
return;
}
}
/**
* @generated
*/
protected void doUpdateStateCache(Object element) throws CoreException {
ResourceSetInfo info = getResourceSetInfo(element);
if (info != null) {
info.setUpdateCache(true);
}
super.doUpdateStateCache(element);
}
/**
* @generated
*/
public boolean isSynchronized(Object element) {
ResourceSetInfo info = getResourceSetInfo(element);
if (info != null) {
return info.isSynchronized();
}
return super.isSynchronized(element);
}
/**
* @generated
*/
protected ISchedulingRule getResetRule(Object element) {
ResourceSetInfo info = getResourceSetInfo(element);
if (info != null) {
Collection rules = new ArrayList();
for (Iterator it = info.getResourceSet().getResources().iterator(); it
.hasNext();) {
Resource nextResource = (Resource) it.next();
IFile file = WorkspaceSynchronizer.getFile(nextResource);
if (file != null) {
rules.add(ResourcesPlugin.getWorkspace().getRuleFactory()
.modifyRule(file));
}
}
return new MultiRule((ISchedulingRule[]) rules
.toArray(new ISchedulingRule[rules.size()]));
}
return null;
}
/**
* @generated
*/
protected ISchedulingRule getSaveRule(Object element) {
ResourceSetInfo info = getResourceSetInfo(element);
if (info != null) {
Collection rules = new ArrayList();
for (Iterator it = info.getResourceSet().getResources().iterator(); it
.hasNext();) {
Resource nextResource = (Resource) it.next();
IFile file = WorkspaceSynchronizer.getFile(nextResource);
if (file != null) {
rules.add(computeSchedulingRule(file));
}
}
return new MultiRule((ISchedulingRule[]) rules
.toArray(new ISchedulingRule[rules.size()]));
}
return null;
}
/**
* @generated
*/
protected ISchedulingRule getSynchronizeRule(Object element) {
ResourceSetInfo info = getResourceSetInfo(element);
if (info != null) {
Collection rules = new ArrayList();
for (Iterator it = info.getResourceSet().getResources().iterator(); it
.hasNext();) {
Resource nextResource = (Resource) it.next();
IFile file = WorkspaceSynchronizer.getFile(nextResource);
if (file != null) {
rules.add(ResourcesPlugin.getWorkspace().getRuleFactory()
.refreshRule(file));
}
}
return new MultiRule((ISchedulingRule[]) rules
.toArray(new ISchedulingRule[rules.size()]));
}
return null;
}
/**
* @generated
*/
protected ISchedulingRule getValidateStateRule(Object element) {
ResourceSetInfo info = getResourceSetInfo(element);
if (info != null) {
Collection files = new ArrayList();
for (Iterator it = info.getResourceSet().getResources().iterator(); it
.hasNext();) {
Resource nextResource = (Resource) it.next();
IFile file = WorkspaceSynchronizer.getFile(nextResource);
if (file != null) {
files.add(file);
}
}
return ResourcesPlugin.getWorkspace().getRuleFactory()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -