001: package org.enhydra.jawe.components.simplenavigator;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005: import java.util.Observable;
006: import java.util.Observer;
007:
008: import javax.swing.JComponent;
009: import javax.swing.event.TreeSelectionEvent;
010: import javax.swing.event.TreeSelectionListener;
011: import javax.swing.tree.TreePath;
012:
013: import org.enhydra.jawe.JaWEComponent;
014: import org.enhydra.jawe.JaWEComponentSettings;
015: import org.enhydra.jawe.JaWEComponentView;
016: import org.enhydra.jawe.JaWEManager;
017: import org.enhydra.jawe.XPDLElementChangeInfo;
018: import org.enhydra.jawe.components.XPDLTreeNode;
019: import org.enhydra.shark.xpdl.XMLCollection;
020: import org.enhydra.shark.xpdl.XMLElement;
021: import org.enhydra.shark.xpdl.XMLElementChangeInfo;
022: import org.enhydra.shark.xpdl.elements.Package;
023:
024: /**
025: * Used to handle events in Package hierarchy tree.
026: *
027: * @author Sasa Bojanic
028: */
029: public class SimpleNavigator implements JaWEComponent, Observer,
030: TreeSelectionListener {
031:
032: protected String type = JaWEComponent.TREE_COMPONENT;
033: protected SimpleNavigatorPanel panel;
034:
035: protected boolean updateInProgress = false;
036:
037: protected SimpleNavigatorSettings settings;
038:
039: public SimpleNavigator(JaWEComponentSettings settings)
040: throws Exception {
041: this .settings = (SimpleNavigatorSettings) settings;
042: this .settings.init(this );
043:
044: init();
045: JaWEManager.getInstance().getJaWEController().addObserver(this );
046: }
047:
048: public JaWEComponentSettings getSettings() {
049: return settings;
050: }
051:
052: public SimpleNavigatorSettings getNavigatorSettings() {
053: return settings;
054: }
055:
056: public void update(Observable o, Object arg) {
057: if (!(arg instanceof XPDLElementChangeInfo))
058: return;
059: XPDLElementChangeInfo info = (XPDLElementChangeInfo) arg;
060: int action = info.getAction();
061: if (!(action == XMLElementChangeInfo.UPDATED
062: || action == XMLElementChangeInfo.INSERTED
063: || action == XMLElementChangeInfo.REMOVED
064: || action == XMLElementChangeInfo.REPOSITIONED
065: || action == XPDLElementChangeInfo.SELECTED
066: || action == XPDLElementChangeInfo.UNDOABLE_ACTION_ENDED
067: || action == XPDLElementChangeInfo.UNDO || action == XPDLElementChangeInfo.REDO))
068: return;
069:
070: long start = System.currentTimeMillis();
071: JaWEManager.getInstance().getLoggingManager().info(
072: "SimpleNavigator -> update for event " + info
073: + " started ...");
074: if (action == XPDLElementChangeInfo.UNDOABLE_ACTION_ENDED
075: || action == XPDLElementChangeInfo.UNDO
076: || action == XPDLElementChangeInfo.REDO) {
077: for (int i = 0; i < info.getChangedSubElements().size(); i++) {
078: update((XPDLElementChangeInfo) info
079: .getChangedSubElements().get(i));
080: }
081: } else {
082: update(info);
083: }
084: JaWEManager.getInstance().getLoggingManager().info(
085: "SimpleNavigator -> update ended...");
086: long end = System.currentTimeMillis();
087: double diffs = (end - start) / 1000.0;
088: JaWEManager.getInstance().getLoggingManager().debug(
089: "THE UPDATE OF NAVIG COMPONENT LASTED FOR " + diffs
090: + " SECONDS!");
091: }
092:
093: public void update(XPDLElementChangeInfo info) {
094: if (updateInProgress)
095: return;
096: if (info.getSource() == this ) {
097: return;
098: }
099:
100: updateInProgress = true;
101: try {
102: panel.handleXPDLChangeEvent(info);
103: } finally {
104: updateInProgress = false;
105: }
106: }
107:
108: protected void init() {
109: panel = new SimpleNavigatorPanel(this );
110: panel.configure();
111: }
112:
113: public void valueChanged(TreeSelectionEvent e) {
114: if (updateInProgress)
115: return;
116: JaWEManager.getInstance().getLoggingManager().info(
117: "SimpleNavigator -> selection changed ...");
118:
119: TreePath oldsel = e.getOldLeadSelectionPath();
120:
121: TreePath[] sel = e.getPaths();
122: List selection = new ArrayList();
123: boolean hasAdded = false;
124: boolean hasRemoved = false;
125:
126: int j = 0;
127: for (int i = 0; i < sel.length; i++) {
128: if (sel[i] == e.getNewLeadSelectionPath()) {
129: j = i;
130: break;
131: }
132: }
133:
134: TreePath temp = sel[j];
135: sel[j] = sel[sel.length - 1];
136: sel[sel.length - 1] = temp;
137:
138: for (int i = 0; i < sel.length; i++) {
139: if (e.isAddedPath(sel[i]))
140: hasAdded = true;
141: else
142: hasRemoved = true;
143: }
144:
145: if (oldsel == null) {
146: hasAdded = false;
147: hasRemoved = false;
148: }
149:
150: for (int i = 0; i < sel.length; i++) {
151: if (e.isAddedPath(sel[i])) {
152: XPDLTreeNode node = (XPDLTreeNode) sel[i]
153: .getLastPathComponent();
154: if (node == null || node.getXPDLElement() == null)
155: return;
156: selection.add(node.getXPDLElement());
157: }
158: }
159:
160: if (hasAdded && hasRemoved) {
161: JaWEManager.getInstance().getJaWEController()
162: .getSelectionManager()
163: .setSelection(selection, true);
164: } else if (hasAdded && !hasRemoved) {
165: JaWEManager.getInstance().getJaWEController()
166: .getSelectionManager().addToSelection(selection);
167: } else {
168: selection.clear();
169: if (panel.tree.isSelectionEmpty()) {
170: selection.add(((XPDLTreeNode) sel[0]
171: .getLastPathComponent()).getXPDLElement());
172: } else {
173: sel = panel.tree.getSelectionPaths();
174: for (int i = 0; i < sel.length; i++) {
175: XPDLTreeNode node = (XPDLTreeNode) sel[i]
176: .getLastPathComponent();
177: if (node == null || node.getXPDLElement() == null)
178: return;
179: selection.add(node.getXPDLElement());
180: }
181: }
182: JaWEManager.getInstance().getJaWEController()
183: .getSelectionManager()
184: .setSelection(selection, true);
185: }
186: JaWEManager
187: .getInstance()
188: .getLoggingManager()
189: .info(
190: "SimpleNavigator -> observers notified about selection changed!");// to " + node + ", xpdlElement=";
191: // + node.getXPDLElement().toName() + ", Id="
192: // + ((XMLComplexElement) node.getXPDLElement()).get("Id").toValue());
193: }
194:
195: public JaWEComponentView getView() {
196: return panel;
197: }
198:
199: public JComponent getDisplay() {
200: return panel.getDisplay();
201: }
202:
203: public String getType() {
204: return type;
205: }
206:
207: public void setType(String type) {
208: this .type = type;
209: }
210:
211: public String getName() {
212: return "SimpleNavigatorComponent";
213: }
214:
215: public boolean adjustXPDL(Package pkg) {
216: return false;
217: }
218:
219: public List checkValidity(XMLElement el, boolean fullCheck) {
220: return null;
221: }
222:
223: public boolean canCreateElement(XMLCollection col) {
224: return true;
225: }
226:
227: public boolean canInsertElement(XMLCollection col, XMLElement el) {
228: return true;
229: }
230:
231: public boolean canModifyElement(XMLElement el) {
232: return true;
233: }
234:
235: public boolean canRemoveElement(XMLCollection col, XMLElement el) {
236: return true;
237: }
238:
239: public boolean canDuplicateElement(XMLCollection col, XMLElement el) {
240: return true;
241: }
242:
243: public boolean canRepositionElement(XMLCollection col, XMLElement el) {
244: return true;
245: }
246:
247: public void setUpdateInProgress(boolean inProgress) {
248: updateInProgress = inProgress;
249: }
250:
251: public boolean isUpdateInProgress() {
252: return updateInProgress;
253: }
254:
255: }
|