KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.netbeans.modules.xml.multiview.ui;
22
23 import org.netbeans.modules.xml.multiview.cookies.ErrorLocator;
24 import org.netbeans.modules.xml.multiview.Utils;
25 import org.openide.nodes.Node;
26
27 import javax.swing.*;
28 import java.awt.*;
29 import java.awt.event.FocusAdapter JavaDoc;
30 import java.awt.event.FocusEvent JavaDoc;
31 import java.awt.event.FocusListener JavaDoc;
32 import java.beans.VetoableChangeListener JavaDoc;
33 import java.beans.PropertyChangeEvent JavaDoc;
34 import java.beans.PropertyVetoException JavaDoc;
35
36 /**
37  * This class represents a panel of a section that can be expanded / collapsed.
38  * Wraps <code>SectionView</code> and controls opening and closing of
39  * the associated inner panels (<code>SectionInnerPanel</code>).
40  *
41  * @author mkuchtiak
42  */

43 public class SectionPanel extends javax.swing.JPanel JavaDoc implements NodeSectionPanel, ErrorLocator {
44     
45     private SectionView sectionView;
46     private String JavaDoc title;
47     private Node node;
48     private boolean active;
49     private SectionInnerPanel innerPanel;
50     private Object JavaDoc key;
51     private int index;
52     
53     private FocusListener JavaDoc sectionFocusListener = new FocusAdapter JavaDoc() {
54         public void focusGained(FocusEvent JavaDoc e) {
55             setActive(true);
56         }
57     };
58     private ToolBarDesignEditor toolBarDesignEditor;
59     
60     /**
61      * Constructs a new section panel whose title will be set to display name
62      * of the given <code>explorerNode</code> and that will not be expanded
63      * by default.
64      * @param sectionView the section view for this panel
65      * @param explorerNode the node for this panel
66      * @param key the key that identifies the panel
67      */

68     public SectionPanel(SectionView sectionView, Node explorerNode, Object JavaDoc key) {
69         this(sectionView, explorerNode, key, false);
70     }
71     
72     /**
73      * Constructs a new section panel whose title will be set to display name
74      * of the given <code>explorerNode</code>.
75      * @param sectionView the section view for this panel
76      * @param explorerNode the node for this panel
77      * @param key the key that identifies the panel
78      * @param autoExpand defines whether the section should be expanded by default
79      */

80     public SectionPanel(SectionView sectionView, Node explorerNode, Object JavaDoc key, boolean autoExpand) {
81         this(sectionView, explorerNode, explorerNode.getDisplayName(), key, autoExpand);
82     }
83     
84     /**
85      * Constructs a new section panel that is not expanded by default.
86      * @param sectionView the section view for this panel
87      * @param node the node for this panel
88      * @param title the title for this panel
89      * @param key the key that identifies the panel
90      */

91     public SectionPanel(SectionView sectionView, Node node, String JavaDoc title, Object JavaDoc key) {
92         this(sectionView, node, title, key, false);
93     }
94     
95     /**
96      * Constructs a new section panel.
97      * @param sectionView the section view for this panel
98      * @param node the node for this panel
99      * @param title the title for this panel
100      * @param key the key that identifies the panel
101      * @param autoExpand defines whether the section should be expanded by default
102      * @param addActionListener defines whether a focus listener that activates
103      * section when focus is gained should be attached to the title button of
104      * this panel.
105      *
106      */

107     public SectionPanel(SectionView sectionView, Node node, String JavaDoc title,
108             Object JavaDoc key, boolean autoExpand, boolean addFocusListenerToButton) {
109         
110         this.sectionView = sectionView;
111         this.title = title;
112         this.node = node;
113         this.key = key;
114         
115         initComponents();
116         headerSeparator.setForeground(SectionVisualTheme.getSectionHeaderLineColor());
117         fillerLine.setForeground(SectionVisualTheme.getFoldLineColor());
118         fillerEnd.setForeground(SectionVisualTheme.getFoldLineColor());
119         fillerLine.setVisible(false);
120         fillerEnd.setVisible(false);
121         setBackground(SectionVisualTheme.getDocumentBackgroundColor());
122         titlePanel.setBackground(SectionVisualTheme.getSectionHeaderColor());
123         actionPanel.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
124         titleButton.setText(title);
125         titleButton.setFont(new Font(getFont().getFontName(), Font.BOLD, getFont().getSize() + 2));
126         titleButton.addMouseListener(new org.openide.awt.MouseUtils.PopupMouseAdapter() {
127             protected void showPopup(java.awt.event.MouseEvent JavaDoc e) {
128                 if (!SectionPanel.this.isActive()) {
129                     SectionPanel.this.setActive(true);
130                 }
131                 JPopupMenu popup = getNode().getContextMenu();
132                 popup.show(titleButton, e.getX(), e.getY());
133             }
134         });
135         if(autoExpand) {
136             open();
137         }
138         if (addFocusListenerToButton){
139             titleButton.addFocusListener(sectionFocusListener);
140         }
141     }
142     
143     
144     /**
145      * Constructs a new section panel.
146      * @param sectionView the section view for this panel
147      * @param node the node for this panel
148      * @param title the title for this panel
149      * @param key the key that identifies the panel
150      * @param autoExpand defines whether the section should be expanded by default
151      *
152      */

153     public SectionPanel(SectionView sectionView, Node node, String JavaDoc title, Object JavaDoc key, boolean autoExpand) {
154         this(sectionView, node, title, key, autoExpand, true);
155     }
156     
157     public SectionView getSectionView() {
158         return sectionView;
159     }
160     
161     protected void openInnerPanel() {
162         if (toolBarDesignEditor == null) {
163             toolBarDesignEditor = sectionView.getToolBarDesignEditor();
164             if (toolBarDesignEditor != null) {
165                 toolBarDesignEditor.addVetoableChangeListener(new VetoableChangeListener JavaDoc() {
166                     public void vetoableChange(PropertyChangeEvent JavaDoc evt) throws PropertyVetoException JavaDoc {
167                         if (ToolBarDesignEditor.PROPERTY_FLUSH_DATA.equals(evt.getPropertyName()) &&
168                                 evt.getNewValue() == null) {
169                             if (innerPanel != null && !innerPanel.canClose()) {
170                                 throw new PropertyVetoException JavaDoc("", evt);
171                             }
172                         }
173                     }
174                 });
175             }
176         }
177         if (innerPanel != null) {
178             return;
179         }
180         innerPanel = createInnerpanel();
181         java.awt.GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
182         gridBagConstraints.gridx = 1;
183         gridBagConstraints.gridy = 2;
184         gridBagConstraints.gridwidth = 2;
185         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
186         gridBagConstraints.weightx = 1.0;
187         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 0, 0, 0);
188         fillerLine.setVisible(true);
189         fillerEnd.setVisible(true);
190         innerPanel.addFocusListener(sectionFocusListener);
191         add(innerPanel, gridBagConstraints);
192         Utils.scrollToVisible(this);
193         innerPanel.setBackground(
194                 active ? SectionVisualTheme.getSectionActiveBackgroundColor() : SectionVisualTheme.getDocumentBackgroundColor());
195     }
196     
197     protected SectionInnerPanel createInnerpanel() {
198         return sectionView.getInnerPanelFactory().createInnerPanel(key);
199     }
200     
201     protected void closeInnerPanel() {
202         if (innerPanel != null) {
203             innerPanel.removeFocusListener(sectionFocusListener);
204             remove(innerPanel);
205             innerPanel = null;
206         }
207         fillerLine.setVisible(false);
208         fillerEnd.setVisible(false);
209     }
210     
211     public String JavaDoc getTitle() {
212         return title;
213     }
214     
215     public void setTitle(String JavaDoc title) {
216         titleButton.setText(title);
217         this.title = title;
218     }
219     
220     /**
221      * Method from NodeSectionPanel interface
222      */

