001: /**
002: *
003: */package newprocess.diagram.cust.annotations.dialogs;
004:
005: import java.util.HashSet;
006: import java.util.Iterator;
007: import java.util.Set;
008: import java.util.Vector;
009:
010: import newprocess.diagram.cust.annotations.commands.DeleteCommand;
011: import newprocess.diagram.cust.annotations.commands.ReadCommand;
012: import newprocess.diagram.cust.annotations.commands.SortCommand;
013: import newprocess.diagram.cust.annotations.commands.WriteCommand;
014: import newprocess.diagram.cust.annotations.utils.URIConvertUtil;
015:
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.emf.common.util.BasicEList;
018: import org.eclipse.emf.common.util.EList;
019: import org.eclipse.gef.EditPart;
020: import org.eclipse.gmf.runtime.diagram.ui.editpolicies.DecorationEditPolicy;
021: import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
022: import org.eclipse.swt.widgets.Shell;
023: import org.eclipse.ui.PlatformUI;
024:
025: /**
026: * Class that plays the role of the domain model in the NewAnnotationDialog
027: * @author sh
028: */
029: public class AnnotationList {
030: private Vector<Annotation> annotations = new Vector<Annotation>();
031: private Set<IAnnotationViewer> changeListeners = new HashSet<IAnnotationViewer>();
032: private EditPart selectedEditPart;
033: private String projectName = null;
034: public static final String STANDARD_TEXT = "(standard)";
035:
036: /**
037: * Constructor
038: */
039: public AnnotationList(EditPart selectedEditPart, String projectName) {
040: super ();
041: this .selectedEditPart = selectedEditPart;
042: this .initData();
043: this .projectName = projectName;
044: }
045:
046: /**
047: * Return the collection of annotations
048: */
049: public Vector<Annotation> getAnnotations() {
050: return annotations;
051: }
052:
053: /**
054: * initialize some default entries
055: */
056: private void initData() {
057: // get the model data
058: ReadCommand readCommand = new ReadCommand();
059: EList urls = readCommand.getValues(selectedEditPart);
060:
061: if (urls == null || urls.isEmpty())
062: return;
063:
064: // create Annotation Objects
065: Annotation exampleAnnotation = null;
066: for (Iterator<Object> iter = urls.iterator(); iter.hasNext();) {
067: Object element = (Object) iter.next();
068: exampleAnnotation = new Annotation(element.toString());
069: exampleAnnotation.setUri(element.toString());
070: annotations.add(exampleAnnotation);
071: }
072: };
073:
074: /**
075: * Add a new annotation to the collection of annotations
076: */
077: public void addAnnotation() {
078: Annotation annotation = new Annotation(
079: AnnotationList.STANDARD_TEXT);
080: annotations.add(annotations.size(), annotation);
081:
082: Iterator<IAnnotationViewer> iterator = changeListeners
083: .iterator();
084: while (iterator.hasNext()) {
085: ((IAnnotationViewer) iterator.next())
086: .addAnnotation(annotation);
087: }
088: }
089:
090: /**
091: * Removes an Annotation
092: * @param annotation
093: */
094: public void removeAnnotation(Annotation annotation) {
095: // check if a standard text entry is in the collection
096: deleteDummies();
097:
098: // removes the annotation from the view
099: annotations.remove(annotation);
100:
101: // remove the annotation from the model
102: // get the annotation to be removed
103: String removingAnno = annotation.getUri().toString();
104: if (removingAnno == null)
105: return;
106:
107: // fetch all stored Annotations from the model
108: ReadCommand readCommand = new ReadCommand();
109: EList urls = readCommand.getValues(selectedEditPart);
110: if (urls.isEmpty() || urls == null)
111: return;
112:
113: // remove the commited annotation
114: DeleteCommand deleteCommand = new DeleteCommand();
115: deleteCommand.executeRemoveCommand(selectedEditPart,
116: removingAnno);
117:
118: // notify the decorator
119: Object editPolicy = selectedEditPart
120: .getEditPolicy(EditPolicyRoles.DECORATION_ROLE);
121: if (editPolicy instanceof DecorationEditPolicy) {
122: ((DecorationEditPolicy) editPolicy).refresh();
123: }
124:
125: Iterator<IAnnotationViewer> iterator = changeListeners
126: .iterator();
127: while (iterator.hasNext())
128: ((IAnnotationViewer) iterator.next())
129: .removeAnnotation(annotation);
130: }
131:
132: /**
133: * does the changes in the model
134: * @param annotation
135: */
136: public void annotationChanged(Annotation annotation) {
137: // check if a standard text entry is in the collection
138: deleteDummies();
139:
140: // first delete all entries in the model
141: ReadCommand readCom = new ReadCommand();
142: EList urls = readCom.getValues(selectedEditPart);
143: DeleteCommand delCom = new DeleteCommand();
144: delCom.executeRemoveCommand(selectedEditPart, urls);
145:
146: // write all current annotations
147: WriteCommand writeCommand = new WriteCommand();
148: for (Iterator<Annotation> iter = annotations.iterator(); iter
149: .hasNext();) {
150: Annotation element = (Annotation) iter.next();
151: String url = element.getUri();
152: if (url.equals(STANDARD_TEXT))
153: continue;
154: writeCommand.executeWriteCommand(selectedEditPart, url);
155: }
156:
157: // notify the decorator
158: Object editPolicy = selectedEditPart
159: .getEditPolicy(EditPolicyRoles.DECORATION_ROLE);
160: if (editPolicy instanceof DecorationEditPolicy) {
161: ((DecorationEditPolicy) editPolicy).refresh();
162: }
163:
164: // update the table view
165: Iterator<IAnnotationViewer> iterator = changeListeners
166: .iterator();
167: while (iterator.hasNext())
168: ((IAnnotationViewer) iterator.next())
169: .updateAnnotation(annotation);
170: }
171:
172: /**
173: * moves the current annotation upwards
174: */
175: public void moveAnnotationUp(Annotation annotation) {
176: //check if it is the first element
177: if (annotations.firstElement() == annotation)
178: return;
179:
180: // check if a standard text entry is in the collection
181: deleteDummies();
182:
183: EList list = new BasicEList();
184: list.addAll(annotations);
185: int annoListIndex = list.indexOf(annotation);
186: list.move(annoListIndex - 1, annotation);
187: annotations.clear();
188: annotations.addAll(list);
189:
190: //*********************** model part *******************************
191: // fetch annotations from the model
192: ReadCommand readCom = new ReadCommand();
193: EList items = readCom.getValues(selectedEditPart);
194: // get the index of the selected annotation in the model
195: int annoIndex = items.indexOf(annotation.getUri().toString());
196: // if it is the first element, do nothing
197: if (annoIndex <= 0)
198: return;
199: // move one position upwards
200: SortCommand sortCom = new SortCommand();
201: sortCom.executeMoveCommand(selectedEditPart, annotation
202: .getUri(), annoIndex - 1);
203:
204: // notify the decorator
205: Object editPolicy = selectedEditPart
206: .getEditPolicy(EditPolicyRoles.DECORATION_ROLE);
207: if (editPolicy instanceof DecorationEditPolicy) {
208: ((DecorationEditPolicy) editPolicy).refresh();
209: }
210:
211: // update the table view
212: Iterator<IAnnotationViewer> iterator = changeListeners
213: .iterator();
214: while (iterator.hasNext())
215: ((IAnnotationViewer) iterator.next())
216: .updateAnnotation(annotation);
217: }
218:
219: /**
220: * moves the current annotation downwards
221: */
222: public void moveAnnotationDown(Annotation annotation) {
223: // check if it is the last element
224: if (annotations.lastElement() == annotation)
225: return;
226:
227: // check if a standard text entry is in the collection
228: deleteDummies();
229:
230: EList list = new BasicEList();
231: list.addAll(annotations);
232: int annoListIndex = list.indexOf(annotation);
233: list.move(annoListIndex + 1, annotation);
234: annotations.clear();
235: annotations.addAll(list);
236:
237: //*********************** model part *******************************
238: // fetch annotations from the model
239: ReadCommand readCom = new ReadCommand();
240: EList items = readCom.getValues(selectedEditPart);
241: // get the index of the selected annotation in the model
242: int annoIndex = items.indexOf(annotation.getUri().toString());
243: // move one position downwards
244: SortCommand sortCom = new SortCommand();
245: sortCom.executeMoveCommand(selectedEditPart, annotation
246: .getUri(), annoIndex + 1);
247:
248: // notify the decorator
249: Object editPolicy = selectedEditPart
250: .getEditPolicy(EditPolicyRoles.DECORATION_ROLE);
251: if (editPolicy instanceof DecorationEditPolicy) {
252: ((DecorationEditPolicy) editPolicy).refresh();
253: }
254:
255: // update the table view
256: Iterator<IAnnotationViewer> iterator = changeListeners
257: .iterator();
258: while (iterator.hasNext())
259: ((IAnnotationViewer) iterator.next())
260: .updateAnnotation(annotation);
261: }
262:
263: /**
264: * link to an existing Annotation
265: */
266: public void linkToAnnotation() {
267: // open the Workspace Selection Dialog
268: Shell shell = PlatformUI.getWorkbench()
269: .getActiveWorkbenchWindow().getShell();
270: ElementChooserDialog chooserDia = new ElementChooserDialog(
271: shell);
272: chooserDia.open();
273:
274: //opens the selection dialog and fetch the project/file
275: String project = chooserDia.getSelectedProject();
276: IFile file = chooserDia.getSelectedFile();
277: if ((project == null) || (file == null)
278: || (projectName == null))
279: return;
280:
281: // get the relative path for the created file
282: URIConvertUtil uriUtil = new URIConvertUtil();
283: String path = uriUtil
284: .getPathForFile(file, projectName, project);
285:
286: //Add a new annotation to the collection of annotations
287: Annotation annotation = new Annotation(path.toString());
288: annotations.add(annotations.size(), annotation);
289:
290: // refreshing the table and the model
291: annotationChanged(annotation);
292: }
293:
294: /**
295: * delete the dummies in the TableViewer
296: */
297: private void deleteDummies() {
298: Vector<Annotation> v = new Vector<Annotation>();
299: for (Iterator<Annotation> iter = annotations.iterator(); iter
300: .hasNext();) {
301: Annotation element = (Annotation) iter.next();
302: if (element.getUri().equals(STANDARD_TEXT)) {
303: v.add(element);
304: }
305: }
306: annotations.removeAll(v);
307: }
308:
309: /**
310: * @param viewer
311: */
312: public void removeChangeListener(IAnnotationViewer viewer) {
313: changeListeners.remove(viewer);
314: }
315:
316: /**
317: * @param viewer
318: */
319: public void addChangeListener(IAnnotationViewer viewer) {
320: changeListeners.add(viewer);
321: }
322:
323: }
|