Eclipse Listener to detect File changes? - java

I am searching a listener in Eclipse, that will detect if a file is changed externally by third party software, like MKS.
I used IResourceChangeListener and it worked. But problem is, it also listens other changes of the file. For example, when I delete the markers, it also listens and execute the codes I wanted after listening.
Is there any Listener, which listens only if the file is changed (newly opened/refreshed) by third party software?
Updated:
My code:
public class Startup implements IStartup {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IResourceChangeListener listener = new IResourceChangeListener() {
public void resourceChanged(IResourceChangeEvent event) {
if(event.getType() == IResourceChangeEvent.POST_CHANGE && IResourceDelta.MARKERS!=0){ //Filtering listener
System.out.println("Listener code should be implemented here");
}
System.out.println("listener is working"); //This line always get executed. That means the listener is working
}
};
#Override
public void earlyStartup() {
workspace.addResourceChangeListener(listener,IResourceChangeEvent.POST_CHANGE);
//... some time later one ...
// workspace.removeResourceChangeListener(listener);
}
}

Related

Minecraft Forge FMLClientSetupEvent fired on Server?

As stated in the Title I think that an FMLClientSetupEvent is Fired on a Forge-Minecraft dedicated Server. Forge Versions should be the same. All in all, this leads to the Error
java.lang.RuntimeException: Attempted to load class net/minecraft/client/renderer/entity/EntityRenderer for invalid dist DEDICATED_SERVER
I figured out that this is due to the CustomRender class which is a subclass of EntityRenderer.
When deleting the line "RenderingRegistry.registerEntityRend..." where I register the CustomRender the Server starts just fine.
Any ideas on that? I'm really not getting it myself because I can't debug the server app.
Documentation of FMLClientSetupEvent: https://mcforge.readthedocs.io/en/latest/conventions/loadstages/
My Code:
#Mod(Utils.MOD_ID)
public class Main {
public Main() {
ModItems.ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
ModSounds.SOUNDS.register(FMLJavaModLoadingContext.get().getModEventBus());
ModEntityType.ENTITY_TYPES.register(FMLJavaModLoadingContext.get().getModEventBus());
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::commonSetup);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::serverSetup);
}
private void commonSetup(FMLCommonSetupEvent evt) {
}
private void clientSetup(FMLClientSetupEvent evt) {
Phaser.arrow = ModEntityType.LASERSTRAHL_ENTITY.get();
RenderingRegistry.registerEntityRenderingHandler(Phaser.arrow, renderManager -> new CustomRender(renderManager));
}
private void serverSetup(FMLDedicatedServerSetupEvent evt) {
}
}
I figured it out. The event isn't fired but apparently the Server doesn't like the EntityRenderer class being imported. So i made a new class for client setup stuff and called that after the FMLClientSetupEvent.

Java FX 8 using Platform.runLater() in Application init() method to display Alert / Login boxes

Please bear with me as this is going to be a pretty detailed and specific requirement. I also looked into numerous questions already listed on stackoverflow regarding Platform.runLater() usage. But I couldn't find an appropriate response. If someone can identify the correct question and mark this as a duplicate I would be great full all the same. Also I'm a newbie to JavaFX programming, so if I need to modify the architecture, please let me know.
I'm creating a stand-alone JavaFX application to run some unit tests based on user-defined requirements. Before running the actual tests, I need to make sure that the end-user is using the test with latest libraries available, by comparing the manifest files from remote Artifactory repo and the manifest files from jars on the current classpath. I want to store the credentials for Artifactory so that I can reuse them for subsequent executions. If the build numbers from remote and local manifest doesn't match, then I want to display an Error Alert window informing the user. I want to use a pre-loader to display a gif and appropriate messages in each of these steps.
public class UI extends Application
{
String username=null, password=null;
Manifest remoteManifest, localManifest;
#Override
public void init() {
notifyPreloader("Loading Stored Credentials");
loadStoredCredentials();
if(username == null && password == null) {
notifyPreloader("Requesting New Credentials");
getNewCredentials();
}
notifyPreloader("Reading remote manifest");
readRemoteManifest();
notifyPreloader("Reading local manifest");
readLocalManifest();
notifyPreloader("Comparing manifests");
compareManifests();
}
private void getNewCredentials() {
Platform.runLater(new Runnable() {
public void run() {
// Login window for new credentials
//Save credentials
}
});
}
private void readRemoteManifest() {
// REST API call to read remote manifest
if(HttpStatus.SC_UNAUTHORIZED) { // Stored credentials might have been read the first time which are expired, so try again.
getNewCredentials();
readRemoteManifest();
} else {
// update remote manifest object
}
}
private void readLocalManifest() {
// Update local manifest object
}
private void showAlert() {
Platform.runLater( new Runnable() {
// Display Alert window
});
}
private void compareManifests() {
if(remoteManifest == null) {
showAlert();
}
if(localAlert == null) {
showAlert();
}
if(remoteBuildNumber != localBuildNumber) {
showAlert();
}
}
#Override
public void start(Stage primaryStage){
//UI to select options and run tests
}
}
I have three issues with this approach.
If I don't user Platform.runLater() for Alert Windows and Login Windows, then I get an error saying Not on FX application thread; currentThread = JavaFX Application Thread error?
If I use Platform.runLater() then even though the login window is active, the remaining alert windows also show up immediately without waiting to read the login credentials.
If I manually create the file to store current credentials, then any alert window that I have shows up twice.
Let me know if you need any additional info.
Thanks in advance.
EDIT-1:
Someone gave me an answer to fix the first two issues. But now the answer is missing. If they can re-post the answer, it would be helpful for others.
The solution was to move the Platform.runLater() code to compareManifests() method from showAlert() method. Instead I moved the Platform.runLater() to init() method. Now I only have one call to runLater().
The modified code to fix the first two issues.
public class UI extends Application
{
String username=null, password=null;
Manifest remoteManifest, localManifest;
#Override
public void init() {
Platform.runLater( new Runnable() {
public void run() {
notifyPreloader("Loading Stored Credentials");
loadStoredCredentials();
if(username == null && password == null) {
notifyPreloader("Requesting New Credentials");
getNewCredentials();
}
notifyPreloader("Reading remote manifest");
readRemoteManifest();
notifyPreloader("Reading local manifest");
readLocalManifest();
notifyPreloader("Comparing manifests");
compareManifests();
}
});
}
private void getNewCredentials() {
// Login window for new credentials
//Save credentials
}
private void readRemoteManifest() {
// REST API call to read remote manifest
if(HttpStatus.SC_UNAUTHORIZED) { // Stored credentials might have been read the first time which are expired, so try again.
getNewCredentials();
readRemoteManifest();
} else {
// update remote manifest object
}
}
private void readLocalManifest() {
// Update local manifest object
}
private void showAlert() {
// Display Alert window
}
private void compareManifests() {
if(remoteManifest == null) {
showAlert();
}
if(localAlert == null) {
showAlert();
}
if(remoteBuildNumber != localBuildNumber) {
showAlert();
}
}
private void loadStoredCredentials() {
// Read the credentials from a local file
}
#Override
public void start(Stage primaryStage){
//UI to select options and run tests
}
}
Issue 3 is still occurring, there are two login windows being displayed now.
Thanks.

IResourceChangeListener being called twice with one change

I'm using the IResourceChangeListener to listen for workspace/project changes. It's implemented as proposed by the Eclipse example:
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IResourceChangeListener resourceChangeListener = new IResourceChangeListener() {
public void resourceChanged(IResourceChangeEvent event) {
System.out.println("Something changed!");
}
};
workspace.addResourceChangeListener(resourceChangeListener, IResourceChangeEvent.POST_BUILD);
// some time later on ...
workspace.removeResourceChangeListener(resourceChangeListener);
I implemented this code in a relatively new plugin project, but whenever changes are made in the workspace "Something changed!" is thrown twice?
If helpful: The class implements org.eclipse.ui.IStartup. IResourceChangeListener and resourceChanged() are created and inside of earlyStartup().

Folder not detected in spite of its creation, when counted in the same button click event

I have an android activity, wherein a folder gets generated within a button click, along with some files within the folder. Now I am trying to programmatically get the count of the number of files in that folder. My algorithm is somewhat like this:
onClick()
{
createFolder();
createFilesWithinFolder();
countFilesInFolder();
}
In this case, when I try to count the files using countFilesInFolder(), it gives zero in spite of the fact that the folder and the files are actually successfully created.
But if I do this instead and press the second button after the first,
onClick1()
{
createFolder();
createFilesWithinFolder();
}
onClick2()
{
countFilesInFolder();
}
It counts the number of files properly. Later when I checked my countFilesInFolder() function, I came to know that it was not detecting the creation of the folder itself, and hence everything was giving zero.
I tried using the following media scanner class to overcome this issue, but it did not work:
class SingleMediaScanner implements MediaScannerConnectionClient
{
private MediaScannerConnection mMs;
private String mPath;
SingleMediaScanner(Context context, String f)
{
mPath = f;
mMs = new MediaScannerConnection(context, this);
System.out.println("Commencing connection");
mMs.connect();
}
#Override
public void onMediaScannerConnected()
{
//mMs.scanFile(mPath, null);
mMs.scanFile(mPath, null);
Log.i("SCANNER","Scanning :"+mPath);
}
#Override
public void onScanCompleted(String arg0, Uri arg1) {
// TODO Auto-generated method stub
Log.i("SCANNER","Media Scan Completed");
mMs.disconnect();
}
}
I require that the file creation and counting must both occur in the Same button click as I have illustrated in my first algorithm. How should I do this? Please assume that the pseudo-functions I have mentioned above are all correct, as I have seen it working properly in my second algorithm.

Eclipse IDE - Perspective is cached and changes has no any effect

I have created new Eclipse IDE (plugin project, Eclipse Kepler, rel. 1) with default template of mail client.
After the first run of the app, the Perspective is stored and remembered (somewhere?) and any changes to Perspective.java has no any effect! Even if I delete the content of createInitialLayout(IPageLayout layout) from the Perspective.java, everything is restored again.
BTW: adding this code to ApplicationWorkbenchAdvisor.java didnt help:
#Override
public void initialize(IWorkbenchConfigurer configurer) {
super.initialize(configurer);
configurer.setSaveAndRestore(false);
}
How can I force the app to not to remmeber the layout?
You could call IWorkbenchPage.resetPerspective() to reinitialize the perspective, perhaps in the WorkbenchWindowAdvisor.postWindowRestore() method.
Thank you. I have added the following code to menu. Now I can reset perspective whenever I need.
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
private IWorkbenchAction resetPerspectiveAction;
#Override
protected void makeActions(IWorkbenchWindow window) {
// ...
// create and register the actions
resetPerspectiveAction = ActionFactory.RESET_PERSPECTIVE.create(window);
register(resetPerspectiveAction);
// ...
}
#Override
protected void fillMenuBar(IMenuManager menuBar) {
// ...
// create and fill the window menu
MenuManager windowMenu = new MenuManager("&Window", WorkbenchActionConstants.M_WINDOW);
menuBar.add(windowMenu);
windowMenu.add(resetPerspectiveAction);
// ...
}
}

Categories

Resources