223     public Node getNode() {
224         return node;
225     }
226     
227     /**
228      * Method from NodeSectionPanel interface
229      */

230     public void open() {
231         foldButton.setSelected(true);
232         openInnerPanel();
233     }
234     
235     /**
236      * Method from NodeSectionPanel interface
237      */

238     public void scroll() {
239         Utils.scrollToVisible(this);
240     }
241     
242     /**
243      * Method from NodeSectionPanel interface
244      */

245     public void setActive(boolean active) {
246         //System.out.println("setActive = "+active +":"+node.getDisplayName());
247
titlePanel.setBackground(
248                 active ? SectionVisualTheme.getSectionHeaderActiveColor() : SectionVisualTheme.getSectionHeaderColor());
249         //headerSeparator.setVisible(!active);
250
if (innerPanel!=null) innerPanel.setBackground(
251                 active ? SectionVisualTheme.getSectionActiveBackgroundColor() : SectionVisualTheme.getDocumentBackgroundColor());
252         if (headerButtons!=null) {
253             for (int i=0;i<headerButtons.length;i++) headerButtons[i].setEnabled(active);
254         }
255         if (active && !this.equals(sectionView.getActivePanel())) {
256             sectionView.sectionSelected(true);
257             sectionView.setActivePanel(this);
258             sectionView.selectNode(node);
259         }
260         this.active = active;
261     }
262     
263     /**
264      * Method from NodeSectionPanel interface
265      */

