001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.design.selection;
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import org.netbeans.modules.bpel.design.model.DiagramModel;
026: import org.netbeans.modules.bpel.design.model.patterns.CompositePattern;
027: import org.netbeans.modules.bpel.design.model.patterns.Pattern;
028: import org.netbeans.modules.bpel.model.api.BpelEntity;
029: import org.netbeans.modules.bpel.model.api.support.UniqueId;
030:
031: public class EntitySelectionModel {
032:
033: private UniqueId selectedID;
034: private DiagramModel model;
035: private Pattern selectedPattern;
036:
037: private List<DiagramSelectionListener> listeners = new ArrayList<DiagramSelectionListener>();
038:
039: // private ArrayList<UniqueId> savedSelection = new ArrayList<UniqueId>();
040:
041: /** Creates a new instance of PatternSelectionModel */
042: public EntitySelectionModel(DiagramModel model) {
043: this .model = model;
044: }
045:
046: public void addSelectionListener(DiagramSelectionListener l) {
047: listeners.add(l);
048: }
049:
050: public void removeSelectionListener(DiagramSelectionListener l) {
051: listeners.remove(l);
052: }
053:
054: /**
055: * method enforces the selection rule: put the selection on element which is visible on diagram
056: */
057: public void setSelectedPattern(Pattern newSelected) {
058: if (newSelected != null) {
059: setSelected(newSelected.getOMReference());
060: } else {
061: setSelected((BpelEntity) null);
062: }
063: }
064:
065: public void setSelected(BpelEntity newSelected) {
066:
067: while (newSelected != null) {
068: Pattern p = model.getPattern(newSelected);
069:
070: if (p != null && p.isSelectable() && p.isInModel()) {
071: break;
072: }
073:
074: newSelected = newSelected.getParent();
075: }
076:
077: UniqueId oldSelectedID = this .selectedID;
078:
079: UniqueId newSelectedID = (newSelected != null) ? newSelected
080: .getUID() : null;
081:
082: if (oldSelectedID != newSelectedID) {
083:
084: this .selectedID = newSelectedID;
085: this .selectedPattern = getSelectedPattern();
086:
087: for (DiagramSelectionListener l : listeners) {
088: l.selectionChanged(model.getEntity(oldSelectedID),
089: newSelected);
090: }
091: }
092: }
093:
094: /**
095: * Handles the situation when currently selected pattern is about to be removed.
096: * Tries to move selection to first available:
097: * 1. next sibling
098: * 2. previous sibling
099: * 3. parent
100: * 4. root
101: *
102: * @param removed - pattern prepared for removal
103: **/
104: public void fixSelection() {
105:
106: //check if currently selected entity is NOT in model
107: if (!(selectedID != null && getSelected() == null)) {
108: return;
109:
110: }
111:
112: CompositePattern parent = (selectedPattern != null) ? selectedPattern
113: .getParent()
114: : null;
115:
116: Pattern next = null;
117: Pattern prev = null;
118:
119: if (parent != null) {
120: Iterator<Pattern> it = parent.getNestedPatterns()
121: .iterator();
122: while (it.hasNext()) {
123: Pattern p = it.next();
124: if (p == selectedPattern) {
125: if (it.hasNext()) {
126: next = it.next();
127: }
128: break;
129: }
130: prev = p;
131: }
132: }
133: if (next != null) {
134: setSelectedPattern(next);
135: } else if (prev != null) {
136: setSelectedPattern(prev);
137: } else if (parent != null) {
138: setSelectedPattern(parent);
139: } else {
140: setSelectedPattern(model.getRootPattern());
141: }
142: }
143:
144: public Pattern getSelectedPattern() {
145: BpelEntity entity = getSelected();
146: return (entity != null) ? model.getPattern(entity) : null;
147: }
148:
149: public UniqueId getSelectedID() {
150: return selectedID;
151:
152: }
153:
154: public BpelEntity getSelected() {
155: return (selectedID != null) ? model.getEntity(selectedID)
156: : null;
157: }
158:
159: public boolean isEmpty() {
160: return selectedID == null;
161: }
162:
163: public void clear() {
164: setSelected((BpelEntity) null);
165: }
166: }
|