KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > ui > memory > AbstractTextRendering


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.ui.memory;
12
13 import java.io.UnsupportedEncodingException JavaDoc;
14 import java.math.BigInteger JavaDoc;
15
16 import org.eclipse.debug.core.model.MemoryByte;
17 import org.eclipse.debug.internal.ui.DebugUIPlugin;
18 import org.eclipse.debug.ui.IDebugUIConstants;
19
20 /**
21  * Abstract implementation of a rendering that translates memory into
22  * text, displayed in a table.
23  * <p>
24  * Clients should subclass from this class if they wish to provide a table
25  * text rendering with a specific code page.
26  * </p>
27  * @since 3.1
28  */

29 abstract public class AbstractTextRendering extends AbstractTableRendering {
30     
31     private String JavaDoc fCodePage;
32
33     /**
34      * Constructs a text rendering of the specified type.
35      *
36      * @param renderingId memory rendering type identifier
37      */

38     public AbstractTextRendering(String JavaDoc renderingId)
39     {
40         super(renderingId);
41     }
42     
43     /**
44      * Constructs a text rendering of the specified type on the given
45      * code page.
46      *
47      * @param renderingId memory rendering type identifier
48      * @param codePage the name of a supported
49      * {@link java.nio.charset.Charset </code>charset<code>}, for
50      * example <code>CP1252</code>
51      */

52     public AbstractTextRendering(String JavaDoc renderingId, String JavaDoc codePage)
53     {
54         super(renderingId);
55         fCodePage = codePage;
56     }
57     
58     /**
59      * Sets the code page for this rendering. This does not cause
60      * the rendering to be updated with the new code page. Clients need
61      * to update the rendering manually when the code page is changed.
62      *
63      * @param codePage the name of a supported
64      * {@link java.nio.charset.Charset </code>charset<code>}, for
65      * example <code>CP1252</code>
66      */

67     public void setCodePage(String JavaDoc codePage)
68     {
69         fCodePage = codePage;
70     }
71     
72     /**
73      * Returns the current code page used by this rendering. Returns null
74      * if not set.
75      * @return Returns the current code page used by this rendering. Returns null
76      * if not set.
77      */

78     public String JavaDoc getCodePage()
79     {
80         return fCodePage;
81     }
82     
83     /* (non-Javadoc)
84      * @see org.eclipse.debug.ui.memory.AbstractTableRendering#getString(java.lang.String, java.math.BigInteger, org.eclipse.debug.core.model.MemoryByte[])
85      */

86     public String JavaDoc getString(String JavaDoc dataType, BigInteger JavaDoc address, MemoryByte[] data) {
87         try {
88             String JavaDoc paddedStr = DebugUIPlugin.getDefault().getPreferenceStore().getString(IDebugUIConstants.PREF_PADDED_STR);
89             if(fCodePage == null)
90                 return ""; //$NON-NLS-1$
91

92             boolean[] invalid = new boolean[data.length];
93             boolean hasInvalid = false;
94             byte byteArray[] = new byte[data.length];
95             for (int i=0; i<data.length; i++)
96             {
97                 if (!data[i].isReadable())
98                 {
99                     invalid[i] = true;
100                     hasInvalid = true;
101                 }
102                 byteArray[i] = data[i].getValue();
103             }
104             
105             if (hasInvalid)
106             {
107                 StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
108                 for (int i=0; i<data.length; i++)
109                 {
110                     if (invalid[i])
111                         strBuf.append(paddedStr);
112                     else
113                         strBuf.append(new String JavaDoc(new byte[]{byteArray[i]}, fCodePage));
114                 }
115                 return strBuf.toString();
116             }
117
118             return new String JavaDoc(byteArray, fCodePage);
119
120         } catch (UnsupportedEncodingException JavaDoc e) {
121             return "-- error --"; //$NON-NLS-1$
122
}
123     }
124     
125
126     /* (non-Javadoc)
127      * @see org.eclipse.debug.ui.memory.AbstractTableRendering#getBytes(java.lang.String, java.math.BigInteger, org.eclipse.debug.core.model.MemoryByte[], java.lang.String)
128      */

129     public byte[] getBytes(String JavaDoc dataType, BigInteger JavaDoc address, MemoryByte[] currentValues, String JavaDoc data) {
130         try {
131             
132             if (fCodePage == null)
133                 return new byte[0];
134             
135             byte[] bytes = data.getBytes(fCodePage);
136             return bytes;
137             
138         } catch (UnsupportedEncodingException JavaDoc e) {
139             return new byte[0];
140         }
141     }
142 }
143
Popular Tags