266     public boolean isActive() {
267         return active;
268     }
269     
270     /** This method is called from within the constructor to
271      * initialize the form.
272      * WARNING: Do NOT modify this code. The content of this method is
273      * always regenerated by the Form Editor.
274      */

275     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
276
private void initComponents() {
277         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
278
279         foldButton = new javax.swing.JToggleButton JavaDoc();
280         headerSeparator = new javax.swing.JSeparator JavaDoc();
281         actionPanel = new javax.swing.JPanel JavaDoc();
282         fillerLine = new javax.swing.JSeparator JavaDoc();
283         fillerEnd = new javax.swing.JSeparator JavaDoc();
284         titlePanel = new javax.swing.JPanel JavaDoc();
285         titleButton = new javax.swing.JButton JavaDoc();
286
287         setLayout(new java.awt.GridBagLayout JavaDoc());
288
289         foldButton.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/org/netbeans/modules/xml/multiview/resources/plus.gif")));
290         foldButton.setBorder(null);
291         foldButton.setBorderPainted(false);
292         foldButton.setContentAreaFilled(false);
293         foldButton.setFocusPainted(false);
294         foldButton.setFocusable(false);
295         foldButton.setSelectedIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/org/netbeans/modules/xml/multiview/resources/minus.gif")));
296         foldButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
297             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
298                 foldButtonActionPerformed(evt);
299             }
300         });
301
302         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
303         gridBagConstraints.gridx = 0;
304         gridBagConstraints.gridy = 0;
305         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
306         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 2, 0, 2);
307         add(foldButton, gridBagConstraints);
308
309         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
310         gridBagConstraints.gridx = 1;
311         gridBagConstraints.gridy = 1;
312         gridBagConstraints.gridwidth = 2;
313         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
314         gridBagConstraints.weightx = 1.0;
315         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 0, 0, 0);
316         add(headerSeparator, gridBagConstraints);
317
318         actionPanel.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.RIGHT, 2, 0));
319
320         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
321         gridBagConstraints.gridx = 2;
322         gridBagConstraints.gridy = 0;
323         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
324         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 0);
325         add(actionPanel, gridBagConstraints);
326
327         fillerLine.setOrientation(javax.swing.SwingConstants.VERTICAL);
328         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
329         gridBagConstraints.gridx = 0;
330         gridBagConstraints.gridy = 1;
331         gridBagConstraints.gridheight = 2;
332         gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
333         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
334         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 0, 0);
335         add(fillerLine, gridBagConstraints);
336
337         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
338         gridBagConstraints.gridx = 0;
339         gridBagConstraints.gridy = 3;
340         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
341         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
342         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 0, 2);
343         add(fillerEnd, gridBagConstraints);
344
345         titlePanel.setLayout(new java.awt.BorderLayout JavaDoc());
346
347         titleButton.setBorderPainted(false);
348         titleButton.setContentAreaFilled(false);
349         titleButton.setFocusPainted(false);
350         titleButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
351         titleButton.setMargin(new java.awt.Insets JavaDoc(0, 4, 0, 4));
352         titleButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
353             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
354                 titleButtonActionPerformed(evt);
355             }
356         });
357
358         titlePanel.add(titleButton, java.awt.BorderLayout.CENTER);
359
360         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
361         gridBagConstraints.gridx = 1;
362         gridBagConstraints.gridy = 0;
363         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
364         gridBagConstraints.weightx = 1.0;
365         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 0);
366         add(titlePanel, gridBagConstraints);
367
368     }// </editor-fold>//GEN-END:initComponents
369

