001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.editors;
006:
007: import org.apache.xmlbeans.XmlObject;
008: import org.eclipse.core.resources.IProject;
009: import org.eclipse.swt.SWT;
010: import org.eclipse.swt.events.FocusAdapter;
011: import org.eclipse.swt.events.FocusEvent;
012: import org.eclipse.swt.events.SelectionAdapter;
013: import org.eclipse.swt.events.SelectionEvent;
014: import org.eclipse.swt.graphics.Image;
015: import org.eclipse.swt.layout.GridData;
016: import org.eclipse.swt.layout.GridLayout;
017: import org.eclipse.swt.widgets.Button;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Event;
020: import org.eclipse.swt.widgets.Group;
021: import org.eclipse.swt.widgets.Label;
022: import org.eclipse.swt.widgets.Listener;
023: import org.eclipse.swt.widgets.Table;
024: import org.eclipse.swt.widgets.TableColumn;
025: import org.eclipse.swt.widgets.TableItem;
026: import org.eclipse.swt.widgets.Text;
027: import org.terracotta.dso.editors.chooser.ClassBehavior;
028: import org.terracotta.dso.editors.chooser.ExpressionChooser;
029: import org.terracotta.dso.editors.chooser.NavigatorBehavior;
030: import org.terracotta.dso.editors.xmlbeans.XmlBooleanToggle;
031: import org.terracotta.ui.util.SWTUtil;
032: import org.w3c.dom.Node;
033: import org.w3c.dom.NodeList;
034:
035: import com.tc.util.event.UpdateEvent;
036: import com.tc.util.event.UpdateEventListener;
037: import com.terracottatech.config.ClassExpression;
038: import com.terracottatech.config.DsoApplication;
039: import com.terracottatech.config.Include;
040: import com.terracottatech.config.InstrumentedClasses;
041: import com.terracottatech.config.OnLoad;
042:
043: public class InstrumentedClassesPanel extends ConfigurationEditorPanel {
044: private IProject m_project;
045: private DsoApplication m_dsoApp;
046: private InstrumentedClasses m_instrumentedClasses;
047:
048: private final Layout m_layout;
049:
050: private TableSelectionListener m_tableSelectionListener;
051: private TableDataListener m_tableDataListener;
052: private AddRuleHandler m_addRuleHandler;
053: private RemoveRuleHandler m_removeRuleHandler;
054: private MoveUpHandler m_moveUpHandler;
055: private MoveDownHandler m_moveDownHandler;
056: private HonorTransientHandler m_honorTransientHandler;
057: private OnLoadDoNothingHandler m_onLoadDoNothingHandler;
058: private OnLoadCallMethodHandler m_onLoadCallMethodHandler;
059: private OnLoadCallMethodTextHandler m_onLoadCallMethodTextHandler;
060: private OnLoadExecuteCodeHandler m_onLoadExecuteCodeHandler;
061: private OnLoadExecuteCodeTextHandler m_onLoadExecuteCodeTextHandler;
062:
063: private static final String INCLUDE = "include";
064: private static final String EXCLUDE = "exclude";
065:
066: public InstrumentedClassesPanel(Composite parent, int style) {
067: super (parent, style);
068: m_layout = new Layout(this );
069: m_tableSelectionListener = new TableSelectionListener();
070: m_tableDataListener = new TableDataListener();
071: m_addRuleHandler = new AddRuleHandler();
072: m_removeRuleHandler = new RemoveRuleHandler();
073: m_moveUpHandler = new MoveUpHandler();
074: m_moveDownHandler = new MoveDownHandler();
075: m_honorTransientHandler = new HonorTransientHandler();
076: m_onLoadDoNothingHandler = new OnLoadDoNothingHandler();
077: m_onLoadCallMethodHandler = new OnLoadCallMethodHandler();
078: m_onLoadCallMethodTextHandler = new OnLoadCallMethodTextHandler();
079: m_onLoadExecuteCodeHandler = new OnLoadExecuteCodeHandler();
080: m_onLoadExecuteCodeTextHandler = new OnLoadExecuteCodeTextHandler();
081: }
082:
083: public boolean hasAnySet() {
084: return m_instrumentedClasses != null
085: && (m_instrumentedClasses.sizeOfExcludeArray() > 0 || m_instrumentedClasses
086: .sizeOfIncludeArray() > 0);
087: }
088:
089: public void ensureXmlObject() {
090: super .ensureXmlObject();
091:
092: if (m_instrumentedClasses == null) {
093: removeListeners();
094: m_instrumentedClasses = m_dsoApp
095: .addNewInstrumentedClasses();
096: updateChildren();
097: addListeners();
098: }
099: }
100:
101: private void testInstrumentedClasses() {
102: if (!hasAnySet() && m_dsoApp.getInstrumentedClasses() != null) {
103: m_dsoApp.unsetInstrumentedClasses();
104: m_instrumentedClasses = null;
105: fireXmlObjectStructureChanged(m_dsoApp);
106: }
107: handleTableSelection();
108: }
109:
110: private void addListeners() {
111: m_layout.m_table.addSelectionListener(m_tableSelectionListener);
112: m_layout.m_table.addListener(SWT.SetData, m_tableDataListener);
113: m_layout.m_addButton.addSelectionListener(m_addRuleHandler);
114: m_layout.m_removeButton
115: .addSelectionListener(m_removeRuleHandler);
116: m_layout.m_moveUpButton.addSelectionListener(m_moveUpHandler);
117: m_layout.m_moveDownButton
118: .addSelectionListener(m_moveDownHandler);
119: m_layout.m_honorTransientCheck
120: .addSelectionListener(m_honorTransientHandler);
121: m_layout.m_doNothingRadio
122: .addSelectionListener(m_onLoadDoNothingHandler);
123: m_layout.m_callAMethodRadio
124: .addSelectionListener(m_onLoadCallMethodHandler);
125: m_layout.m_callAMethodText
126: .addFocusListener(m_onLoadCallMethodTextHandler);
127: m_layout.m_executeCodeRadio
128: .addSelectionListener(m_onLoadExecuteCodeHandler);
129: m_layout.m_executeCodeText
130: .addFocusListener(m_onLoadExecuteCodeTextHandler);
131: }
132:
133: private void removeListeners() {
134: m_layout.m_table
135: .removeSelectionListener(m_tableSelectionListener);
136: m_layout.m_table.removeListener(SWT.SetData,
137: m_tableDataListener);
138: m_layout.m_addButton.removeSelectionListener(m_addRuleHandler);
139: m_layout.m_removeButton
140: .removeSelectionListener(m_removeRuleHandler);
141: m_layout.m_moveUpButton
142: .removeSelectionListener(m_moveUpHandler);
143: m_layout.m_moveDownButton
144: .removeSelectionListener(m_moveDownHandler);
145: m_layout.m_honorTransientCheck
146: .removeSelectionListener(m_honorTransientHandler);
147: m_layout.m_doNothingRadio
148: .removeSelectionListener(m_onLoadDoNothingHandler);
149: m_layout.m_callAMethodRadio
150: .removeSelectionListener(m_onLoadCallMethodHandler);
151: m_layout.m_callAMethodText
152: .removeFocusListener(m_onLoadCallMethodTextHandler);
153: m_layout.m_executeCodeRadio
154: .removeSelectionListener(m_onLoadExecuteCodeHandler);
155: m_layout.m_executeCodeText
156: .removeFocusListener(m_onLoadExecuteCodeTextHandler);
157: }
158:
159: public void updateChildren() {
160: initTableItems();
161: handleTableSelection();
162: }
163:
164: public void updateModel() {
165: removeListeners();
166: updateChildren();
167: addListeners();
168: }
169:
170: public void setup(IProject project, DsoApplication dsoApp) {
171: setEnabled(true);
172: removeListeners();
173:
174: m_project = project;
175: m_dsoApp = dsoApp;
176: m_instrumentedClasses = m_dsoApp != null ? m_dsoApp
177: .getInstrumentedClasses() : null;
178:
179: updateChildren();
180: addListeners();
181: }
182:
183: public void tearDown() {
184: removeListeners();
185:
186: m_dsoApp = null;
187: m_instrumentedClasses = null;
188:
189: setEnabled(false);
190: }
191:
192: private void initTableItems() {
193: m_layout.m_table.removeAll();
194: if (m_instrumentedClasses == null)
195: return;
196: SWTUtil.makeTableComboItem(m_layout.m_table, 0, new String[] {
197: INCLUDE, EXCLUDE });
198: updateRules();
199: if (m_layout.m_table.getItemCount() > 0) {
200: m_layout.m_table.setSelection(0);
201: }
202: }
203:
204: private TableItem createIncludeTableItem(Include include) {
205: TableItem item = new TableItem(m_layout.m_table, SWT.NONE);
206: item.setText(Layout.RULE_COLUMN, INCLUDE);
207: item.setText(Layout.EXPRESSION_COLUMN, include
208: .getClassExpression()
209: + "");
210: item.setData(include);
211: return item;
212: }
213:
214: private TableItem createExcludeTableItem(ClassExpression exclude) {
215: TableItem item = new TableItem(m_layout.m_table, SWT.NONE);
216: item.setText(Layout.RULE_COLUMN, EXCLUDE);
217: item
218: .setText(Layout.EXPRESSION_COLUMN, exclude
219: .getStringValue());
220: item.setData(exclude);
221: return item;
222: }
223:
224: private void updateRules() {
225: m_layout.m_table.removeAll();
226: if (m_instrumentedClasses != null) {
227: XmlObject[] classes = m_instrumentedClasses.selectPath("*");
228: for (int i = 0; i < classes.length; i++) {
229: if (classes[i] instanceof Include) {
230: createIncludeTableItem((Include) classes[i]);
231: } else {
232: createExcludeTableItem((ClassExpression) classes[i]);
233: }
234: }
235: }
236: }
237:
238: private void internalAddInclude(String classExpr) {
239: ensureXmlObject();
240: Include include = m_instrumentedClasses.addNewInclude();
241: include.setClassExpression(classExpr);
242: createIncludeTableItem(include);
243: m_layout.m_table.select(m_layout.m_table.getItemCount() - 1);
244: }
245:
246: XmlObject getSelectedRule() {
247: int i = m_layout.m_table.getSelectionIndex();
248: return (i != -1) ? getRuleAt(i) : null;
249: }
250:
251: OnLoad ensureSelectedOnLoad() {
252: XmlObject rule = getSelectedRule();
253: OnLoad onLoad = null;
254:
255: if (rule instanceof Include) {
256: Include include = (Include) rule;
257: if ((onLoad = include.getOnLoad()) == null) {
258: onLoad = include.addNewOnLoad();
259: }
260: }
261: return onLoad;
262: }
263:
264: XmlObject getRuleAt(int index) {
265: TableItem item = m_layout.m_table.getItem(index);
266: return (XmlObject) item.getData();
267: }
268:
269: void setRuleAt(int index, XmlObject rule) {
270: try {
271: TableItem item = m_layout.m_table.getItem(index);
272: item.setData(rule);
273: if (rule instanceof Include) {
274: Include include = (Include) rule;
275: item.setText(Layout.RULE_COLUMN, INCLUDE);
276: item.setText(Layout.EXPRESSION_COLUMN, include
277: .getClassExpression()
278: + "");
279: } else {
280: ClassExpression exclude = (ClassExpression) rule;
281: item.setText(Layout.RULE_COLUMN, EXCLUDE);
282: item.setText(Layout.EXPRESSION_COLUMN, exclude
283: .getStringValue());
284: }
285: } catch (Throwable t) {
286: t.printStackTrace();
287: }
288: }
289:
290: String getExpressionAt(int index) {
291: XmlObject rule = getRuleAt(index);
292:
293: if (rule instanceof Include) {
294: return ((Include) rule).getClassExpression();
295: } else {
296: return ((ClassExpression) rule).getStringValue();
297: }
298: }
299:
300: int indexOfInclude(Include include) {
301: int index = -1;
302: if (m_instrumentedClasses != null) {
303: Include[] includes = m_instrumentedClasses
304: .getIncludeArray();
305: for (int i = 0; i < includes.length; i++) {
306: if (includes[i] == include)
307: return i;
308: }
309: }
310: return index;
311: }
312:
313: int indexOfExclude(ClassExpression exclude) {
314: int index = -1;
315: if (m_instrumentedClasses != null) {
316: ClassExpression[] excludes = m_instrumentedClasses
317: .xgetExcludeArray();
318: for (int i = 0; i < excludes.length; i++) {
319: if (excludes[i] == exclude)
320: return i;
321: }
322: }
323: return index;
324: }
325:
326: public void removeRule(int index) {
327: XmlObject rule = getRuleAt(index);
328: Node ruleNode = rule.getDomNode();
329: Node topNode = m_instrumentedClasses.getDomNode();
330:
331: topNode.removeChild(ruleNode);
332: }
333:
334: public void moveRuleUp(int index) {
335: XmlObject rule = getRuleAt(index);
336: Node ruleNode = rule.getDomNode();
337: Node topNode = m_instrumentedClasses.getDomNode();
338: NodeList topNodeList = topNode.getChildNodes();
339: int listSize = topNodeList.getLength();
340: Node prevNode;
341:
342: for (int i = 0; i < listSize; i++) {
343: if (ruleNode == topNodeList.item(i)) {
344: while (--i >= 0) {
345: prevNode = topNodeList.item(i);
346: if (prevNode.getNodeType() == Node.ELEMENT_NODE) {
347: topNode.removeChild(ruleNode);
348: topNode.insertBefore(ruleNode, prevNode);
349: fireInstrumentationRulesChanged();
350: return;
351: }
352: }
353: }
354: }
355: }
356:
357: public void moveRuleDown(int index) {
358: XmlObject rule = getRuleAt(index);
359: Node ruleNode = rule.getDomNode();
360: Node topNode = m_instrumentedClasses.getDomNode();
361: NodeList topNodeList = topNode.getChildNodes();
362: int listSize = topNodeList.getLength();
363: Node nextNode;
364:
365: for (int i = 0; i < listSize; i++) {
366: if (ruleNode == topNodeList.item(i)) {
367: while (++i < listSize) {
368: nextNode = topNodeList.item(i);
369: if (nextNode.getNodeType() == Node.ELEMENT_NODE) {
370: while (++i < listSize) {
371: nextNode = topNodeList.item(i);
372: if (nextNode.getNodeType() == Node.ELEMENT_NODE) {
373: topNode.removeChild(ruleNode);
374: topNode
375: .insertBefore(ruleNode,
376: nextNode);
377: fireInstrumentationRulesChanged();
378: return;
379: }
380: }
381: topNode.removeChild(ruleNode);
382: topNode.appendChild(ruleNode);
383: updateRules();
384: return;
385: }
386: }
387: }
388: }
389: }
390:
391: public void replace(int index, XmlObject newRule) {
392: Node newRuleNode = newRule.getDomNode();
393: XmlObject rule = getRuleAt(index);
394: Node ruleNode = rule.getDomNode();
395: Node topNode = m_instrumentedClasses.getDomNode();
396:
397: topNode.replaceChild(newRuleNode, ruleNode);
398: fireInstrumentationRulesChanged();
399: }
400:
401: public void toggleRuleType(int index) {
402: String expr = getExpressionAt(index);
403: XmlObject xmlObj = getRuleAt(index);
404:
405: if (xmlObj instanceof Include) {
406: ClassExpression ce = m_instrumentedClasses.addNewExclude();
407: ce.setStringValue(expr);
408: xmlObj = ce;
409: } else {
410: Include include = m_instrumentedClasses.addNewInclude();
411: include.setClassExpression(expr);
412: xmlObj = include;
413: }
414:
415: replace(index, xmlObj);
416: }
417:
418: private void handleTableSelection() {
419: int index = m_layout.m_table.getSelectionIndex();
420: if (index == -1) {
421: m_layout.m_removeButton.setEnabled(false);
422: m_layout.m_moveUpButton.setEnabled(false);
423: m_layout.m_moveDownButton.setEnabled(false);
424: m_layout.resetIncludeAttributes();
425: return;
426: } else {
427: int count = m_layout.m_table.getItemCount();
428: m_layout.m_removeButton.setEnabled(true);
429: if (index == 0) {
430: m_layout.m_moveUpButton.setEnabled(false);
431: m_layout.m_moveDownButton.setEnabled(count > 1);
432: } else if (index == count - 1) {
433: m_layout.m_moveUpButton.setEnabled(true);
434: m_layout.m_moveDownButton.setEnabled(false);
435: } else {
436: m_layout.m_moveUpButton.setEnabled(true);
437: m_layout.m_moveDownButton.setEnabled(true);
438: }
439: TableItem item = m_layout.m_table.getItem(index);
440: if (item.getText(Layout.RULE_COLUMN).equals(INCLUDE)) {
441: initIncludeAttributes();
442: } else if (item.getText(Layout.RULE_COLUMN).equals(EXCLUDE)) {
443: m_layout.resetIncludeAttributes();
444: }
445: }
446: }
447:
448: private void initIncludeAttributes() {
449: int selected = m_layout.m_table.getSelectionIndex();
450: XmlObject rule = (XmlObject) m_layout.m_table.getItem(selected)
451: .getData();
452:
453: if (rule instanceof Include) {
454: Include include = (Include) rule;
455: m_layout.resetIncludeAttributes();
456: m_layout.enableIncludeAttributes();
457: m_layout.setInclude(include);
458: }
459: }
460:
461: private void handleRemoveOnLoad() {
462: m_layout.m_callAMethodText.setText("");
463: m_layout.m_callAMethodText.setEnabled(false);
464: m_layout.m_executeCodeText.setText("");
465: m_layout.m_executeCodeText.setEnabled(false);
466:
467: Include include = (Include) getSelectedRule();
468: include.unsetOnLoad();
469: fireIncludeRuleChanged(indexOfInclude(include));
470: }
471:
472: private class Layout {
473: private static final int RULE_COLUMN = 0;
474: private static final int EXPRESSION_COLUMN = 1;
475: private static final String UP = "/com/tc/admin/icons/view_menu.gif";
476: private static final String DOWN = "/com/tc/admin/icons/hide_menu.gif";
477: private static final String INSTRUMENTATION_RULES = "Instrumentation Rules";
478: private static final String RULE = "Rule";
479: private static final String EXPRESSION = "Expression";
480: private static final String DETAILS = "Details";
481: private static final String ADD = "Add...";
482: private static final String REMOVE = "Remove";
483: private static final String RAISE_PRIORITY = "Raise priority";
484: private static final String LOWER_PRIORITY = "Lower priority";
485: private static final String HONOR_TRANSIENT = "Honor Transient";
486: private static final String ON_LOAD = "On Load Behavior";
487: private static final String DO_NOTHING = "Do Nothing";
488: private static final String CALL_A_METHOD = "Call a Method";
489: private static final String EXECUTE_CODE = "Execute Code";
490:
491: private Table m_table;
492: private Button m_honorTransientCheck;
493: private Button m_doNothingRadio;
494: private Button m_callAMethodRadio;
495: private Text m_callAMethodText;
496: private Button m_executeCodeRadio;
497: private Text m_executeCodeText;
498: private Button m_addButton;
499: private Button m_removeButton;
500: private Button m_moveUpButton;
501: private Button m_moveDownButton;
502: private Group m_onLoadGroup;
503: private Group m_detailGroup;
504:
505: private void resetIncludeAttributes() {
506: m_detailGroup.setEnabled(false);
507: m_onLoadGroup.setEnabled(false);
508: m_honorTransientCheck.setSelection(false);
509: m_honorTransientCheck.setEnabled(false);
510: m_doNothingRadio.setSelection(false);
511: m_doNothingRadio.setEnabled(false);
512: m_callAMethodRadio.setSelection(false);
513: m_callAMethodRadio.setEnabled(false);
514: m_callAMethodText.setText("");
515: m_callAMethodText.setEnabled(false);
516: m_executeCodeRadio.setSelection(false);
517: m_executeCodeRadio.setEnabled(false);
518: m_executeCodeText.setText("");
519: m_executeCodeText.setEnabled(false);
520: }
521:
522: private void enableIncludeAttributes() {
523: m_detailGroup.setEnabled(true);
524: m_onLoadGroup.setEnabled(true);
525: m_honorTransientCheck.setEnabled(true);
526: m_doNothingRadio.setEnabled(true);
527: m_callAMethodRadio.setEnabled(true);
528: m_executeCodeRadio.setEnabled(true);
529: }
530:
531: private void setInclude(Include include) {
532: ((XmlBooleanToggle) m_honorTransientCheck.getData())
533: .setup(include);
534:
535: if (include != null) {
536: OnLoad onLoad = include.getOnLoad();
537: if (onLoad != null) {
538: if (onLoad.isSetExecute()) {
539: m_executeCodeRadio.setSelection(true);
540: m_executeCodeText.setText(onLoad.getExecute());
541: m_executeCodeText.setEnabled(true);
542: m_callAMethodText.setEnabled(false);
543: } else if (onLoad.isSetMethod()) {
544: m_callAMethodRadio.setSelection(true);
545: m_callAMethodText.setText(onLoad.getMethod());
546: m_executeCodeText.setEnabled(false);
547: m_callAMethodText.setEnabled(true);
548: }
549: } else {
550: m_doNothingRadio.setSelection(true);
551: }
552: }
553: }
554:
555: private Layout(Composite parent) {
556: Composite comp = new Composite(parent, SWT.NONE);
557: GridLayout gridLayout = new GridLayout(2, false);
558: gridLayout.marginWidth = gridLayout.marginHeight = 10;
559: comp.setLayout(gridLayout);
560:
561: Composite sidePanel = new Composite(comp, SWT.NONE);
562: gridLayout = new GridLayout();
563: gridLayout.marginWidth = gridLayout.marginHeight = 0;
564: sidePanel.setLayout(gridLayout);
565: sidePanel.setLayoutData(new GridData(GridData.FILL_BOTH));
566:
567: Label label = new Label(sidePanel, SWT.NONE);
568: label.setText(INSTRUMENTATION_RULES);
569: label.setLayoutData(new GridData(
570: GridData.VERTICAL_ALIGN_BEGINNING));
571:
572: m_table = new Table(sidePanel, SWT.BORDER | SWT.MULTI
573: | SWT.FULL_SELECTION | SWT.V_SCROLL);
574: m_table.setHeaderVisible(true);
575: m_table.setLinesVisible(true);
576: SWTUtil.makeTableColumnsResizeWeightedWidth(sidePanel,
577: m_table, new int[] { 1, 4 });
578: SWTUtil.makeTableColumnsEditable(m_table, new int[] { 1 });
579: GridData gridData = new GridData(GridData.FILL_BOTH);
580: gridData.heightHint = SWTUtil.tableRowsToPixels(m_table, 3);
581: m_table.setLayoutData(gridData);
582:
583: TableColumn ruleColumn = new TableColumn(m_table, SWT.NONE,
584: RULE_COLUMN);
585: ruleColumn.setResizable(true);
586: ruleColumn.setText(RULE);
587: ruleColumn.pack();
588:
589: TableColumn expressionColumn = new TableColumn(m_table,
590: SWT.NONE, EXPRESSION_COLUMN);
591: expressionColumn.setResizable(true);
592: expressionColumn.setText(EXPRESSION);
593: expressionColumn.pack();
594:
595: m_detailGroup = new Group(sidePanel, SWT.SHADOW_NONE);
596: m_detailGroup.setText(DETAILS);
597: m_detailGroup.setEnabled(false);
598: gridLayout = new GridLayout();
599: gridLayout.marginWidth = gridLayout.marginHeight = 5;
600: m_detailGroup.setLayout(gridLayout);
601: m_detailGroup.setLayoutData(new GridData(
602: GridData.FILL_HORIZONTAL));
603:
604: m_honorTransientCheck = new Button(m_detailGroup, SWT.CHECK);
605: m_honorTransientCheck.setText(HONOR_TRANSIENT);
606: m_honorTransientCheck.setEnabled(false);
607: initBooleanField(m_honorTransientCheck, Include.class,
608: "honor-transient");
609:
610: //new Label(m_detailGroup, SWT.NONE); // filler
611:
612: m_onLoadGroup = new Group(m_detailGroup, SWT.SHADOW_NONE);
613: m_onLoadGroup.setText(ON_LOAD);
614: m_onLoadGroup.setEnabled(false);
615: gridLayout = new GridLayout(2, false);
616: gridLayout.marginWidth = gridLayout.marginHeight = 5;
617: m_onLoadGroup.setLayout(gridLayout);
618: m_onLoadGroup
619: .setLayoutData(new GridData(GridData.FILL_BOTH));
620:
621: m_doNothingRadio = new Button(m_onLoadGroup, SWT.RADIO);
622: m_doNothingRadio.setText(DO_NOTHING);
623: m_doNothingRadio.setEnabled(false);
624: gridData = new GridData();
625: gridData.horizontalSpan = 2;
626: m_doNothingRadio.setLayoutData(gridData);
627:
628: m_callAMethodRadio = new Button(m_onLoadGroup, SWT.RADIO);
629: m_callAMethodRadio.setText(CALL_A_METHOD);
630: m_callAMethodRadio.setEnabled(false);
631:
632: m_callAMethodText = new Text(m_onLoadGroup, SWT.BORDER);
633: m_callAMethodText.setEnabled(false);
634: int width = SWTUtil.textColumnsToPixels(m_callAMethodText,
635: 50);
636: gridData = new GridData(GridData.GRAB_HORIZONTAL);
637: gridData.minimumWidth = width;
638: m_callAMethodText.setLayoutData(gridData);
639:
640: m_executeCodeRadio = new Button(m_onLoadGroup, SWT.RADIO);
641: m_executeCodeRadio.setEnabled(false);
642: m_executeCodeRadio.setText(EXECUTE_CODE);
643: gridData = new GridData();
644: gridData.horizontalSpan = 2;
645: m_executeCodeRadio.setLayoutData(gridData);
646:
647: m_executeCodeText = new Text(m_onLoadGroup, SWT.BORDER
648: | SWT.MULTI);
649: m_executeCodeText.setEnabled(false);
650: gridData = new GridData(GridData.FILL_BOTH
651: | GridData.GRAB_VERTICAL);
652: gridData.horizontalSpan = 2;
653: gridData.minimumHeight = SWTUtil.textRowsToPixels(
654: m_executeCodeText, 4);
655: m_executeCodeText.setLayoutData(gridData);
656:
657: Composite buttonPanel = new Composite(comp, SWT.NONE);
658: gridLayout = new GridLayout();
659: gridLayout.marginWidth = gridLayout.marginHeight = 0;
660: buttonPanel.setLayout(gridLayout);
661: buttonPanel.setLayoutData(new GridData(
662: GridData.VERTICAL_ALIGN_BEGINNING));
663:
664: new Label(buttonPanel, SWT.NONE); // filler
665:
666: m_addButton = new Button(buttonPanel, SWT.PUSH);
667: m_addButton.setText(ADD);
668: m_addButton.setLayoutData(new GridData(
669: GridData.VERTICAL_ALIGN_BEGINNING));
670: SWTUtil.applyDefaultButtonSize(m_addButton);
671:
672: m_removeButton = new Button(buttonPanel, SWT.PUSH);
673: m_removeButton.setText(REMOVE);
674: m_removeButton.setEnabled(false);
675: m_removeButton.setLayoutData(new GridData(
676: GridData.VERTICAL_ALIGN_BEGINNING));
677: SWTUtil.applyDefaultButtonSize(m_removeButton);
678:
679: new Label(buttonPanel, SWT.NONE); // filler
680:
681: m_moveUpButton = new Button(buttonPanel, SWT.PUSH);
682: m_moveUpButton.setText(LOWER_PRIORITY);
683: m_moveUpButton.setEnabled(false);
684: m_moveUpButton.setImage(new Image(parent.getDisplay(), this
685: .getClass().getResourceAsStream(UP)));
686: m_moveUpButton.setLayoutData(new GridData(
687: GridData.VERTICAL_ALIGN_BEGINNING));
688: SWTUtil.applyDefaultButtonSize(m_moveUpButton);
689:
690: m_moveDownButton = new Button(buttonPanel, SWT.PUSH);
691: m_moveDownButton.setText(RAISE_PRIORITY);
692: m_moveDownButton.setEnabled(false);
693: m_moveDownButton.setImage(new Image(parent.getDisplay(),
694: this .getClass().getResourceAsStream(DOWN)));
695: m_moveDownButton.setLayoutData(new GridData(
696: GridData.VERTICAL_ALIGN_BEGINNING));
697: SWTUtil.applyDefaultButtonSize(m_moveDownButton);
698: }
699: }
700:
701: class TableSelectionListener extends SelectionAdapter {
702: public void widgetSelected(SelectionEvent e) {
703: handleTableSelection();
704: }
705: }
706:
707: class TableDataListener implements Listener {
708: public void handleEvent(Event event) {
709: TableItem item = (TableItem) event.item;
710: int index = m_layout.m_table.indexOf(item);
711:
712: if (event.index == Layout.RULE_COLUMN) {
713: toggleRuleTypeLater(index);
714: } else {
715: XmlObject xmlObj = (XmlObject) item.getData();
716: String text = item.getText(event.index).trim();
717:
718: if (xmlObj instanceof ClassExpression) {
719: ClassExpression exclude = (ClassExpression) xmlObj;
720:
721: if (text.length() == 0) {
722: item.setText(exclude.getStringValue());
723: removeRuleLater(index);
724: } else {
725: exclude.setStringValue(text);
726: fireExcludeRuleChanged(indexOfExclude(exclude));
727: }
728: } else {
729: Include include = (Include) xmlObj;
730:
731: if (text.length() == 0) {
732: item.setText(include.getClassExpression());
733: removeRuleLater(index);
734: } else {
735: include.setClassExpression(text);
736: fireIncludeRuleChanged(indexOfInclude(include));
737: }
738: }
739: }
740: }
741:
742: private void toggleRuleTypeLater(final int index) {
743: getDisplay().asyncExec(new Runnable() {
744: public void run() {
745: m_layout.m_table.setRedraw(false);
746: try {
747: toggleRuleType(index);
748: m_layout.m_table.setSelection(index);
749: handleTableSelection();
750: } finally {
751: m_layout.m_table.setRedraw(true);
752: }
753: }
754: });
755: }
756:
757: private void removeRuleLater(final int index) {
758: getDisplay().asyncExec(new Runnable() {
759: public void run() {
760: removeRule(index);
761: fireInstrumentationRulesChanged();
762: testInstrumentedClasses();
763: }
764: });
765: }
766: }
767:
768: class AddRuleHandler extends SelectionAdapter {
769: public void widgetSelected(SelectionEvent e) {
770: m_layout.m_table.forceFocus();
771: NavigatorBehavior behavior = new ClassBehavior();
772: final ExpressionChooser chooser = new ExpressionChooser(
773: getShell(), behavior.getTitle(),
774: ClassBehavior.ADD_MSG, m_project, behavior);
775: chooser.addValueListener(new UpdateEventListener() {
776: public void handleUpdate(UpdateEvent updateEvent) {
777: String[] items = (String[]) updateEvent.data;
778: for (int i = 0; i < items.length; i++) {
779: internalAddInclude(items[i]);
780: }
781: fireInstrumentationRulesChanged();
782: }
783: });
784: chooser.open();
785: }
786: }
787:
788: class RemoveRuleHandler extends SelectionAdapter {
789: public void widgetSelected(SelectionEvent e) {
790: m_layout.m_table.forceFocus();
791: int[] selection = m_layout.m_table.getSelectionIndices();
792: for (int i = selection.length - 1; i >= 0; i--) {
793: removeRule(selection[i]);
794: }
795: fireInstrumentationRulesChanged();
796: testInstrumentedClasses();
797: }
798: }
799:
800: class MoveUpHandler extends SelectionAdapter {
801: public void widgetSelected(SelectionEvent e) {
802: m_layout.m_table.forceFocus();
803: int index = m_layout.m_table.getSelectionIndex();
804: moveRuleUp(index);
805: m_layout.m_table.setSelection(index - 1);
806: }
807: }
808:
809: class MoveDownHandler extends SelectionAdapter {
810: public void widgetSelected(SelectionEvent e) {
811: m_layout.m_table.forceFocus();
812: int index = m_layout.m_table.getSelectionIndex();
813: moveRuleDown(index);
814: m_layout.m_table.setSelection(index - 1);
815: }
816: }
817:
818: class HonorTransientHandler extends SelectionAdapter {
819: public void widgetSelected(SelectionEvent e) {
820: fireIncludeRuleChanged(indexOfInclude((Include) getSelectedRule()));
821: }
822: }
823:
824: class OnLoadDoNothingHandler extends SelectionAdapter {
825: public void widgetSelected(SelectionEvent e) {
826: handleRemoveOnLoad();
827: }
828: }
829:
830: class OnLoadCallMethodHandler extends SelectionAdapter {
831: public void widgetSelected(SelectionEvent e) {
832: m_layout.m_callAMethodText.setEnabled(true);
833: m_layout.m_executeCodeText.setText("");
834: m_layout.m_executeCodeText.setEnabled(false);
835:
836: ensureSelectedOnLoad().unsetExecute();
837: fireIncludeRuleChanged(indexOfInclude((Include) getSelectedRule()));
838: }
839: }
840:
841: class OnLoadExecuteCodeHandler extends SelectionAdapter {
842: public void widgetSelected(SelectionEvent e) {
843: m_layout.m_executeCodeText.setEnabled(true);
844: m_layout.m_callAMethodText.setText("");
845: m_layout.m_callAMethodText.setEnabled(false);
846:
847: ensureSelectedOnLoad().unsetMethod();
848: fireIncludeRuleChanged(indexOfInclude((Include) getSelectedRule()));
849: }
850: }
851:
852: class OnLoadExecuteCodeTextHandler extends FocusAdapter {
853: public void focusLost(FocusEvent e) {
854: ensureSelectedOnLoad().setExecute(
855: m_layout.m_executeCodeText.getText());
856: fireIncludeRuleChanged(indexOfInclude((Include) getSelectedRule()));
857: }
858: }
859:
860: class OnLoadCallMethodTextHandler extends FocusAdapter {
861: public void focusLost(FocusEvent e) {
862: ensureSelectedOnLoad().setMethod(
863: m_layout.m_callAMethodText.getText());
864: fireIncludeRuleChanged(indexOfInclude((Include) getSelectedRule()));
865: }
866: }
867:
868: public void includeRuleChanged(IProject project, int index) {
869: if (project.equals(getProject())) {
870: int selIndex = m_layout.m_table.getSelectionIndex();
871: updateRules();
872: if (selIndex != -1) {
873: m_layout.m_table.setSelection(selIndex);
874: handleTableSelection();
875: }
876: }
877: }
878:
879: public void excludeRulesChanged(IProject project) {
880: if (project.equals(getProject())) {
881: initTableItems();
882: }
883: }
884:
885: public void excludeRuleChanged(IProject project, int index) {
886: if (project.equals(getProject())) {
887: int selIndex = m_layout.m_table.getSelectionIndex();
888: updateRules();
889: if (selIndex != -1) {
890: m_layout.m_table.setSelection(selIndex);
891: handleTableSelection();
892: }
893: }
894: }
895:
896: public void includeRulesChanged(IProject project) {
897: // if (project.equals(getProject())) {
898: // initTableItems();
899: // }
900: }
901: }
|