KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > memory > provisional > AbstractAsyncTextRendering


1 /*******************************************************************************
2  * Copyright (c) 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.internal.ui.memory.provisional;
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.2
28  */

29 abstract public class AbstractAsyncTextRendering extends AbstractAsyncTableRendering {
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 AbstractAsyncTextRendering(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 AbstractAsyncTextRendering(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
84     /* (non-Javadoc)
85      * @see org.eclipse.debug.internal.ui.views.memory.provisional.AbstractAsyncTableRendering#getString(java.lang.String, java.math.BigInteger, org.eclipse.debug.core.model.MemoryByte[])
86      */

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

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

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