370     private void titleButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_titleButtonActionPerformed
371
if (!foldButton.isSelected()) {
372             openInnerPanel();
373             foldButton.setSelected(true);
374         } else {
375             if (isActive()) {
376                 closeInnerPanel();
377                 foldButton.setSelected(false);
378             }
379         }
380         if (!isActive()) setActive(true);
381         
382     }//GEN-LAST:event_titleButtonActionPerformed
383

384     private void foldButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_foldButtonActionPerformed
385
if (foldButton.isSelected()) {
386             openInnerPanel();
387         } else {
388             closeInnerPanel();
389         }
390     }//GEN-LAST:event_foldButtonActionPerformed
391

392     
393     // Variables declaration - do not modify//GEN-BEGIN:variables
394
private javax.swing.JPanel JavaDoc actionPanel;
395     private javax.swing.JSeparator JavaDoc fillerEnd;
396     private javax.swing.JSeparator JavaDoc fillerLine;
397     private javax.swing.JToggleButton JavaDoc foldButton;
398     private javax.swing.JSeparator JavaDoc headerSeparator;
399     private javax.swing.JButton JavaDoc titleButton;
400     private javax.swing.JPanel JavaDoc titlePanel;
401     // End of variables declaration//GEN-END:variables
402

403     public void setKey(Object JavaDoc key) {
404         this.key = key;
405     }
406     
407     public Object JavaDoc getKey() {
408         return key;
409     }
410     
411     public JComponent getErrorComponent(String JavaDoc errorId) {
412         if (innerPanel != null) {
413             return innerPanel.getErrorComponent(errorId);
414         }
415         return null;
416     }
417     
418     public SectionInnerPanel getInnerPanel() {
419         return innerPanel;
420     }
421     
422     public void setIndex(int index) {
423         this.index = index;
424     }
425     
426     public int getIndex() {
427         return index;
428     }
429     
430     private HeaderButton[] headerButtons;
431     
432     public void setHeaderActions(Action[] actions) {
433         headerButtons = new HeaderButton[actions.length];
434         for (int i=0;i<actions.length;i++) {
435             headerButtons[i] = new HeaderButton(this,actions[i]);
436             headerButtons[i].setOpaque(false);
437             actionPanel.add(headerButtons[i]);
438         }
439     }
440     
441     public HeaderButton[] getHeaderButtons(){
442         return headerButtons;
443     }
444     
445     protected JComponent getFillerLine() {
446         return fillerLine;
447     }
448     
449     protected JComponent getFillerEnd() {
450         return fillerEnd;
451     }
452     
453     protected JToggleButton getFoldButton() {
454         return foldButton;
455     }
456     
457     protected JSeparator getHeaderSeparator() {
458         return headerSeparator;
459     }
460     
461     protected JButton getTitleButton() {
462         return titleButton;
463     }
464     
465     public static class HeaderButton extends javax.swing.JButton JavaDoc {
466         private SectionPanel panel;
467         public HeaderButton(SectionPanel panel, Action action) {
468             super(action);
469             this.panel=panel;
470             setMargin(new java.awt.Insets JavaDoc(0,14,0,14));
471             setEnabled(false);
472         }
473         public SectionPanel getSectionPanel() {
474             return panel;
475         }
476     }
477     
478 }
479
Popular Tags