1

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..

Shashi
  • 12,487
  • 17
  • 65
  • 111
  • I have added in my answer below a link to change the file attributes in Java. – VonC Oct 15 '12 at 05:34
  • Is my answer relevant? Did you need more information? – VonC Nov 06 '12 at 12:07
  • 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 Nov 08 '12 at 08:23
  • 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 Nov 08 '12 at 09:39

2 Answers2

1

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);
        }
Shashi
  • 12,487
  • 17
  • 65
  • 111
0

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250