001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc;
005:
006: import org.dijon.Button;
007: import org.dijon.ContainerResource;
008:
009: import com.tc.admin.common.XContainer;
010: import com.tc.pattern.PatternHelper;
011: import com.terracottatech.config.DsoApplication;
012: import com.terracottatech.config.Include;
013: import com.terracottatech.config.InstrumentedClasses;
014:
015: import java.awt.event.ActionEvent;
016: import java.awt.event.ActionListener;
017:
018: import javax.swing.JOptionPane;
019: import javax.swing.event.ListSelectionEvent;
020: import javax.swing.event.ListSelectionListener;
021: import javax.swing.event.TableModelEvent;
022: import javax.swing.event.TableModelListener;
023:
024: public class InstrumentedClassesPanel extends XContainer implements
025: TableModelListener {
026: private DsoApplication m_dsoApp;
027: private InstrumentedClasses m_instrumentedClasses;
028: private RuleTable m_ruleTable;
029: private RuleModel m_ruleModel;
030: private Button m_addRuleButton;
031: private ActionListener m_addRuleListener;
032: private Button m_removeRuleButton;
033: private ActionListener m_removeRuleListener;
034: private Button m_moveUpButton;
035: private ActionListener m_moveUpListener;
036: private Button m_moveDownButton;
037: private ActionListener m_moveDownListener;
038: private ListSelectionListener m_rulesListener;
039: private boolean m_syncingModel;
040:
041: public InstrumentedClassesPanel() {
042: super ();
043: }
044:
045: public void load(ContainerResource containerRes) {
046: super .load(containerRes);
047:
048: m_ruleTable = (RuleTable) findComponent("RuleTable");
049: m_ruleTable.setModel(m_ruleModel = new RuleModel());
050: m_rulesListener = new ListSelectionListener() {
051: public void valueChanged(ListSelectionEvent lse) {
052: if (!lse.getValueIsAdjusting()) {
053: int sel = m_ruleTable.getSelectedRow();
054: int rowCount = m_ruleModel.size();
055:
056: m_removeRuleButton.setEnabled(sel != -1);
057: m_moveUpButton.setEnabled(rowCount > 1 && sel > 0);
058: m_moveDownButton.setEnabled(rowCount > 1
059: && sel < rowCount - 1);
060: }
061: }
062: };
063:
064: m_addRuleButton = (Button) findComponent("AddRuleButton");
065: m_addRuleListener = new ActionListener() {
066: public void actionPerformed(ActionEvent ae) {
067: String include = JOptionPane.showInputDialog(
068: "Enter rule expression", "");
069:
070: if (include != null) {
071: include = include.trim();
072:
073: if (include != null && include.length() > 0) {
074: internalAddInclude(include);
075: }
076: }
077: }
078: };
079:
080: m_removeRuleButton = (Button) findComponent("RemoveRuleButton");
081: m_removeRuleListener = new ActionListener() {
082: public void actionPerformed(ActionEvent ae) {
083: int row = m_ruleTable.getSelectedRow();
084:
085: m_ruleModel.removeRuleAt(row);
086: m_ruleModel.fireTableDataChanged();
087: }
088: };
089:
090: m_moveUpButton = (Button) findComponent("MoveUpButton");
091: m_moveUpListener = new ActionListener() {
092: public void actionPerformed(ActionEvent ae) {
093: m_ruleTable.moveUp();
094: }
095: };
096:
097: m_moveDownButton = (Button) findComponent("MoveDownButton");
098: m_moveDownListener = new ActionListener() {
099: public void actionPerformed(ActionEvent ae) {
100: m_ruleTable.moveDown();
101: }
102: };
103: }
104:
105: public boolean hasAnySet() {
106: return m_instrumentedClasses != null
107: && (m_instrumentedClasses.sizeOfExcludeArray() > 0 || m_instrumentedClasses
108: .sizeOfIncludeArray() > 0);
109: }
110:
111: private InstrumentedClasses ensureInstrumentedClasses() {
112: if (m_instrumentedClasses == null) {
113: ensureXmlObject();
114: }
115: return m_instrumentedClasses;
116: }
117:
118: public void ensureXmlObject() {
119: if (m_instrumentedClasses == null) {
120: removeListeners();
121: m_instrumentedClasses = m_dsoApp
122: .addNewInstrumentedClasses();
123: updateChildren();
124: addListeners();
125: }
126: }
127:
128: private void syncModel() {
129: m_syncingModel = true;
130: SessionIntegratorFrame frame = (SessionIntegratorFrame) getAncestorOfClass(SessionIntegratorFrame.class);
131: frame.modelChanged();
132: m_syncingModel = false;
133: }
134:
135: private void addListeners() {
136: m_ruleModel.addTableModelListener(this );
137: m_ruleTable.getSelectionModel().addListSelectionListener(
138: m_rulesListener);
139: m_addRuleButton.addActionListener(m_addRuleListener);
140: m_removeRuleButton.addActionListener(m_removeRuleListener);
141: m_moveUpButton.addActionListener(m_moveUpListener);
142: m_moveDownButton.addActionListener(m_moveDownListener);
143: }
144:
145: private void removeListeners() {
146: m_ruleModel.removeTableModelListener(this );
147: m_ruleTable.getSelectionModel().removeListSelectionListener(
148: m_rulesListener);
149: m_addRuleButton.removeActionListener(m_addRuleListener);
150: m_removeRuleButton.removeActionListener(m_removeRuleListener);
151: m_moveUpButton.removeActionListener(m_moveUpListener);
152: m_moveDownButton.removeActionListener(m_moveDownListener);
153: }
154:
155: private void updateChildren() {
156: m_ruleModel.setInstrumentedClasses(m_instrumentedClasses);
157: }
158:
159: public void setup(DsoApplication dsoApp) {
160: if (!m_syncingModel) {
161: setEnabled(true);
162: m_moveUpButton.setEnabled(false);
163: m_moveDownButton.setEnabled(false);
164: removeListeners();
165:
166: m_dsoApp = dsoApp;
167: m_instrumentedClasses = m_dsoApp != null ? m_dsoApp
168: .getInstrumentedClasses() : null;
169:
170: updateChildren();
171: addListeners();
172: }
173: }
174:
175: public void tearDown() {
176: removeListeners();
177:
178: m_dsoApp = null;
179: m_instrumentedClasses = null;
180:
181: m_ruleModel.clear();
182:
183: setEnabled(false);
184: }
185:
186: private boolean isAdaptable(String classExpr) {
187: InstrumentedClasses classes = ensureInstrumentedClasses();
188: int size = classes.sizeOfIncludeArray();
189: Include include;
190: String expr;
191:
192: for (int i = 0; i < size; i++) {
193: include = classes.getIncludeArray(i);
194: expr = include.getClassExpression();
195:
196: if (PatternHelper.getHelper().matchesClass(expr, classExpr)) {
197: return true;
198: }
199: }
200:
201: return false;
202: }
203:
204: private void internalAddInclude(String classExpr) {
205: int row = m_ruleModel.getRowCount();
206:
207: m_ruleModel.addInclude(classExpr);
208: m_ruleModel.fireTableRowsInserted(row, row);
209: }
210:
211: private void internalRemoveInclude(String classExpr) {
212: int count = m_ruleModel.getRowCount();
213: Rule rule;
214: String expr;
215:
216: for (int i = 0; i < count; i++) {
217: rule = m_ruleModel.getRuleAt(i);
218:
219: if (rule.isIncludeRule()) {
220: expr = ((Include) rule).getClassExpression();
221:
222: if (PatternHelper.getHelper().matchesClass(expr,
223: classExpr)) {
224: m_ruleModel.removeRuleAt(i);
225: m_ruleModel.fireTableRowsDeleted(i, i);
226: break;
227: }
228: }
229: }
230: }
231:
232: private boolean isExcluded(String classExpr) {
233: InstrumentedClasses classes = ensureInstrumentedClasses();
234: int size = classes.sizeOfExcludeArray();
235: String expr;
236:
237: for (int i = 0; i < size; i++) {
238: expr = classes.getExcludeArray(i);
239:
240: if (PatternHelper.getHelper().matchesClass(expr, classExpr)) {
241: return true;
242: }
243: }
244:
245: return false;
246: }
247:
248: private void internalAddExclude(String classExpr) {
249: if (classExpr != null && classExpr.length() > 0) {
250: m_ruleModel.addExclude(classExpr);
251: }
252: }
253:
254: private void internalRemoveExclude(String classExpr) {
255: int count = m_ruleModel.size();
256: Rule rule;
257:
258: for (int i = 0; i < count; i++) {
259: rule = m_ruleModel.getRuleAt(i);
260:
261: if (rule.isExcludeRule()) {
262: if (PatternHelper.getHelper().matchesClass(
263: rule.getExpression(), classExpr)) {
264: m_ruleModel.removeRuleAt(i);
265: m_ruleModel.fireTableRowsDeleted(i, i);
266: break;
267: }
268: }
269: }
270: }
271:
272: public void ensureAdaptable(String classExpr) {
273: internalAddInclude(classExpr);
274: }
275:
276: public void ensureNotAdaptable(String classExpr) {
277: if (isAdaptable(classExpr)) {
278: internalRemoveInclude(classExpr);
279: }
280: }
281:
282: public void ensureExcluded(String classExpr) {
283: if (!isExcluded(classExpr)) {
284: internalAddExclude(classExpr);
285: }
286: }
287:
288: public void ensureNotExcluded(String classExpr) {
289: if (isExcluded(classExpr)) {
290: internalRemoveExclude(classExpr);
291: }
292: }
293:
294: public void tableChanged(TableModelEvent e) {
295: syncModel();
296: }
297: }
|