下面列出了org.eclipse.ui.IEditorPart#isDirty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final AnalysisKickOff akf = new AnalysisKickOff();
IEditorPart openEditor = UIUtils.getCurrentlyOpenEditor();
// check if there are unsaved changes
if (openEditor != null && openEditor.isDirty()) {
int answr = saveFile(Utils.getCurrentlyOpenFile());
// save file and analyze
if (answr == JOptionPane.YES_OPTION) {
openEditor.doSave(null);
}
// no analyze no save file
else if (answr == JOptionPane.CLOSED_OPTION) {
return null;
}
}
if (akf.setUp(JavaCore.create(Utils.getCurrentlySelectedIProject()))) {
akf.run();
}
return null;
}
public static Map<IFile, IDocument> evalDirtyDocs(IFile filter) {
Map<IFile, IDocument> result = new HashMap<IFile, IDocument>();
IWorkbench workbench = SearchCorePlugin.getDefault().getWorkbench();
IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
for (int i = 0; i < windows.length; i++) {
IWorkbenchPage[] pages = windows[i].getPages();
for (int x = 0; x < pages.length; x++) {
IEditorReference[] editorRefs = pages[x].getEditorReferences();
for (int z = 0; z < editorRefs.length; z++) {
IEditorPart ep = editorRefs[z].getEditor(false);
if (ep instanceof ITextEditor && ep.isDirty()) { // only
// dirty
// editors
evaluateTextEditor(result, ep, filter);
}
}
}
}
return result;
}
private static void checkAndAddToEditorLists(
List<IEditorPart> openedEditorRefs,
List<IEditorPart> openedDirtyEditorRefs, IEditorReference fileRef )
{
if ( fileRef != null )
{
IEditorPart part = (IEditorPart) fileRef.getPart( false );
if ( part != null )
{
if ( part.isDirty( ) )
{
openedDirtyEditorRefs.add( part );
}
openedEditorRefs.add( part );
}
}
}
/**
* Saves any modified files after confirmation from the user (if needed).
*
* @return true if the files were saved, false otherwise.
*/
public static boolean checkAndSaveAllFiles( )
{
ArrayList<IEditorPart> editorsToSave = new ArrayList<IEditorPart>( );
IWorkbench workbench = PlatformUI.getWorkbench( );
IWorkbenchWindow windows[] = workbench.getWorkbenchWindows( );
for ( int currWindow = 0; currWindow < windows.length; currWindow++ )
{
IWorkbenchPage pages[] = windows[currWindow].getPages( );
for ( int currPage = 0; currPage < pages.length; currPage++ )
{
IEditorReference editors[] = pages[currPage].getEditorReferences( );
for ( IEditorReference currEditorRef : editors )
{
IEditorPart currEditor = currEditorRef.getEditor( false );
if ( currEditor != null && currEditor.isDirty( ) )
{
editorsToSave.add( currEditor );
}
}
}
}
// Ask to save open files
return checkAndSaveDirtyFiles( editorsToSave );
}
protected void saveDeclaringEditor(IRenameElementContext context, IWorkbenchPage workbenchPage) {
IEditorPart declaringEditor = getOpenEditor(context.getTargetElementURI(), workbenchPage);
if (declaringEditor != null && declaringEditor.isDirty())
declaringEditor.doSave(new NullProgressMonitor());
}
/**
* Forces re-validation of a set of compilation units by the JDT Java Builder.
*
* @param cus the compilation units to re-validate
* @param description a brief description of the external job that forcing the
* re-validation. This shows up in the Eclipse status bar while
* re-validation is taking place.
*/
public static void revalidateCompilationUnits(
final Set<ICompilationUnit> cus, String description) {
WorkspaceJob revalidateJob = new WorkspaceJob(description) {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor)
throws CoreException {
final IWorkingCopyManager wcManager = JavaPlugin.getDefault().getWorkingCopyManager();
for (ICompilationUnit cu : cus) {
if (!cu.getResource().exists()) {
CorePluginLog.logWarning(MessageFormat.format(
"Cannot revalidate non-existent compilation unit {0}",
cu.getElementName()));
continue;
}
final IEditorPart editorPart = getOpenEditor(cu);
/*
* If the .java file is open in an editor (without unsaved changes),
* make a "null" edit by inserting an empty string at the top of the
* file and then tell the editor to save. If incremental building is
* enabled, this will trigger a re-validation of the file.
*/
if (editorPart != null && !editorPart.isDirty()) {
// Need to do the editor stuff from the UI thread
Display.getDefault().asyncExec(new Runnable() {
public void run() {
try {
// Get the working copy open in the editor
ICompilationUnit wc = wcManager.getWorkingCopy(editorPart.getEditorInput());
wc.getBuffer().replace(0, 0, "");
editorPart.doSave(new NullProgressMonitor());
} catch (JavaModelException e) {
CorePluginLog.logError(e);
}
}
});
} else {
/*
* If the .java file is not currently open, or if it's open with
* unsaved changes, trigger re-validation by touching the underlying
* resource.
*/
cu.getResource().touch(null);
}
}
return StatusUtilities.OK_STATUS;
}
};
revalidateJob.schedule();
}
/**
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
*/
public void run( IAction action )
{
IFile file = getSelectedFile( );
if ( file != null )
{
String url = file.getLocation( ).toOSString( );
try
{
ModuleHandle handle = SessionHandleAdapter.getInstance( )
.getSessionHandle( )
.openDesign( url );
if ( handle == null )
{
action.setEnabled( false );
return;
}
IEditorPart editor = org.eclipse.birt.report.designer.internal.ui.util.UIUtil.findOpenedEditor( url );
if ( editor != null && editor.isDirty( ) )
{
MessageDialog md = new MessageDialog( UIUtil.getDefaultShell( ),
Messages.getString( "PublishTemplateAction.SaveBeforeGenerating.dialog.title" ), //$NON-NLS-1$
null,
Messages.getFormattedString( "PublishTemplateAction.SaveBeforeGenerating.dialog.message", new Object[]{file.getName( )} ), //$NON-NLS-1$
MessageDialog.CONFIRM,
new String[]{
Messages.getString( "PublishTemplateAction.SaveBeforeGenerating.dialog.button.yes" ), //$NON-NLS-1$
Messages.getString( "PublishTemplateAction.SaveBeforeGenerating.dialog.button.no" ) //$NON-NLS-1$
},
0 );
switch ( md.open( ) )
{
case 0 :
editor.doSave( null );
break;
case 1 :
default :
}
}
WizardDialog dialog = new BaseWizardDialog( UIUtil.getDefaultShell( ),
new PublishTemplateWizard( (ReportDesignHandle) handle ) );
dialog.setPageSize( 500, 250 );
dialog.open( );
handle.close( );
}
catch ( Exception e )
{
ExceptionUtil.handle( e );
return;
}
}
else
{
action.setEnabled( false );
}
}
protected boolean isDirty() {
final IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
return part != null && part.isDirty();
}