1 4 package org.terracotta.dso.editors; 5 6 import org.dijon.Button; 7 import org.dijon.ContainerResource; 8 import org.eclipse.core.resources.IProject; 9 import org.eclipse.jdt.core.IMethod; 10 import org.terracotta.dso.TcPlugin; 11 import org.terracotta.dso.editors.chooser.MethodChooser; 12 13 import com.tc.admin.common.XTable; 14 import com.terracottatech.config.DistributedMethods; 15 import com.terracottatech.config.DsoApplication; 16 import com.terracottatech.config.DistributedMethods.MethodExpression; 17 18 import java.awt.Frame ; 19 import java.awt.event.ActionEvent ; 20 import java.awt.event.ActionListener ; 21 22 import javax.swing.event.ListSelectionEvent ; 23 import javax.swing.event.ListSelectionListener ; 24 import javax.swing.event.TableModelEvent ; 25 import javax.swing.event.TableModelListener ; 26 import javax.swing.table.DefaultTableModel ; 27 28 public class DistributedMethodsPanel extends ConfigurationEditorPanel 29 implements TableModelListener 30 { 31 private IProject m_project; 32 private DsoApplication m_dsoApp; 33 private DistributedMethods m_distributedMethods; 34 private XTable m_methodTable; 35 private MethodModel m_methodModel; 36 private Button m_addButton; 37 private ActionListener m_addListener; 38 private Button m_removeButton; 39 private ActionListener m_removeListener; 40 private ListSelectionListener m_methodsListener; 41 private MethodChooser m_methodChooser; 42 43 public DistributedMethodsPanel() { 44 super(); 45 } 46 47 public void load(ContainerResource containerRes) { 48 super.load(containerRes); 49 50 m_methodTable = (XTable)findComponent("DistributedMethodTable"); 51 m_methodTable.setModel(m_methodModel = new MethodModel()); 52 m_methodsListener = new ListSelectionListener () { 53 public void valueChanged(ListSelectionEvent lse) { 54 if(!lse.getValueIsAdjusting()) { 55 int[] sel = m_methodTable.getSelectedRows(); 56 m_removeButton.setEnabled(sel != null && sel.length > 0); 57 } 58 } 59 }; 60 61 m_addButton = (Button)findComponent("AddDistributedMethodButton"); 62 m_addListener = new ActionListener () { 63 public void actionPerformed(ActionEvent ae) { 64 MethodChooser chsr = getMethodChooser(); 65 66 chsr.setup(m_project); 67 chsr.center(DistributedMethodsPanel.this.getAncestorOfClass(java.awt.Frame .class)); 68 chsr.setVisible(true); 69 } 70 }; 71 72 m_removeButton = (Button)findComponent("RemoveDistributedMethodButton"); 73 m_removeListener = new ActionListener () { 74 public void actionPerformed(ActionEvent ae) { 75 int[] selection = m_methodTable.getSelectedRows(); 76 77 for(int i = selection.length-1; i >= 0; i--) { 78 ensureDistributedMethods().removeMethodExpression(selection[i]); 79 m_methodModel.removeRow(selection[i]); 80 } 81 } 82 }; 83 } 84 85 private MethodChooser getMethodChooser() { 86 if(m_methodChooser == null) { 87 Frame owner = (Frame )getAncestorOfClass(Frame .class); 88 89 m_methodChooser = new MethodChooser(owner); 90 m_methodChooser.setListener(new MethodChooserListener()); 91 } 92 93 return m_methodChooser; 94 } 95 96 class MethodChooserListener implements ActionListener { 97 public void actionPerformed(ActionEvent ae) { 98 String [] exprs = m_methodChooser.getMethodExpressions(); 99 String expr; 100 101 for(int i = 0; i < exprs.length; i++) { 102 expr = exprs[i]; 103 104 if(expr != null && (expr = expr.trim()) != null && expr.length() > 0) { 105 internalAddDistributed(exprs[i]); 106 } 107 } 108 } 109 } 110 111 public boolean hasAnySet() { 112 return m_distributedMethods != null && 113 m_distributedMethods.sizeOfMethodExpressionArray() > 0; 114 } 115 116 private DistributedMethods ensureDistributedMethods() { 117 if(m_distributedMethods == null) { 118 ensureXmlObject(); 119 } 120 return m_distributedMethods; 121 } 122 123 public void ensureXmlObject() { 124 super.ensureXmlObject(); 125 126 if(m_distributedMethods == null) { 127 removeListeners(); 128 m_distributedMethods = m_dsoApp.addNewDistributedMethods(); 129 updateChildren(); 130 addListeners(); 131 } 132 } 133 134 private void syncModel() { 135 if(!hasAnySet() && m_dsoApp.getDistributedMethods() != null){ 136 m_dsoApp.unsetDistributedMethods(); 137 m_distributedMethods = null; 138 fireXmlObjectStructureChanged(m_dsoApp); 139 } 140 141 setDirty(); 142 } 143 144 private void addListeners() { 145 m_methodModel.addTableModelListener(this); 146 m_methodTable.getSelectionModel().addListSelectionListener(m_methodsListener); 147 m_addButton.addActionListener(m_addListener); 148 m_removeButton.addActionListener(m_removeListener); 149 } 150 151 private void removeListeners() { 152 m_methodModel.removeTableModelListener(this); 153 m_methodTable.getSelectionModel().removeListSelectionListener(m_methodsListener); 154 m_addButton.removeActionListener(m_addListener); 155 m_removeButton.removeActionListener(m_removeListener); 156 } 157 158 public void updateChildren() { 159 m_methodModel.clear(); 160 161 if(m_distributedMethods != null) { 162 MethodExpression[] mes = m_distributedMethods.getMethodExpressionArray(); 163 String [] vals = new String [mes.length]; 164 for (int i = 0; i < mes.length; i++) { 165 vals[i] = mes[i].getStringValue(); 166 } 167 m_methodModel.setMethods(vals); 168 } else { 169 m_methodModel.fireTableDataChanged(); 170 } 171 } 172 173 public void updateModel() { 174 removeListeners(); 175 updateChildren(); 176 addListeners(); 177 } 178 179 public void setup(IProject project, DsoApplication dsoApp) { 180 setEnabled(true); 181 removeListeners(); 182 183 m_project = project; 184 m_dsoApp = dsoApp; 185 m_distributedMethods = m_dsoApp != null ? 186 m_dsoApp.getDistributedMethods() : 187 null; 188 189 updateChildren(); 190 addListeners(); 191 } 192 193 public void tearDown() { 194 removeListeners(); 195 196 m_dsoApp = null; 197 m_distributedMethods = null; 198 199 m_methodModel.clear(); 200 201 setEnabled(false); 202 } 203 204 class MethodModel extends DefaultTableModel { 205 MethodModel() { 206 super(); 207 setColumnIdentifiers(new String []{"Distributed methods"}); 208 } 209 210 public int size() { 211 return getRowCount(); 212 } 213 214 void clear() { 215 setRowCount(0); 216 } 217 218 void setMethods(String [] methods) { 219 clear(); 220 221 if(methods != null) { 222 for(int i = 0; i < methods.length; i++) { 223 addMethod(methods[i]); 224 } 225 } 226 } 227 228 void addMethod(String method) { 229 addRow(new Object [] {method}); 230 } 231 232 String getExcludeAt(int row) { 233 return (String )getValueAt(row, 0); 234 } 235 236 public void setValueAt(Object value, int row, int col) { 237 DistributedMethods dms = ensureDistributedMethods(); 238 MethodExpression me = dms.getMethodExpressionArray(row); 239 me.setStringValue((String )value); 240 super.setValueAt(value, row, col); 241 } 242 } 243 244 public void tableChanged(TableModelEvent tme) { 245 syncModel(); 246 } 247 248 public boolean isDistributed(IMethod method) { 249 return TcPlugin.getDefault().getConfigurationHelper(m_project).isDistributedMethod(method); 250 } 251 252 private void internalAddDistributed(String expr) { 253 if(expr != null && expr.length() > 0) { 254 DistributedMethods dms = ensureDistributedMethods(); 255 MethodExpression me = dms.addNewMethodExpression(); 256 me.setStringValue(expr); 257 m_methodModel.addMethod(expr); 258 } 259 } 260 } 261 | Popular Tags |