KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.*;
23 import java.util.*;
24 import java.net.*;
25
26 import javax.swing.*;
27 import javax.swing.border.*;
28 import javax.swing.event.*;
29 import org.openide.nodes.Node;
30
31 import org.netbeans.modules.xml.multiview.Utils;
32
33 /**
34  * A container for <code>NodeSectionPanel</code>s. Provides fold button and support
35  * for defining "header actions", i.e. top level actions by means of <code>setHeaderActions()</code>
36  * method.
37  *
38  *
39  * @author mkuchtiak
40  */

41 public class SectionContainer extends javax.swing.JPanel JavaDoc implements NodeSectionPanel, ContainerPanel {
42
43     //private HashMap map = new HashMap();
44
//private JScrollPane scrollPane;
45
private Node activeNode=null;
46     private SectionView sectionView;
47     private String JavaDoc title;
48     private Node root;
49     private boolean active;
50     private int sectionCount=0;
51     private int index;
52     private boolean foldable;
53
54     public SectionContainer(SectionView sectionView, Node root, String JavaDoc title) {
55         this(sectionView, root, title, true);
56     }
57     
58     public SectionContainer(SectionView sectionView, Node root, String JavaDoc title, boolean foldable) {
59         this.sectionView=sectionView;
60         this.root=root;
61         this.title=title;
62         this.foldable=foldable;
63         initComponents();
64         setBackground(SectionVisualTheme.getDocumentBackgroundColor());
65         headerSeparator.setForeground(SectionVisualTheme.getSectionHeaderLineColor());
66         titlePanel.setBackground(foldable?SectionVisualTheme.getSectionHeaderColor():SectionVisualTheme.getContainerHeaderColor());
67         actionPanel.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
68         fillerLine.setForeground(SectionVisualTheme.getFoldLineColor());
69         fillerEnd.setForeground(SectionVisualTheme.getFoldLineColor());
70         titleButton.setText(title);
71         titleButton.setFont(new Font(getFont().getFontName(), Font.BOLD, getFont().getSize() + 2));
72         titleButton.addMouseListener(new org.openide.awt.MouseUtils.PopupMouseAdapter() {
73             protected void showPopup(java.awt.event.MouseEvent JavaDoc e) {
74                 JPopupMenu popup = getNode().getContextMenu();
75                 popup.show(titleButton,e.getX(), e.getY());
76             }
77         });
78         
79         if (foldable) {
80             foldButton.setSelected(true);
81         } else {
82             remove(fillerLine);
83             remove(fillerEnd);
84             remove(foldButton);
85         }
86         setIcon(true);
87     }
88     
89     /** Method from NodeSectionPanel interface */
90     public Node getNode() {
91         return root;
92     }
93     /** Method from ContainerPanel interface */
94     public Node getRoot() {
95         return root;
96     }
97     
98     /** Method from NodeSectionPanel interface */
99     public void open(){
100         if (foldable) {
101             foldButton.setSelected(true);
102             contentPanel.setVisible(true);
103             fillerLine.setVisible(true);
104             fillerEnd.setVisible(true);
105             setIcon(true);
106         }
107     }
108
109     /** Method from NodeSectionPanel interface */
110     public void scroll() {
111         Utils.scrollToVisible(this);
112     }
113     
114     /** Method from NodeSectionPanel interface */
115     public void setActive(boolean active) {
116         titlePanel.setBackground(active?SectionVisualTheme.getSectionHeaderActiveColor():
117             (foldable?SectionVisualTheme.getSectionHeaderColor():SectionVisualTheme.getContainerHeaderColor()));
118         if (active && !this.equals(sectionView.getActivePanel())) {
119             sectionView.sectionSelected(true);
120             sectionView.setActivePanel(this);
121             sectionView.selectNode(root);
122         }
123         this.active=active;
124     }
125     
126     /** Method from NodeSectionPanel interface */
127     public boolean isActive() {
128         return active;
129     }
130
131     /** Maps section to a node
132     */

133     private void mapSection(Node key, NodeSectionPanel panel){
134         sectionView.mapSection(key,panel);
135     }
136     
137     /** Maps section to a node
138     */

139     private void deleteSection(Node key){
140         sectionView.deleteSection(key);
141     }
142     
143     /** Method from ContainerPanel interface */
144     public NodeSectionPanel getSection(Node key){
145         return sectionView.getSection(key);
146     }
147     
148     public void addSection(NodeSectionPanel section, boolean open) {
149         addSection(section);
150         if (open) {
151             section.open();
152             section.scroll();
153             section.setActive(true);
154         }
155     }
156     
157     /** Method from ContainerPanel interface */
158     public void addSection(NodeSectionPanel section){
159         GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
160         gridBagConstraints.gridx = 0;
161         gridBagConstraints.gridy = sectionCount;
162         gridBagConstraints.weightx = 1.0;
163         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
164         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
165         gridBagConstraints.weightx = 1.0;
166         contentPanel.add((JPanel)section,gridBagConstraints);
167         section.setIndex(sectionCount);
168         mapSection(section.getNode(), section);
169         sectionCount++;
170     }
171     /** Method from ContainerPanel interface */
172     public void removeSection(NodeSectionPanel section) {
173         int panelIndex = section.getIndex();
174         contentPanel.remove((JPanel)section);
175         
176         // the rest components have to be moved up
177
java.awt.Component JavaDoc[] components = contentPanel.getComponents();
178         java.util.AbstractList JavaDoc removedPanels = new java.util.ArrayList JavaDoc();
179         for (int i=0;i<components.length;i++) {
180             if (components[i] instanceof NodeSectionPanel) {
181                 NodeSectionPanel pan = (NodeSectionPanel)components[i];
182                 int index = pan.getIndex();
183                 if (index>panelIndex) {
184                     contentPanel.remove((JPanel)pan);
185                     pan.setIndex(index-1);
186                     removedPanels.add(pan);
187                 }
188             }
189         }
190         for (int i=0;i<removedPanels.size();i++) {
191             NodeSectionPanel pan = (NodeSectionPanel)removedPanels.get(i);
192             java.awt.GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
193             gridBagConstraints.gridx = 0;
194             gridBagConstraints.gridy = pan.getIndex();
195             gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
196             gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
197             gridBagConstraints.weightx = 1.0;
198             //gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 6);
199
contentPanel.add((JPanel)pan,gridBagConstraints);
200         }
201         deleteSection(section.getNode());
202         sectionCount--;
203     }
204     
205     /** This method is called from within the constructor to
206      * initialize the form.
207      * WARNING: Do NOT modify this code. The content of this method is
208      * always regenerated by the Form Editor.
209      */

210     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
211
private void initComponents() {
212         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
213
214         foldButton = new javax.swing.JToggleButton JavaDoc();
215         headerSeparator = new javax.swing.JSeparator JavaDoc();
216         contentPanel = new javax.swing.JPanel JavaDoc();
217         actionPanel = new javax.swing.JPanel JavaDoc();
218         fillerLine = new javax.swing.JSeparator JavaDoc();
219         fillerEnd = new javax.swing.JSeparator JavaDoc();
220         titlePanel = new javax.swing.JPanel JavaDoc();
221         titleButton = new javax.swing.JButton JavaDoc();
222
223         setLayout(new java.awt.GridBagLayout JavaDoc());
224
225         foldButton.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/org/netbeans/modules/xml/multiview/resources/plus.gif")));
226         foldButton.setBorder(null);
227         foldButton.setBorderPainted(false);
228         foldButton.setContentAreaFilled(false);
229         foldButton.setFocusPainted(false);
230         foldButton.setSelectedIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/org/netbeans/modules/xml/multiview/resources/minus.gif")));
231         foldButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
232             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
233                 foldButtonActionPerformed(evt);
234             }
235         });
236
237         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
238         gridBagConstraints.gridx = 0;
239         gridBagConstraints.gridy = 0;
240         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
241         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 2, 0, 2);
242         add(foldButton, gridBagConstraints);
243
244         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
245         gridBagConstraints.gridx = 1;
246         gridBagConstraints.gridy = 1;
247         gridBagConstraints.gridwidth = 2;
248         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
249         gridBagConstraints.weightx = 1.0;
250         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 0, 0, 0);
251         add(headerSeparator, gridBagConstraints);
252
253         contentPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
254
255         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
256         gridBagConstraints.gridx = 1;
257         gridBagConstraints.gridy = 2;
258         gridBagConstraints.gridwidth = 2;
259         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
260         gridBagConstraints.weightx = 1.0;
261         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 0, 0, 0);
262         add(contentPanel, gridBagConstraints);
263
264         actionPanel.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.RIGHT, 2, 0));
265
266         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
267         gridBagConstraints.gridx = 2;
268         gridBagConstraints.gridy = 0;
269         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
270         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 0);
271         add(actionPanel, gridBagConstraints);
272
273         fillerLine.setOrientation(javax.swing.SwingConstants.VERTICAL);
274         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
275         gridBagConstraints.gridx = 0;
276         gridBagConstraints.gridy = 1;
277         gridBagConstraints.gridheight = 2;
278         gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
279         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
280         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 0, 0);
281         add(fillerLine, gridBagConstraints);
282
283         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
284         gridBagConstraints.gridx = 0;
285         gridBagConstraints.gridy = 3;
286         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
287         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
288         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 0, 2);
289         add(fillerEnd, gridBagConstraints);
290
291         titlePanel.setLayout(new java.awt.BorderLayout JavaDoc());
292
293         titleButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
294         titleButton.setBorderPainted(false);
295         titleButton.setContentAreaFilled(false);
296         titleButton.setFocusPainted(false);
297         titleButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
298         titleButton.setMargin(new java.awt.Insets JavaDoc(0, 4, 0, 4));
299         titleButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
300             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
301                 titleButtonActionPerformed(evt);
302             }
303         });
304
305         titlePanel.add(titleButton, java.awt.BorderLayout.CENTER);
306
307         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
308         gridBagConstraints.gridx = 1;
309         gridBagConstraints.gridy = 0;
310         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
311         gridBagConstraints.weightx = 1.0;
312         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 0);
313         add(titlePanel, gridBagConstraints);
314
315     }// </editor-fold>//GEN-END:initComponents
316

