Skip to content
Snippets Groups Projects
Commit b42e12ae authored by GIRARDOT Raphael's avatar GIRARDOT Raphael Committed by MADELA Patrick
Browse files

Perspective saving and loading managed by factory (Jira: JAVAAPI-120)

parent 13152001
Branches
Tags
No related merge requests found
......@@ -12,6 +12,7 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
@Deprecated
public class FilePerspective extends Perspective {
public FilePerspective(String name, File file) throws IOException {
......@@ -19,7 +20,7 @@ public class FilePerspective extends Perspective {
if ((file != null) && (file.isFile())) {
InputStream in = new FileInputStream(file);
if (in != null) {
byteArray = readByteArray(in);
byteArray = PerspectiveFactory.readByteArray(in);
}
}
}
......
......@@ -59,6 +59,8 @@ public interface IPerspectiveFactory {
public void saveSelected(File file) throws DockingException;
public void savePerspective(File file, IPerspective perspective) throws DockingException;
public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener);
public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener);
......@@ -71,4 +73,12 @@ public interface IPerspectiveFactory {
public IPerspective[] getPerspectives();
public IPerspective loadPerspectiveFromFile(File perspectiveFile, String perpectiveName) throws DockingException;
public void loadFileInPerspective(File perspectiveFile, IPerspective perspective) throws DockingException;
public IPerspective loadPerspectiveFromResource(String resource, String perpectiveName) throws DockingException;
public void loadResourceInPerspective(String resource, IPerspective perspective) throws DockingException;
}
......@@ -3,8 +3,6 @@
*/
package fr.soleil.docking.perspective;
import java.io.IOException;
import java.io.InputStream;
public class Perspective implements IPerspective {
private final String name;
......@@ -35,23 +33,4 @@ public class Perspective implements IPerspective {
return this.name;
}
/**
* Reads a <code>byte</code> array from an {@link InputStream} and returns it
*
* @param in The {@link InputStream}
* @return A <code>byte[]</code>
* @throws IOException If a problem occurred while trying to read the {@link InputStream}
*/
protected byte[] readByteArray(InputStream in) throws IOException {
byte[] result = null;
if (in != null) {
result = new byte[in.available()];
int length = in.read(result);
if (length != result.length) {
result = null;
}
}
return result;
}
}
\ No newline at end of file
......@@ -10,7 +10,10 @@ package fr.soleil.docking.perspective;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.BackingStoreException;
......@@ -171,33 +174,40 @@ public class PerspectiveFactory implements IPerspectiveFactory {
@Override
public void saveSelected(File file) throws DockingException {
DockingException dockingException = null;
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
savePerspective(file, selectedPerspective);
}
@Override
public void savePerspective(File file, IPerspective perspective) throws DockingException {
if ((file != null) && (perspective != null)) {
DockingException dockingException = null;
FileOutputStream out = null;
try {
out.write(selectedPerspective.getByteArray());
} catch (Exception e) {
if (dockingException == null) {
dockingException = new DockingException("Error during file writing", e);
}
} finally {
out = new FileOutputStream(file);
try {
out.close();
out.write(perspective.getByteArray());
} catch (Exception e) {
if (dockingException == null) {
dockingException = new DockingException("Error while releasing file access", e);
dockingException = new DockingException("Error during file writing", e);
}
} finally {
try {
out.close();
} catch (Exception e) {
if (dockingException == null) {
dockingException = new DockingException("Error while releasing file access", e);
}
}
}
} catch (Exception e) {
if (dockingException == null) {
dockingException = new DockingException("Error during file access", e);
}
}
} catch (Exception e) {
if (dockingException == null) {
dockingException = new DockingException("Error during file access", e);
if (dockingException != null) {
throw dockingException;
}
}
if (dockingException != null) {
throw dockingException;
}
}
/**
......@@ -237,8 +247,7 @@ public class PerspectiveFactory implements IPerspectiveFactory {
} else {
index = perspectives.indexOf(p);
}
if ((index > -1) && (!(p instanceof ResourcePerspective))
&& (!ObjectUtils.sameObject(p.getName(), defaultPerspectiveName))) {
if ((index > -1) && (!ObjectUtils.sameObject(p.getName(), defaultPerspectiveName))) {
// Exist and it's not the default perspective
perspectives.remove(index);
support.fireIndexedPropertyChange(PERSPECTIVES, index, p, null);
......@@ -250,4 +259,69 @@ public class PerspectiveFactory implements IPerspectiveFactory {
return this.removePerspective(this.getPerspective(name));
}
/**
* Reads a <code>byte</code> array from an {@link InputStream} and returns it
*
* @param in The {@link InputStream}
* @return A <code>byte[]</code>
* @throws IOException If a problem occurred while trying to read the {@link InputStream}
*/
protected static byte[] readByteArray(InputStream in) throws IOException {
byte[] result = null;
if (in != null) {
result = new byte[in.available()];
int length = in.read(result);
if (length != result.length) {
result = null;
}
}
return result;
}
@Override
public IPerspective loadPerspectiveFromFile(File perspectiveFile, String perpectiveName) throws DockingException {
Perspective perspective;
if (perpectiveName == null) {
perspective = null;
} else {
perspective = new Perspective(perpectiveName);
}
loadFileInPerspective(perspectiveFile, perspective);
return perspective;
}
@Override
public void loadFileInPerspective(File perspectiveFile, IPerspective perspective) throws DockingException {
if ((perspectiveFile != null) && perspectiveFile.isFile() && (perspective != null)) {
try {
perspective.setByteArray(readByteArray(new FileInputStream(perspectiveFile)));
} catch (Exception e) {
throw new DockingException("Error during file reading", e);
}
}
}
@Override
public IPerspective loadPerspectiveFromResource(String resource, String perpectiveName) throws DockingException {
Perspective perspective;
if (perpectiveName == null) {
perspective = null;
} else {
perspective = new Perspective(perpectiveName);
}
loadResourceInPerspective(resource, perspective);
return perspective;
}
@Override
public void loadResourceInPerspective(String resource, IPerspective perspective) throws DockingException {
if ((resource != null) && (!resource.trim().isEmpty()) && (perspective != null)) {
try {
perspective.setByteArray(readByteArray(getClass().getResourceAsStream(resource)));
} catch (Exception e) {
throw new DockingException("Error during resource reading", e);
}
}
}
}
......@@ -10,13 +10,14 @@ package fr.soleil.docking.perspective;
import java.io.IOException;
import java.io.InputStream;
@Deprecated
public class ResourcePerspective extends Perspective {
public ResourcePerspective(String name, String resource) throws IOException {
super(name);
InputStream in = ResourcePerspective.this.getClass().getResourceAsStream(resource);
if (in != null) {
byteArray = readByteArray(in);
byteArray = PerspectiveFactory.readByteArray(in);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment