1 19 27 28 package org.netbeans.modules.xml.schema.abe; 29 30 import java.awt.Color ; 31 import java.awt.Component ; 32 import java.awt.Dimension ; 33 import java.awt.Graphics ; 34 import java.awt.Polygon ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.ActionListener ; 37 import java.awt.event.ComponentAdapter ; 38 import java.awt.event.ComponentEvent ; 39 import java.awt.event.ComponentListener ; 40 import java.awt.event.MouseAdapter ; 41 import java.awt.event.MouseEvent ; 42 import java.beans.PropertyChangeEvent ; 43 import java.beans.PropertyChangeListener ; 44 import java.util.ArrayList ; 45 import java.util.List ; 46 import javax.swing.JPanel ; 47 import javax.swing.SwingUtilities ; 48 49 53 class ExpandCollapseButton extends JPanel { 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 str){ 62 this(str, true); 63 } 64 65 public ExpandCollapseButton(String str, final boolean autoChangeState){ 66 if(!str.equals("+") && !str.equals("-")) 68 throw new IllegalArgumentException ("Arg can be only + or -"); 69 this.text = str; 70 setOpaque(false); 71 addMouseListener(new MouseAdapter (){ 72 public void mouseClicked(MouseEvent e) { 73 super.mouseClicked(e); 74 fireActionPerformedEvent(); 75 if(autoChangeState) 76 setText(getText().equals("+") ? "-" : "+"); 77 } 78 79 public void mouseExited(MouseEvent e) { 80 super.mouseExited(e); 81 mouseInside = false; 82 repaint(); 83 } 84 85 public void mouseEntered(MouseEvent e) { 86 super.mouseEntered(e); 87 mouseInside = true; 88 repaint(); 89 } 90 91 }); 92 93 } 94 95 96 List <ActionListener > all = new ArrayList <ActionListener >(); 97 public void addActionListener(ActionListener al){ 98 all.add(al); 99 } 100 101 private void fireActionPerformedEvent(){ 102 for(ActionListener al: all){ 103 al.actionPerformed(new ActionEvent (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 g){ 116 super.paintComponent(g); 117 118 int w = 10 - 2; int h = 10 - 2; int xo = 0; 121 int yo = 0; 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 shape = null; 131 if(getText().equals("+")) 132 shape = new Polygon ( 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 ( 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 getPreferredSize(){ 145 Dimension dim = new Dimension (WIDTH, HEIGHT); 146 return dim; 147 } 148 149 public Dimension getMinimumSize(){ 150 return getPreferredSize(); 151 } 152 153 154 public Dimension getMaximumSize(){ 155 return getPreferredSize(); 156 } 157 158 String text; 159 public synchronized String getText(){ 160 return text; 161 } 162 163 public synchronized void setText(String 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 comp){ 175 comp.addComponentListener(new ComponentAdapter () { 176 public void componentShown(ComponentEvent e) { 177 SwingUtilities.invokeLater(new Runnable (){ 178 public void run() { 179 if(ExpandCollapseButton.this.isCollapsed()) 180 ExpandCollapseButton.this.setText("-"); 181 } 182 }); 183 } 184 public void componentHidden(ComponentEvent e) { 185 SwingUtilities.invokeLater(new Runnable (){ 186 public void run() { 187 if(ExpandCollapseButton.this.isExpanded()) 188 ExpandCollapseButton.this.setText("+"); 189 } 190 191 }); 192 } 193 }); 194 } 195 } | Popular Tags |