317     private void titleButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_titleButtonActionPerformed
318
// TODO add your handling code here:
319
if (foldable) {
320             if (!foldButton.isSelected()) {
321                 open();
322                 foldButton.setSelected(true);
323             } else {
324                 if (isActive()) {
325                     foldButton.setSelected(false);
326                     contentPanel.setVisible(false);
327                     fillerLine.setVisible(false);
328                     fillerEnd.setVisible(false);
329                     setIcon(false);
330                 }
331             }
332         }
333         if (!isActive()) setActive(true);
334     }//GEN-LAST:event_titleButtonActionPerformed
335

336     private void foldButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_foldButtonActionPerformed
337
// TODO add your handling code here:
338
contentPanel.setVisible(foldButton.isSelected());
339         fillerLine.setVisible(foldButton.isSelected());
340         fillerEnd.setVisible(foldButton.isSelected());
341         setIcon(foldButton.isSelected());
342     }//GEN-LAST:event_foldButtonActionPerformed
343

344     
345     // Variables declaration - do not modify//GEN-BEGIN:variables
346
private javax.swing.JPanel JavaDoc actionPanel;
347     private javax.swing.JPanel JavaDoc contentPanel;
348     private javax.swing.JSeparator JavaDoc fillerEnd;
349     private javax.swing.JSeparator JavaDoc fillerLine;
350     private javax.swing.JToggleButton JavaDoc foldButton;
351     private javax.swing.JSeparator JavaDoc headerSeparator;
352     private javax.swing.JButton JavaDoc titleButton;
353     private javax.swing.JPanel JavaDoc titlePanel;
354     // End of variables declaration//GEN-END:variables
355

