use of org.eclipse.core.resources.IWorkspaceRoot in project yamcs-studio by yamcs.
the class FileUtil method writeTextFile.
/**
* Write a text file.
*
* @param filePath
* path of the file. It can be an absolute path or a relative path to the OPI that contains the specified
* widget. If it is an absolute path, it can be either<br>
* a workspace path such as <code>/BOY Examples/Scripts/myfile.xml</code><br>
* a local file system path such as <code>C:\myfile.xml</code><br>
* or an URL path such as <code>http://mysite.com/myfile.xml</code>.
* @param inWorkspace
* true if the file path is a workspace file path. Otherwise, it will be recognized as a local file
* system file.
* @param widget
* a widget in the OPI, which is used to provide relative path reference. It can be null if the path is
* an absolute path.
* @param text
* the text to be written to the file.
* @param append
* true if the text should be appended to the end of the file.
* @throws Exception
* if error happens.
*/
public static void writeTextFile(String filePath, boolean inWorkspace, AbstractBaseEditPart widget, String text, boolean append) throws Exception {
IPath path = FileUtil.buildAbsolutePath(filePath, widget);
if (inWorkspace) {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
String projectName = path.segment(0);
IProject project = root.getProject(projectName);
if (!(project.exists())) {
project.create(new NullProgressMonitor());
}
project.open(new NullProgressMonitor());
IFolder folder = null;
for (int i = 1; i < path.segmentCount() - 1; i++) {
if (i == 1)
folder = project.getFolder(path.segment(i));
else
folder = folder.getFolder(path.segment(i));
if (!(folder.exists())) {
folder.create(true, true, null);
}
}
IContainer container;
if (folder == null)
container = project;
else
container = folder;
IFile file = container.getFile(ResourceUtil.getPathFromString(path.lastSegment()));
if (file.exists()) {
StringBuilder sb = new StringBuilder();
if (append) {
sb.append(FileUtil.readTextFile(filePath, widget));
}
sb.append(text);
file.setContents(new ByteArrayInputStream(sb.toString().getBytes("UTF-8")), true, false, // $NON-NLS-1$
null);
} else {
File sysFile = file.getLocation().toFile();
BufferedWriter writer = new BufferedWriter(// $NON-NLS-1$
new OutputStreamWriter(new FileOutputStream(sysFile, append), "UTF-8"));
writer.write(text);
writer.flush();
writer.close();
}
} else {
BufferedWriter writer = new BufferedWriter(// $NON-NLS-1$
new OutputStreamWriter(new FileOutputStream(path.toString(), append), "UTF-8"));
writer.write(text);
writer.flush();
writer.close();
}
}
use of org.eclipse.core.resources.IWorkspaceRoot in project yamcs-studio by yamcs.
the class ResourceAndContainerGroup method validateContainer.
/**
* Returns a <code>boolean</code> indicating whether a container name
* represents a valid container resource in the workbench. An error message
* is stored for future reference if the name does not represent a valid
* container.
*
* @return <code>boolean</code> indicating validity of the container name
*/
private boolean validateContainer() {
IPath path = _containerGroup.getFullPath();
if (path == null) {
_problemType = PROBLEM_CONTAINER_EMPTY;
_problemMessage = Messages.ResourceAndContainerGroup_PROBLEM_EMPTY;
return false;
}
IWorkspace workspace = ResourcesPlugin.getWorkspace();
String projectName = path.segment(0);
if (projectName == null || !workspace.getRoot().getProject(projectName).exists()) {
_problemType = PROBLEM_PROJECT_DOES_NOT_EXIST;
_problemMessage = Messages.ResourceAndContainerGroup_PROBLEM_DOES_NOT_EXIST;
return false;
}
// path is invalid if any prefix is occupied by a file
IWorkspaceRoot root = workspace.getRoot();
while (path.segmentCount() > 1) {
if (root.getFile(path).exists()) {
_problemType = PROBLEM_PATH_OCCUPIED;
_problemMessage = NLS.bind(Messages.ResourceAndContainerGroup_PROBLEM_FILE_ALREADY_EXISTS_AT_LOCATION, path.makeRelative());
return false;
}
path = path.removeLastSegments(1);
}
return true;
}
use of org.eclipse.core.resources.IWorkspaceRoot in project yamcs-studio by yamcs.
the class ResourceUtil method workspacePathToSysPath.
/**
* Convert workspace path to OS system path.
*
* @param path
* the workspace path
* @return the corresponding system path. null if it is not exist.
*/
public static IPath workspacePathToSysPath(IPath path) {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IResource resource = root.findMember(path);
if (resource != null)
// existing resource
return resource.getLocation();
else
// for not existing resource
return root.getFile(path).getLocation();
}
use of org.eclipse.core.resources.IWorkspaceRoot in project yamcs-studio by yamcs.
the class ResourceUtil method workspacePathToSysPath.
/**
* Convert workspace path to OS system path.
*
* @param path
* the workspace path
* @return the corresponding system path. null if it is not exist.
*/
public static IPath workspacePathToSysPath(IPath path) {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IResource resource = root.findMember(path);
if (resource != null)
// existing resource
return resource.getLocation();
else
// for not existing resource
return root.getFile(path).getLocation();
}
use of org.eclipse.core.resources.IWorkspaceRoot in project yamcs-studio by yamcs.
the class InstallOPIImageLibraryAction method run.
@Override
public void run(IAction action) {
final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
if (root.getProject(PROJECT_NAME).exists()) {
MessageDialog.openError(null, "Failed", NLS.bind("There is already a project named \"{0}\"." + "Please make sure there is no project named {0} in the workspace.", PROJECT_NAME));
return;
}
Job job = new Job(JOB_NAME) {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
// copy the sample displays
IProject project = root.getProject(PROJECT_NAME);
project.create(new NullProgressMonitor());
project.open(new NullProgressMonitor());
Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
URL url = FileLocator.find(bundle, new Path(SRC_FOLDER_TOCOPY), null);
try {
File directory = new File(FileLocator.toFileURL(url).getPath());
if (directory.isDirectory()) {
File[] files = directory.listFiles();
monitor.beginTask(TASK_NAME, count(files));
copy(files, project, monitor);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (CoreException e) {
e.printStackTrace();
}
return Status.OK_STATUS;
}
};
job.schedule();
}
Aggregations