use of org.eclipse.core.resources.IResourceChangeListener in project bndtools by bndtools.
the class OSGiRunLaunchDelegate method registerLaunchPropertiesRegenerator.
/**
* Registers a resource listener with the project model file to update the launcher when the model or any of the
* run-bundles changes. The resource listener is automatically unregistered when the launched process terminates.
*
* @param project
* @param launch
* @throws CoreException
*/
private void registerLaunchPropertiesRegenerator(final Project project, final ILaunch launch) throws CoreException {
final IResource targetResource = LaunchUtils.getTargetResource(launch.getLaunchConfiguration());
if (targetResource == null)
return;
final IPath bndbndPath;
try {
bndbndPath = Central.toPath(project.getPropertiesFile());
} catch (Exception e) {
throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error querying bnd.bnd file location", e));
}
try {
Central.toPath(project.getTarget());
} catch (Exception e) {
throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error querying project output folder", e));
}
final IResourceChangeListener resourceListener = new IResourceChangeListener() {
@Override
public void resourceChanged(IResourceChangeEvent event) {
try {
final AtomicBoolean update = new AtomicBoolean(false);
// Was the properties file (bnd.bnd or *.bndrun) included in
// the delta?
IResourceDelta propsDelta = event.getDelta().findMember(bndbndPath);
if (propsDelta == null && targetResource.getType() == IResource.FILE)
propsDelta = event.getDelta().findMember(targetResource.getFullPath());
if (propsDelta != null) {
if (propsDelta.getKind() == IResourceDelta.CHANGED) {
update.set(true);
}
}
// list
if (!update.get()) {
final Set<String> runBundleSet = new HashSet<String>();
for (String bundlePath : bndLauncher.getRunBundles()) {
runBundleSet.add(new org.eclipse.core.runtime.Path(bundlePath).toPortableString());
}
event.getDelta().accept(new IResourceDeltaVisitor() {
@Override
public boolean visit(IResourceDelta delta) throws CoreException {
// match
if (update.get())
return false;
IResource resource = delta.getResource();
if (resource.getType() == IResource.FILE) {
IPath location = resource.getLocation();
boolean isRunBundle = location != null ? runBundleSet.contains(location.toPortableString()) : false;
update.compareAndSet(false, isRunBundle);
return false;
}
// Recurse into containers
return true;
}
});
}
if (update.get()) {
project.forceRefresh();
project.setChanged();
bndLauncher.update();
}
} catch (Exception e) {
logger.logError("Error updating launch properties file.", e);
}
}
};
ResourcesPlugin.getWorkspace().addResourceChangeListener(resourceListener);
// Register a listener for termination of the launched process
Runnable onTerminate = new Runnable() {
@Override
public void run() {
ResourcesPlugin.getWorkspace().removeResourceChangeListener(resourceListener);
display.asyncExec(new Runnable() {
@Override
public void run() {
if (dialog != null && dialog.getShell() != null) {
dialog.getShell().dispose();
}
}
});
}
};
DebugPlugin.getDefault().addDebugEventListener(new TerminationListener(launch, onTerminate));
}
use of org.eclipse.core.resources.IResourceChangeListener in project titan.EclipsePlug-ins by eclipse.
the class MetricsView method createHead.
private void createHead(final Composite head) {
// upper composite, the head itself
final GridLayout layout = new GridLayout();
layout.numColumns = 3;
layout.marginWidth = 0;
layout.marginHeight = 0;
head.setLayout(layout);
GridData g = new GridData(SWT.DEFAULT, 25);
g.grabExcessHorizontalSpace = true;
g.horizontalAlignment = SWT.FILL;
head.setLayoutData(g);
// project selector in the head
projectSelector = new Combo(head, SWT.READ_ONLY);
for (final IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
if (TITANNature.hasTITANNature(project)) {
projectSelector.add(project.getName());
}
}
projectSelector.select(0);
g = new GridData();
g.horizontalAlignment = SWT.FILL;
g.grabExcessHorizontalSpace = true;
projectSelector.setLayoutData(g);
// refresh button in the head
refresh = new Button(head, SWT.NONE);
refresh.setImage(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "resources/icons/metrics_start_measure.gif").createImage());
refresh.setToolTipText(REFRESH_TOOLTIP);
refresh.setLayoutData(new GridData(SWT.DEFAULT, 25));
refresh.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(final SelectionEvent event) {
final String selectedProjectName = projectSelector.getText();
if ("".equals(selectedProjectName)) {
// Nothing is selected (probably there are no projects in
// the project browser);
data = null;
refreshData();
return;
}
// Analyze project.
// To avoid further analysis requests, disable the refresh
// button.
refresh.setEnabled(false);
final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(selectedProjectName);
if (project == null) {
data = null;
refreshData();
return;
}
new ProjectAnalyzerJob("Metrics calculations") {
@Override
public IStatus doPostWork(final IProgressMonitor monitor) {
try {
data = MetricData.measure(project);
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
refreshData();
export.setEnabled(true);
}
});
return Status.OK_STATUS;
} finally {
// The view should recover from any unexpected
// error, so
// the refresh button should not remain
// disabled.
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
refresh.setEnabled(true);
}
});
}
}
}.quickSchedule(project);
}
@Override
public void widgetDefaultSelected(final SelectionEvent e) {
// Do nothing
}
});
createExportToXlsButton(head);
final IResourceChangeListener PROJECT_CLOSE_LISTENER = new IResourceChangeListener() {
@Override
public void resourceChanged(final IResourceChangeEvent event) {
switch(event.getType()) {
case IResourceChangeEvent.PRE_CLOSE:
case IResourceChangeEvent.PRE_DELETE:
final IResource resource = event.getResource();
// projectSelector.getText();
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
if (projectSelector == null || projectSelector.isDisposed()) {
return;
}
projectSelector.deselectAll();
projectSelector.removeAll();
for (final IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
if (!project.equals(resource.getProject()) && TITANNature.hasTITANNature(project)) {
projectSelector.add(project.getName());
}
}
data = null;
refreshData();
}
});
break;
default:
break;
}
}
};
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
workspace.addResourceChangeListener(PROJECT_CLOSE_LISTENER);
}
use of org.eclipse.core.resources.IResourceChangeListener in project pmd-eclipse-plugin by pmd.
the class PMDPlugin method start.
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework. BundleContext )
*/
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
// this needs to be executed before the preferences are loaded, because
// the standard
// rulesets are needed for the default active rules.
registerStandardRuleSets();
IPreferences prefs = loadPreferences();
configureLogs(prefs);
registerAdditionalRuleSets();
fileChangeListenerEnabled(prefs.isCheckAfterSaveEnabled());
// if a project is deleted, remove the cached project properties
ResourcesPlugin.getWorkspace().addResourceChangeListener(new IResourceChangeListener() {
public void resourceChanged(IResourceChangeEvent arg0) {
if (arg0.getType() == IResourceChangeEvent.PRE_DELETE && arg0.getResource() instanceof IProject) {
getPropertiesManager().removeProjectProperties((IProject) arg0.getResource());
}
}
});
version = context.getBundle().getHeaders().get("Bundle-Version");
}
use of org.eclipse.core.resources.IResourceChangeListener in project xtext-eclipse by eclipse.
the class StratumBreakpointAdapterFactory method toggleBreakpoints.
@Override
public void toggleBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
if (!(part instanceof XtextEditor) || !(selection instanceof ITextSelection)) {
return;
}
try {
final XtextEditor xtextEditor = (XtextEditor) part;
final IEditorInput editorInput = xtextEditor.getEditorInput();
final IResource breakpointResource = breakpointUtil.getBreakpointResource(editorInput);
final SourceRelativeURI breakpointUri = breakpointUtil.getBreakpointURI(editorInput);
final int offset = ((ITextSelection) selection).getOffset();
final int line = xtextEditor.getDocument().getLineOfOffset(offset) + 1;
Data data = xtextEditor.getDocument().tryReadOnly(new IUnitOfWork<Data, XtextResource>() {
@Override
public Data exec(XtextResource state) throws Exception {
IResourceServiceProvider provider = state.getResourceServiceProvider();
IStratumBreakpointSupport breakpointSupport = provider.get(IStratumBreakpointSupport.class);
Data result = new Data();
result.name = state.getURI().lastSegment();
result.valid = breakpointSupport != null && breakpointSupport.isValidLineForBreakPoint(state, line);
result.types = getClassNamePattern(state);
result.lang = provider.get(LanguageInfo.class);
result.sourceUri = state.getURI();
IClassFile classFile = Adapters.adapt(editorInput, IClassFile.class);
if (classFile != null) {
result.classHandle = classFile.getHandleIdentifier();
}
return result;
}
});
if (data == null)
return;
IJavaStratumLineBreakpoint existingBreakpoint = findExistingBreakpoint(breakpointResource, breakpointUri, line);
if (existingBreakpoint != null) {
existingBreakpoint.delete();
return;
}
if (!data.valid || data.types == null)
return;
if (log.isDebugEnabled()) {
log.debug("Types the breakpoint listens for : " + data.types);
}
final IRegion information = xtextEditor.getDocument().getLineInformation(line - 1);
final int charStart = information.getOffset();
final int charEnd = information.getOffset() + information.getLength();
final String shortName = data.lang.getShortName();
Map<String, Object> attributes = Maps.newHashMap();
if (breakpointUri != null)
attributes.put(JarFileMarkerAnnotationModel.MARKER_URI, breakpointUri.toString());
attributes.put(ORG_ECLIPSE_JDT_DEBUG_CORE_SOURCE_NAME, data.name);
attributes.put(ORG_ECLIPSE_XTEXT_XBASE_SOURCE_URI, data.sourceUri.toString());
if (data.classHandle != null)
attributes.put(ORG_ECLIPSE_XTEXT_XBASE_CLASS_HANDLE, data.classHandle);
final IJavaStratumLineBreakpoint breakpoint = JDIDebugModel.createStratumBreakpoint(breakpointResource, shortName, null, null, data.types, line, charStart, charEnd, 0, true, attributes);
// make sure the class name pattern gets updated on change
final IMarker marker = breakpoint.getMarker();
final IWorkspace ws = marker.getResource().getWorkspace();
IResourceChangeListener listener = new IResourceChangeListener() {
@Override
public void resourceChanged(IResourceChangeEvent event) {
if (!marker.exists())
ws.removeResourceChangeListener(this);
IResourceDelta delta = event.getDelta();
if (delta == null)
return;
final IResourceDelta findMember = event.getDelta().findMember(marker.getResource().getFullPath());
if (findMember == null)
return;
IResource res = findMember.getResource();
if (res == null || !res.exists())
return;
if (event.getType() == IResourceChangeEvent.PRE_DELETE) {
ws.removeResourceChangeListener(this);
} else if (event.getType() == IResourceChangeEvent.POST_CHANGE && (findMember.getFlags() & IResourceDelta.CONTENT) != 0) {
String classNamePattern = getClassNamePattern(event.getResource());
try {
breakpoint.getMarker().setAttribute("org.eclipse.jdt.debug.pattern", classNamePattern);
} catch (CoreException e) {
log.info(e.getMessage(), e);
}
}
}
};
ws.addResourceChangeListener(listener, IResourceChangeEvent.POST_CHANGE | IResourceChangeEvent.PRE_DELETE);
} catch (BadLocationException e) {
log.info(e.getMessage(), e);
}
}
Aggregations