Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvlorenzo2013-02-05 13:48:30 +0000
committervlorenzo2013-02-05 13:48:30 +0000
commitc9cd811e5cf708e8c3397ff2cb46e26fcf78ff38 (patch)
tree9f1a66f920b6bae51c5b1a2ba46004f606f05a4b
parent5ebcfe36a2093a9c9322d47225d6f5e0212305b1 (diff)
downloadorg.eclipse.papyrus-c9cd811e5cf708e8c3397ff2cb46e26fcf78ff38.tar.gz
org.eclipse.papyrus-c9cd811e5cf708e8c3397ff2cb46e26fcf78ff38.tar.xz
org.eclipse.papyrus-c9cd811e5cf708e8c3397ff2cb46e26fcf78ff38.zip
399838: [Enhancement] [Table 2] Papyrus shall improve the Tabular Editor
https://bugs.eclipse.org/bugs/show_bug.cgi?id=399838
-rw-r--r--plugins/infra/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/util/IntegerAndSpreadsheetNumberConverter.java90
1 files changed, 90 insertions, 0 deletions
diff --git a/plugins/infra/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/util/IntegerAndSpreadsheetNumberConverter.java b/plugins/infra/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/util/IntegerAndSpreadsheetNumberConverter.java
new file mode 100644
index 00000000000..08d4e17cb3b
--- /dev/null
+++ b/plugins/infra/org.eclipse.papyrus.infra.tools/src/org/eclipse/papyrus/infra/tools/util/IntegerAndSpreadsheetNumberConverter.java
@@ -0,0 +1,90 @@
+/*****************************************************************************
+ * Copyright (c) 2012 CEA LIST.
+ *
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.tools.util;
+
+/**
+ *
+ * This class allows to converter an int into a String like a spreadsheet numerotation and vice-versa. Fox example :
+ * <ul>
+ * <li>1 <-> A</li>
+ * <li>26 <-> Z</li>
+ * <li>27 <-> AA</li>
+ * <li>28<-> AB</li>
+ * </ul>
+ *
+ *
+ *
+ *
+ */
+//adapted code from http://www.developpez.net/forums/d1197058/dotnet/general-dotnet/contribuez/extensions-types-int-string-conversion-format-colonne-excel/
+public class IntegerAndSpreadsheetNumberConverter {
+
+ /**
+ *
+ * Constructor.
+ *
+ */
+ private IntegerAndSpreadsheetNumberConverter() {
+ //to prevent instanciation
+ }
+
+ /**
+ *
+ * @param number
+ * an integer
+ * @return
+ * the string representing this integer in a spreedsheet
+ */
+ public static String toString(int number) {
+ if(number <= 0) {
+ throw new NumberFormatException();
+ }
+ int tmp = number;
+ String string = ""; //$NON-NLS-1$
+ while(tmp > 0) {
+ final int r = (tmp - 1) % 26;
+ string = (char)('A' + r) + string;
+ tmp = (tmp - r) / 26;
+ }
+
+ return string;
+ }
+
+ /**
+ *
+ * @unused
+ * @param string
+ * a string
+ * @return
+ * the number corresponding to the string
+ */
+ public static int toInt(String string) {
+ if(string == null || string.length() == 0) {
+ throw new NumberFormatException();
+ }
+ string = string.toUpperCase();
+ int multiplier = 1;
+ int columnNumber = 0;
+ for(int i = string.length() - 1; i >= 0; i--) {
+ final char c = string.charAt(i);
+ if(c < 'A' || c > 'Z') {
+ throw new NumberFormatException();
+ }
+ final int value = (c - 'A' + 1) * multiplier;
+ columnNumber += value;
+ multiplier *= 26;
+ }
+ return columnNumber;
+ }
+}

Back to the top