Refacted notification and Office UI packages for better maintainability
This commit introduces fixes to address issues reported by sonarqube.
In particular, the following changes have been made:
* Uses logger instead of printing stack traces to the console
* Utility classes can no longer be instantiated
* Correctly marked constants as final
* Custom exceptions don't hide root causes any more
* Added missing equals() and hashCode() implementations
diff --git a/bundles/org.eclipse.capra.ui.notification/META-INF/MANIFEST.MF b/bundles/org.eclipse.capra.ui.notification/META-INF/MANIFEST.MF
index 31343ac..e2aed4d 100644
--- a/bundles/org.eclipse.capra.ui.notification/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.capra.ui.notification/META-INF/MANIFEST.MF
@@ -14,6 +14,7 @@
Import-Package: org.eclipse.emf.ecore,
org.eclipse.emf.ecore.resource,
org.eclipse.emf.ecore.resource.impl,
- org.eclipse.emf.edit.ui.util
+ org.eclipse.emf.edit.ui.util,
+ org.slf4j
Bundle-Vendor: %Bundle-Vendor
Export-Package: org.eclipse.capra.ui.notification
diff --git a/bundles/org.eclipse.capra.ui.notification/src/org/eclipse/capra/ui/notification/CapraNotificationHelper.java b/bundles/org.eclipse.capra.ui.notification/src/org/eclipse/capra/ui/notification/CapraNotificationHelper.java
index 6d2f030..2c62e4c 100644
--- a/bundles/org.eclipse.capra.ui.notification/src/org/eclipse/capra/ui/notification/CapraNotificationHelper.java
+++ b/bundles/org.eclipse.capra.ui.notification/src/org/eclipse/capra/ui/notification/CapraNotificationHelper.java
@@ -22,13 +22,17 @@
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Contains methods that support the Capra notification (marker) solution.
- *
+ *
* @author Dusan Kalanj
*/
public class CapraNotificationHelper {
+
+ private static final Logger LOG = LoggerFactory.getLogger(CapraNotificationHelper.class);
/**
* ID of Capra custom marker for reporting a generic problem.
@@ -82,6 +86,10 @@
*/
public static final String MESSAGE = "message";
+ private CapraNotificationHelper() {
+ // Deliberately do nothing
+ }
+
// TODO necessary to specify all the fields that have to be filled out in
// order for the method to work! Maybe make a custom exception for when
// something is not filled out?
@@ -106,9 +114,9 @@
String existingMarkerIssue = existingMarker.getAttribute(ISSUE_TYPE, null);
String existingMarkerUri = existingMarker.getAttribute(OLD_URI, null);
- if (existingMarkerUri.equals(newMarkerUri) && existingMarkerIssue.equals(newMarkerIssue))
+ if (existingMarkerUri.equals(newMarkerUri) && existingMarkerIssue.equals(newMarkerIssue)) {
existingMarker.delete();
-
+ }
// The code bellow deletes the marker that signifies a delete
// operation in case the new marker signifies a rename/move
// operation. The only thing that doesn't work with this
@@ -119,28 +127,30 @@
// rename and delete). This problem disappears if automatic
// marker removal is implemented (already done for EMF).
if (existingMarkerUri.equals(newMarkerUri) && existingMarkerIssue.equals(IssueType.DELETED.getValue())
- && newMarkerIssue.matches(IssueType.RENAMED.getValue() + "|" + IssueType.MOVED.getValue()))
+ && newMarkerIssue.matches(IssueType.RENAMED.getValue() + "|" + IssueType.MOVED.getValue())) {
existingMarker.delete();
-
- if (existingMarkerUri.equals(newMarkerUri) && newMarkerIssue.equals(IssueType.ADDED))
+ }
+ if (existingMarkerUri.equals(newMarkerUri) && newMarkerIssue.equalsIgnoreCase(IssueType.ADDED.getValue())) {
existingMarker.delete();
+ }
}
String message = markerInfo.get(MESSAGE);
- if (message == null || message.isEmpty())
+ if (message == null || message.isEmpty()) {
return;
-
+ }
IMarker marker = container.createMarker(CAPRA_PROBLEM_MARKER_ID);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
marker.setAttribute(IMarker.MESSAGE, message);
markerInfo.remove(MESSAGE);
- for (Entry<String, String> entry : markerInfo.entrySet())
+ for (Entry<String, String> entry : markerInfo.entrySet()) {
marker.setAttribute(entry.getKey(), entry.getValue());
-
+ }
} catch (CoreException e) {
- if (container.exists())
- e.printStackTrace();
+ if (container.exists()) {
+ LOG.warn("CoreException occured when creating a marker even though container exists.", e);
+ }
}
}
@@ -164,16 +174,20 @@
String existingMarkerUri = marker.getAttribute(OLD_URI, null);
String existingMarkerIssue = marker.getAttribute(ISSUE_TYPE, null);
- if (existingMarkerUri.equals(uri))
- if (issues == null)
+ if (existingMarkerUri.equals(uri)) {
+ if (issues == null) {
marker.delete();
- else
- for (IssueType issue : issues)
- if (existingMarkerIssue.equals(issue.getValue()))
+ } else {
+ for (IssueType issue : issues) {
+ if (existingMarkerIssue.equals(issue.getValue())) {
marker.delete();
+ }
+ }
+ }
+ }
}
} catch (CoreException e) {
- e.printStackTrace();
+ LOG.warn("CoreException occured when deleting a marker.", e);
}
}
@@ -187,17 +201,17 @@
*/
public static URI convertToFileUri(URI uri) {
- if (!uri.isPlatformResource())
+ if (!uri.isPlatformResource()) {
return uri;
- else {
+ } else {
String platformUri = uri.toPlatformString(true);
IPath filePath = ResourcesPlugin.getWorkspace().getRoot().findMember(platformUri).getRawLocation();
URI fileUri = URI.createFileURI(filePath.toString());
String fragment = uri.fragment();
- if (fragment != null)
+ if (fragment != null) {
fileUri = fileUri.appendFragment(fragment);
-
+ }
return fileUri;
}
}
diff --git a/bundles/org.eclipse.capra.ui.office/build.properties b/bundles/org.eclipse.capra.ui.office/build.properties
index c2ae5d4..80191fd 100644
--- a/bundles/org.eclipse.capra.ui.office/build.properties
+++ b/bundles/org.eclipse.capra.ui.office/build.properties
@@ -17,5 +17,4 @@
lib/poi-ooxml-3.10.1-filtered.jar,\
lib/poi-ooxml-schemas-3.10.1-20140818.jar,\
lib/xmlbeans-2.6.0.jar,\
- OSGI-INF/l10n/bundle.properties
-
+ OSGI-INF/
diff --git a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/exceptions/CapraOfficeFileNotSupportedException.java b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/exceptions/CapraOfficeFileNotSupportedException.java
index 10c7e9c..b99b3d6 100644
--- a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/exceptions/CapraOfficeFileNotSupportedException.java
+++ b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/exceptions/CapraOfficeFileNotSupportedException.java
@@ -21,7 +21,7 @@
public class CapraOfficeFileNotSupportedException extends Exception {
private static final long serialVersionUID = -7730053652692861930L;
- private static String EXCEPTION_MESSAGE = "%s file type is not supported.";
+ private static final String EXCEPTION_MESSAGE = "%s file type is not supported.";
/**
* A default constructor.
diff --git a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/exceptions/CapraOfficeObjectNotFound.java b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/exceptions/CapraOfficeObjectNotFound.java
index 40b9f32..1545dd2 100644
--- a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/exceptions/CapraOfficeObjectNotFound.java
+++ b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/exceptions/CapraOfficeObjectNotFound.java
@@ -21,16 +21,31 @@
public class CapraOfficeObjectNotFound extends Exception {
private static final long serialVersionUID = -3973348630832482778L;
- private static String EXCEPTION_MESSAGE = "Could not find the object with ID %s in its document. Maybe the file has been edited or moved.";
+ private static final String EXCEPTION_MESSAGE = "Could not find the object with ID %s in its document. Maybe the file has been edited or moved.";
/**
- * A default constructor.
+ * A default constructor. Please use {@link CapraOfficeObjectNotFound(String, Throwable)}
+ * whenever possible to propagate the original cause.
*
* @param id
* the id of the object that couldn't be found
*/
public CapraOfficeObjectNotFound(String id) {
- super(String.format(EXCEPTION_MESSAGE, id));
+ super(formatExceptionMessage(id));
+ }
+
+ /**
+ * Creates a new exception indicating that an office object could not be found.
+ * This is the preferred constructor since it does not hide the original cause.
+ *
+ * @param id
+ * @param cause
+ */
+ public CapraOfficeObjectNotFound(String id, Throwable cause) {
+ super(formatExceptionMessage(id), cause);
}
+ private static String formatExceptionMessage(String id) {
+ return String.format(EXCEPTION_MESSAGE, id);
+ }
}
diff --git a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraExcelRow.java b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraExcelRow.java
index 85a95dc..f5f39d6 100644
--- a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraExcelRow.java
+++ b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraExcelRow.java
@@ -268,7 +268,7 @@
* the row to identify
* @return the ID of the row
*/
- protected String getRowIdFromExcelRow(Row row) {
+ final protected String getRowIdFromExcelRow(Row row) {
String rowId = "";
if (idColumn.equals(OfficePreferences.EXCEL_COLUMN_VALUE_DEFAULT)) {
rowId = Integer.toString(row.getRowNum() + 1);
@@ -277,4 +277,30 @@
}
return rowId;
}
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = super.hashCode();
+ result = prime * result + ((idColumn == null) ? 0 : idColumn.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (!super.equals(obj))
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ CapraExcelRow other = (CapraExcelRow) obj;
+ if (idColumn == null) {
+ if (other.idColumn != null)
+ return false;
+ } else if (!idColumn.equals(other.idColumn))
+ return false;
+ return true;
+ }
+
}
diff --git a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraGoogleSheetsRow.java b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraGoogleSheetsRow.java
index 7d19c28..e0d8c12 100644
--- a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraGoogleSheetsRow.java
+++ b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraGoogleSheetsRow.java
@@ -18,6 +18,8 @@
import org.apache.poi.ss.usermodel.Row;
import org.eclipse.capra.ui.office.exceptions.CapraOfficeObjectNotFound;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class extends the CapraExcelRow and provides an object to describe a
@@ -29,6 +31,8 @@
*/
public class CapraGoogleSheetsRow extends CapraExcelRow {
+ private static final Logger LOG = LoggerFactory.getLogger(CapraGoogleSheetsRow.class);
+
/**
* Extracts the data from the Excel row the same way as its parent
* (CapraExcelRow), but sets a different URI. Because the excel file is only
@@ -59,7 +63,7 @@
try {
Desktop.getDesktop().browse(new URI("https://docs.google.com/spreadsheets/d/" + this.getFileId()));
} catch (IOException | URISyntaxException e) {
- e.printStackTrace();
+ LOG.info("Could not open Google spreadsheet.", e);
}
}
}
diff --git a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraOfficeObject.java b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraOfficeObject.java
index ebe921b..f417508 100644
--- a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraOfficeObject.java
+++ b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraOfficeObject.java
@@ -17,6 +17,8 @@
import java.nio.file.NoSuchFileException;
import org.eclipse.capra.ui.office.exceptions.CapraOfficeObjectNotFound;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class provides a custom object for describing the contents of MS Excel
@@ -26,6 +28,8 @@
*
*/
public class CapraOfficeObject {
+
+ private static final Logger LOG = LoggerFactory.getLogger(CapraOfficeObject.class);
/**
* The MS Office file-types that are supported by the plugin.
@@ -109,10 +113,11 @@
public File getFile() throws NoSuchFileException {
String fileId = getFileId();
File officeFile = new File(fileId);
- if (officeFile.exists())
+ if (officeFile.exists()) {
return officeFile;
- else
+ } else {
throw new NoSuchFileException(fileId);
+ }
}
/**
@@ -160,8 +165,8 @@
try {
Desktop.getDesktop().open(getFile());
} catch (IOException e) {
- e.printStackTrace();
- throw new CapraOfficeObjectNotFound(getId());
+ LOG.error("Could not oben office file.", e);
+ throw new CapraOfficeObjectNotFound(getId(), e);
}
}
@@ -203,23 +208,29 @@
*/
@Override
public boolean equals(Object obj) {
- if (this == obj)
+ if (this == obj) {
return true;
- if (obj == null)
+ }
+ if (obj == null) {
return false;
- if (getClass() != obj.getClass())
+ }
+ if (getClass() != obj.getClass()) {
return false;
+ }
CapraOfficeObject other = (CapraOfficeObject) obj;
if (data == null) {
- if (other.data != null)
+ if (other.data != null) {
return false;
+ }
} else if (!data.equals(other.data))
return false;
if (uri == null) {
- if (other.uri != null)
+ if (other.uri != null) {
return false;
- } else if (!uri.equals(other.uri))
+ }
+ } else if (!uri.equals(other.uri)) {
return false;
+ }
return true;
}
}
\ No newline at end of file
diff --git a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraWordRequirement.java b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraWordRequirement.java
index 51cf6ee..0c4abe7 100644
--- a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraWordRequirement.java
+++ b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/model/CapraWordRequirement.java
@@ -12,13 +12,19 @@
package org.eclipse.capra.ui.office.model;
import java.io.File;
+import java.io.IOException;
import java.util.Arrays;
+import javax.xml.parsers.ParserConfigurationException;
+
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.eclipse.capra.ui.office.utils.CapraOfficeUtils;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
/**
* This class extends the CapraOfficeObject and provides an object to describe a
@@ -28,6 +34,8 @@
*
*/
public class CapraWordRequirement extends CapraOfficeObject {
+
+ private static final Logger LOG = LoggerFactory.getLogger(CapraWordRequirement.class);
/**
* RegEx of characters (tabs, newlines, carriage returns and invisible
@@ -72,10 +80,16 @@
Document doc;
try {
doc = CapraOfficeUtils.createDOMDocument(pCtp.toString());
- } catch (Exception e) {
- e.printStackTrace();
+ } catch (IOException e) {
+ LOG.info("Could not create DOM document: error reading file.", e);
return;
- }
+ } catch (ParserConfigurationException e) {
+ LOG.info("Could not create DOM document: parser not configured properly.", e);
+ return;
+ } catch (SAXException e) {
+ LOG.info("Could not create DOM document: malformed XML.", e);
+ return;
+ }
// Get all nodes from the paragraph (there should be just one node if
// the TODO bellow isn't implemented)
diff --git a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/preferences/OfficePreferences.java b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/preferences/OfficePreferences.java
index bdf964e..57865e3 100644
--- a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/preferences/OfficePreferences.java
+++ b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/preferences/OfficePreferences.java
@@ -74,10 +74,10 @@
* Controls
*/
private Text charCount;
- private Button excel_rowIdIsRowNumber;
- private Button excel_rowIdIsCustomColumn;
- private Text excel_customIdColumnName;
- private Text word_fieldName;
+ private Button excelRowIdIsRowNumber;
+ private Button excelRowIdIsCustomColumn;
+ private Text excelCustomIdColumnName;
+ private Text wordFieldName;
/**
* Creates a swt widget that takes the numColumns of grid space inside the
@@ -118,22 +118,23 @@
boolean idIsRowNumber;
String idColumn;
- if (excel_rowIdIsRowNumber.getSelection()) {
+ if (excelRowIdIsRowNumber.getSelection()) {
idColumn = EXCEL_COLUMN_VALUE_DEFAULT;
- excel_customIdColumnName.setText("");
+ excelCustomIdColumnName.setText("");
idIsRowNumber = true;
} else {
- if (excel_customIdColumnName.getText().isEmpty())
- excel_customIdColumnName.setText(EXCEL_CUSTOM_COLUMN_DEFAULT);
- idColumn = excel_customIdColumnName.getText();
+ if (excelCustomIdColumnName.getText().isEmpty()) {
+ excelCustomIdColumnName.setText(EXCEL_CUSTOM_COLUMN_DEFAULT);
+ }
+ idColumn = excelCustomIdColumnName.getText();
idIsRowNumber = false;
}
store.setValue(CHAR_COUNT, charCount.getText());
store.setValue(EXCEL_COLUMN_RADIO_CHOICE, idIsRowNumber);
- store.setValue(EXCEL_CUSTOM_COLUMN, excel_customIdColumnName.getText());
+ store.setValue(EXCEL_CUSTOM_COLUMN, excelCustomIdColumnName.getText());
store.setValue(EXCEL_COLUMN_VALUE, idColumn);
- store.setValue(WORD_FIELD_NAME, word_fieldName.getText());
+ store.setValue(WORD_FIELD_NAME, wordFieldName.getText());
}
/**
@@ -145,32 +146,32 @@
boolean idIsRowNumber = store.getBoolean(EXCEL_COLUMN_RADIO_CHOICE);
if (idIsRowNumber) {
- excel_rowIdIsRowNumber.setSelection(true);
- excel_customIdColumnName.setText("");
- excel_customIdColumnName.setEnabled(false);
+ excelRowIdIsRowNumber.setSelection(true);
+ excelCustomIdColumnName.setText("");
+ excelCustomIdColumnName.setEnabled(false);
} else {
- excel_rowIdIsCustomColumn.setSelection(true);
- excel_customIdColumnName.setText(store.getString(EXCEL_CUSTOM_COLUMN));
+ excelRowIdIsCustomColumn.setSelection(true);
+ excelCustomIdColumnName.setText(store.getString(EXCEL_CUSTOM_COLUMN));
}
charCount.setText(store.getString(CHAR_COUNT));
- word_fieldName.setText(store.getString(WORD_FIELD_NAME));
+ wordFieldName.setText(store.getString(WORD_FIELD_NAME));
}
/**
* Sets the default values if the user clicks on restore defaults option.
*/
private void initializeDefaults() {
- excel_rowIdIsRowNumber.setSelection(EXCEL_COLUMN_RADIO_ID_IS_LINE_NUMBER);
- excel_rowIdIsCustomColumn.setSelection(!EXCEL_COLUMN_RADIO_ID_IS_LINE_NUMBER);
+ excelRowIdIsRowNumber.setSelection(EXCEL_COLUMN_RADIO_ID_IS_LINE_NUMBER);
+ excelRowIdIsCustomColumn.setSelection(!EXCEL_COLUMN_RADIO_ID_IS_LINE_NUMBER);
- if (excel_rowIdIsRowNumber.getSelection()) {
- excel_customIdColumnName.setText("");
- excel_customIdColumnName.setEnabled(false);
+ if (excelRowIdIsRowNumber.getSelection()) {
+ excelCustomIdColumnName.setText("");
+ excelCustomIdColumnName.setEnabled(false);
}
charCount.setText(CHAR_COUNT_DEFAULT);
- word_fieldName.setText(WORD_FIELD_NAME_DEFAULT);
+ wordFieldName.setText(WORD_FIELD_NAME_DEFAULT);
}
@Override
@@ -184,17 +185,17 @@
createLabel(compositeExcelIdColumn, EXCEL_COLUMN_RADIO_CHOICE_DESC, 2);
Composite compositeRadioButtons = createComposite(compositeExcelIdColumn, 3);
- excel_rowIdIsRowNumber = createRadioButton(compositeRadioButtons, EXCEL_COLUMN_IS_LINE_NUMBER_OPTION_DESC, 3);
- excel_rowIdIsCustomColumn = createRadioButton(compositeRadioButtons, EXCEL_COLUMN_IS_CUSTOM_OPTION_DESC, 1);
- excel_customIdColumnName = createTextField(compositeRadioButtons, 1, FIXED_TEXT_FIELD_WIDTH);
+ excelRowIdIsRowNumber = createRadioButton(compositeRadioButtons, EXCEL_COLUMN_IS_LINE_NUMBER_OPTION_DESC, 3);
+ excelRowIdIsCustomColumn = createRadioButton(compositeRadioButtons, EXCEL_COLUMN_IS_CUSTOM_OPTION_DESC, 1);
+ excelCustomIdColumnName = createTextField(compositeRadioButtons, 1, FIXED_TEXT_FIELD_WIDTH);
createLabel(compositeRadioButtons, EXCEL_COLUMN_IS_CUSTOM_OPTION_HINT, 1);
- excel_rowIdIsRowNumber.addSelectionListener(new SelectionListener() {
+ excelRowIdIsRowNumber.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
- excel_customIdColumnName.setText("");
- excel_customIdColumnName.setEnabled(false);
+ excelCustomIdColumnName.setText("");
+ excelCustomIdColumnName.setEnabled(false);
}
@Override
@@ -202,12 +203,12 @@
}
});
- excel_rowIdIsCustomColumn.addSelectionListener(new SelectionListener() {
+ excelRowIdIsCustomColumn.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
- excel_customIdColumnName.setEnabled(true);
- excel_customIdColumnName.setText(EXCEL_CUSTOM_COLUMN_DEFAULT);
+ excelCustomIdColumnName.setEnabled(true);
+ excelCustomIdColumnName.setText(EXCEL_CUSTOM_COLUMN_DEFAULT);
}
@Override
@@ -217,7 +218,7 @@
Composite compositeWordRequirementFieldName = createComposite(parent, 2);
createLabel(compositeWordRequirementFieldName, WORD_FIELD_NAME_DESC, 1);
- word_fieldName = createTextField(compositeWordRequirementFieldName, 1, FIXED_TEXT_FIELD_WIDTH);
+ wordFieldName = createTextField(compositeWordRequirementFieldName, 1, FIXED_TEXT_FIELD_WIDTH);
initializeValues();
diff --git a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/CapraOfficeUtils.java b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/CapraOfficeUtils.java
index 9dc5b9b..98837a6 100644
--- a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/CapraOfficeUtils.java
+++ b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/CapraOfficeUtils.java
@@ -46,7 +46,7 @@
* @author Dusan Kalanj
*
*/
-public class CapraOfficeUtils {
+public final class CapraOfficeUtils {
/**
* Hide default constructor.
@@ -110,12 +110,7 @@
}
}
- // Should be without the try block, but the library contains a bug.
- try {
- actualSheetName = workBook.getSheetName(activeSheetIndex);
- } catch (NullPointerException e) {
- throw e;
- }
+ actualSheetName = workBook.getSheetName(activeSheetIndex);
}
return workBook.getSheet(actualSheetName);
diff --git a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/OfficePropertyTester.java b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/OfficePropertyTester.java
index cf93e7b..96ecdd9 100644
--- a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/OfficePropertyTester.java
+++ b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/OfficePropertyTester.java
@@ -30,12 +30,12 @@
@Override
public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
- if (property.equals(IS_VIEW_POPULATED))
+ if (property.equals(IS_VIEW_POPULATED)) {
return receiver != null;
-
- if (property.equals(IS_EXCEL_OBJECT))
+ }
+ if (property.equals(IS_EXCEL_OBJECT)) {
return receiver instanceof CapraExcelRow;
-
+ }
return false;
}
}
diff --git a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/OfficeTransferType.java b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/OfficeTransferType.java
index 367c548..cb30458 100644
--- a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/OfficeTransferType.java
+++ b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/utils/OfficeTransferType.java
@@ -44,11 +44,11 @@
private static final String MIME_TYPE = "capra_office";
private static final int MIME_TYPE_ID = registerType(MIME_TYPE);
- private static OfficeTransferType _instance = new OfficeTransferType();
+ private static OfficeTransferType theInstance = new OfficeTransferType();
/** Provides an instance of the class. */
public static OfficeTransferType getInstance() {
- return _instance;
+ return theInstance;
}
@Override
diff --git a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/views/OfficeView.java b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/views/OfficeView.java
index b224adc..8d6331e 100644
--- a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/views/OfficeView.java
+++ b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/views/OfficeView.java
@@ -174,8 +174,9 @@
int minAllowed = Activator.getDefault().getPreferenceStore().getInt(OfficePreferences.CHAR_COUNT);
String text = obj.toString();
int textLength = Math.min(text.length(), minAllowed);
- if (textLength == minAllowed)
+ if (textLength == minAllowed) {
text = text.substring(0, textLength) + "...";
+ }
return text;
};
@@ -209,8 +210,9 @@
try {
if (data instanceof String[]) {
File file = new File(((String[]) data)[0]);
- if (file != null && file.exists())
+ if (file != null && file.exists()) {
parseGenericFile(file);
+ }
}
} catch (CapraOfficeFileNotSupportedException e) {
LOG.debug("Capra does not support this file.", e);
@@ -229,9 +231,9 @@
/**
* Adapter used by the view to handle drag events.
*/
- class SelectionDragAdapter extends ViewerDragAdapter {
+ private static class SelectionDragAdapter extends ViewerDragAdapter {
- TableViewer viewer;
+ private TableViewer viewer;
public SelectionDragAdapter(TableViewer viewer) {
super(viewer);
@@ -245,9 +247,9 @@
TableItem[] items = viewer.getTable().getSelection();
ArrayList<CapraOfficeObject> officeObjects = new ArrayList<CapraOfficeObject>();
- for (int i = 0; i < items.length; i++)
+ for (int i = 0; i < items.length; i++) {
officeObjects.add((CapraOfficeObject) items[i].getData());
-
+ }
event.data = officeObjects;
}
}
@@ -297,12 +299,13 @@
private void parseGenericFile(File file) throws CapraOfficeFileNotSupportedException {
String fileExtension = Files.getFileExtension(file.getName());
- if (fileExtension.equals(CapraOfficeObject.XLSX) || fileExtension.equals(CapraOfficeObject.XLS))
+ if (fileExtension.equals(CapraOfficeObject.XLSX) || fileExtension.equals(CapraOfficeObject.XLS)) {
parseExcelDocument(file, null, null);
- else if (fileExtension.equals(CapraOfficeObject.DOCX))
+ } else if (fileExtension.equals(CapraOfficeObject.DOCX)) {
parseWordDocument(file);
- else
+ } else {
throw new CapraOfficeFileNotSupportedException(fileExtension);
+ }
}
/**
@@ -333,6 +336,7 @@
showErrorMessage(ERROR_TITLE, e.getMessage(), null);
return;
} catch (IOException e) {
+ showErrorMessage(ERROR_TITLE, "Could not open the Excel workbook.", null);
LOG.warn("Could not open the Excel workbook", e);
return;
}
@@ -356,8 +360,8 @@
}
// Check if the whole workbook (all of the sheets) is empty.
- Map<String, Boolean> isSheetEmptyMap = CapraOfficeUtils.getSheetsEmptinessInfo(workBook);
- if (!isSheetEmptyMap.values().contains(false)) {
+ Map<String, Boolean> isNewSheetEmptyMap = CapraOfficeUtils.getSheetsEmptinessInfo(workBook);
+ if (!isNewSheetEmptyMap.values().contains(false)) {
showErrorMessage(ERROR_TITLE, "There are no rows to display in any of the sheets.", null);
clearSelection();
return;
@@ -370,7 +374,7 @@
this.selectedSheetName = sheet.getSheetName();
this.selectedFile = officeFile;
this.selectedFileId = googleDriveFileId;
- this.isSheetEmptyMap = isSheetEmptyMap;
+ this.isSheetEmptyMap = isNewSheetEmptyMap;
// Populate the view with Excel rows
String idColumn = Activator.getDefault().getPreferenceStore().getString(OfficePreferences.EXCEL_COLUMN_VALUE);
@@ -381,20 +385,21 @@
CapraOfficeObject cRow;
// If the file is in the java's "temporary-file" folder, it was
// obtained from Google drive
- if (isGoogleSheet)
+ if (isGoogleSheet) {
cRow = new CapraGoogleSheetsRow(officeFile, row, idColumn, googleDriveFileId);
- else
+ } else {
cRow = new CapraExcelRow(officeFile, row, idColumn);
-
- if (!cRow.getData().isEmpty())
+ }
+ if (!cRow.getData().isEmpty()) {
selection.add(cRow);
+ }
}
}
// Save info about the type of the data displayed in the Office view.
- if (!selection.isEmpty())
+ if (!selection.isEmpty()) {
provider.setResource(selection.get(0));
-
+ }
viewer.refresh();
}
@@ -420,9 +425,9 @@
return;
}
- if (paragraphs.isEmpty())
+ if (paragraphs.isEmpty()) {
return;
-
+ }
// Clear the Office view and all static variables
clearSelection();
@@ -435,14 +440,15 @@
XWPFParagraph paragraph = paragraphs.get(i);
if (paragraph != null) {
CapraWordRequirement cRequirement = new CapraWordRequirement(officeFile, paragraph, fieldName);
- if (!cRequirement.getData().isEmpty())
+ if (!cRequirement.getData().isEmpty()) {
selection.add(cRequirement);
+ }
}
}
- if (!selection.isEmpty())
+ if (!selection.isEmpty()) {
provider.setResource(selection.get(0));
- else {
+ } else {
showErrorMessage(ERROR_TITLE, "There are no fields with the specified field name in this document.", null);
clearSelection();
return;
@@ -463,17 +469,16 @@
*/
public void showObjectDetails(Object event, Shell parentShell) {
CapraOfficeObject officeObject;
-
+ IStructuredSelection eventSelection;
+
if (event instanceof DoubleClickEvent) { // If called with double click
- IStructuredSelection selection = (IStructuredSelection) ((DoubleClickEvent) event).getSelection();
- officeObject = (CapraOfficeObject) selection.getFirstElement();
+ eventSelection = (IStructuredSelection) ((DoubleClickEvent) event).getSelection();
+ officeObject = (CapraOfficeObject) eventSelection.getFirstElement();
} else if (event instanceof ExecutionEvent) { // If called from menu
- IStructuredSelection selection;
-
try {
- selection = (IStructuredSelection) HandlerUtil.getCurrentSelectionChecked((ExecutionEvent) event);
- officeObject = (CapraOfficeObject) selection.getFirstElement();
+ eventSelection = (IStructuredSelection) HandlerUtil.getCurrentSelectionChecked((ExecutionEvent) event);
+ officeObject = (CapraOfficeObject) eventSelection.getFirstElement();
} catch (ExecutionException e) {
LOG.warn("Could not get office object.", e);
return;
@@ -534,10 +539,11 @@
* the name of the sheet to be displayed in the Office view.
*/
public void displaySheet(String sheetName) {
- if (selection.isEmpty())
+ if (selection.isEmpty()) {
return;
- else if (selection.get(0) instanceof CapraExcelRow)
+ } else if (selection.get(0) instanceof CapraExcelRow) {
parseExcelDocument(selectedFile, selectedFileId, sheetName);
+ }
}
/**
@@ -556,7 +562,7 @@
isSheetEmptyMap = CapraOfficeUtils.getSheetsEmptinessInfo(
CapraOfficeUtils.getExcelWorkbook(((CapraExcelRow) (selection.get(0))).getFile()));
} catch (OldExcelFormatException | IOException e) {
- LOG.debug("Could not open Excel file.", e);;
+ LOG.debug("Could not open Excel file.", e);
}
}
@@ -592,13 +598,14 @@
*/
public void refreshView() {
- if (selection.isEmpty())
+ if (selection.isEmpty()) {
return;
-
- if (selection.get(0) instanceof CapraExcelRow)
+ }
+ if (selection.get(0) instanceof CapraExcelRow) {
parseExcelDocument(selectedFile, selectedFileId, selectedSheetName);
- else if (selection.get(0) instanceof CapraWordRequirement)
+ } else if (selection.get(0) instanceof CapraWordRequirement) {
parseWordDocument(selectedFile);
+ }
}
/**
@@ -617,8 +624,7 @@
}
private void showErrorMessage(String caption, String message, String url) {
- new HyperlinkDialog(viewer.getControl().getShell(), caption, null, MessageDialog.ERROR,
- new String[] { "OK" }, 0, message, url).open();
+ new HyperlinkDialog(new HyperlinkDialog.HyperlinkDialogParameter(viewer.getControl().getShell(), caption, null, MessageDialog.ERROR, new String[] { "OK" }, 0), message, url).open();
}
/**
@@ -627,26 +633,33 @@
*/
static class HyperlinkDialog extends MessageDialog {
+ private static final int PREFERRED_DIALOG_WIDTH = 300;
private String hyperlinkMessage;
private String url;
+ public static class HyperlinkDialogParameter {
+ public Shell parentShell;
+ public String dialogTitle;
+ public Image dialogTitleImage;
+ public int dialogImageType;
+ public String[] dialogButtonLabels;
+ public int defaultIndex;
+
+ public HyperlinkDialogParameter(Shell parentShell, String dialogTitle, Image dialogTitleImage,
+ int dialogImageType, String[] dialogButtonLabels, int defaultIndex) {
+ this.parentShell = parentShell;
+ this.dialogTitle = dialogTitle;
+ this.dialogTitleImage = dialogTitleImage;
+ this.dialogImageType = dialogImageType;
+ this.dialogButtonLabels = dialogButtonLabels;
+ this.defaultIndex = defaultIndex;
+ }
+ }
+
/**
* A constructor that creates the dialog with the provided parameters.
* Call open() in order to display the dialog.
- *
- * @param parentShell
- * the parent shell
- * @param dialogTitle
- * the title of the dialog
- * @param dialogTitleImage
- * the dialog title image, or null if none
- * @param dialogImageType
- * one of the static values from MessageDialog class (NONE,
- * ERROR, INFORMATION, QUESTION, WARNING)
- * @param dialogButtonLabels
- * varargs of Strings for the button labels in the button bar
- * @param defaultIndex
- * the index in the button label array of the default button
+ * @param parameterObject TODO
* @param hyperlinkMessage
* a String that will be shown to the user and can contain a
* hyperlink, that will, on click, open a browser window at
@@ -654,9 +667,8 @@
* @param url
* the hyperlink to the web page, or null, if not required
*/
- public HyperlinkDialog(Shell parentShell, String dialogTitle, Image dialogTitleImage, int dialogImageType,
- String[] dialogButtonLabels, int defaultIndex, String hyperlinkMessage, String url) {
- super(parentShell, dialogTitle, dialogTitleImage, null, dialogImageType, dialogButtonLabels, defaultIndex);
+ public HyperlinkDialog(HyperlinkDialogParameter parameterObject, String hyperlinkMessage, String url) {
+ super(parameterObject.parentShell, parameterObject.dialogTitle, parameterObject.dialogTitleImage, null, parameterObject.dialogImageType, parameterObject.dialogButtonLabels, parameterObject.defaultIndex);
this.hyperlinkMessage = hyperlinkMessage;
this.url = url;
}
@@ -665,7 +677,7 @@
Link link = new Link(parent, SWT.None);
link.setText(hyperlinkMessage);
GridData gd = new GridData();
- gd.widthHint = 300;
+ gd.widthHint = PREFERRED_DIALOG_WIDTH;
link.setLayoutData(gd);
if (url != null && !url.contentEquals("")) {
link.setToolTipText(url);
@@ -675,9 +687,9 @@
Desktop.getDesktop().browse(new URI(url));
HyperlinkDialog.this.okPressed();
} catch (IOException e1) {
- e1.printStackTrace();
+ LOG.info("No default browser found.", e);
} catch (URISyntaxException e1) {
- e1.printStackTrace();
+ LOG.info("Provided malformed URI.", e);
}
}
});
diff --git a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/views/SelectSheetDynamicMenu.java b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/views/SelectSheetDynamicMenu.java
index d83275c..ceabf09 100644
--- a/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/views/SelectSheetDynamicMenu.java
+++ b/bundles/org.eclipse.capra.ui.office/src/org/eclipse/capra/ui/office/views/SelectSheetDynamicMenu.java
@@ -57,12 +57,12 @@
}
});
- if (OfficeView.getOpenedView().getSelectedSheetName().contentEquals(sheetName))
+ if (OfficeView.getOpenedView().getSelectedSheetName().contentEquals(sheetName)) {
menuItem.setSelection(true);
-
- if (!isSheetEmptyMap.get(sheetName))
+ }
+ if (!isSheetEmptyMap.get(sheetName)) {
menuItem.setText(sheetName);
- else {
+ } else {
menuItem.setText(sheetName + " (Empty)");
menuItem.setEnabled(false);
}
diff --git a/bundles/org.eclipse.capra.ui/build.properties b/bundles/org.eclipse.capra.ui/build.properties
index 5d86019..ac59cf1 100644
--- a/bundles/org.eclipse.capra.ui/build.properties
+++ b/bundles/org.eclipse.capra.ui/build.properties
@@ -15,5 +15,5 @@
.,\
plugin.xml,\
schema/,\
- OSGI-INF/l10n/bundle.properties
+ OSGI-INF/