992ce023b151e9eb2861fbd7b393d7b91769688d
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / corext / refactoring / nls / NLSElement.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.corext.refactoring.nls;
12
13 import org.eclipse.jface.text.Region;
14 import org.eclipse.jface.util.Assert;
15
16 public class NLSElement {
17
18         public static final String TAG_PREFIX = "//$NON-NLS-"; //$NON-NLS-1$
19
20         public static final int TAG_PREFIX_LENGTH = TAG_PREFIX.length();
21
22         public static final String TAG_POSTFIX = "$"; //$NON-NLS-1$
23
24         public static final int TAG_POSTFIX_LENGTH = TAG_POSTFIX.length();
25
26         /** The original string denoted by the position */
27         private String fValue;
28
29         /** The position of the original string */
30         private Region fPosition;
31
32         /** Position of the // $NON_NLS_*$ tag */
33         private Region fTagPosition;
34
35         /** Index of the Element in an NLSLine */
36         private int fIndex;
37
38         /**
39          * Creates a new NLS element for the given string and position.
40          */
41         public NLSElement(String value, int start, int length, int index) {
42                 fValue = value;
43                 fIndex = index;
44                 Assert.isNotNull(fValue);
45                 fPosition = new Region(start, length);
46         }
47
48         /**
49          * Returns the position of the string to be NLSed.
50          * 
51          * @return Returns the position of the string to be NLSed
52          */
53         public Region getPosition() {
54                 return fPosition;
55         }
56
57         /**
58          * Returns the actual string value.
59          * 
60          * @return the actual string value
61          */
62         public String getValue() {
63                 return fValue;
64         }
65
66         /**
67          * Sets the actual string value.
68          */
69         public void setValue(String value) {
70                 fValue = value;
71         }
72
73         /**
74          * Sets the tag position if one is associated with the NLS element.
75          */
76         public void setTagPosition(int start, int length) {
77                 fTagPosition = new Region(start, length);
78         }
79
80         /**
81          * Returns the tag position for this element. The method can return
82          * <code>null</code>. In this case no tag has been found for this NLS
83          * element.
84          */
85         public Region getTagPosition() {
86                 return fTagPosition;
87         }
88
89         /**
90          * Returns <code>true</code> if the NLS element has an assicated
91          * $NON-NLS-*$ tag. Otherwise <code>false</code> is returned.
92          */
93         public boolean hasTag() {
94                 return fTagPosition != null && fTagPosition.getLength() > 0;
95         }
96
97         public static String createTagText(int index) {
98                 return TAG_PREFIX + index + TAG_POSTFIX;
99         }
100
101         public String getTagText() {
102                 return TAG_PREFIX + (fIndex + 1) + TAG_POSTFIX;
103         }
104
105         /*
106          * (Non-Javadoc) Method declared in Object. only for debugging
107          */
108         public String toString() {
109                 return fPosition + ": " + fValue + "    Tag position: " + //$NON-NLS-2$ //$NON-NLS-1$
110                                 (hasTag() ? fTagPosition.toString() : "no tag found"); //$NON-NLS-1$
111         }
112 }