1 19 20 package org.netbeans.modules.xml.multiview.ui; 21 22 import javax.swing.JPanel ; 23 import java.awt.*; 24 25 import org.openide.nodes.Node; 26 import org.netbeans.modules.xml.multiview.cookies.SectionFocusCookie; 27 import org.netbeans.modules.xml.multiview.Utils; 28 29 35 public class SectionView extends PanelView implements SectionFocusCookie, ContainerPanel { 36 private JPanel scrollPanel, filler; 37 javax.swing.JScrollPane scrollPane; 38 private java.util.Hashtable map; 39 private int sectionCount=0; 40 private NodeSectionPanel activePanel; 41 private InnerPanelFactory factory = null; 42 boolean sectionSelected; 43 44 45 49 public SectionView(InnerPanelFactory factory) { 50 super(); 51 this.factory=factory; 52 } 53 54 57 public SectionView() { 58 super(); 59 } 60 61 public void initComponents() { 62 super.initComponents(); 63 map = new java.util.Hashtable (); 64 setLayout(new java.awt.BorderLayout ()); 65 scrollPanel = new JPanel (); 66 scrollPanel.setLayout(new java.awt.GridBagLayout ()); 67 scrollPane = new javax.swing.JScrollPane (); 68 scrollPane.setViewportView(scrollPanel); 69 scrollPane.getVerticalScrollBar().setUnitIncrement(15); 70 filler = new JPanel (); 71 filler.setBackground(SectionVisualTheme.getDocumentBackgroundColor()); 72 add(scrollPane, BorderLayout.CENTER); 73 } 74 75 78 public boolean focusSection(NodeSectionPanel panel) { 79 panel.open(); 80 openParents((JPanel )panel); 81 panel.scroll(); 82 setActivePanel(panel); 83 panel.setActive(true); 84 return true; 85 } 86 87 protected void openSection(Node node){ 88 NodeSectionPanel panel = (NodeSectionPanel) map.get(node); 89 if (panel != null) { 90 focusSection(panel); 91 } 92 } 93 94 private void openParents(JPanel panel){ 95 javax.swing.JScrollPane scrollP = null; 96 NodeSectionPanel parentSection=null; 97 java.awt.Container ancestor = panel.getParent(); 98 while (ancestor !=null && scrollP == null){ 99 if (ancestor instanceof javax.swing.JScrollPane ){ 100 scrollP = (javax.swing.JScrollPane ) ancestor; 101 } 102 if (ancestor instanceof NodeSectionPanel){ 103 parentSection = (NodeSectionPanel) ancestor; 104 parentSection.open(); 105 } 106 ancestor = ancestor.getParent(); 107 } 108 } 109 110 void mapSection(Node key, NodeSectionPanel panel){ 111 map.put(key,panel); 112 } 113 114 void deleteSection(Node key){ 115 map.remove(key); 116 } 117 118 123 public NodeSectionPanel getSection(Node key){ 124 return (NodeSectionPanel)map.get(key); 125 } 126 127 133 public void addSection(NodeSectionPanel section, boolean open) { 134 addSection(section); 135 if (open) { 136 section.open(); 137 section.scroll(); 138 section.setActive(true); 139 } 140 } 141 142 146 public void addSection(NodeSectionPanel section) { 147 scrollPanel.remove(filler); 148 java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 149 gridBagConstraints.gridx = 0; 150 gridBagConstraints.gridy = sectionCount; 151 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 152 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 153 gridBagConstraints.weightx = 1.0; 154 scrollPanel.add((JPanel )section,gridBagConstraints); 156 section.setIndex(sectionCount); 157 158 gridBagConstraints.gridx = 0; 159 gridBagConstraints.gridy = sectionCount+1; 160 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 161 gridBagConstraints.weighty = 2.0; 162 scrollPanel.add(filler,gridBagConstraints); 164 165 mapSection(section.getNode(), section); 166 sectionCount++; 167 } 168 172 public void removeSection(Node node) { 173 NodeSectionPanel section = getSection(node); 174 if (section!=null) { 175 java.awt.Container cont = ((java.awt.Component )section).getParent(); 177 while (cont!=null && !(cont instanceof ContainerPanel)) { 178 cont = cont.getParent(); 179 } 180 if ( cont!= null) { 181 ContainerPanel contPanel = (ContainerPanel)cont; 183 if (section instanceof SectionPanel) { 184 Object key = ((SectionPanel)section).getKey(); 185 if (key!=null && key==getLastActive()) { 186 setLastActive(null); 187 } 188 } 189 contPanel.removeSection(section); 191 contPanel.getRoot().getChildren().remove(new Node[]{node}); 193 } 194 } 195 } 196 197 200 public void removeSection(NodeSectionPanel panel){ 201 int panelIndex = panel.getIndex(); 202 scrollPanel.remove((JPanel )panel); 203 204 java.awt.Component [] components = scrollPanel.getComponents(); 206 java.util.AbstractList removedPanels = new java.util.ArrayList (); 207 for (int i=0;i<components.length;i++) { 208 if (components[i] instanceof NodeSectionPanel) { 209 NodeSectionPanel pan = (NodeSectionPanel)components[i]; 210 int index = pan.getIndex(); 211 if (index>panelIndex) { 212 scrollPanel.remove((JPanel )pan); 213 pan.setIndex(index-1); 214 removedPanels.add(pan); 215 } 216 } 217 } 218 for (int i=0;i<removedPanels.size();i++) { 219 NodeSectionPanel pan = (NodeSectionPanel)removedPanels.get(i); 220 java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 221 gridBagConstraints.gridx = 0; 222 gridBagConstraints.gridy = pan.getIndex(); 223 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 224 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 225 gridBagConstraints.weightx = 1.0; 226 scrollPanel.add((JPanel )pan,gridBagConstraints); 228 } 229 deleteSection(panel.getNode()); 230 sectionCount--; 231 } 232 233 236 public void setActivePanel(NodeSectionPanel activePanel) { 237 if (this.activePanel!=null && this.activePanel!=activePanel) { 238 this.activePanel.setActive(false); 239 } 240 this.activePanel = activePanel; 241 if (activePanel instanceof SectionPanel) { 242 setLastActive(((SectionPanel)activePanel).getKey()); 243 } 244 } 245 246 public NodeSectionPanel getActivePanel() { 247 return activePanel; 248 } 249 250 public void selectNode(Node node) { 251 setManagerSelection(new Node[]{node}); 252 } 253 254 258 public void showSelection(org.openide.nodes.Node[] nodes) { 259 if (sectionSelected) { 260 sectionSelected=false; 261 return; 262 } 263 if (nodes!=null && nodes.length>0) { 264 openSection(nodes[0]); 265 } 266 } 267 268 void sectionSelected(boolean sectionSelected) { 269 this.sectionSelected=sectionSelected; 270 } 271 272 protected org.netbeans.modules.xml.multiview.Error validateView() { 273 return null; 274 } 275 276 280 public SectionPanel findSectionPanel(Object key) { 281 java.util.Enumeration en = map.keys(); 282 while (en.hasMoreElements()) { 283 NodeSectionPanel pan = (NodeSectionPanel)map.get(en.nextElement()); 284 if (pan instanceof SectionPanel) { 285 SectionPanel p = (SectionPanel)pan; 286 if (key==p.getKey()) { 287 return p; 288 } 289 } 290 } 291 return null; 292 } 293 294 InnerPanelFactory getInnerPanelFactory() { 295 return factory; 296 } 297 298 public void setInnerPanelFactory(InnerPanelFactory factory) { 299 this.factory=factory; 300 } 301 302 305 public void openPanel(Object key) { 306 if (key!=null) { 307 SectionPanel panel = findSectionPanel(key); 308 if (panel!=null) { 309 if (panel.getInnerPanel()==null) panel.open(); 310 openParents((JPanel )panel); 311 panel.scroll(); 312 panel.setActive(true); 313 } 314 } 315 } 316 317 private Object getLastActive() { 318 ToolBarDesignEditor toolBarDesignEditor = getToolBarDesignEditor(); 319 return toolBarDesignEditor == null ? null : toolBarDesignEditor.getLastActive(); 320 } 321 322 private void setLastActive(Object key) { 323 ToolBarDesignEditor toolBarDesignEditor = getToolBarDesignEditor(); 324 if(toolBarDesignEditor != null) { 325 toolBarDesignEditor.setLastActive(key); 326 } 327 } 328 329 protected ToolBarDesignEditor getToolBarDesignEditor() { 330 Container parent = getParent(); 331 return parent == null ? null : (ToolBarDesignEditor) parent.getParent(); 332 } 333 } 334 | Popular Tags |