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

Better error messages (EXPDATA-99)

parent 62760c59
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,10 @@ package org.cdma.gui.databrowser;
import java.awt.Cursor;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
......@@ -26,8 +30,11 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingWorker;
import javax.xml.bind.DatatypeConverter;
......@@ -68,20 +75,21 @@ import fr.soleil.comete.swing.util.CometeUtils;
import fr.soleil.data.exception.KeyCompatibilityException;
import fr.soleil.data.service.AbstractKey;
import fr.soleil.data.service.IKey;
import fr.soleil.lib.project.swing.text.HtmlTextPane;
import fr.soleil.lib.project.xmlhelpers.XMLUtils;
import fr.soleil.lib.project.xmlhelpers.exception.XMLWarning;
public class DataBrowserController implements IAbstractController {
private static final Logger LOGGER = LoggerFactory.getLogger(DataBrowserController.class);
private static TreeViewer treeViewer = null;
private DataItemViewer dataItemViewer = null;
private static TreeViewer treeViewer;
private DataItemViewer dataItemViewer;
private final Set<IDataBrowserItemListener> itemListeners;
private IChartViewer chartViewer = null;
private IChartViewer chartViewer;
private boolean isDevicePanel = false;
private boolean isDevicePanel;
private JComponent rootWindow = null;
private JComponent rootWindow;
// <browser, sources' keys>
protected static Map<IDataSourceBrowser, List<IKey>> openedSourcesFromBrowser;
......@@ -95,12 +103,17 @@ public class DataBrowserController implements IAbstractController {
// <item key, item>
protected final Map<IKey, Item> items;
private IKey currentItemKey = null;
private IKey currentItemKey;
private DataBrowser dataBrowserPanel = null;
private DataBrowser dataBrowserPanel;
private List<Item> openedNumericalSpectrumItems = null;
private List<Item> openedNumericalMatrixItems = null;
private List<Item> openedNumericalSpectrumItems;
private List<Item> openedNumericalMatrixItems;
protected JDialog errorDialog;
protected HtmlTextPane errorTextPane;
protected JButton okButton;
protected final Object errorLock;
public DataBrowserController(final DataBrowser dataBrowserPanel) {
this();
......@@ -119,6 +132,7 @@ public class DataBrowserController implements IAbstractController {
items = new HashMap<IKey, Item>();
openedNumericalSpectrumItems = new ArrayList<Item>();
openedNumericalMatrixItems = new ArrayList<Item>();
errorLock = new Object();
}
......@@ -186,6 +200,49 @@ public class DataBrowserController implements IAbstractController {
return result;
}
public void showErrorDialog(String message, String title) {
if (errorDialog == null) {
synchronized (errorLock) {
if (errorDialog == null) {
errorDialog = new JDialog(CometeUtils.getFrameForComponent(dataBrowserPanel), title, true);
JPanel mainPanel = new JPanel(new GridBagLayout());
errorTextPane = new HtmlTextPane();
errorTextPane.setEditable(false);
JScrollPane errorScrollPane = new JScrollPane(errorTextPane);
okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
errorDialog.setVisible(false);
}
});
GridBagConstraints errorScrollPaneConstraints = new GridBagConstraints();
errorScrollPaneConstraints.fill = GridBagConstraints.BOTH;
errorScrollPaneConstraints.gridx = 0;
errorScrollPaneConstraints.gridy = 0;
errorScrollPaneConstraints.weightx = 1;
errorScrollPaneConstraints.weighty = 1;
mainPanel.add(errorScrollPane, errorScrollPaneConstraints);
GridBagConstraints okButtonConstraints = new GridBagConstraints();
okButtonConstraints.fill = GridBagConstraints.NONE;
okButtonConstraints.gridx = 0;
okButtonConstraints.gridy = 1;
okButtonConstraints.weightx = 0;
okButtonConstraints.weighty = 0;
okButtonConstraints.anchor = GridBagConstraints.EAST;
mainPanel.add(okButton, okButtonConstraints);
errorDialog.setContentPane(mainPanel);
}
}
}
errorDialog.setTitle(title);
errorTextPane.setText(message);
errorDialog.pack();
errorDialog.setLocationRelativeTo(errorDialog.getOwner());
okButton.grabFocus();
errorDialog.setVisible(true);
}
protected void showErrors(final List<DataBrowserException> list) {
if (!list.isEmpty()) {
StringBuilder sb = new StringBuilder("<html><ul>");
......@@ -210,9 +267,9 @@ public class DataBrowserController implements IAbstractController {
}
}
sb.append("</ul></html>");
JOptionPane.showMessageDialog(CometeUtils.getFrameForComponent(dataBrowserPanel), sb.toString(),
DataBrowser.MESSAGES.getString("Error.Title"), JOptionPane.WARNING_MESSAGE);
showErrorDialog(sb.toString(), DataBrowser.MESSAGES.getString("Error.Title"));
// JOptionPane.showMessageDialog(CometeUtils.getFrameForComponent(dataBrowserPanel), sb.toString(),
// DataBrowser.MESSAGES.getString("Error.Title"), JOptionPane.WARNING_MESSAGE);
}
}
......@@ -1054,8 +1111,9 @@ public class DataBrowserController implements IAbstractController {
IDataSourceBrowser browser = DataSourceManager.getProducerFromKey(sourceKey);
if (browser == null) {
throw new LoadException(filename + " (source node #"
+ (1 + elemSourceList.indexOf(elemSource)) + ")",
throw new LoadException(
filename + " (source node #" + (1 + elemSourceList.indexOf(elemSource))
+ ")",
DataBrowser.MESSAGES.getString("Error.LoadSource.UnavailablePlugin")
+ " " + sourceKey.getInformationKey());
}
......@@ -1069,9 +1127,10 @@ public class DataBrowserController implements IAbstractController {
LOGGER.error("Impossible to import view configuration {} because {}", filename,
e.getMessage());
LOGGER.debug("Stack trace", e);
errors.add(new LoadException(filename + " (source node #"
+ (1 + elemSourceList.indexOf(elemSource)) + ")", DataBrowser.MESSAGES
.getString("Error.LoadConfig.XmlKeyRecovering")));
errors.add(new LoadException(
filename + " (source node #" + (1 + elemSourceList.indexOf(elemSource))
+ ")",
DataBrowser.MESSAGES.getString("Error.LoadConfig.XmlKeyRecovering")));
} catch (LoadException e) {
LOGGER.error("Impossible to load view configuration {} because {}", filename,
......@@ -1220,8 +1279,8 @@ public class DataBrowserController implements IAbstractController {
boolean changeScale = false;
if (openStructure.xKey != null) {
changeScale = true;
IDataSourceBrowser dataSourceBrowserForId = getDataSourceBrowserForId(openStructure.xKey
.getSourceProduction());
IDataSourceBrowser dataSourceBrowserForId = getDataSourceBrowserForId(
openStructure.xKey.getSourceProduction());
DataType dataType = dataSourceBrowserForId
.getType(openStructure.xKey);
DataFormat format = dataSourceBrowserForId
......@@ -1232,8 +1291,8 @@ public class DataBrowserController implements IAbstractController {
if (openStructure.yKey != null) {
changeScale = true;
IDataSourceBrowser dataSourceBrowserForId = getDataSourceBrowserForId(openStructure.yKey
.getSourceProduction());
IDataSourceBrowser dataSourceBrowserForId = getDataSourceBrowserForId(
openStructure.yKey.getSourceProduction());
DataType dataType = dataSourceBrowserForId
.getType(openStructure.yKey);
DataFormat format = dataSourceBrowserForId
......@@ -1268,13 +1327,13 @@ public class DataBrowserController implements IAbstractController {
} catch (JDOMException e1) {
LOGGER.error("Impossible to import view configuration {} because {}", filename, e1.getMessage());
LOGGER.debug("Stack trace", e1);
errors.add(new LoadException(filename, DataBrowser.MESSAGES
.getString("Error.LoadConfig.StructureProblem")));
errors.add(new LoadException(filename,
DataBrowser.MESSAGES.getString("Error.LoadConfig.StructureProblem")));
} catch (IOException e1) {
LOGGER.error("Impossible to import view configuration {} because {}", filename, e1.getMessage());
LOGGER.debug("Stack trace", e1);
errors.add(new LoadException(filename, DataBrowser.MESSAGES
.getString("Error.LoadConfig.ReadProblem")));
errors.add(new LoadException(filename,
DataBrowser.MESSAGES.getString("Error.LoadConfig.ReadProblem")));
} finally {
DataBrowser.runInEDT(new Runnable() {
@Override
......
......@@ -12,6 +12,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cdma.gui.databrowser.exception.DataBrowserException;
import org.cdma.gui.databrowser.util.ApplicationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -108,26 +109,28 @@ public abstract class AbstractDataSourceProducerBrowser extends AbstractDataSour
return version;
}
protected AbstractDataSource<?> createDataSource(final IKey key) throws Exception {
protected AbstractDataSource<?> createDataSource(final IKey key) throws DataBrowserException {
AbstractDataSource<?> source = null;
if ((key != null) && (producer != null)) {
LOGGER.trace("producer class for Key {} = {} ", key, producer.getClass().getName());
try {
source = producer.createDataSource(key);
} catch (Exception e) {
LOGGER.error("Impossible to create data source for {} because {}", key, e.getMessage());
String message = "Impossible to create data source for {} because {}";
LOGGER.error(message, key, e.getMessage());
LOGGER.debug("Stack trace", e);
throw e;
throw new DataBrowserException(
message.replaceFirst("{}", String.valueOf(key)).replace("{}", e.getMessage()), e);
}
}
return source;
}
protected Object getDataSource(final IKey key) throws Exception {
protected Object getDataSource(final IKey key) throws DataBrowserException {
return getDataSource(key, false);
}
protected Object getDataSource(final IKey key, final boolean closeSource) throws Exception {
protected Object getDataSource(final IKey key, final boolean closeSource) throws DataBrowserException {
Object data = null;
try {
AbstractDataSource<?> source = createDataSource(key);
......@@ -138,9 +141,11 @@ public abstract class AbstractDataSourceProducerBrowser extends AbstractDataSour
}
}
} catch (Exception e) {
LOGGER.error("Impossible to read data for source {} because {}", key, e.getMessage());
String message = "Impossible to read data for source {} because {}";
LOGGER.error(message, key, e.getMessage());
LOGGER.debug("Stack trace", e);
throw e;
message = message.replaceFirst("{}", String.valueOf(key)).replace("{}", e.getMessage());
throw new DataBrowserException(message, e);
}
if (data != null) {
if (closeSource) {
......@@ -150,7 +155,7 @@ public abstract class AbstractDataSourceProducerBrowser extends AbstractDataSour
return data;
}
protected String[] getStringArrayValue(final IKey key) throws Exception {
protected String[] getStringArrayValue(final IKey key) throws DataBrowserException {
String[] values = null;
Object data = getDataSource(key);
if ((data != null) && (data instanceof StringMatrix)) {
......@@ -159,7 +164,7 @@ public abstract class AbstractDataSourceProducerBrowser extends AbstractDataSour
return values;
}
protected String getStringValue(final IKey key) throws Exception {
protected String getStringValue(final IKey key) throws DataBrowserException {
String value = null;
Object data = getDataSource(key);
if (data != null) {
......
......@@ -8,6 +8,7 @@
package org.cdma.gui.databrowser.impl.cdma;
import java.io.File;
import java.io.FileReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -72,17 +73,21 @@ public class CDMADataSourceBrowser extends AbstractDataSourceProducerBrowser {
public static final String DB_FACTOR_PROPERTY = "DB_FACTOR_PROPERTY";
private static final String DB_REFRESHING_ENABLE = "DB_REFRESHING_ENABLE";
protected final Map<ITreeNode, List<IKey>> dimensionMap = new HashMap<ITreeNode, List<IKey>>();
private static final String INVALID_FILE_STRUCTURE = "Invalid file structure";
private static final String FILE_IS_NOT_READABLE = "File is not readable (please check your user rights)";
private static final String INVALID_URI = "Invalid URI";
private IFactory factory = null;
private IDataSourceSeeker seeker = null;
protected final Map<ITreeNode, List<IKey>> dimensionMap;
private List<IDataSourceBrowser> dataBrowserList = null;
private IFactory factory;
private IDataSourceSeeker seeker;
private boolean refreshingEnable = false;
private List<IDataSourceBrowser> dataBrowserList;
private boolean refreshingEnable;
public CDMADataSourceBrowser() {
super(CDMADataSourceFactory.class);
this(null);
initFactoryKeyMap();
String refreshingProp = System.getProperty(DB_REFRESHING_ENABLE);
refreshingEnable = ((refreshingProp != null) && Boolean.parseBoolean(refreshingProp));
......@@ -92,6 +97,12 @@ public class CDMADataSourceBrowser extends AbstractDataSourceProducerBrowser {
initDataSourceBrowserList();
}
private CDMADataSourceBrowser(final IFactory factory) {
super(CDMADataSourceFactory.class);
this.factory = factory;
dimensionMap = new HashMap<ITreeNode, List<IKey>>();
}
private void initFactoryKeyMap() {
if (keyFactoryMap == null) {
keyFactoryMap = new HashMap<String, CDMAKeyFactory>();
......@@ -141,11 +152,6 @@ public class CDMADataSourceBrowser extends AbstractDataSourceProducerBrowser {
return keyFactory;
}
private CDMADataSourceBrowser(final IFactory factory) {
super(CDMADataSourceFactory.class);
this.factory = factory;
}
private void initDataSourceBrowserList() {
Set<Class<? extends IDataSourceSeeker>> implementationSeekerSet = getDataSourceSeekerImplementations();
Map<String, IDataSourceSeeker> tmpSeekerMap = new HashMap<String, IDataSourceSeeker>();
......@@ -392,7 +398,7 @@ public class CDMADataSourceBrowser extends AbstractDataSourceProducerBrowser {
return result;
}
private String getStringFromURI(final URI uri, final boolean withFragment) throws Exception {
private String getStringFromURI(final URI uri, final boolean withFragment) throws DataBrowserException {
String result = null;
if ((uri != null) && (uri.getPath() != null) && !uri.getPath().isEmpty()) {
File file = new File(uri.getPath());
......@@ -691,12 +697,35 @@ public class CDMADataSourceBrowser extends AbstractDataSourceProducerBrowser {
}
private void createGroupNode(final CDMAKeyFactory keyFactory, final ITreeNode parent, final URI uri,
final String[] path) throws Exception {
final String[] path) throws DataBrowserException {
if ((parent != null) && (uri != null) && (producer != null) && (path != null) && (keyFactory != null)) {
CDMAKey cdmaKey = keyFactory.generateKeyGroupList(uri, path);
String[] groupList = getStringArrayValue(cdmaKey);
if (groupList != null) {
if (groupList == null) {
// a problem occurred
String errorMessage;
try {
File file = new File(uri);
if (file.exists()) {
if (file.canRead()) {
errorMessage = INVALID_FILE_STRUCTURE;
try (FileReader reader = new FileReader(file)) {
reader.read();
} catch (Exception e) {
errorMessage = FILE_IS_NOT_READABLE;
}
} else {
errorMessage = FILE_IS_NOT_READABLE;
}
} else {
errorMessage = INVALID_URI;
}
} catch (Exception e) {
errorMessage = INVALID_FILE_STRUCTURE;
}
LOGGER.error("Group list for uri {} is null", cdmaKey);
throw new BadFileException(errorMessage);
} else {
ITreeNode child = null;
String[] newPath = null;
CDMAKey groupKey = null;
......@@ -712,24 +741,17 @@ public class CDMADataSourceBrowser extends AbstractDataSourceProducerBrowser {
createItemNode(keyFactory, child, uri, newPath);
createDimensionNode(keyFactory, child, uri, newPath);
}
} else {
// a problem occurred
LOGGER.error("Group list for uri {} is null", cdmaKey);
throw new BadFileException("Invalid file structure");
}
}
}
private void createItemNode(final CDMAKeyFactory keyFactory, final ITreeNode parent, final URI uri,
final String[] path) throws Exception {
final String[] path) throws DataBrowserException {
if ((parent != null) && (uri != null) && (path != null) && (keyFactory != null)) {
CDMAKey cdmaKey = keyFactory.generateKeyItemList(uri, path);
String[] itemList = getStringArrayValue(cdmaKey);
String pluginId = keyFactory.getPluginRestriction();
boolean isMamboPlugin = DBDataSourceSeeker.MamboSoleilPluginId.equals(pluginId);
if (itemList != null) {
ItemTreeNode child = null;
CDMAKey keyNode = null;
......@@ -756,7 +778,6 @@ public class CDMADataSourceBrowser extends AbstractDataSourceProducerBrowser {
}
if (itemName.endsWith(MAMBO_VALUE)) {
tmpKeyList.add(keyNode);
}
}
}
......@@ -765,9 +786,8 @@ public class CDMADataSourceBrowser extends AbstractDataSourceProducerBrowser {
}
private void createDimensionNode(final CDMAKeyFactory keyFactory, final ITreeNode parent, final URI uri,
final String[] path) throws Exception {
final String[] path) throws DataBrowserException {
if ((parent != null) && (uri != null) && (path != null) && (keyFactory != null)) {
CDMAKey cdmaKey = keyFactory.generateKeyDimensionList(uri, path);
String[] dimensionList = getStringArrayValue(cdmaKey);
if (dimensionList != null) {
......
......@@ -213,7 +213,7 @@ public class LogicalPanel extends JPanel implements ActionListener {
experimentLoadingPanel.setLoading(true);
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
protected Void doInBackground() {
setTopUri(topUriField.getText());
return null;
}
......
......@@ -16,8 +16,6 @@ import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
import org.cdma.gui.databrowser.DataBrowserController;
import org.cdma.gui.databrowser.interfaces.DataFormat;
import org.cdma.gui.databrowser.interfaces.IDataSourceBrowser;
......@@ -34,7 +32,7 @@ import fr.soleil.comete.box.scalarbox.StringScalarBox;
public class ScalarViewer extends AbstractItemViewer<ScalarItem> implements IItemViewer {
private static final long serialVersionUID = 312054827522085942L;
private static final long serialVersionUID = 207694906855906L;
private JSplitPane splitPane;
private JScrollPane itemScrollPane;
......@@ -72,11 +70,8 @@ public class ScalarViewer extends AbstractItemViewer<ScalarItem> implements IIte
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
gbc.fill = GridBagConstraints.HORIZONTAL;
itemScrollPane.setViewportView(itemPanel);
splitPane.setRightComponent(itemScrollPane);
setLayout(new BorderLayout());
add(splitPane, BorderLayout.CENTER);
}
......@@ -104,25 +99,19 @@ public class ScalarViewer extends AbstractItemViewer<ScalarItem> implements IIte
case TEXT:
scalarItem = new TextScalarItem(this, item, controller);
break;
case NUMERICAL:
scalarItem = new NumberScalarItem(this, item, controller);
break;
case BOOLEAN:
scalarItem = new BooleanScalarItem(this, item, controller);
break;
default:
// nop
break;
}
if (scalarItem != null) {
scalarItem.connect();
displayedItems.put(item.getKey(), scalarItem);
ScalarRowPanel rowPanel = scalarItem.getRowPanel();
itemPanel.add(rowPanel, gbc);
validate();
......@@ -135,14 +124,12 @@ public class ScalarViewer extends AbstractItemViewer<ScalarItem> implements IIte
@Override
public void removeItem(final Item item) {
ScalarItem scalarItem = displayedItems.remove(item.getKey());
if (scalarItem != null) {
// remove the components of the row
ScalarRowPanel rowPanel = scalarItem.getRowPanel();
itemPanel.remove(rowPanel);
validate();
repaint();
// disconnect item (viewer and setter)
scalarItem.disconnect();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment