Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 71f60d1b46e1a8695417f3f130000cfd58362b42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*******************************************************************************
 * Copyright (c) 2012 Tilera Corporation and others.
 * 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:
 *     William R. Swanson (Tilera Corporation)
 *******************************************************************************/

package org.eclipse.cdt.visualizer.examples.sourcegraph;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;

import org.eclipse.cdt.visualizer.ui.canvas.BufferedCanvas;
import org.eclipse.cdt.visualizer.ui.util.Colors;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;

// ---------------------------------------------------------------------------
// SourceGraphControl
// ---------------------------------------------------------------------------

/** Simple control that displays a graph based on a source text selection. */
public class SourceGraphControl extends BufferedCanvas
{	
	// --- constants ---
	
	/** Margin used in drawing graph and computing control height. */
	public static final int MARGIN = 10;	
	
	/** Line height used in drawing graph and computing control height. */
	public static final int LINE_HEIGHT = 20;	
	
	// --- members ---
	
	/** Text we're currently displaying. */
	protected String m_sourceText = ""; //$NON-NLS-1$
	
	/** Data structure used to hold character stats. */
	class CharStat
		implements Comparable<CharStat>
	{
		public String characters;
		public int count;
		public CharStat(String c) {
			characters = c;
			count = 0;
		}
		@Override
		public int compareTo(CharStat o) {
			int c1 = count;
			int c2 = o.count;
			int cmp = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
			// we want to sort in descending order, so negate result
			return -cmp;
		};	
	};
	
	/** List of characters we discovered and their occurrences. */
	ArrayList<CharStat> m_characters;
	
	
	// --- constructors/destructors ---
	
	/** Constructor. */
	public SourceGraphControl(Composite parent) {
		super(parent);
		m_characters = new ArrayList<CharStat>();
	}
	
	/** Dispose method. */
	@Override
	public void dispose() {
		super.dispose();
	}

	
	// --- accessors ---
	
	/** Sets source text to graph. */
	public void setSourceText(String text)
	{
		processText(text);
		SourceGraphControl.this.update();
	}

	
	// --- text processing methods ---
	
	/** Processes text into digested display form. */
	public void processText(String text)
	{
		if (text == null) text = ""; //$NON-NLS-1$
		m_sourceText = text;

		// TODO: reuse the array/hashtable and stat objects
		
		Hashtable<String, CharStat> characters = 
			new Hashtable<String, CharStat>();
		
		int len = m_sourceText.length();
		int fragment_length = 2;
		if (len >= fragment_length) {
			for (int i = 0; i<len-fragment_length+1; ++i) {
				String c = m_sourceText.substring(i,i+fragment_length);
				
				// Don't bother with fragments containing spaces
				// and non-printing chars.
				boolean skip = false;
				for (int j=0; j<c.length(); ++j)
				{
					if (c.charAt(j) <= 32 || c.charAt(j) > 127) {
						skip = true;
						break;
					}
				}
				if (skip) continue;
				
				CharStat cs = characters.get(c);
				if (cs == null) {
					cs = new CharStat(c);
					characters.put(c, cs);
				}
				++cs.count;
			}
		}
		
		m_characters.clear();
		m_characters.addAll(characters.values());
		Collections.sort(m_characters);
		
		characters.clear();
		
		Rectangle bounds = getBounds();
		int height = MARGIN * 2 + m_characters.size() * LINE_HEIGHT;
		bounds.height = height;
		setBounds(bounds);
		
	}
	
	// --- painting methods ---

	/** Invoked when canvas repaint event is raised.
	 *  Default implementation clears canvas to background color.
	 */
	@Override
	public void paintCanvas(GC gc) {
		gc.setBackground(Colors.BLACK);
		gc.setForeground(Colors.WHITE);

		clearCanvas(gc);

		int margin = MARGIN;
		int tw  = 90;
		int tw2 = 45;
		int lh = LINE_HEIGHT;
		
		int x = margin;
		int y = margin;
		
		Rectangle area = getClientArea();
		int w  = area.width - margin*2 - tw;

		int maxcount = 0;
		for (CharStat cs : m_characters)
		{
			// We're sorted in descending order, so first element
			// that we draw will always have the largest count.
			if (maxcount == 0) maxcount = cs.count;
			
			gc.drawText("[" + cs.characters + "]",  x, y); //$NON-NLS-1$ //$NON-NLS-2$
			gc.drawText("(" + cs.count + ")", x + tw2, y); //$NON-NLS-1$ //$NON-NLS-2$

			double proportion = cs.count * 1.0 / maxcount;
			int bw = (int) Math.round(proportion * w);
			Color oldb = gc.getBackground();
			if (proportion > .70)
				gc.setBackground(Colors.GREEN);
			else if (proportion > .40)
				gc.setBackground(Colors.YELLOW);
			else 
				gc.setBackground(Colors.RED);
			gc.fillRectangle(x + tw, y, bw, lh-5);
			gc.setBackground(oldb);

			y += lh;
		}
	}


	// --- update methods ---

	/**
	 * Refresh the control's content based on current data.
	 */
	public void refresh() {
		SourceGraphControl.this.update();
	}

	// --- event handlers ---

	/** Invoked when panel is resized. */
	@Override
	public void resized(Rectangle bounds) {
		refresh();
	}
}

Back to the top