diff --git a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/library/ImportImagesAction.java b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/library/ImportImagesAction.java index 9dd9d28c4289e7f411611df7b07dce6b163be33c..24be5c8565e068727a7fa00746079c6c437cd9fe 100644 --- a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/library/ImportImagesAction.java +++ b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/library/ImportImagesAction.java @@ -7,16 +7,20 @@ package de.bmotionstudio.gef.editor.library; import java.io.File; -import java.io.IOException; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.MessageBox; import de.bmotionstudio.gef.editor.BMotionStudioImage; -import de.bmotionstudio.gef.editor.util.FileUtil; public class ImportImagesAction extends AbstractLibraryAction { @@ -49,52 +53,45 @@ public class ImportImagesAction extends AbstractLibraryAction { // The project file IFile pFile = getPage().getEditor().getVisualization().getProjectFile(); - String imagePath = (pFile.getProject().getLocationURI() + "/images") - .replace("file:", ""); - File imageFilePath = new File(imagePath); - - // Check if images folder exists! - // If no such folder exists -> create - if (!imageFilePath.exists()) { - Boolean check = imageFilePath.mkdir(); - if (check) { - // TODO: Do something with return value - } - } - - // Iterate the selected files - for (String fileName : selectedFiles) { - - String filePath = (pFile.getProject().getLocation() + "/images/" + fileName) - .replace("file:", ""); - - // Copy files into project sub folder project/images - File inputFile = new File(folderPath + File.separator + fileName); - File outputFile = new File(filePath); - - boolean ok = false; - - if (outputFile.exists()) { - // The file already exists; asks for confirmation - MessageBox mb = new MessageBox(fd.getParent(), SWT.ICON_WARNING - | SWT.YES | SWT.NO); - mb.setMessage(fileName - + " already exists. Do you want to replace it?"); - // If they click Yes, we're done and we drop out. If - // they click No, we redisplay the File Dialog - ok = mb.open() == SWT.YES; - } else { - ok = true; - } - - if (ok) { - try { - FileUtil.copyFile(inputFile, outputFile); - } catch (IOException e) { - e.printStackTrace(); + try { + + IProject project = pFile.getProject(); + IFolder folder = project.getFolder("images"); + NullProgressMonitor monitor = new NullProgressMonitor(); + + if (!folder.exists()) + folder.create(true, true, monitor); + + // Iterate the selected files + for (String fileName : selectedFiles) { + + File inputFile = new File(folderPath + File.separator + + fileName); + IFile newFile = folder.getFile(fileName); + FileInputStream fileInputStream = new FileInputStream(inputFile); + + if (!newFile.exists()) { + newFile.create(fileInputStream, true, + monitor); + } else { + // The file already exists; asks for confirmation + MessageBox mb = new MessageBox(fd.getParent(), + SWT.ICON_WARNING | SWT.YES | SWT.NO); + mb.setMessage(fileName + + " already exists. Do you want to replace it?"); + // If they click Yes, we're done and we drop out. If + // they click No, we redisplay the File Dialog + if (mb.open() == SWT.YES) + newFile.setContents(fileInputStream, true, false, + monitor); } + } + } catch (CoreException e1) { + e1.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); } getPage().refresh(); diff --git a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/library/LibraryImageObject.java b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/library/LibraryImageObject.java index eed6dc2930f259b9b10c54af1d912bc7257371f0..3931e467070b0f1b4d637a63591fdddbc2741e5a 100644 --- a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/library/LibraryImageObject.java +++ b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/library/LibraryImageObject.java @@ -6,14 +6,13 @@ package de.bmotionstudio.gef.editor.library; -import java.io.File; - import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; -import de.bmotionstudio.gef.editor.util.FileUtil; - public class LibraryImageObject extends LibraryObject { public LibraryImageObject(String name, String type, Image typeImage) { @@ -22,10 +21,19 @@ public class LibraryImageObject extends LibraryObject { @Override public void delete(LibraryPage page) { - String myPath = (page.getEditor().getVisualization().getProjectFile() - .getProject().getLocation() - + "/images/" + getName()).replace("file:", ""); - FileUtil.deleteFile(new File(myPath)); + + try { + IFolder imageFolder = page.getEditor().getVisualization() + .getProjectFile().getProject().getFolder("images"); + if (imageFolder.exists()) { + IFile file = imageFolder.getFile(getName()); + if (file.exists()) + file.delete(true, new NullProgressMonitor()); + } + } catch (CoreException e) { + e.printStackTrace(); + } + } @Override