1 19 20 package org.netbeans.core.multiview; 21 22 import java.beans.PropertyChangeListener ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Set ; 31 import java.util.TreeMap ; 32 import javax.swing.ButtonGroup ; 33 import javax.swing.ButtonModel ; 34 import org.netbeans.core.api.multiview.MultiViewPerspective; 35 import org.netbeans.core.spi.multiview.MultiViewDescription; 36 import org.netbeans.core.spi.multiview.MultiViewElement; 37 import org.netbeans.core.spi.multiview.MultiViewElementCallback; 38 39 import org.openide.windows.TopComponent; 40 41 46 47 class MultiViewModel { 48 49 private MultiViewDescription currentEditor; 50 private Map nestedElements; private Map nestedCallbacks; private Map nestedPerspectives; private MultiViewDescription[] descriptions; 55 private ButtonGroup group; 56 private Collection shownElements; 57 private ArrayList listeners; 58 private ActionRequestObserverFactory observerFactory; 59 60 MultiViewModel(MultiViewDescription[] descs, MultiViewDescription defaultDescr, 61 MultiViewModel.ActionRequestObserverFactory factory) { 62 this(descs, defaultDescr, factory, Collections.EMPTY_MAP); 63 } 64 65 68 MultiViewModel(MultiViewDescription[] descs, MultiViewDescription defaultDescr, 69 MultiViewModel.ActionRequestObserverFactory factory, Map existingElements) { 70 observerFactory = factory; 71 nestedElements = new HashMap (); 72 nestedPerspectives = new HashMap (); 74 nestedCallbacks = new HashMap (); 75 shownElements = new HashSet (descs.length + 3); 76 descriptions = descs; 77 this.group = group; 78 for (int i = 0; i < descriptions.length; i++) { 79 MultiViewElement element = (MultiViewElement)existingElements.get(descriptions[i]); 80 nestedElements.put(descriptions[i], element); 81 nestedPerspectives.put(descriptions[i], Accessor.DEFAULT.createPerspective(descriptions[i])); 82 if (element != null) { 83 MultiViewElementCallback call = factory.createElementCallback(descriptions[i]); 85 nestedCallbacks.put(element, call); 86 element.setMultiViewCallback(call); 87 } 89 } 90 currentEditor = (defaultDescr == null || !nestedElements.containsKey(defaultDescr) ? descriptions[0] : defaultDescr); 91 group = new BtnGroup(); 92 } 93 94 95 void setActiveDescription(MultiViewDescription description) { 96 if (currentEditor == description) return; 97 MultiViewDescription old = currentEditor; 98 currentEditor = description; 99 fireSelectionChanged(old, description); 100 } 101 102 MultiViewDescription getActiveDescription() { 103 return currentEditor; 104 } 105 106 MultiViewElement getActiveElement() { 107 return getActiveElement(true); 108 } 109 110 MultiViewElement getActiveElement(boolean createIfNotCreatedYet) { 111 return getElementForDescription(currentEditor, createIfNotCreatedYet); 112 } 113 114 117 synchronized Collection getCreatedElements() { 118 Collection col = new ArrayList (nestedElements.size()); 119 Iterator it = nestedElements.entrySet().iterator(); 120 while (it.hasNext()) { 121 Map.Entry entry = (Map.Entry )it.next(); 122 if (entry.getValue() != null) { 123 col.add(entry.getValue()); 124 } 125 } 126 return col; 127 } 128 129 133 boolean wasShownBefore(MultiViewElement element) { 134 return shownElements.contains(element); 135 } 136 139 void markAsShown(MultiViewElement element) { 140 shownElements.add(element); 141 } 142 143 146 147 void markAsHidden(MultiViewElement element) { 148 shownElements.remove(element); 149 } 150 151 154 MultiViewDescription[] getDescriptions() { 155 return descriptions; 156 } 157 158 MultiViewPerspective[] getPerspectives() { 159 MultiViewPerspective[] toReturn = new MultiViewPerspective[descriptions.length]; 160 for (int i = 0; i < descriptions.length; i++) { 161 toReturn[i] = (MultiViewPerspective)nestedPerspectives.get(descriptions[i]); 162 } 163 return toReturn; 164 } 165 166 MultiViewPerspective getSelectedPerspective() { 167 return (MultiViewPerspective)nestedPerspectives.get(getActiveDescription()); 168 } 169 170 174 177 ButtonGroup getButtonGroup() { 178 return group; 179 } 180 181 MultiViewElement getElementForDescription(MultiViewDescription description) { 182 return getElementForDescription(description, true); 183 } 184 185 188 synchronized MultiViewElement getElementForDescription(MultiViewDescription description, boolean create) { 189 MultiViewElement element = (MultiViewElement)nestedElements.get(description); 190 if (element == null && create) { 191 element = description.createElement(); 192 MultiViewElementCallback call = observerFactory.createElementCallback(description); 193 nestedCallbacks.put(element, call); 194 element.setMultiViewCallback(call); 195 nestedElements.put(description, element); 196 } 198 return element; 199 } 200 201 synchronized MultiViewElementCallback getCallbackForElement(MultiViewElement elem) { 202 return (MultiViewElementCallback)nestedCallbacks.get(elem); 203 } 204 205 206 void addElementSelectionListener(ElementSelectionListener listener) { 207 if (listeners == null) { 208 listeners = new ArrayList (); 209 } 210 synchronized (listeners) { 211 listeners.add(listener); 212 } 213 } 214 215 void removeElementSelectionListener(ElementSelectionListener listener) { 216 if (listeners == null) { 217 listeners = new ArrayList (); 218 } 219 synchronized (listeners) { 220 listeners.remove(listener); 221 } 222 } 223 224 private void fireSelectionChanged(MultiViewDescription oldOne, MultiViewDescription newOne) { 225 if (listeners != null) { 226 synchronized (listeners) { 227 Iterator it = listeners.iterator(); 228 while (it.hasNext()) { 229 ElementSelectionListener list = (ElementSelectionListener)it.next(); 230 list.selectionChanged(oldOne, newOne); 231 } 232 } 233 } 234 } 235 236 void fireActivateCurrent() { 237 if (listeners != null) { 238 synchronized (listeners) { 239 Iterator it = listeners.iterator(); 240 while (it.hasNext()) { 241 ElementSelectionListener list = (ElementSelectionListener)it.next(); 242 list.selectionActivatedByButton(); 243 } 244 } 245 } 246 } 247 248 public String toString() { 249 return "current=" + currentEditor; } 251 252 255 static interface ElementSelectionListener { 256 257 public void selectionChanged(MultiViewDescription oldOne, MultiViewDescription newOne); 258 259 public void selectionActivatedByButton(); 260 } 261 262 265 static interface ActionRequestObserverFactory { 266 MultiViewElementCallback createElementCallback(MultiViewDescription desc); 267 } 268 269 272 273 private class BtnGroup extends ButtonGroup { 274 275 public void setSelected(ButtonModel m, boolean b) { 276 super.setSelected(m, b); 277 if (getSelection() instanceof TabsComponent.TabsButtonModel) { 278 TabsComponent.TabsButtonModel mod = (TabsComponent.TabsButtonModel)m; 279 MultiViewDescription desc = mod.getButtonsDescription(); 280 setActiveDescription(desc); 281 } 282 } 283 284 } 285 286 287 } | Popular Tags |