001: /**
002: *
003: */package gui.section;
004:
005: import gui.actions.RefactorEnvEntryAction;
006:
007: import java.util.HashMap;
008: import java.util.HashSet;
009: import java.util.Iterator;
010: import java.util.Map;
011: import java.util.Set;
012: import java.util.Vector;
013:
014: import org.eclipse.emf.common.util.EList;
015: import org.eclipse.gef.EditPart;
016:
017: import core.Util;
018: import diagram.commands.ConfigureCommand;
019: import diagram.commands.DeleteCommand;
020: import diagram.commands.ReadCommand;
021: import diagram.commands.WriteCommand;
022: import diagram.section.EnvEntry;
023:
024: /**
025: * Class that plays the role of the domain model in the EnvEntrySheetPage.
026: *
027: * @author sh
028: */
029: public class EnvEntryList {
030: private Vector<EnvEntry> envEntries = new Vector<EnvEntry>();
031: private Set<IEnvEntryViewer> changeListeners = new HashSet<IEnvEntryViewer>();
032: private EditPart selectedEditPart;
033: Map<EnvEntry, newprocess.EnvEntry> entryMap = new HashMap<EnvEntry, newprocess.EnvEntry>();
034: public static final String STANDARD_TEXT = "(standard)";
035:
036: /**
037: * Constructor
038: */
039: public EnvEntryList(EditPart selectedEditPart) {
040: super ();
041: this .selectedEditPart = selectedEditPart;
042: this .initData();
043: }
044:
045: /**
046: * Return the collection of EnvEntries.
047: */
048: public Vector<EnvEntry> getEnvEntries() {
049: return envEntries;
050: }
051:
052: /**
053: * initialize some default entries.
054: */
055: private void initData() {
056: // get the EMF model data
057: ReadCommand readCommand = new ReadCommand();
058: EList<newprocess.EnvEntry> entries = readCommand
059: .getValues(selectedEditPart);
060:
061: if (entries == null || entries.isEmpty())
062: return;
063:
064: // clear the hashmap
065: entryMap.clear();
066:
067: // create the table model EnvEntry Objects
068: EnvEntry exampleEnvEntry = null;
069: for (Iterator<newprocess.EnvEntry> iter = entries.iterator(); iter
070: .hasNext();) {
071: newprocess.EnvEntry element = (newprocess.EnvEntry) iter
072: .next();
073: exampleEnvEntry = new EnvEntry(element.getName());
074: exampleEnvEntry.setName(element.getName());
075: exampleEnvEntry.setType(element.getType());
076: exampleEnvEntry.setValue(element.getValue());
077: envEntries.add(exampleEnvEntry);
078: entryMap.put(exampleEnvEntry, element);
079: }
080: };
081:
082: /**
083: * Add a new table model EnvEntry and an EMF model EnvEntry.
084: */
085: public void addEnvEntry() {
086: // create the table model EnvEntry Object
087: EnvEntry envEntry = new EnvEntry(EnvEntryList.STANDARD_TEXT);
088: envEntries.add(envEntries.size(), envEntry);
089:
090: // create the EMF model EnvEntry Object
091: ConfigureCommand confCom = new ConfigureCommand();
092: newprocess.EnvEntry modelEntry = confCom
093: .executeConfigureElementCommand(selectedEditPart,
094: envEntry);
095:
096: // add both to the hasmap
097: entryMap.put(envEntry, modelEntry);
098:
099: Iterator<IEnvEntryViewer> iterator = changeListeners.iterator();
100: while (iterator.hasNext()) {
101: ((IEnvEntryViewer) iterator.next()).addEnvEntry(envEntry);
102: }
103: }
104:
105: /**
106: * Removes a table model EnvEntry and an EMF model EnvEntry Object.
107: * It also checks if EnvEntry source code exist. If so the
108: * source will be removed if confirmed.
109: *
110: * @param envEntry
111: */
112: public void removeEnvEntry(EnvEntry envEntry) {
113: // check if a standard text entry is in the collection
114: deleteDummies();
115:
116: // removes the table EnvEntry Object from the view
117: envEntries.remove(envEntry);
118:
119: // fetch the EMF Model Entry of the concerning table model Entry Object
120: newprocess.EnvEntry modelEntry = entryMap.get(envEntry);
121:
122: DeleteCommand deleteCommand = new DeleteCommand();
123: deleteCommand
124: .executeRemoveCommand(selectedEditPart, modelEntry);
125:
126: // remove the env entry source
127: Util.removeEnvEntrySource(selectedEditPart, envEntry);
128:
129: Iterator<IEnvEntryViewer> iterator = changeListeners.iterator();
130: while (iterator.hasNext())
131: ((IEnvEntryViewer) iterator.next())
132: .removeEnvEntry(envEntry);
133: }
134:
135: /**
136: * Refactors the table model EnvEntry,the EMF model EnvEntry
137: * and finally the source code.
138: *
139: * @param envEntry the selected EnvEntry
140: */
141: public void refactorEnvEntry(EnvEntry envEntry) {
142: // check if a standard text entry is in the collection
143: deleteDummies();
144:
145: // invoke the refactoring
146: //new RefactorEnvEntryAction(selectedEditPart, envEntry).run();
147: RefactorEnvEntryAction refAction = new RefactorEnvEntryAction(
148: selectedEditPart, envEntry, changeListeners);
149: refAction.run();
150:
151: // the model update will be executed by the model change object in the EnventryDelegate
152: }
153:
154: /**
155: * does the changes in the model.
156: *
157: * @param envEntry
158: */
159: public void envEntryChanged(EnvEntry envEntry) {
160: // check if a standard text entry is in the collection
161: deleteDummies();
162:
163: // fetch the EMF Model Entry of the concerning table model Entry
164: newprocess.EnvEntry modelEntry = entryMap.get(envEntry);
165:
166: // set the name, value and type attributs
167: WriteCommand writeCommand = new WriteCommand();
168: if (!envEntry.getName().equals(STANDARD_TEXT)) {
169: writeCommand.executeWriteCommand(selectedEditPart,
170: modelEntry, envEntry);
171: }
172:
173: // update the table view
174: Iterator<IEnvEntryViewer> iterator = changeListeners.iterator();
175: while (iterator.hasNext())
176: ((IEnvEntryViewer) iterator.next())
177: .updateEnvEntry(envEntry);
178: }
179:
180: /**
181: * delete the dummies in the TableViewer.
182: */
183: private void deleteDummies() {
184: Vector<EnvEntry> v = new Vector<EnvEntry>();
185: for (Iterator<EnvEntry> iter = envEntries.iterator(); iter
186: .hasNext();) {
187: EnvEntry element = (EnvEntry) iter.next();
188: if (element.getName().equals(STANDARD_TEXT)) {
189: v.add(element);
190: }
191: }
192: envEntries.removeAll(v);
193: }
194:
195: /**
196: * @param viewer
197: */
198: public void removeChangeListener(IEnvEntryViewer viewer) {
199: changeListeners.remove(viewer);
200: }
201:
202: /**
203: * @param viewer
204: */
205: public void addChangeListener(IEnvEntryViewer viewer) {
206: changeListeners.add(viewer);
207: }
208: }
|