|
1 /* |
|
2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * SourceEditorUpdater instance to add a new trace location |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracebuilder.engine.source; |
|
20 |
|
21 import com.nokia.tracebuilder.engine.TraceLocationProperties; |
|
22 import com.nokia.tracebuilder.model.Trace; |
|
23 import com.nokia.tracebuilder.source.SourceContext; |
|
24 import com.nokia.tracebuilder.source.SourceLocationBase; |
|
25 import com.nokia.tracebuilder.source.SourceParser; |
|
26 import com.nokia.tracebuilder.source.SourceParserException; |
|
27 |
|
28 /** |
|
29 * SourceEditorUpdater instance to add a new trace location |
|
30 * |
|
31 */ |
|
32 class TraceLocationCreator extends SourceEditorUpdater { |
|
33 |
|
34 /** |
|
35 * Trace to be added to source |
|
36 */ |
|
37 private Trace trace; |
|
38 |
|
39 /** |
|
40 * Offset where trace is added |
|
41 */ |
|
42 private SourceLocationBase location; |
|
43 |
|
44 /** |
|
45 * Trace location properties |
|
46 */ |
|
47 private TraceLocationProperties locationProperties; |
|
48 |
|
49 /** |
|
50 * Constructor |
|
51 * |
|
52 * @param properties |
|
53 * the source to be updated |
|
54 * @param trace |
|
55 * the trace to be added to source |
|
56 * @param offset |
|
57 * the offset where trace is added |
|
58 * @param locationProperties |
|
59 * optional properties for the new location |
|
60 * @throws SourceParserException |
|
61 * if source parser fails |
|
62 */ |
|
63 TraceLocationCreator(SourceProperties properties, Trace trace, int offset, |
|
64 TraceLocationProperties locationProperties) |
|
65 throws SourceParserException { |
|
66 super(properties); |
|
67 this.trace = trace; |
|
68 this.locationProperties = locationProperties; |
|
69 // The location properties may adjust the location where the trace gets |
|
70 // inserted. If the location properties do not contain a location rule, |
|
71 // the trace is queried for it |
|
72 SourceLocationRule locationRule = null; |
|
73 if (locationProperties != null) { |
|
74 locationRule = locationProperties.getLocationRule(); |
|
75 } |
|
76 if (locationRule == null) { |
|
77 locationRule = trace.getExtension(SourceLocationRule.class); |
|
78 // The extension is removed from the trace to avoid relocation |
|
79 // problems when inserting the trace to other places |
|
80 if (locationRule != null && locationRule.isRemovedAfterInsert()) { |
|
81 trace.removeExtension(locationRule); |
|
82 } |
|
83 } |
|
84 offset = processLocationRule(locationRule, offset); |
|
85 // Offset is stored into the document |
|
86 // This is needed since the update is run asynchronously and the |
|
87 // document may change before this update has chance to run |
|
88 location = properties.getSourceEditor().createHiddenLocation(offset, 0); |
|
89 } |
|
90 |
|
91 /* |
|
92 * (non-Javadoc) |
|
93 * |
|
94 * @see com.nokia.tracebuilder.engine.source.SourceEditorUpdater#runUpdate() |
|
95 */ |
|
96 @Override |
|
97 protected boolean runUpdate() throws SourceParserException { |
|
98 SourceProperties source = getSource(); |
|
99 try { |
|
100 source.internalInsertTrace(trace, location.getOffset(), null, |
|
101 locationProperties); |
|
102 } finally { |
|
103 // The location is removed after use |
|
104 source.getSourceEditor().removeHiddenLocation(location); |
|
105 } |
|
106 // Always makes an update |
|
107 return true; |
|
108 } |
|
109 |
|
110 /* |
|
111 * (non-Javadoc) |
|
112 * |
|
113 * @see com.nokia.tracebuilder.engine.source.SourceEditorUpdater#getPosition() |
|
114 */ |
|
115 @Override |
|
116 protected SourceLocationBase getPosition() { |
|
117 return location; |
|
118 } |
|
119 |
|
120 /** |
|
121 * Processes the location rule of the trace and adjusts the |
|
122 * traceInsertLocation if the rule exists |
|
123 * |
|
124 * @param locationRule |
|
125 * the location rule of the trace |
|
126 * @param offset |
|
127 * the original offset |
|
128 * @return the new offset or -1 if not adjusted |
|
129 * @throws SourceParserException |
|
130 * if source parser fails |
|
131 */ |
|
132 private int processLocationRule(SourceLocationRule locationRule, int offset) |
|
133 throws SourceParserException { |
|
134 SourceParser sourceEditor = getSource().getSourceEditor(); |
|
135 if (locationRule != null) { |
|
136 int start = 0; |
|
137 int end = sourceEditor.getDataLength(); |
|
138 SourceContext context; |
|
139 switch (locationRule.getLocationType()) { |
|
140 case SourceLocationRule.ABSOLUTE: |
|
141 offset = locationRule.getLocationOffset(); |
|
142 break; |
|
143 case SourceLocationRule.CONTEXT_RELATIVE: |
|
144 context = sourceEditor.getContext(offset); |
|
145 if (context != null) { |
|
146 offset = locationRule.getLocationOffset() |
|
147 + context.getOffset(); |
|
148 // If location traceInsertLocation is >0, the trace is exit |
|
149 // trace and |
|
150 // this needs to search for return statements |
|
151 if (locationRule.getLocationOffset() > 0) { |
|
152 end = sourceEditor.findReturn(context); |
|
153 } else { |
|
154 end = context.getOffset() + context.getLength(); |
|
155 } |
|
156 start = context.getOffset(); |
|
157 } |
|
158 break; |
|
159 } |
|
160 if (offset > end) { |
|
161 offset = end; |
|
162 } else if (offset < start) { |
|
163 offset = start; |
|
164 } |
|
165 } |
|
166 // Location is inserted to the beginning of the line |
|
167 // Indent is calculated when the location is inserted |
|
168 return sourceEditor.findStartOfLine(offset, false, true); |
|
169 } |
|
170 |
|
171 } |