Skip to content
Snippets Groups Projects
Commit 3b32ea62 authored by Raphael GIRARDOT's avatar Raphael GIRARDOT
Browse files

better programming practices

parent f2fc55eb
Branches
Tags
No related merge requests found
......@@ -15,7 +15,6 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
......@@ -33,6 +32,7 @@ import org.slf4j.Logger;
import com.google.common.eventbus.EventBus;
import fr.soleil.lib.project.ObjectUtils;
import fr.soleil.lib.project.SystemUtils;
import fr.soleil.salsa.client.exception.SalsaPerspectiveException;
import fr.soleil.salsa.client.tool.PerspectiveEvent.Type;
......@@ -54,7 +54,7 @@ public class PerspectivesManager {
public static final String SALSA_PERSPECTIVES_DIRECTORY = "SALSA_PERSPECTIVES_DIRECTORY";
/** The default list of perspectives */
private static final List<String> DEFAULT_PERSPECTIVES = Arrays.asList("Default", "Configuration", "Scan");
private static final String[] DEFAULT_PERSPECTIVES = { "Default", "Configuration", "Scan" };
/** The perspective file extension */
private static final String PERSPECTIVE_FILE_EXTENSION = ".dat";
......@@ -69,7 +69,7 @@ public class PerspectivesManager {
private List<Perspective> perspectives = new ArrayList<>();
/** List of perspective name of restored default perspective since the last init call */
private List<String> restoredDefaultPerspectiveNames = new ArrayList<>();
private Collection<String> restoredDefaultPerspectiveNames = new LinkedHashSet<>();
/** Map of wrong perspective file by perspective name since the last init call */
private Map<String, String> wrongPerspectiveFiles = new LinkedHashMap<>();
......@@ -138,7 +138,7 @@ public class PerspectivesManager {
* @return name of first default perspective
*/
public String getDefaultPerspectiveName() {
return DEFAULT_PERSPECTIVES.get(0);
return DEFAULT_PERSPECTIVES[0];
}
/**
......@@ -147,7 +147,7 @@ public class PerspectivesManager {
* @return array of default perspective names
*/
public String[] getDefaultPerspectiveNames() {
return DEFAULT_PERSPECTIVES.toArray(new String[DEFAULT_PERSPECTIVES.size()]);
return DEFAULT_PERSPECTIVES.clone();
}
/**
......@@ -199,8 +199,8 @@ public class PerspectivesManager {
*
* @return List of perspective name of restored default perspective
*/
public List<String> getRestoredDefaultPerspectiveNames() {
return Collections.unmodifiableList(restoredDefaultPerspectiveNames);
public Collection<String> getRestoredDefaultPerspectiveNames() {
return Collections.unmodifiableCollection(restoredDefaultPerspectiveNames);
}
/**
......@@ -298,7 +298,9 @@ public class PerspectivesManager {
Perspective referencePerspective = loadPerspective(bis);
getCurrentPerspective().setWindowLayout(referencePerspective.getWindowLayout());
fireChange(Type.SELECT, getCurrentPerspective());
} catch (IOException e) {
} catch (SalsaPerspectiveException e) {
throw e;
} catch (Exception e) {
throw new SalsaPerspectiveException("Unable to reset layout with layout of perspective \""
+ referencePerspectiveName + "\": " + e.getMessage(), e);
}
......@@ -356,37 +358,54 @@ public class PerspectivesManager {
}
private Optional<Perspective> loadPerspective(String perspectiveName) {
Optional<Perspective> optional;
Path perspectivePath = getPerspectivePath(perspectiveName);
try {
if (!Files.exists(perspectivePath)) {
return Optional.of(overwriteDefaultPerspective(perspectiveName));
Perspective p;
if (Files.exists(perspectivePath)) {
p = loadPerspective(perspectivePath);
} else {
p = overwriteDefaultPerspective(perspectiveName);
}
Perspective current = getCurrentPerspective();
if (current != null && ObjectUtils.sameObject(current.getName(), p.getName())) {
current.setWindowLayout(p.getWindowLayout());
fireChange(Type.SELECT, getCurrentPerspective());
}
return Optional.of(loadPerspective(perspectivePath));
optional = Optional.of(p);
} catch (SalsaPerspectiveException e) {
LOGGER.error("Unable to load perspective {} from file {}: {}", perspectiveName, perspectivePath,
e.getMessage());
LOGGER.trace("Unable to load perspective {} from file {}", perspectiveName, perspectivePath, e);
if (!DEFAULT_PERSPECTIVES.contains(perspectiveName)) {
wrongPerspectiveFiles.put(perspectiveName, perspectivePath.toAbsolutePath().toString());
if (isDefaultPerspective(perspectiveName)) {
try {
optional = restoreDefaultPerspective(perspectiveName);
} catch (Exception e2) {
optional = Optional.empty();
LOGGER.error("Unable to restore default perspective " + perspectiveName, e2);
}
} else {
return restoreDefaultPerspective(perspectiveName);
optional = Optional.empty();
wrongPerspectiveFiles.put(perspectiveName, perspectivePath.toAbsolutePath().toString());
}
}
return Optional.empty();
return optional;
}
private Optional<Perspective> restoreDefaultPerspective(String perspectiveName) {
Optional<Perspective> optional;
Path perspectivePath = getPerspectivePath(perspectiveName);
try {
Perspective restoredDefaultPerfective = overwriteDefaultPerspective(perspectiveName);
restoredDefaultPerspectiveNames.add(perspectiveName);
return Optional.of(restoredDefaultPerfective);
optional = Optional.of(restoredDefaultPerfective);
} catch (SalsaPerspectiveException e) {
optional = Optional.empty();
LOGGER.error("Unable to restore default perspective {} to {}: {}", perspectiveName, perspectivePath,
e.getMessage());
LOGGER.trace("Unable to restore default perspective {} to {}", perspectiveName, perspectivePath, e);
}
return Optional.empty();
return optional;
}
private Perspective overwriteDefaultPerspective(String perspectiveName) throws SalsaPerspectiveException {
......@@ -395,7 +414,9 @@ public class PerspectivesManager {
BufferedInputStream bis = new BufferedInputStream(is)) {
Files.copy(bis, perspectivePath, StandardCopyOption.REPLACE_EXISTING);
return loadPerspective(perspectivePath);
} catch (IOException e) {
} catch (SalsaPerspectiveException e) {
throw e;
} catch (Exception e) {
throw new SalsaPerspectiveException("Unable to overwrite default perspective: " + e.getMessage(), e);
}
}
......@@ -404,9 +425,11 @@ public class PerspectivesManager {
try (FileInputStream fis = new FileInputStream(perspectivePath.toFile());
BufferedInputStream bis = new BufferedInputStream(fis)) {
return loadPerspective(bis);
} catch (SalsaPerspectiveException e) {
throw e;
} catch (EOFException e) {
throw new SalsaPerspectiveException("Unable to load perspective: file is empty", e);
} catch (IOException | ClassCastException e) {
} catch (Exception e) {
throw new SalsaPerspectiveException("Unable to load perspective: " + e.getMessage(), e);
}
}
......@@ -414,7 +437,7 @@ public class PerspectivesManager {
private Perspective loadPerspective(InputStream istream) throws SalsaPerspectiveException {
try (ObjectInputStream objectInputStream = new ObjectInputStream(istream)) {
return (Perspective) objectInputStream.readObject();
} catch (IOException | ClassNotFoundException e) {
} catch (Exception e) {
throw new SalsaPerspectiveException(e.getMessage(), e);
}
}
......@@ -432,7 +455,10 @@ public class PerspectivesManager {
}
private Set<String> getPerpectiveNames() {
Set<String> perspectiveNames = new LinkedHashSet<>(DEFAULT_PERSPECTIVES);
Set<String> perspectiveNames = new LinkedHashSet<>();
for (String perspective : DEFAULT_PERSPECTIVES) {
perspectiveNames.add(perspective);
}
perspectiveNames.addAll(getPerspectiveNamesFromFiles());
return perspectiveNames;
}
......@@ -446,16 +472,18 @@ public class PerspectivesManager {
}
private List<String> getPerspectiveNamesFromFiles() {
List<String> names;
Path perspectiveFilesLocation = getPerspectiveFilesLocation();
try (Stream<Path> paths = Files.list(perspectiveFilesLocation)) {
return paths.map(Path::getFileName).map(Path::toString).filter(f -> f.endsWith(PERSPECTIVE_FILE_EXTENSION))
names = paths.map(Path::getFileName).map(Path::toString).filter(f -> f.endsWith(PERSPECTIVE_FILE_EXTENSION))
.map(f -> f.substring(0, f.lastIndexOf("."))).collect(Collectors.toList());
} catch (IOException e) {
} catch (Exception e) {
names = Collections.emptyList();
LOGGER.error("Unable to get perspectives names from files in {}: {}", perspectiveFilesLocation,
e.getMessage());
LOGGER.trace("Unable to get perspectives names from files in {}", perspectiveFilesLocation, e);
}
return Collections.emptyList();
return names;
}
private void savePerspective(Perspective perspective, Path perspectivePath) {
......@@ -466,14 +494,25 @@ public class PerspectivesManager {
ObjectOutputStream objectOutPutStream = new ObjectOutputStream(bos);) {
objectOutPutStream.writeObject(perspective);
objectOutPutStream.flush();
} catch (IOException e) {
} catch (Exception e) {
LOGGER.error("Unable to save perspective {} to {}: {}", perspective.getName(), perspectivePath,
e.getMessage());
LOGGER.trace("Unable to save perspective {} to {}", perspective.getName(), perspectivePath, e);
}
}
private boolean isDefaultPerspective(String perspectiveName) {
boolean found = false;
for (String perspective : DEFAULT_PERSPECTIVES) {
if (perspective.equals(perspectiveName)) {
found = true;
break;
}
}
return found;
}
private boolean isDefaultPerspective(Perspective perspective) {
return DEFAULT_PERSPECTIVES.contains(perspective.getName());
return isDefaultPerspective(perspective.getName());
}
}
......@@ -6,6 +6,7 @@ import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collection;
import java.util.List;
import java.util.MissingResourceException;
import java.util.Optional;
......@@ -65,7 +66,7 @@ import net.infonode.util.Direction;
public class ApplicationView extends JFrame implements IView<ApplicationController> {
/** Serialization id */
private static final long serialVersionUID = 2928523501112316355L;
private static final long serialVersionUID = -6463941089265352L;
/** The logger for messages */
public static final Logger LOGGER = LoggingUtil.getLogger(ApplicationView.class);
......@@ -376,7 +377,7 @@ public class ApplicationView extends JFrame implements IView<ApplicationControll
titleBuilder.append(getBundleMessage("project.version")).append(" (");
titleBuilder.append(getBundleMessage("build.date")).append(")");
preferredTitle = titleBuilder.toString();
this.initialize();
initialize();
}
/**
......@@ -407,10 +408,10 @@ public class ApplicationView extends JFrame implements IView<ApplicationControll
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(rootWindow, BorderLayout.CENTER);
this.setContentPane(mainPanel);
this.setTitle(getPreferredTitle());
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
setContentPane(mainPanel);
setTitle(getPreferredTitle());
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(final WindowEvent e) {
if (applicationController != null) {
......@@ -422,7 +423,7 @@ public class ApplicationView extends JFrame implements IView<ApplicationControll
initializeSize();
// Create menu bar
this.setJMenuBar(getMenuBarViewTool());
setJMenuBar(getMenuBarViewTool());
initializeController();
initializeLayouts();
......@@ -454,11 +455,11 @@ public class ApplicationView extends JFrame implements IView<ApplicationControll
private void maximizeWindow() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screensize = toolkit.getScreenSize();
this.setSize(screensize.width - 80, screensize.height - 80);
setSize(screensize.width - 80, screensize.height - 80);
if (toolkit.isFrameStateSupported(MAXIMIZED_BOTH)) {
this.setExtendedState(MAXIMIZED_BOTH);
setExtendedState(MAXIMIZED_BOTH);
}
this.setLocationRelativeTo(null);
setLocationRelativeTo(null);
}
private void initializeController() {
......@@ -495,7 +496,7 @@ public class ApplicationView extends JFrame implements IView<ApplicationControll
}
private void initializeDockingWindowLocker(ViewMap viewMap) {
this.windowLayoutManager = new WindowLayoutManager(rootWindow, viewMap);
windowLayoutManager = new WindowLayoutManager(rootWindow, viewMap);
}
private ResourceBundle getResourceBundle() {
......@@ -1494,7 +1495,7 @@ public class ApplicationView extends JFrame implements IView<ApplicationControll
*
* @param restoredDefaultPerspectiveNames list of restored default perspective
*/
public void showRestoredDefaultPerspective(List<String> restoredDefaultPerspectiveNames) {
public void showRestoredDefaultPerspective(Collection<String> restoredDefaultPerspectiveNames) {
StringBuilder errorBuffer = new StringBuilder();
errorBuffer.append("An error occured while loading following perspectives:\n");
errorBuffer.append(
......@@ -1510,7 +1511,7 @@ public class ApplicationView extends JFrame implements IView<ApplicationControll
* @param wrongPerspectiveFiles list of wrong perspective files
* @return true if user confirms deletion
*/
public boolean askForDeletionOfWrongPerspectiveFiles(List<String> wrongPerspectiveFiles) {
public boolean askForDeletionOfWrongPerspectiveFiles(Collection<String> wrongPerspectiveFiles) {
StringBuilder askBuffer = new StringBuilder();
askBuffer.append("An error occured while loading following perspectives:\n");
askBuffer.append(wrongPerspectiveFiles.stream().map(s -> "\'" + s + "\'").collect(Collectors.joining(", ")));
......@@ -1526,7 +1527,7 @@ public class ApplicationView extends JFrame implements IView<ApplicationControll
*
* @param wrongPerspectiveFiles wrong perspective files that were unable to delete
*/
public void showFailedToDeletePerspectiveFile(List<String> wrongPerspectiveFiles) {
public void showFailedToDeletePerspectiveFile(Collection<String> wrongPerspectiveFiles) {
StringBuilder errorBuffer = new StringBuilder();
errorBuffer.append("Failed to delete following file(s):\n");
errorBuffer.append(wrongPerspectiveFiles.stream().map(s -> "\'" + s + "\'").collect(Collectors.joining("\n")));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment