1
votes

I am developing a component which generates code based on templates inside java class. The project use clearcase as SCM. After the code update, the files are in read-only state. If i am adding anything to any java class, i have to make it hijack and paste the source code templates inside the class. Let's suppose the jAutoDoc plugin which is used for adding comment. If user select a class, click on generate comment. The comment will not paste if the file is not in write mode.

Clearcase Plugin Vendor : IBM Rational.

Eclipse Version : 3.5

Please help. Is there any way to do hijack a file from java code?

Thanks in advance..

2
I have added in my answer below a link to change the file attributes in Java. - VonC
Is my answer relevant? Did you need more information? - VonC
Its' working.. I am just trying to find the way to get the eclipse preference for a Class and set the read-only mode to false.. - Shashi
So you want to make sure Eclipse will set a certain type of files in read-write mode when it needs to modify them, right? - VonC

2 Answers

1
votes

Thanks VonC.

For making a java file in write mode through eclipse JDT API. This method will set the preference "read only" of the resource to "false".

private static void setCompilationUnitWriteMode(ICompilationUnit cu) throws CoreException {

        ResourceAttributes resourceAttributes = cu.getResource().getResourceAttributes();

        if (resourceAttributes != null) {
              // Setting Writemode true
            resourceAttributes.setReadOnly(false);
        cu.getResource().setResourceAttributes(resourceAttributes);
    }
  }

For Non Java Resource

First create the IFile object, set the preference "read only" of the resource to "false".

       IFile file = path.getFile() 

        file.getFile().getResourceAttributes();

        if (resourceAttributes != null) {
            resourceAttributes.setReadOnly(false);
            file.setResourceAttributes(resourceAttributes);
        }
0
votes

Hijacking files only means making them read - write through an OS-based operation. (for snapshot view only, not dynamic ones)

The question is though: do you need to version your classes completed by your plugin?
Because in that case, a cleartool checkout is more appropriate.

Otherwise, read write is enough, changing file attribute through java.