Debian Codegraph @

rule-api/src/main/java/com/android/ide/common/api/IClientRulesEngine.java

raw
1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.eclipse.org/org/documents/epl-v10.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 package com.android.ide.common.api;
19
20 import com.android.annotations.NonNull;
21 import com.android.annotations.Nullable;
22 import com.google.common.annotations.Beta;
23
24 import java.util.Collection;
25 import java.util.Map;
26
27 /**
28 * A Client Rules Engine is a set of methods that {@link IViewRule}s can use to
29 * access the client public API of the Rules Engine.
30 * <p>
31 * <b>NOTE: This is not a public or final API; if you rely on this be prepared
32 * to adjust your code for the next tools release.</b>
33 * </p>
34 */
35 @Beta
36 public interface IClientRulesEngine {
37
38 /**
39 * Returns the FQCN for which the rule was loaded.
40 *
41 * @return the fully qualified name of the rule
42 */
43 @NonNull
44 String getFqcn();
45
46 /**
47 * Returns the most recently rendered View object for this node, if any.
48 *
49 * @param node the node to look up the view object for
50 * @return the corresponding view object, or null
51 */
52 @Nullable
53 Object getViewObject(@NonNull INode node);
54
55 /**
56 * Prints a debug line in the Eclipse console using the ADT formatter.
57 *
58 * @param msg A String format message.
59 * @param params Optional parameters for the message.
60 */
61 void debugPrintf(@NonNull String msg, Object...params);
62
63 /**
64 * Loads and returns an {@link IViewRule} for the given FQCN.
65 *
66 * @param fqcn A non-null, non-empty FQCN for the rule to load.
67 * @return The rule that best matches the given FQCN according to the
68 * inheritance chain. Rules are cached and requesting the same FQCN twice
69 * is fast and will return the same rule instance.
70 */
71 @Nullable
72 IViewRule loadRule(@NonNull String fqcn);
73
74 /**
75 * Returns the metadata associated with the given fully qualified class name. Note that
76 * this will always return an {@link IViewMetadata} instance, even when the class name
77 * is unknown to the layout editor, such as for custom views. In that case, some
78 * heuristics will be applied to return metadata information such as guesses for
79 * what the most common attribute is, and so on.
80 *
81 * @param fqcn a fully qualified class name for an Android view class
82 * @return the metadata associated with the given fully qualified class name.
83 */
84 @NonNull
85 IViewMetadata getMetadata(@NonNull String fqcn);
86
87 /**
88 * Displays the given message string in an alert dialog with an "OK" button.
89 *
90 * @param message the message to be shown
91 */
92 void displayAlert(@NonNull String message);
93
94 /**
95 * Displays a simple input alert dialog with an OK and Cancel buttons.
96 *
97 * @param message The message to display in the alert dialog.
98 * @param value The initial value to display in the input field. Can be null.
99 * @param filter An optional filter to validate the input. Specify null (or
100 * a validator which always returns true) if you do not want
101 * input validation.
102 * @return Null if canceled by the user. Otherwise the possibly-empty input string.
103 */
104 @Nullable
105 String displayInput(@NonNull String message, @Nullable String value,
106 @Nullable IValidator filter);
107
108 /**
109 * Renames the given node
110 *
111 * @param node the node to be renamed
112 * @return true if renaming was handled
113 */
114 boolean rename(INode node);
115
116 /**
117 * Returns the minimum API level that the current Android project is targeting.
118 *
119 * @return the minimum API level to be supported, or -1 if it cannot be determined
120 */
121 int getMinApiLevel();
122
123 /**
124 * Returns a resource name validator for the current project
125 *
126 * @param resourceTypeName resource type, such as "id", "string", and so on
127 * @param uniqueInProject if true, the resource name must be unique in the
128 * project (not already be defined anywhere else)
129 * @param uniqueInLayout if true, the resource name must be unique at least
130 * within the current layout. This only applies to {@code @id}
131 * resources since only those resources can be defined in-place
132 * within a layout
133 * @param exists if true, the resource name must already exist
134 * @param allowed allowed names (optional). This can for example be used to
135 * request a unique-in-layout validator, but to remove the
136 * current value of the node being edited from consideration such
137 * that it allows you to leave the value the same
138 * @return an {@link IValidator} for validating a new resource name in the
139 * current project
140 */
141 @Nullable
142 IValidator getResourceValidator(@NonNull String resourceTypeName,
143 boolean uniqueInProject, boolean uniqueInLayout, boolean exists,
144 String... allowed);
145
146 /**
147 * Displays an input dialog where the user can enter an Android reference value
148 *
149 * @param currentValue the current reference to select
150 * @return the reference selected by the user, or null
151 */
152 @Nullable
153 String displayReferenceInput(@Nullable String currentValue);
154
155 /**
156 * Displays an input dialog where the user can enter an Android resource name of the
157 * given resource type ("id", "string", "drawable", and so on.)
158 *
159 * @param currentValue the current reference to select
160 * @param resourceTypeName resource type, such as "id", "string", and so on (never
161 * null)
162 * @return the margins selected by the user in the same order as the input arguments,
163 * or null
164 */
165 @Nullable
166 String displayResourceInput(@NonNull String resourceTypeName, @Nullable String currentValue);
167
168 /**
169 * Displays an input dialog tailored for editing margin properties.
170 *
171 * @param all The current, initial value display for "all" margins (applied to all
172 * sides)
173 * @param left The current, initial value to display for the "left" margin
174 * @param right The current, initial value to display for the "right" margin
175 * @param top The current, initial value to display for the "top" margin
176 * @param bottom The current, initial value to display for the "bottom" margin
177 * @return an array of length 5 containing the user entered values for the all, left,
178 * right, top and bottom margins respectively, or null if canceled
179 */
180 @Nullable
181 String[] displayMarginInput(
182 @Nullable String all,
183 @Nullable String left,
184 @Nullable String right,
185 @Nullable String top,
186 @Nullable String bottom);
187
188 /**
189 * Displays an input dialog tailored for inputting the source of an {@code <include>}
190 * layout tag. This is similar to {@link #displayResourceInput} for resource type
191 * "layout", but should also attempt to filter out layout resources that cannot be
192 * included from the current context (because it would result in a cyclic dependency).
193 *
194 * @return the layout resource to include, or null if canceled
195 */
196 @Nullable
197 String displayIncludeSourceInput();
198
199 /**
200 * Displays an input dialog tailored for inputting the source of a {@code <fragment>}
201 * layout tag.
202 *
203 * @return the fully qualified class name of the fragment activity, or null if canceled
204 */
205 @Nullable
206 String displayFragmentSourceInput();
207
208 /**
209 * Displays an input dialog tailored for inputting the source of a {@code <view>}
210 * layout tag.
211 *
212 * @return the fully qualified class name of the custom view class, or null if canceled
213 */
214 @Nullable
215 String displayCustomViewClassInput();
216
217 /**
218 * Select the given nodes
219 *
220 * @param nodes the nodes to be selected, never null
221 */
222 void select(@NonNull Collection<INode> nodes);
223
224 /**
225 * Triggers a redraw
226 */
227 void redraw();
228
229 /**
230 * Triggers a layout refresh and redraw
231 */
232 void layout();
233
234 /**
235 * Converts a pixel to a dp (device independent pixel) for the current screen density
236 *
237 * @param px the pixel dimension
238 * @return the corresponding dp dimension
239 */
240 int pxToDp(int px);
241
242 /**
243 * Converts a device independent pixel to a screen pixel for the current screen density
244 *
245 * @param dp the device independent pixel dimension
246 * @return the corresponding pixel dimension
247 */
248 int dpToPx(int dp);
249
250 /**
251 * Converts an IDE screen pixel distance to the corresponding layout distance. This
252 * can be used to draw annotations on the graphics object that should be unaffected by
253 * the zoom, or handle mouse events within a certain pixel distance regardless of the
254 * screen zoom.
255 *
256 * @param pixels the size in IDE screen pixels
257 * @return the corresponding pixel distance in the layout coordinate system
258 */
259 int screenToLayout(int pixels);
260
261 /**
262 * Measure the preferred or actual ("wrap_content") size of the given nodes.
263 *
264 * @param parent the parent whose children should be measured
265 * @param filter a filter to change attributes in the process of measuring, for
266 * example forcing the layout_width to wrap_content or the layout_weight to
267 * unset
268 * @return the corresponding bounds of the nodes, or null if a rendering error occurs
269 */
270 @Nullable
271 Map<INode, Rect> measureChildren(@NonNull INode parent, @Nullable AttributeFilter filter);
272
273 /**
274 * The {@link AttributeFilter} allows a client of
275 * {@link IClientRulesEngine#measureChildren} to modify the actual XML values of the
276 * nodes being rendered, for example to force width and height values to wrap_content
277 * when measuring preferred size.
278 */
279 interface AttributeFilter {
280 /**
281 * Returns the attribute value for the given node and attribute name. This filter
282 * allows a client to adjust the attribute values that a node presents to the
283 * layout library.
284 * <p>
285 * Returns "" to unset an attribute. Returns null to return the unfiltered value.
286 *
287 * @param node the node for which the attribute value should be returned
288 * @param namespace the attribute namespace
289 * @param localName the attribute local name
290 * @return an override value, or null to return the unfiltered value
291 */
292 @Nullable
293 String getAttribute(
294 @NonNull INode node,
295 @Nullable String namespace,
296 @NonNull String localName);
297 }
298
299 /**
300 * Given a UI root node and a potential XML node name, returns the first available id
301 * that matches the pattern "prefix%d".
302 * <p>
303 * TabWidget is a special case and the method will always return "@android:id/tabs".
304 *
305 * @param fqcn The fully qualified class name of the view to generate a unique id for
306 * @return A suitable generated id in the attribute form needed by the XML id tag
307 * (e.g. "@+id/something")
308 */
309 @NonNull
310 String getUniqueId(@NonNull String fqcn);
311
312 /**
313 * Returns the namespace URI for attributes declared and used inside the
314 * app. (This is not the Android namespace.)
315 *
316 * @return the namespace URI
317 */
318 @NonNull
319 String getAppNameSpace();
320 }
321