KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > ui > SectionView


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.multiview.ui;
21
22 import javax.swing.JPanel JavaDoc;
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 /**
30  * This class acts as a container for <code>NodeSectionPanel</code>s. Generally
31  * used with {@link org.netbeans.modules.xml.multiview.ui.SectionPanel}.
32  *
33  * @author mkuchtiak
34  */

35 public class SectionView extends PanelView implements SectionFocusCookie, ContainerPanel {
36     private JPanel JavaDoc scrollPanel, filler;
37     javax.swing.JScrollPane JavaDoc scrollPane;
38     private java.util.Hashtable JavaDoc map;
39     private int sectionCount=0;
40     private NodeSectionPanel activePanel;
41     private InnerPanelFactory factory = null;
42     boolean sectionSelected;
43     
44     
45     /**
46      * Constructs a new SectionView.
47      * @param factory the factory for creating inner panels.
48      */

49     public SectionView(InnerPanelFactory factory) {
50         super();
51         this.factory=factory;
52     }
53     
54     /**
55      * Constructs a new SectionView.
56      */

57     public SectionView() {
58         super();
59     }
60     
61     public void initComponents() {
62         super.initComponents();
63         map = new java.util.Hashtable JavaDoc();
64         setLayout(new java.awt.BorderLayout JavaDoc());
65         scrollPanel = new JPanel JavaDoc();
66         scrollPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
67         scrollPane = new javax.swing.JScrollPane JavaDoc();
68         scrollPane.setViewportView(scrollPanel);
69         scrollPane.getVerticalScrollBar().setUnitIncrement(15);
70         filler = new JPanel JavaDoc();
71         filler.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
72         add(scrollPane, BorderLayout.CENTER);
73     }
74     
75     /**
76      * Opens and activates the given <code>panel</code>.
77      */

78     public boolean focusSection(NodeSectionPanel panel) {
79         panel.open();
80         openParents((JPanel JavaDoc)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 JavaDoc panel){
95         javax.swing.JScrollPane JavaDoc scrollP = null;
96         NodeSectionPanel parentSection=null;
97         java.awt.Container JavaDoc ancestor = panel.getParent();
98         while (ancestor !=null && scrollP == null){
99             if (ancestor instanceof javax.swing.JScrollPane JavaDoc){
100                 scrollP = (javax.swing.JScrollPane JavaDoc) 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     /**
119      * Gets the corresponding <code>NodeSectionPanel</code> for the
120      * given <code>key</code>.
121      * @return the corresponding panel or null.
122      */

123     public NodeSectionPanel getSection(Node key){
124         return (NodeSectionPanel)map.get(key);
125     }
126     
127     /**
128      * Adds a section for this.
129      * @param section the section to be added.
130      * @param open indicates whether given <code>section</code>
131      * should be opened.
132      */

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     /**
143      * Adds a section for this.
144      * @param section the section to be added.
145      */

146     public void addSection(NodeSectionPanel section) {
147         scrollPanel.remove(filler);
148         java.awt.GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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         //gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 6);
155
scrollPanel.add((JPanel JavaDoc)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         //gridBagConstraints.insets = new java.awt.Insets(6, 2, 0, 6);
163
scrollPanel.add(filler,gridBagConstraints);
164         
165         mapSection(section.getNode(), section);
166         sectionCount++;
167     }
168     /**
169      * Removes given <code>node</code> and its corresponding
170      * section.
171      */

172     public void removeSection(Node node) {
173         NodeSectionPanel section = getSection(node);
174         if (section!=null) {
175             // looking for enclosing container
176
java.awt.Container JavaDoc cont = ((java.awt.Component JavaDoc)section).getParent();
177             while (cont!=null && !(cont instanceof ContainerPanel)) {
178                 cont = cont.getParent();
179             }
180             if ( cont!= null) {
181                 // removing last active component
182
ContainerPanel contPanel = (ContainerPanel)cont;
183                 if (section instanceof SectionPanel) {
184                     Object JavaDoc key = ((SectionPanel)section).getKey();
185                     if (key!=null && key==getLastActive()) {
186                         setLastActive(null);
187                     }
188                 }
189                 // removing section
190
contPanel.removeSection(section);
191                 // removing node
192
contPanel.getRoot().getChildren().remove(new Node[]{node});
193             }
194         }
195     }
196     
197     /**
198      * Removes given <code>panel</code> and moves up remaining panels.
199      */

200     public void removeSection(NodeSectionPanel panel){
201         int panelIndex = panel.getIndex();
202         scrollPanel.remove((JPanel JavaDoc)panel);
203         
204         // the rest components have to be moved up
205
java.awt.Component JavaDoc[] components = scrollPanel.getComponents();
206         java.util.AbstractList JavaDoc removedPanels = new java.util.ArrayList JavaDoc();
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 JavaDoc)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 JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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             //gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 6);
227
scrollPanel.add((JPanel JavaDoc)pan,gridBagConstraints);
228         }
229         deleteSection(panel.getNode());
230         sectionCount--;
231     }
232     
233     /**
234      * Sets given <code>activePanel</code> as the currently active panel.
235      */

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     /**
255      * Opens the panels that are associated with the given
256      * <code>nodes</code>.
257      */

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     /**
277      * @return panel with the given <code>key</code> or null
278      * if no matching panel was found.
279      */

280     public SectionPanel findSectionPanel(Object JavaDoc key) {
281         java.util.Enumeration JavaDoc 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     /**
303      * Opens the panel identified by given <code>key</code>.
304      */

305     public void openPanel(Object JavaDoc key) {
306         if (key!=null) {
307             SectionPanel panel = findSectionPanel(key);
308             if (panel!=null) {
309                 if (panel.getInnerPanel()==null) panel.open();
310                 openParents((JPanel JavaDoc)panel);
311                 panel.scroll();
312                 panel.setActive(true);
313             }
314         }
315     }
316     
317     private Object JavaDoc getLastActive() {
318         ToolBarDesignEditor toolBarDesignEditor = getToolBarDesignEditor();
319         return toolBarDesignEditor == null ? null : toolBarDesignEditor.getLastActive();
320     }
321     
322     private void setLastActive(Object JavaDoc 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