KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > ExpandCollapseButton


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * ExpandCollapseButton.java
21  *
22  * Created on May 25, 2006, 10:53 AM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.xml.schema.abe;
29
30 import java.awt.Color JavaDoc;
31 import java.awt.Component JavaDoc;
32 import java.awt.Dimension JavaDoc;
33 import java.awt.Graphics JavaDoc;
34 import java.awt.Polygon JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.ActionListener JavaDoc;
37 import java.awt.event.ComponentAdapter JavaDoc;
38 import java.awt.event.ComponentEvent JavaDoc;
39 import java.awt.event.ComponentListener JavaDoc;
40 import java.awt.event.MouseAdapter JavaDoc;
41 import java.awt.event.MouseEvent JavaDoc;
42 import java.beans.PropertyChangeEvent JavaDoc;
43 import java.beans.PropertyChangeListener JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.List JavaDoc;
46 import javax.swing.JPanel JavaDoc;
47 import javax.swing.SwingUtilities JavaDoc;
48
49 /**
50  *
51  * @author girix
52  */

53 class ExpandCollapseButton extends JPanel JavaDoc{
54     private static final long serialVersionUID = 7526472295622776147L;
55     
56     public static final int WIDTH = 12;
57     public static final int HEIGHT = 12;
58     
59     boolean mouseInside = false;
60     
61     public ExpandCollapseButton(String JavaDoc str){
62         this(str, true);
63     }
64     
65     public ExpandCollapseButton(String JavaDoc str, final boolean autoChangeState){
66         //super(str);
67
if(!str.equals("+") && !str.equals("-"))
68             throw new IllegalArgumentException JavaDoc("Arg can be only + or -");
69         this.text = str;
70         setOpaque(false);
71         addMouseListener(new MouseAdapter JavaDoc(){
72             public void mouseClicked(MouseEvent JavaDoc e) {
73                 super.mouseClicked(e);
74                 fireActionPerformedEvent();
75                 if(autoChangeState)
76                     setText(getText().equals("+") ? "-" : "+");
77             }
78             
79             public void mouseExited(MouseEvent JavaDoc e) {
80                 super.mouseExited(e);
81                 mouseInside = false;
82                 repaint();
83             }
84             
85             public void mouseEntered(MouseEvent JavaDoc e) {
86                 super.mouseEntered(e);
87                 mouseInside = true;
88                 repaint();
89             }
90             
91         });
92         
93     }
94     
95     
96     List JavaDoc<ActionListener JavaDoc> all = new ArrayList JavaDoc<ActionListener JavaDoc>();
97     public void addActionListener(ActionListener JavaDoc al){
98         all.add(al);
99     }
100     
101     private void fireActionPerformedEvent(){
102         for(ActionListener JavaDoc al: all){
103             al.actionPerformed(new ActionEvent JavaDoc(this, 1980, getText()));
104         }
105     }
106     
107     public boolean isExpanded(){
108         return getText().equals("-") ? true : false;
109     }
110     
111     public boolean isCollapsed(){
112         return !isExpanded();
113     }
114     
115     public void paintComponent(Graphics JavaDoc g){
116         super.paintComponent(g);
117         
118         int w = 10 - 2;//8
119
int h = 10 - 2;//8
120
int xo = 0;
121         int yo = 0;//getHeight()/2 - 7;// (getHeight()-h)/2-2;
122

123         if(mouseInside)
124             g.setColor(InstanceDesignConstants.MOUSEOVER_EXPAND_BUTTON_COLOR);
125         else
126             g.setColor(InstanceDesignConstants.DARK_BLUE);
127         if(dragMode)
128             g.setColor(Color.WHITE);
129         
130         Polygon JavaDoc shape = null;
131         if(getText().equals("+"))
132             shape = new Polygon JavaDoc(
133                     new int[] {xo, xo , xo+w, xo},
134                     new int[] {yo, yo+h, yo+h/2, yo}, 3);
135         else{
136             shape = new Polygon JavaDoc(
137                     new int[] {xo, xo+w, xo+w/2, xo },
138                     new int[] {yo, yo , yo+h, yo }, 3);
139         }
140         g.drawPolygon(shape);
141         g.fillPolygon(shape);
142     }
143     
144     public Dimension JavaDoc getPreferredSize(){
145         Dimension JavaDoc dim = new Dimension JavaDoc(WIDTH, HEIGHT);
146         return dim;
147     }
148     
149     public Dimension JavaDoc getMinimumSize(){
150         return getPreferredSize();
151     }
152     
153     
154     public Dimension JavaDoc getMaximumSize(){
155         return getPreferredSize();
156     }
157     
158     String JavaDoc text;
159     public synchronized String JavaDoc getText(){
160         return text;
161     }
162     
163     public synchronized void setText(String JavaDoc text){
164         this.text = text;
165         repaint();
166     }
167     
168     boolean dragMode = false;
169     public void setDragMode(boolean dragMode) {
170         this.dragMode = dragMode;
171         repaint();
172     }
173     
174     public void setWatchForComponent(Component JavaDoc comp){
175         comp.addComponentListener(new ComponentAdapter JavaDoc() {
176             public void componentShown(ComponentEvent JavaDoc e) {
177                 SwingUtilities.invokeLater(new Runnable JavaDoc(){
178                     public void run() {
179                         if(ExpandCollapseButton.this.isCollapsed())
180                             ExpandCollapseButton.this.setText("-");
181                     }
182                 });
183             }
184             public void componentHidden(ComponentEvent JavaDoc e) {
185                 SwingUtilities.invokeLater(new Runnable JavaDoc(){
186                     public void run() {
187                         if(ExpandCollapseButton.this.isExpanded())
188                             ExpandCollapseButton.this.setText("+");
189                     }
190                     
191                 });
192             }
193         });
194     }
195 }
Popular Tags