1 11 12 package org.eclipse.pde.internal.ui.editor.cheatsheet.comp.details; 13 14 import org.eclipse.jface.text.DocumentEvent; 15 import org.eclipse.jface.text.IDocument; 16 import org.eclipse.jface.text.IDocumentListener; 17 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSIntro; 18 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModelFactory; 19 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject; 20 import org.eclipse.pde.internal.core.util.PDETextHelper; 21 22 26 public class CompCSIntroductionTextListener implements IDocumentListener { 27 28 private ICompCSTaskObject fDataTaskObject; 29 30 private boolean fBlockEvents; 31 32 35 public CompCSIntroductionTextListener() { 36 fDataTaskObject = null; 37 fBlockEvents = false; 38 } 39 40 43 public void setBlockEvents(boolean block) { 44 fBlockEvents = block; 45 } 46 47 50 public boolean getBlockEvents() { 51 return fBlockEvents; 52 } 53 54 57 public void setData(ICompCSTaskObject object) { 58 fDataTaskObject = object; 60 } 61 62 65 public void documentAboutToBeChanged(DocumentEvent e) { 66 } 68 69 72 public void documentChanged(DocumentEvent event) { 73 if (fBlockEvents) { 75 return; 76 } 77 if (fDataTaskObject == null) { 79 return; 80 } 81 IDocument document = event.getDocument(); 83 if (document == null) { 84 return; 85 } 86 String text = document.get().trim(); 87 boolean hasText = PDETextHelper.isDefined(text); 89 if (hasText) { 90 updateIntroductionText(text); 92 } else { 93 removeIntroductionText(text); 95 } 96 } 97 98 101 private void updateIntroductionText(String text) { 102 if (fDataTaskObject.getFieldIntro() == null) { 103 addIntroductionText(text); 105 } else { 106 modifyIntroductionText(text); 108 } 109 } 110 111 114 private void addIntroductionText(String text) { 115 ICompCSModelFactory factory = 116 fDataTaskObject.getModel().getFactory(); 117 ICompCSIntro intro = factory.createCompCSIntro(fDataTaskObject); 118 intro.setFieldContent(text); 119 fDataTaskObject.setFieldIntro(intro); 120 } 121 122 125 private void modifyIntroductionText(String text) { 126 ICompCSIntro intro = fDataTaskObject.getFieldIntro(); 127 intro.setFieldContent(text); 128 } 129 130 133 private void removeIntroductionText(String text) { 134 ICompCSIntro intro = fDataTaskObject.getFieldIntro(); 135 if (intro != null) { 136 fDataTaskObject.setFieldIntro(null); 137 } 138 } 139 140 } 141 | Popular Tags |