1 19 20 package org.netbeans.modules.web.jsf.refactoring; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.logging.Logger ; 27 import javax.swing.text.BadLocationException ; 28 import javax.swing.text.Position.Bias; 29 import org.netbeans.editor.BaseDocument; 30 import org.netbeans.modules.web.api.webmodule.WebModule; 31 import org.netbeans.modules.web.jsf.JSFConfigDataObject; 32 import org.netbeans.modules.web.jsf.api.ConfigurationUtils; 33 import org.netbeans.modules.web.jsf.api.facesmodel.Converter; 34 import org.netbeans.modules.web.jsf.editor.JSFEditorUtilities; 35 import org.netbeans.modules.web.jsf.api.facesmodel.FacesConfig; 36 import org.netbeans.modules.web.jsf.api.facesmodel.ManagedBean; 37 import org.openide.ErrorManager; 38 import org.openide.filesystems.FileObject; 39 import org.openide.loaders.DataObject; 40 import org.openide.loaders.DataObjectNotFoundException; 41 import org.openide.text.CloneableEditorSupport; 42 import org.openide.text.PositionBounds; 43 import org.openide.text.PositionRef; 44 import org.openide.util.NbBundle; 45 46 50 public class Occurrences { 51 52 private static final Logger LOGGER = Logger.getLogger(Occurrences.class.getName()); 53 54 public static abstract class OccurrenceItem { 55 protected FileObject config; 57 protected String newValue; 58 protected String oldValue; 59 60 public OccurrenceItem(FileObject config, String newValue, String oldValue){ 61 this.config = config; 62 this.newValue = newValue; 63 this.oldValue = oldValue; 64 } 65 66 public FileObject getFacesConfig() { 67 return config; 68 } 69 70 public String getElementText(){ 71 StringBuffer stringBuffer = new StringBuffer (); 72 stringBuffer.append("<font color=\"#0000FF\">"); 73 stringBuffer.append("<").append(getXMLElementName()).append("></font><b>"); 74 stringBuffer.append(oldValue).append("</b><font color=\"#0000FF\"></").append(getXMLElementName()); 75 stringBuffer.append("></font>"); 76 return stringBuffer.toString(); 77 } 78 79 protected abstract String getXMLElementName(); 80 81 public abstract void performRename(); 82 public abstract void undoRename(); 83 public abstract String getRenameMessage(); 84 85 public abstract void performSafeDelete(); 86 public abstract void undoSafeDelete(); 87 public abstract String getSafeDeleteMessage(); 88 89 public abstract String getWhereUsedMessage(); 90 91 protected PositionBounds createPosition(int startOffset, int endOffset) { 92 try{ 93 DataObject dataObject = DataObject.find(config); 94 if (dataObject instanceof JSFConfigDataObject){ 95 CloneableEditorSupport editor 96 = JSFEditorUtilities.findCloneableEditorSupport((JSFConfigDataObject)dataObject); 97 if (editor != null){ 98 PositionRef start=editor.createPositionRef(startOffset, Bias.Forward); 99 PositionRef end=editor.createPositionRef(endOffset, Bias.Backward); 100 return new PositionBounds(start,end); 101 } 102 } 103 } catch (DataObjectNotFoundException ex) { 104 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 105 ex.getMessage(), 106 ex); 107 } 108 return null; 109 } 110 111 public PositionBounds getClassDefinitionPosition() { 112 return createPosition(0, 0); 113 }; 114 115 public PositionBounds getElementDefinitionPosition() { 116 return createPosition(0, 0); 117 }; 118 } 119 120 public static class ManagedBeanClassItem extends OccurrenceItem{ 121 private final ManagedBean bean; 122 123 public ManagedBeanClassItem(FileObject config, ManagedBean bean, String newValue){ 124 super(config, newValue, bean.getManagedBeanClass()); 125 this.bean = bean; 126 } 127 128 protected String getXMLElementName(){ 129 return "managed-bean-class"; } 131 132 public void performRename(){ 133 changeBeanClass(newValue); 134 } 135 136 public void undoRename(){ 137 changeBeanClass(oldValue); 138 } 139 140 public String getWhereUsedMessage(){ 141 return NbBundle.getMessage(Occurrences.class, "MSG_ManagedBeanClass_WhereUsed", new Object [] { bean.getManagedBeanName(), getElementText()}); 143 } 144 145 public String getRenameMessage(){ 146 return NbBundle.getMessage(Occurrences.class, "MSG_ManagedBeanClass_Rename", new Object [] { bean.getManagedBeanName(), getElementText()}); 148 } 149 150 public void performSafeDelete() { 151 FacesConfig faces = ConfigurationUtils.getConfigModel(config, true).getRootComponent(); 152 Collection <ManagedBean> beans = faces.getManagedBeans(); 153 for (Iterator <ManagedBean> it = beans.iterator(); it.hasNext();) { 154 ManagedBean managedBean = it.next(); 155 if (bean.getManagedBeanName().equals(managedBean.getManagedBeanName())){ 156 faces.getModel().startTransaction(); 157 faces.removeManagedBean(managedBean); 158 faces.getModel().endTransaction(); 159 continue; 160 } 161 } 162 } 163 164 public void undoSafeDelete() { 165 FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent(); 166 facesConfig.getModel().startTransaction(); 167 facesConfig.addManagedBean(bean); 168 facesConfig.getModel().endTransaction(); 169 } 170 171 public String getSafeDeleteMessage() { 172 return NbBundle.getMessage(Occurrences.class, "MSG_ManagedBeanClass_SafeDelete", new Object [] { bean.getManagedBeanName(), getElementText()}); 174 } 175 176 private void changeBeanClass(String className){ 177 FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent(); 178 List <ManagedBean> beans = facesConfig.getManagedBeans(); 179 for (Iterator <ManagedBean> it = beans.iterator(); it.hasNext();) { 180 ManagedBean managedBean = it.next(); 181 if (bean.getManagedBeanName().equals(managedBean.getManagedBeanName())){ 182 facesConfig.getModel().startTransaction(); 183 managedBean.setManagedBeanClass(className); 184 facesConfig.getModel().endTransaction(); 185 continue; 186 } 187 188 } 189 } 190 191 public PositionBounds getClassDefinitionPosition() { 192 PositionBounds position = null; 193 try{ 194 JSFConfigDataObject dataObject = (JSFConfigDataObject)DataObject.find(config); 195 BaseDocument document = JSFEditorUtilities.getBaseDocument(dataObject); 196 int [] offsets = JSFEditorUtilities.getManagedBeanDefinition(document, bean.getManagedBeanName()); 197 String text = document.getText(offsets); 198 int offset = offsets[0] + text.indexOf(oldValue); 199 position = createPosition(offset, offset + oldValue.length()); 200 } catch (BadLocationException ex) { 201 ErrorManager.getDefault().notify(ex); 202 } catch (DataObjectNotFoundException ex) { 203 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 204 ex.getMessage(), 205 ex); 206 } 207 return position; 208 }; 209 210 public PositionBounds getElementDefinitionPosition() { 211 PositionBounds position = null; 212 try { 213 JSFConfigDataObject dataObject = (JSFConfigDataObject)DataObject.find(config); 214 BaseDocument document = JSFEditorUtilities.getBaseDocument(dataObject); 215 int [] offsets = JSFEditorUtilities.getManagedBeanDefinition(document, bean.getManagedBeanName()); 216 position = createPosition(offsets[0], offsets[1]); 217 } catch (DataObjectNotFoundException ex) { 218 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 219 ex.getMessage(), 220 ex); 221 } 222 return position; 223 }; 224 } 225 226 public static class ConverterClassItem extends OccurrenceItem { 227 private final Converter converter; 228 229 public ConverterClassItem(FileObject config, Converter converter, String newValue){ 230 super(config, newValue, converter.getConverterClass()); 231 this.converter = converter; 232 } 233 234 protected String getXMLElementName(){ 235 return "converter-class"; } 237 238 public void performRename(){ 239 changeConverterClass(oldValue, newValue); 240 } 241 242 public void undoRename(){ 243 changeConverterClass(newValue, oldValue); 244 } 245 246 public String getWhereUsedMessage(){ 247 return NbBundle.getMessage(Occurrences.class, "MSG_ConverterClass_WhereUsed", getElementText()); } 249 250 public String getRenameMessage(){ 251 return NbBundle.getMessage(Occurrences.class, "MSG_ConverterClass_Rename", getElementText()); } 253 254 public void performSafeDelete() { 255 FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent(); 256 List <Converter> converters = facesConfig.getConverters(); 257 for (Iterator <Converter> it = converters.iterator(); it.hasNext();) { 258 Converter converter = it.next(); 259 if (oldValue.equals(converter.getConverterClass())){ 260 facesConfig.getModel().startTransaction(); 261 facesConfig.removeConverter(converter); 262 facesConfig.getModel().endTransaction(); 263 continue; 264 } 265 } 266 } 267 268 public void undoSafeDelete() { 269 FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent(); 270 facesConfig.getModel().startTransaction(); 271 facesConfig.addConverter(converter); 272 facesConfig.getModel().endTransaction(); 273 } 274 275 public String getSafeDeleteMessage() { 276 return NbBundle.getMessage(Occurrences.class, "MSG_ConverterClass_SafeDelete", new Object [] { getElementText()}); 278 } 279 280 private void changeConverterClass(String oldClass, String newClass){ 281 FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent(); 282 List <Converter> converters = facesConfig.getConverters(); 283 for (Iterator <Converter> it = converters.iterator(); it.hasNext();) { 284 Converter converter = it.next(); 285 if (oldClass.equals(converter.getConverterClass())){ 286 converter.getModel().startTransaction(); 287 converter.setConverterClass(newClass); 288 converter.getModel().endTransaction(); 289 continue; 290 } 291 } 292 } 293 294 public PositionBounds getClassDefinitionPosition() { 295 PositionBounds position = null; 296 try{ 297 JSFConfigDataObject dataObject = (JSFConfigDataObject)DataObject.find(config); 298 BaseDocument document = JSFEditorUtilities.getBaseDocument(dataObject); 299 int [] offsets = JSFEditorUtilities.getConverterDefinition(document, converter.getConverterForClass()); 300 301 String text = document.getText(offsets); 302 int offset = offsets[0] + text.indexOf(oldValue); 303 position = createPosition(offset, offset + oldValue.length()); 304 } catch (BadLocationException ex) { 305 ErrorManager.getDefault().notify(ex); 306 } catch (DataObjectNotFoundException ex) { 307 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 308 ex.getMessage(), 309 ex); 310 } 311 return position; 312 }; 313 314 public PositionBounds getElementDefinitionPosition() { 315 PositionBounds position = null; 316 try{ 317 JSFConfigDataObject dataObject = (JSFConfigDataObject)DataObject.find(config); 318 BaseDocument document = JSFEditorUtilities.getBaseDocument(dataObject); 319 int [] offsets = JSFEditorUtilities.getConverterDefinition(document, converter.getConverterForClass()); 320 position = createPosition(offsets[0], offsets[1]); 321 } catch (DataObjectNotFoundException ex) { 322 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 323 ex.getMessage(), 324 ex); 325 } 326 return position; 327 }; 328 } 329 330 public static class ConverterForClassItem extends OccurrenceItem { 331 private final Converter converter; 332 333 public ConverterForClassItem(FileObject config, Converter converter, String newValue){ 334 super(config, newValue, converter.getConverterForClass()); 335 this.converter = converter; 336 } 337 338 protected String getXMLElementName(){ 339 return "converter-for-class"; } 341 342 public void performRename(){ 343 changeConverterForClass(oldValue, newValue); 344 } 345 346 public void undoRename(){ 347 changeConverterForClass(newValue, oldValue); 348 } 349 350 public String getWhereUsedMessage(){ 351 return NbBundle.getMessage(Occurrences.class, "MSG_ConverterForClass_WhereUsed", getElementText()); } 353 354 public String getRenameMessage(){ 355 return NbBundle.getMessage(Occurrences.class, "MSG_ConverterForClass_Rename", getElementText()); } 357 358 public void performSafeDelete() { 359 FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent(); 360 List <Converter> converters = facesConfig.getConverters(); 361 for (Iterator <Converter> it = converters.iterator(); it.hasNext();) { 362 Converter converter = it.next(); 363 if (oldValue.equals(converter.getConverterClass())){ 364 facesConfig.getModel().startTransaction(); 365 facesConfig.removeConverter(converter); 366 facesConfig.getModel().endTransaction(); 367 continue; 368 } 369 } 370 } 371 372 public void undoSafeDelete() { 373 FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent(); 374 facesConfig.getModel().startTransaction(); 375 facesConfig.addConverter(converter); 376 facesConfig.addConverter(converter); 377 facesConfig.getModel().endTransaction(); 378 } 379 380 public String getSafeDeleteMessage() { 381 return NbBundle.getMessage(Occurrences.class, "MSG_ManagedBeanClass_SafeDelete", new Object [] { getElementText()}); 383 } 384 385 private void changeConverterForClass(String oldClass, String newClass){ 386 FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent(); 387 List <Converter> converters = facesConfig.getConverters(); 388 for (Iterator <Converter> it = converters.iterator(); it.hasNext();) { 389 Converter converter = it.next(); 390 if (oldClass.equals(converter.getConverterForClass())){ 391 converter.getModel().startTransaction(); 392 converter.setConverterForClass(newClass); 393 converter.getModel().endTransaction(); 394 continue; 395 } 396 } 397 } 398 399 public PositionBounds getClassDefinitionPosition() { 400 PositionBounds position = null; 401 try{ 402 JSFConfigDataObject dataObject = (JSFConfigDataObject)DataObject.find(config); 403 BaseDocument document = JSFEditorUtilities.getBaseDocument(dataObject); 404 int [] offsets = JSFEditorUtilities.getConverterDefinition(document, converter.getConverterForClass()); 405 String text = document.getText(offsets); 406 int offset = offsets[0] + text.indexOf(oldValue); 407 position = createPosition(offset, offset + oldValue.length()); 408 } catch (BadLocationException ex) { 409 ErrorManager.getDefault().notify(ex); 410 } catch (DataObjectNotFoundException ex) { 411 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 412 ex.getMessage(), 413 ex); 414 } 415 return position; 416 }; 417 418 public PositionBounds getElementDefinitionPosition() { 419 PositionBounds position = null; 420 try{ 421 JSFConfigDataObject dataObject = (JSFConfigDataObject)DataObject.find(config); 422 BaseDocument document = JSFEditorUtilities.getBaseDocument(dataObject); 423 int [] offsets = JSFEditorUtilities.getConverterDefinition(document, converter.getConverterForClass()); 424 position = createPosition(offsets[0], offsets[1]); 425 } catch (DataObjectNotFoundException ex) { 426 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 427 ex.getMessage(), 428 ex); 429 } 430 return position; 431 }; 432 } 433 434 public static List <OccurrenceItem> getAllOccurrences(WebModule webModule, String oldName, String newName){ 435 List result = new ArrayList (); 436 assert webModule != null; 437 assert oldName != null; 438 assert newName != null; 439 440 LOGGER.fine("getAllOccurences("+ webModule.getDocumentBase().getPath() + ", " + oldName + ", " + newName + ")"); 441 if (webModule != null){ 442 FileObject[] configs = ConfigurationUtils.getFacesConfigFiles(webModule); 444 445 if (configs != null){ 446 for (int i = 0; i < configs.length; i++) { 447 FacesConfig facesConfig = ConfigurationUtils.getConfigModel(configs[i], true).getRootComponent(); 448 List <Converter> converters = facesConfig.getConverters(); 449 for (Iterator <Converter> it = converters.iterator(); it.hasNext();) { 450 Converter converter = it.next(); 451 if (oldName.equals(converter.getConverterClass())) 452 result.add(new ConverterClassItem(configs[i], converter, newName)); 453 else if (oldName.equals(converter.getConverterForClass())) 454 result.add(new ConverterForClassItem(configs[i], converter, newName)); 455 } 456 List <ManagedBean> managedBeans = facesConfig.getManagedBeans(); 457 for (Iterator <ManagedBean> it = managedBeans.iterator(); it.hasNext();) { 458 ManagedBean managedBean = it.next(); 459 if (oldName.equals(managedBean.getManagedBeanClass())) 460 result.add(new ManagedBeanClassItem(configs[i], managedBean, newName)); 461 462 } 463 464 } 465 } 466 } 467 return result; 468 } 469 470 } 471 | Popular Tags |