356     /** Method from NodeSectionPanel interface */
357     public void setIndex(int index) {
358         this.index=index;
359     }
360     
361     /** Method from NodeSectionPanel interface */
362     public int getIndex() {
363         return index;
364     }
365     
366     private javax.swing.JButton JavaDoc[] headerButtons;
367     
368     public void setHeaderActions(Action[] actions) {
369         headerButtons = new javax.swing.JButton JavaDoc[actions.length];
370         for (int i=0;i<actions.length;i++) {
371             headerButtons[i] = new javax.swing.JButton JavaDoc(actions[i]);
372             headerButtons[i].setMargin(new java.awt.Insets JavaDoc(0,14,0,14));
373             headerButtons[i].setOpaque(false);
374             actionPanel.add(headerButtons[i]);
375         }
376     }
377     
378     public javax.swing.JButton JavaDoc[] getHeaderButtons(){
379         return headerButtons;
380     }
381     
382     public boolean isFoldable() {
383         return foldable;
384     }
385     
386     private void setIcon(boolean opened) {
387         java.awt.Image JavaDoc image=null;
388         if (opened)
389             image = (root == null ? null : root.getOpenedIcon(java.beans.BeanInfo.ICON_COLOR_16x16));
390         else
391             image = (root == null ? null : root.getIcon(java.beans.BeanInfo.ICON_COLOR_16x16));
392         if (image != null) {
393             titleButton.setIcon(new javax.swing.ImageIcon JavaDoc(image));
394         }
395     }
396
397     
398 }
399
Popular Tags