001: package com.xoetrope.editor.netbeans.visualizer;
002:
003: import com.xoetrope.carousel.visualizer.VisualiserDebuggerModel;
004: import net.xoetrope.xui.data.XModel;
005: import net.xoetrope.xui.helper.NumberFormatter;
006:
007: import org.netbeans.api.debugger.jpda.JPDADebugger;
008: import org.netbeans.api.debugger.jpda.Variable;
009: import org.netbeans.api.debugger.jpda.ObjectVariable;
010:
011: // See http://java.sun.com/docs/books/tutorial/native1.1/implementing/method.html
012: // for details of the method signatures
013:
014: /**
015: * A wrapper for the debugger node.
016: *
017: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
018: * the GNU Public License (GPL), please see license.txt for more details. If
019: * you make commercial use of this software you must purchase a commercial
020: * license from Xoetrope.</p>
021: * <p> $Revision: 1.9 $</p>
022: */
023: public class NbDebuggerModel extends VisualiserDebuggerModel {
024: private JPDADebugger debugger;
025: private ObjectVariable modelNode;
026:
027: private static Variable[] zeroArgsArray = new Variable[0];
028: private static Variable[] oneArgsArray = new Variable[1];
029: private static Variable[] twoArgsArray = new Variable[2];
030: private static Variable[] threeArgsArray = new Variable[3];
031:
032: /** Creates a new instance of JPDAModelNode */
033: public NbDebuggerModel(JPDADebugger jpdaDebugger,
034: ObjectVariable objVar) {
035: debugger = jpdaDebugger;
036: modelNode = objVar;
037: }
038:
039: /**
040: * Calls the <code>getToStringValue()</code> method on the encapsulated object
041: * and returns its value. This method is used to determine
042: * class which instance is wrapped by <code>JPDAModelNode</code>
043: * object.
044: * @return String containing name of the wrapped class.
045: */
046: public String getNodeString() {
047: String val = null;
048: try {
049: val = modelNode.getToStringValue();
050: } catch (Exception e) {
051: e.printStackTrace();
052: }
053:
054: return val;
055: }
056:
057: public void setObjectVariable(XModel node) {
058: try {
059: modelNode = (ObjectVariable) debugger.evaluate("\"" + node
060: + "\"");
061: } catch (Exception e) {
062: e.printStackTrace();
063: }
064: }
065:
066: /**
067: * Reset the whole model, giving a new root node and a new hierarchy
068: * 'deprecated since 1.0.3
069: * @return The base model
070: */
071: public XModel reset() {
072: throw new UnsupportedOperationException();
073: }
074:
075: public void setTagName(String name) {
076: try {
077: oneArgsArray[0] = debugger.evaluate("\"" + name + "\"");
078: modelNode.invokeMethod("setTagName",
079: "(Ljava/lang/String;)V", oneArgsArray);
080: } catch (Exception e) {
081: e.printStackTrace();
082: }
083:
084: }
085:
086: /**
087: * Gets the model element tag name, e.g. 'Component' from the XML fragment
088: * <Component ....
089: * @return the model name
090: */
091: public String getTagName() {
092: try {
093: Variable var2 = modelNode.invokeMethod("getTagName",
094: "()Ljava/lang/String;", zeroArgsArray);
095: if (var2 != null)
096: return stripQuotes(var2.getValue());
097: } catch (Exception e) {
098: e.printStackTrace();
099: }
100:
101: return null;
102: }
103:
104: /**
105: * Determine if there is no id set for this model in the datasource
106: * @return true if there was no name for the element in the DataSource, an
107: * example of this is the annonymouse nodes used to represent the record of
108: * a table e.g. <tr><td>...</td><td>...</td></tr>
109: */
110: public boolean hasAutoId() {
111: try {
112: Variable var = modelNode.invokeMethod("hasAutoId", "()Z;",
113: zeroArgsArray);
114: return "true".equals(var.getValue());
115: } catch (Exception e) {
116: e.printStackTrace();
117: }
118: return false;
119: }
120:
121: /**
122: * Gets the value of the ID attribute
123: * @return the ID attribute
124: */
125: public String getId() {
126: try {
127: Variable var = modelNode.invokeMethod("getId",
128: "()Ljava/lang/String;", zeroArgsArray);
129: return stripQuotes(var.getValue());
130: } catch (Exception e) {
131: e.printStackTrace();
132: }
133:
134: return null;
135: }
136:
137: /**
138: * Sets the ID attribute
139: * @param newId the new name
140: */
141: public void setId(String newId) {
142: try {
143: oneArgsArray[0] = debugger.evaluate("\"" + newId + "\"");
144: Variable var = modelNode.invokeMethod("setId",
145: "(Ljava/lang/String;)V", oneArgsArray);
146: } catch (Exception e) {
147: e.printStackTrace();
148: }
149: }
150:
151: /**
152: * Get the value of the element located at the path in the element parameter
153: * If the attribName parameter is not null we get the value of the
154: * attributeValues
155: * @param element The path to the XModel we require
156: * @return The value of the XModel or the attribute
157: */
158: public Object get(String element) {
159: try {
160: oneArgsArray[0] = debugger.evaluate("\"" + element + "\"");
161: // Use the JNI method signature
162: Variable var2 = modelNode.invokeMethod("get",
163: "(Ljava/lang/String;)Ljava/lang/Object;",
164: oneArgsArray);
165: return new NbDebuggerModel(debugger, (ObjectVariable) var2);
166: } catch (Exception e) {
167: e.printStackTrace();
168: }
169: return null;
170: }
171:
172: /**
173: * Set the named attribute value of this XModel node. If the attribName is
174: * null then this node's value is updated.
175: * @param elementName The path to the XModel in the format 'base/foo
176: * @param newObject The new value of the XModel
177: */
178: public void set(String elementName, Object newObject) {
179: try {
180: twoArgsArray[0] = debugger.evaluate("\"" + elementName
181: + "\"");
182: twoArgsArray[1] = debugger.evaluate("\""
183: + newObject.toString() + "\"");
184: modelNode.invokeMethod("set",
185: "(Ljava/lang/String;Ljava/lang/Object;)V",
186: twoArgsArray);
187: } catch (Exception e) {
188: e.printStackTrace();
189: }
190: }
191:
192: /**
193: * Returns the index of the attribiteNames array whose value is the same
194: * as the attribName
195: * @param attribName The name of the attribute we are trying to locate
196: * @return The index of the attributeNames array containg the name
197: */
198: public int getAttribute(String attribName) {
199: try {
200: oneArgsArray[0] = debugger.evaluate("\"" + attribName
201: + "\"");
202: Variable var = modelNode.invokeMethod("getAttribute",
203: "(Ljava/lang/String;)I", oneArgsArray);
204: return Integer.parseInt(stripQuotes(var.getValue()));
205: } catch (Exception e) {
206: e.printStackTrace();
207: }
208:
209: return 0;
210: }
211:
212: /**
213: * Sets the attribute value
214: * @param i The index of the attributeValues array whose value we want
215: * @param value the value object
216: */
217: public void setAttribValue(int i, Object value) {
218: try {
219: twoArgsArray[0] = debugger.evaluate(String.valueOf(i));
220: twoArgsArray[1] = debugger.evaluate("\"" + value.toString()
221: + "\"");
222: modelNode.invokeMethod("setAttribValue",
223: "(ILjava/lang/Object;)V", twoArgsArray);
224: } catch (Exception e) {
225: e.printStackTrace();
226: }
227: }
228:
229: /**
230: * Sets the attribute name and value
231: * @param i The index of the attributeValues array whose value we want
232: * @param attribName the name of the attribute
233: * @param value the value object
234: */
235: public void setAttribValue(int i, String attribName, Object value) {
236: try {
237: threeArgsArray[0] = debugger.evaluate(String.valueOf(i));
238: threeArgsArray[1] = debugger.evaluate("\"" + attribName
239: + "\"");
240: threeArgsArray[2] = debugger.evaluate("\"" + value + "\"");
241: modelNode.invokeMethod("setAttribValue",
242: "(ILjava/lang/String;Ljava/lang/Object;)V",
243: threeArgsArray);
244: } catch (Exception e) {
245: e.printStackTrace();
246: }
247: }
248:
249: /**
250: * Get the XModel at element i
251: * @param i The index of the values array
252: * @return The XModel at location i
253: */
254: public XModel get(int i) {
255: try {
256: oneArgsArray[0] = debugger.evaluate("" + i);
257: // Use the JNI method signature
258: Variable var2 = modelNode.invokeMethod("get",
259: "(I)Lnet/xoetrope/xui/data/XModel;", oneArgsArray);
260: return new NbDebuggerModel(debugger, (ObjectVariable) var2);
261: } catch (Exception e) {
262: e.printStackTrace();
263: }
264: return null;
265: }
266:
267: /**
268: * retrieve the parent XModel of this XModel instance
269: * @return The parent XModel to which this instance belongs
270: */
271: public XModel getParent() {
272: try {
273: Variable var2 = modelNode.invokeMethod("getParent",
274: "()Lnet/xoetrope/xui/data/XModel;", zeroArgsArray);
275: return new NbDebuggerModel(debugger, (ObjectVariable) var2);
276: } catch (Exception e) {
277: e.printStackTrace();
278: }
279: return null;
280: }
281:
282: /**
283: * gets the value attribute
284: * @return the value of the model
285: */
286: public Object get() {
287: try {
288: // Use the JNI method signature
289: Variable var2 = modelNode.invokeMethod("get",
290: "()Ljava/lang/Object;", zeroArgsArray);
291:
292: if (var2.getType().equals("java.lang.String"))
293: return stripQuotes(var2.getValue());
294: else
295: return new NbDebuggerModel(debugger,
296: (ObjectVariable) var2);
297: } catch (Exception e) {
298: e.printStackTrace();
299: }
300: return null;
301: }
302:
303: /**
304: * Sets the model value
305: * @param s the new value
306: */
307: public void set(Object s) {
308: try {
309: oneArgsArray[0] = debugger.evaluate("\"" + s.toString()
310: + "\"");
311: modelNode.invokeMethod("set", "(Ljava/lang/Object;)V",
312: oneArgsArray);
313: } catch (Exception e) {
314: e.printStackTrace();
315: }
316: }
317:
318: /**
319: * Used for elements which need a name assigned temporarily because one doesn't
320: * exist in the DataSource.
321: * @param b true if there was no name in the DataSource
322: */
323: public void hasAutoId(boolean b) {
324: try {
325: oneArgsArray[0] = debugger.evaluate("\"" + b + "\"");
326: modelNode.invokeMethod("hasAutoId", "(Z)V", oneArgsArray);
327: } catch (Exception e) {
328: e.printStackTrace();
329: }
330: }
331:
332: /**
333: * Retrieve the name of the attribute at the specified index
334: * @param i The index of the attributeNames array whose value we want
335: * @return The string value of the attributeNames array at position i
336: */
337: public String getAttribName(int i) {
338: try {
339: oneArgsArray[0] = debugger.evaluate("" + i);
340: // Use the JNI method signature
341: Variable var2 = modelNode.invokeMethod("getAttribName",
342: "(I)Ljava/lang/String;", oneArgsArray);
343: if (var2 != null)
344: return stripQuotes(var2.getValue());
345: } catch (Exception e) {
346: e.printStackTrace();
347: }
348: return null;
349: }
350:
351: /**
352: * Retrieve the value of the attribute at the specified index, convert it to a
353: * String and return it.
354: * @param i The index of the attributeValues array whose value we want
355: * @return The string value of the attributeValues array at position i
356: */
357: public String getAttribValueAsString(int i) {
358: try {
359: oneArgsArray[0] = debugger.evaluate("" + i);
360: // Use the JNI method signature
361: if (modelNode.getType().equals("java.lang.String")) {
362: String attribValue = modelNode.getValue();
363: return stripQuotes(attribValue);
364: }
365: Variable var2 = modelNode.invokeMethod(
366: "getAttribValueAsString", "(I)Ljava/lang/String;",
367: oneArgsArray);
368: if (var2 != null) {
369: String attribValue = var2.getValue();
370: return stripQuotes(attribValue);
371: }
372: } catch (Exception e) {
373: e.printStackTrace();
374: }
375: return null;
376: }
377:
378: /**
379: * Retrieve the value of the attribute at the specified index and return it.
380: * @param i The index of the attributeValues array whose value we want
381: * @return The string value of the attributeValues array at position i
382: */
383: public Object getAttribValue(int i) {
384: try {
385: oneArgsArray[0] = debugger.evaluate("" + i);
386: // Use the JNI method signature
387: Variable var2 = modelNode.invokeMethod("getAttribValue",
388: "(I)Ljava/lang/Object;", oneArgsArray);
389: return new NbDebuggerModel(debugger, (ObjectVariable) var2);
390: } catch (Exception e) {
391: e.printStackTrace();
392: }
393: return null;
394: }
395:
396: /**
397: * Retrieve the value of the attribute at the specified index, convert it to a
398: * double and return it.
399: * @param i The index of the attributeValues array whose value we want
400: * @return The double value of the attributeValues array at position i
401: * @deprecated use getAttribValueAsDouble( i, decimalSeparator, groupingSeparator )
402: * instead, if the locale is different from the locale used to write the values
403: * to the model, then the parsed value may be incorrect.
404: */
405: public double getAttribValueAsDouble(int i) {
406: try {
407: oneArgsArray[0] = debugger.evaluate("" + i);
408: // Use the JNI method signature
409: Variable var2 = modelNode.invokeMethod(
410: "getAttribValueAsDouble", "(I)D", oneArgsArray);
411: return Double.parseDouble(var2.getValue());
412: } catch (Exception e) {
413: e.printStackTrace();
414: }
415: return 0.0;
416: }
417:
418: /**
419: * Convert the attribute at the specified index to a double and return it
420: * @param i The index of the attributeValues array whose value we want
421: * @param decimalSeparator the decimal separator
422: * @param groupingSeparator the grouping (thousands) separator
423: * @return The double value of the attributeValues array at position i
424: */
425: public double getAttribValueAsDouble(int i, char decimalSeparator,
426: char groupingSeparator) {
427: try {
428: oneArgsArray[0] = debugger.evaluate("" + i);
429: // Use the JNI method signature
430: Variable var2 = modelNode.invokeMethod(
431: "getAttribValueAsDouble", "(I)D", oneArgsArray);
432: return NumberFormatter.parseDouble(var2.getValue(),
433: decimalSeparator, groupingSeparator);
434: } catch (Exception e) {
435: e.printStackTrace();
436: }
437: return 0.0;
438: }
439:
440: /**
441: * Retrieve the value of the attribute at the specified index, convert it to an int
442: * and return it.
443: * @param i The index of the attributeValues array whose value we want
444: * @return The int value of the attributeValues array at position i
445: */
446: public int getAttribValueAsInt(int i) {
447: try {
448: oneArgsArray[0] = debugger.evaluate("" + i);
449: // Use the JNI method signature
450: Variable var2 = modelNode.invokeMethod(
451: "getAttribValueAsInt", "(I)I", oneArgsArray);
452: return Integer.parseInt(var2.getValue());
453: } catch (Exception e) {
454: e.printStackTrace();
455: }
456: return 0;
457: }
458:
459: /**
460: * Retrieve the named child, convert it's value to a double and return it.
461: * @param elementName The name of the child whose value is required
462: * @return the value as a double
463: */
464: public double getValueAsDouble(String elementName) {
465: try {
466: oneArgsArray[0] = debugger.evaluate("\"" + elementName
467: + "\"");
468: // Use the JNI method signature
469: Variable var2 = modelNode.invokeMethod("getValueAsDouble",
470: "(Ljava/lang/String;)D", oneArgsArray);
471: return Double.parseDouble(var2.getValue());
472: } catch (Exception e) {
473: e.printStackTrace();
474: }
475: return 0.0;
476: }
477:
478: /**
479: * Retrieve the named child, convert it's value to an int and return it.
480: * @param elementName The name of the child to be retrieved
481: * @return the value as an int
482: */
483: public int getValueAsInt(String elementName) {
484: try {
485: oneArgsArray[0] = debugger.evaluate("\"" + elementName
486: + "\"");
487: // Use the JNI method signature
488: Variable var2 = modelNode.invokeMethod("getValueAsInt",
489: "(Ljava/lang/String;)I", oneArgsArray);
490: return Integer.parseInt(var2.getValue());
491: } catch (Exception e) {
492: e.printStackTrace();
493: }
494: return 0;
495: }
496:
497: /**
498: * Retrieve the named child, convert it's value to a String and return it.
499: * @param elementName The name of the child whose value is required
500: * @return the value as a string
501: */
502: public String getValueAsString(String elementName) {
503: try {
504: oneArgsArray[0] = debugger.evaluate("\"" + elementName
505: + "\"");
506: // Use the JNI method signature
507: Variable var2 = modelNode.invokeMethod("getValueAsString",
508: "(Ljava/lang/String;)Ljava/lang/String;",
509: oneArgsArray);
510: return stripQuotes(var2.getValue());
511: } catch (Exception e) {
512: e.printStackTrace();
513: }
514: return null;
515: }
516:
517: /**
518: * Get a hash code for the node.
519: * @return the node's hash code
520: */
521: public int hashCode() {
522: try {
523: // Use the JNI method signature
524: Variable var2 = modelNode.invokeMethod("hashCode", "()I",
525: zeroArgsArray);
526: return Integer.parseInt(var2.getValue());
527: } catch (Exception e) {
528: e.printStackTrace();
529: }
530: return -1;
531: }
532:
533: /**
534: * Gets the number of immediate children of this node
535: * @retrun the number of child nodes
536: */
537: public int getNumChildren() {
538: try {
539: // Use the JNI method signature
540: Variable var2 = modelNode.invokeMethod("getNumChildren",
541: "()I", zeroArgsArray);
542: if ((var2 != null) && (var2.getValue() != null))
543: return Integer.parseInt(var2.getValue());
544: } catch (Exception e) {
545: e.printStackTrace();
546: }
547: return 0;
548: }
549:
550: /**
551: * Gets the number of attributes of this node
552: * @return the number of attributes
553: */
554: public int getNumAttributes() {
555: try {
556: // Use the JNI method signature
557: Variable var2 = modelNode.invokeMethod("getNumAttributes",
558: "()I", zeroArgsArray);
559: if ((var2 != null) && (var2.getValue() != null))
560: return Integer.parseInt(var2.getValue());
561: } catch (Exception e) {
562: e.printStackTrace();
563: }
564: return 0;
565: }
566:
567: /**
568: * Set the number of children of this node
569: * @param num the new number of children
570: */
571: public void setNumChildren(int num) {
572: try {
573: oneArgsArray[0] = debugger.evaluate(String.valueOf(num));
574: modelNode.invokeMethod("setNumChildren", "(I)V",
575: oneArgsArray);
576: } catch (Exception e) {
577: e.printStackTrace();
578: }
579: }
580:
581: /**
582: * Append a node
583: * @param childNode the child node
584: */
585: public void append(XModel childNode) {
586: try {
587: oneArgsArray[0] = debugger
588: .evaluate("\"" + childNode + "\"");
589: modelNode.invokeMethod("append",
590: "(Lnet/xoetrope/xui/data/XModel;)V", oneArgsArray);
591: } catch (Exception e) {
592: e.printStackTrace();
593: }
594: }
595:
596: /**
597: * Append a new node with the specified name. This method does not replace any
598: * existing nodes.
599: * @param elementName The immediate path to the XModel required
600: * @return The value of the XModel or the attribute
601: */
602: public Object append(String elementName) {
603: try {
604: oneArgsArray[0] = debugger.evaluate("\"" + elementName
605: + "\"");
606: Variable var = modelNode.invokeMethod("append",
607: "(Ljava/lang/String;)Ljava/lang/Object;",
608: oneArgsArray);
609: return new NbDebuggerModel(debugger, (ObjectVariable) var);
610: } catch (Exception e) {
611: e.printStackTrace();
612: }
613: return null;
614: }
615:
616: /**
617: * Remove a child
618: * @param name the ID or name of the node
619: */
620: public void removeChild(String name) {
621: try {
622: oneArgsArray[0] = debugger.evaluate("\"" + name + "\"");
623: modelNode.invokeMethod("removeChild",
624: "(Ljava/lang/String;)V", oneArgsArray);
625: } catch (Exception e) {
626: e.printStackTrace();
627: }
628: }
629:
630: /**
631: * Remove a child node
632: * @param child the child to be removed
633: */
634: public void removeChild(XModel childNode) {
635: try {
636: oneArgsArray[0] = debugger
637: .evaluate("\"" + childNode + "\"");
638: modelNode.invokeMethod("remove",
639: "(Lnet/xoetrope/xui/data/XModel;)V", oneArgsArray);
640: } catch (Exception e) {
641: e.printStackTrace();
642: }
643: }
644:
645: /**
646: * Insert a node at a specified index in the list of children
647: * @param newNode the new model node
648: * @param idx the index at which to insert
649: */
650: public void insertChildAt(XModel newNode, int idx) {
651: try {
652: twoArgsArray[0] = debugger.evaluate("\"" + newNode + "\"");
653: twoArgsArray[1] = debugger.evaluate(String.valueOf(idx));
654: modelNode.invokeMethod("insertChildAt",
655: "(Lnet/xoetrope/xui/data/XModel;I)V", twoArgsArray);
656: } catch (Exception e) {
657: e.printStackTrace();
658: }
659: }
660:
661: private String stripQuotes(String txt) {
662: int idx = (txt.charAt(0) == '"' ? 1 : 0);
663: return txt.substring(idx, txt.length() - idx);
664: }
665:
666: }
|