Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThanh Liem PHAN2016-12-09 14:39:24 +0000
committerGerrit Code Review @ Eclipse.org2017-02-09 16:20:06 +0000
commit5d8412473b5630ff3a1b7c52ade8daf4bbe534c1 (patch)
treebfd5044bc2b4050b8a8ca1fb11dbed20d3d7dc9f /plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src
parentae489bf2993c5ebb58a5f1ca12b255f83191a376 (diff)
downloadorg.eclipse.papyrus-5d8412473b5630ff3a1b7c52ade8daf4bbe534c1.tar.gz
org.eclipse.papyrus-5d8412473b5630ff3a1b7c52ade8daf4bbe534c1.tar.xz
org.eclipse.papyrus-5d8412473b5630ff3a1b7c52ade8daf4bbe534c1.zip
Bug 499466: [Table] Table Fill action ignore number prefixed by 000.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=499466 - Handle the fill action for string value such as "001_Class", "Class_0001" and "001_Class_0001" - Format the number string correctly according to the length of its real value - Add unit tests for string with beginning number, with ending number or string surrounded by both beginning and ending number Change-Id: Ia2cd8bb3b393c921d10e6817598e7d237ce4b72e Signed-off-by: Thanh Liem PHAN <thanhliem.phan@all4tec.net>
Diffstat (limited to 'plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src')
-rw-r--r--plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/action/PapyrusFillHandleDragMode.java17
-rw-r--r--plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/command/PapyrusFillHandlePasteCommandHandler.java50
-rw-r--r--plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/utils/PapyrusFillHandleUtils.java119
3 files changed, 120 insertions, 66 deletions
diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/action/PapyrusFillHandleDragMode.java b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/action/PapyrusFillHandleDragMode.java
index 9ece24e2d81..f0fb0ce3849 100644
--- a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/action/PapyrusFillHandleDragMode.java
+++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/action/PapyrusFillHandleDragMode.java
@@ -157,7 +157,7 @@ public class PapyrusFillHandleDragMode extends FillHandleDragMode {
boolean result = false;
createDoubleSeriesMenu = false;
- String templateString = null;
+ String templateString = ""; //$NON-NLS-1$
if (null != this.clipboard && null != this.clipboard.getCopiedCells()) {
final ILayerCell[][] copiedCells = this.clipboard.getCopiedCells();
@@ -175,18 +175,19 @@ public class PapyrusFillHandleDragMode extends FillHandleDragMode {
type = cell.getDataValue().getClass();
if (String.class.isAssignableFrom(type)) {
final String stringValue = (String) cell.getDataValue();
- final String currentTemplateString = PapyrusFillHandleUtils.getTemplateString(stringValue);
- templateString = currentTemplateString;
+ final String templateStringWithoutBeginningNumber = PapyrusFillHandleUtils.getTemplateWithoutBeginningNumber(stringValue);
+ final String templateStringWithoutEndingNumber = PapyrusFillHandleUtils.getTemplateWithoutEndingNumber(stringValue);
- final String number = stringValue.replace(currentTemplateString, "");
+ final String beginningNumberString = stringValue.replace(templateStringWithoutBeginningNumber, ""); //$NON-NLS-1$
+ final String endingNumberString = stringValue.replace(templateStringWithoutEndingNumber, ""); //$NON-NLS-1$
// Calculate if the 'increment' and 'decrement' menu must be displayed
// and calculate if the double series menu must be displayed
- // (increment the beginning of the ending of a string)
- if (!number.isEmpty()) {
- final boolean isBeginningByNumber = PapyrusFillHandleUtils.isBeginningByNumber(stringValue, templateString);
- final boolean isEndingByNumber = PapyrusFillHandleUtils.isEndingBNumber(stringValue, templateString);
+ // (increment/decrement the beginning or the ending of a string)
+ if (!beginningNumberString.isEmpty() || !endingNumberString.isEmpty()) {
+ final boolean isBeginningByNumber = PapyrusFillHandleUtils.isBeginningByNumber(stringValue, beginningNumberString);
+ final boolean isEndingByNumber = PapyrusFillHandleUtils.isEndingByNumber(stringValue, endingNumberString);
createDoubleSeriesMenu = isBeginningByNumber && isEndingByNumber;
} else {
diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/command/PapyrusFillHandlePasteCommandHandler.java b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/command/PapyrusFillHandlePasteCommandHandler.java
index 23911a8f8b4..df1ad4ee74b 100644
--- a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/command/PapyrusFillHandlePasteCommandHandler.java
+++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/command/PapyrusFillHandlePasteCommandHandler.java
@@ -107,25 +107,46 @@ public class PapyrusFillHandlePasteCommandHandler extends FillHandlePasteCommand
if (value instanceof String && FillHandleOperation.SERIES.equals(command.operation)) {
final String stringValue = (String) value;
result = stringValue;
- String templateString = PapyrusFillHandleUtils.getTemplateString(stringValue);
+ String templateString = ""; //$NON-NLS-1$
+ String numberString = ""; //$NON-NLS-1$
- boolean isBeginningByNumber = PapyrusFillHandleUtils.isBeginningByNumber(stringValue, templateString);
- boolean isEndingByNumber = PapyrusFillHandleUtils.isEndingBNumber(stringValue, templateString);
+ final String templateStringWithoutBeginningNumber = PapyrusFillHandleUtils.getTemplateWithoutBeginningNumber(stringValue);
+ final String templateStringWithoutEndingNumber = PapyrusFillHandleUtils.getTemplateWithoutEndingNumber(stringValue);
+ final String beginningNumberString = stringValue.replace(templateStringWithoutBeginningNumber, ""); //$NON-NLS-1$
+ final String endingNumberString = stringValue.replace(templateStringWithoutEndingNumber, ""); //$NON-NLS-1$
+
+ boolean isBeginningByNumber = PapyrusFillHandleUtils.isBeginningByNumber(stringValue, beginningNumberString);
+ boolean isEndingByNumber = PapyrusFillHandleUtils.isEndingByNumber(stringValue, endingNumberString);
+
+ // If string value is both prefixed and suffixed by numbers
if (isBeginningByNumber && isEndingByNumber) {
+ // Redefine its value based on the prefix of the command
isBeginningByNumber = ((PapyrusFillHandlePasteCommand) command).isPrefix();
isEndingByNumber = !isBeginningByNumber;
if (isBeginningByNumber) {
templateString = PapyrusFillHandleUtils.getTemplateWithoutBeginningNumber(stringValue);
+ numberString = beginningNumberString;
} else {
templateString = PapyrusFillHandleUtils.getTemplateWithoutEndingNumber(stringValue);
+ numberString = endingNumberString;
}
}
- final String number = stringValue.replace(templateString, ""); //$NON-NLS-1$
+ // If string value is prefixed or suffixed, the template string and the number string are still empty
+ // Calculate them according to the prefix or the suffix
+ if (templateString.isEmpty() && numberString.isEmpty()) {
+ if (isBeginningByNumber) {
+ templateString = PapyrusFillHandleUtils.getTemplateWithoutBeginningNumber(stringValue);
+ numberString = beginningNumberString;
+ } else {
+ templateString = PapyrusFillHandleUtils.getTemplateWithoutEndingNumber(stringValue);
+ numberString = endingNumberString;
+ }
+ }
- if (!number.isEmpty()) {
- final int intValue = Integer.parseInt(number);
+ if (!numberString.isEmpty()) {
+ final int intValue = Integer.parseInt(numberString);
Object diff = 0;
if (MoveDirectionEnum.LEFT == command.direction || MoveDirectionEnum.RIGHT == command.direction) {
@@ -143,21 +164,24 @@ public class PapyrusFillHandlePasteCommandHandler extends FillHandlePasteCommand
newValue = intValue - ((Integer) diff);
}
+ final String numDigitsFormat = PapyrusFillHandleUtils.getZeroLeadingFormatString(numberString.length(), newValue);
+ final String newValueString = String.format(numDigitsFormat, newValue);
+
if (isBeginningByNumber) {
- result = newValue + templateString;
+ result = newValueString + templateString;
} else if (isEndingByNumber) {
- result = templateString + newValue;
+ result = templateString + newValueString;
}
}
}
}
}
boolean isEditable = EditUtils.isCellEditable(
- this.selectionLayer,
- command.configRegistry,
- new PositionCoordinate(this.selectionLayer,
- cell.getColumnIndex(),
- cell.getRowIndex()));
+ this.selectionLayer,
+ command.configRegistry,
+ new PositionCoordinate(this.selectionLayer,
+ cell.getColumnIndex(),
+ cell.getRowIndex()));
return !isEditable || null != result ? result : super.getPasteValue(cell, command, toColumn, toRow);
}
diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/utils/PapyrusFillHandleUtils.java b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/utils/PapyrusFillHandleUtils.java
index 18540580e05..03f0aebe6b2 100644
--- a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/utils/PapyrusFillHandleUtils.java
+++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/fillhandle/utils/PapyrusFillHandleUtils.java
@@ -17,106 +17,135 @@ package org.eclipse.papyrus.infra.nattable.fillhandle.utils;
* The papyrus fill handle utils.
*/
public class PapyrusFillHandleUtils {
-
+
/**
* The negative char.
*/
private static String NEGATIVE_CHAR = "-"; //$NON-NLS-1$
-
+
/**
* Get the template string from a string (without beginning and ending number).
*
- * @param initialString The initial string.
+ * @param initialString
+ * The initial string.
* @return The modified string (without beginning and ending number).
*/
- public static String getTemplateString(final String initialString){
+ public static String getTemplateString(final String initialString) {
return getTemplateWithoutEndingNumber(getTemplateWithoutBeginningNumber(initialString));
}
-
+
/**
* Get the template string from a string (without beginning number).
*
- * @param initialString The initial string.
+ * @param initialString
+ * The initial string.
* @return The modified string (without beginning number).
*/
- public static String getTemplateWithoutBeginningNumber(final String initialString){
+ public static String getTemplateWithoutBeginningNumber(final String initialString) {
String result = initialString;
-
+
// The number must be a negative number
- try{
- if(2 <= result.length() && NEGATIVE_CHAR.equals(result.substring(0, 1))){
+ try {
+ if (2 <= result.length() && NEGATIVE_CHAR.equals(result.substring(0, 1))) {
Integer.parseInt(result.substring(1, 2));
result = result.substring(2);
}
- }catch(Exception e){
+ } catch (Exception e) {
// Continue
}
-
+
// Try to remove beginning number
- try{
- while(!result.isEmpty()){
+ try {
+ while (!result.isEmpty()) {
final String firstChar = result.substring(0, 1);
Integer.parseInt(firstChar);
result = result.substring(1);
}
- }catch(Exception e){
+ } catch (Exception e) {
// Continue
}
-
+
return result;
}
-
+
/**
* Get the template string from a string (without ending number).
*
- * @param initialString The initial string.
+ * @param initialString
+ * The initial string.
* @return The modified string (without ending number).
*/
- public static String getTemplateWithoutEndingNumber(final String initialString){
+ public static String getTemplateWithoutEndingNumber(final String initialString) {
String result = initialString;
-
+
boolean hasNumber = false;
-
+
// Try to remove ending number
- try{
- while(!result.isEmpty()){
- final String lastChar = result.substring(result.length()-1);
+ try {
+ while (!result.isEmpty()) {
+ final String lastChar = result.substring(result.length() - 1);
Integer.parseInt(lastChar);
hasNumber = true;
- result = result.substring(0, result.length()-1);
+ result = result.substring(0, result.length() - 1);
}
- }catch(Exception e){
+ } catch (Exception e) {
// Continue
}
-
+
// If a number is found, check if this is not a negative number
- if(hasNumber && NEGATIVE_CHAR.equals(result.substring(result.length()-1))){
- result = result.substring(0, result.length()-1);
+ if (hasNumber && NEGATIVE_CHAR.equals(result.substring(result.length() - 1))) {
+ result = result.substring(0, result.length() - 1);
}
-
+
return result;
}
-
+
+ /**
+ * Check if a string is starting with the number string.
+ *
+ * @param initialString
+ * The initial string to check
+ * @param numberString
+ * The number string
+ * @return <code>true</code> if the initial string starts with the number string, <code>false</code> otherwise
+ */
+ public static boolean isBeginningByNumber(final String initialString, final String numberString) {
+ return !numberString.isEmpty() && initialString.startsWith(numberString);
+ }
+
/**
- * Check if a string is starting with the template string.
+ * Check if a string is ending with the number string.
*
- * @param initialString The initial string to check.
- * @param templateString The template string.
- * @return <code>true</code> if the initial string starts with template string, <code>false</code> otherwise.
+ * @param initialString
+ * The initial string to check
+ * @param numberString
+ * The number string
+ * @return <code>true</code> if the initial string ends with the number string, <code>false</code> otherwise
*/
- public static boolean isBeginningByNumber(final String initialString, final String templateString){
- return !templateString.isEmpty() && !initialString.startsWith(templateString);
+ public static boolean isEndingByNumber(final String initialString, final String numberString) {
+ return !numberString.isEmpty() && initialString.endsWith(numberString);
}
-
+
/**
- * Check if a string is ending with the template string.
+ * Create the string format for zero leading number.
+ * For instance, if the number of digits is equal to 5, the format string becomes %05d
+ * which specifies an integer with a width of 5 printing leading zeroes.
*
- * @param initialString The initial string to check.
- * @param templateString The template string.
- * @return <code>true</code> if the initial string ends with template string, <code>false</code> otherwise.
+ * @param numDigits
+ * The number of digits to display
+ * @return The created string format
*/
- public static boolean isEndingBNumber(final String initialString, final String templateString){
- return templateString.isEmpty() || !initialString.endsWith(templateString);
+ public static String getZeroLeadingFormatString(final int numDigits, final int numValue) {
+
+ // If the number value is a negative integer, increase the number of digits by 1
+ // which is used for the minus sign
+ int currentNumDigits = numValue >= 0 ? numDigits : numDigits + 1;
+
+ // Meaning of the string format
+ // %% --> %
+ // 0 --> 0
+ // %d --> <value of numDigits>
+ // d --> d
+ return String.format("%%0%dd", currentNumDigits);
}
-
}

Back to the top