annotate java/src/uk/ac/qmul/eecs/ccmi/utils/ResourceFileWriter.java @ 8:ea7885bd9bff tip

fixed bug : render solid line as dotted/dashed when moving the stylus from dotted/dashed to solid
author ccmi-guest
date Thu, 03 Jul 2014 16:12:20 +0100
parents d66dd5880081
children
rev   line source
fiore@3 1 /*
fiore@3 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
fiore@3 3
fiore@3 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
fiore@3 5
fiore@3 6 This program is free software: you can redistribute it and/or modify
fiore@3 7 it under the terms of the GNU General Public License as published by
fiore@3 8 the Free Software Foundation, either version 3 of the License, or
fiore@3 9 (at your option) any later version.
fiore@3 10
fiore@3 11 This program is distributed in the hope that it will be useful,
fiore@3 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
fiore@3 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fiore@3 14 GNU General Public License for more details.
fiore@3 15
fiore@3 16 You should have received a copy of the GNU General Public License
fiore@3 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
fiore@3 18 */
fiore@3 19
fiore@3 20 package uk.ac.qmul.eecs.ccmi.utils;
fiore@3 21
fiore@3 22 import java.io.File;
fiore@3 23 import java.io.FileOutputStream;
fiore@3 24 import java.io.IOException;
fiore@3 25 import java.io.InputStream;
fiore@3 26 import java.net.URL;
fiore@3 27
fiore@3 28 /**
fiore@5 29 * This class is used to store a resource (e.g. .dll file in windows)to the local file system.
fiore@5 30 *
fiore@5 31 * This class can be used to allow the virtual machine to load resources which come embedded
fiore@5 32 * into a jar file.
fiore@3 33 */
fiore@3 34 public class ResourceFileWriter {
fiore@3 35
fiore@3 36 /**
fiore@5 37 * Creates an instance of the the class linked to no resource. an resource file writer
fiore@5 38 * created with this constructor is useless, unless {@code setResource} is called before
fiore@5 39 * writing the resource on the disk.
fiore@5 40 *
fiore@5 41 * @see Class#getResource(String)
fiore@5 42 */
fiore@5 43 public ResourceFileWriter(){
fiore@5 44 }
fiore@5 45
fiore@5 46 /**
fiore@5 47 * Creates an instance of the the class linked to a specific resource
fiore@5 48 *
fiore@5 49 * @param resource the resource URL. The URL can be obtained by
fiore@5 50 * {@code getResource}, therefore can be called from a class within a jar file
fiore@5 51 * which needs to access embedded resources.
fiore@3 52 */
fiore@3 53 public ResourceFileWriter(URL resource){
fiore@3 54 this.resource = resource;
fiore@3 55 }
fiore@3 56
fiore@5 57 /**
fiore@5 58 * Sets a new resource for this resource file writer. Successive calls to {@code writeToDisk}
fiore@5 59 * will store the resouce passed as argument in the file system.
fiore@5 60 *
fiore@5 61 * @param resource the URL of the new resource this resource file writer is linked to
fiore@5 62 */
fiore@5 63 public void setResource(URL resource){
fiore@3 64 this.resource = resource;;
fiore@3 65 path = null;
fiore@3 66 }
fiore@3 67
fiore@3 68 /**
fiore@5 69 * Writes the resource in the file passed as argument.
fiore@3 70 *
fiore@5 71 * The path to the file can be retrieved afterwards through @see {@link #getFilePath()}
fiore@5 72 *
fiore@5 73 * @param dir the directory where the resource will be saved
fiore@5 74 * @param fileName the name of the file the resoruce will be saved in
fiore@3 75 */
fiore@5 76 public void writeOnDisk(String dir,String fileName){
fiore@3 77 if (resource == null)
fiore@3 78 return;
fiore@3 79 InputStream in = null;
fiore@3 80 FileOutputStream out = null;
fiore@3 81 File file = new File(dir,fileName);
fiore@3 82 if(file.exists()){ //if file already exists. no job needs to be done.
fiore@3 83 path = file.getAbsolutePath();
fiore@3 84 return;
fiore@3 85 }
fiore@3 86 try{
fiore@3 87 in = resource.openStream();
fiore@3 88 out = new FileOutputStream(file);
fiore@3 89 int byteRead;
fiore@3 90 byte[] b = new byte[1024];
fiore@3 91 while((byteRead = in.read(b)) > 0){
fiore@3 92 out.write(b, 0, byteRead);
fiore@3 93 }
fiore@3 94 path = file.getAbsolutePath();
fiore@3 95 }catch(IOException ioe){
fiore@3 96 path = null;
fiore@3 97 }finally{
fiore@3 98 if(in != null)
fiore@5 99 try{in.close();}catch(IOException ioe){ioe.printStackTrace();}
fiore@3 100 if(out != null)
fiore@5 101 try{out.close();}catch(IOException ioe){ioe.printStackTrace();}
fiore@3 102 }
fiore@3 103
fiore@3 104 }
fiore@3 105
fiore@3 106 /**
fiore@3 107 * Returns the absolute path of the last written file. If the writing wasn't successfully
fiore@3 108 * or no writing took place yet, then {@code null} is returned.
fiore@5 109 *
fiore@5 110 * @return the path of the last written file or {@code null} if no resource was set for this
fiore@5 111 * resource file writer or {@code writeToDisk} was unsuccessful
fiore@3 112 */
fiore@3 113 public String getFilePath(){
fiore@3 114 return path;
fiore@3 115 }
fiore@3 116
fiore@3 117 private URL resource;
fiore@3 118 private String path;
fiore@3 119 }