1 19 20 28 29 package org.netbeans.modules.xml.schema.abe; 30 31 import java.awt.BorderLayout ; 32 import java.awt.Color ; 33 import java.awt.Cursor ; 34 import java.awt.Font ; 35 import java.awt.dnd.DropTarget ; 36 import java.awt.dnd.DropTargetDragEvent ; 37 import java.awt.dnd.DropTargetDropEvent ; 38 import java.awt.dnd.DropTargetEvent ; 39 import java.awt.dnd.DropTargetListener ; 40 import java.awt.event.ActionEvent ; 41 import java.awt.event.ActionListener ; 42 import java.awt.event.ComponentAdapter ; 43 import java.awt.event.ComponentEvent ; 44 import java.awt.event.MouseAdapter ; 45 import java.awt.event.MouseEvent ; 46 import java.beans.PropertyChangeEvent ; 47 import javax.swing.JLabel ; 48 import javax.swing.JPanel ; 49 import javax.swing.JSeparator ; 50 import javax.swing.SpringLayout ; 51 import javax.swing.SwingUtilities ; 52 import org.netbeans.modules.xml.axi.AXIComponent; 53 import org.openide.util.NbBundle; 54 55 59 public abstract class TitleWrapperPanel extends ABEBaseDropPanel{ 60 private static final long serialVersionUID = 7526472295622776147L; 61 ContainerPanel child; 62 String titleString; 63 AXIComponent countComponent; 64 ExpandCollapseButton expandCollapseButton; 65 JLabel countLabel; 66 private boolean openByDefault; 67 68 69 70 public TitleWrapperPanel(ContainerPanel child, String titleString, AXIComponent 71 countComponent, boolean openByDefault, InstanceUIContext context) { 72 super(context); 73 this.child = child; 74 this.titleString = titleString; 75 this.countComponent = countComponent; 76 this.openByDefault = openByDefault; 77 initialize(); 78 79 this.addMouseListener(new MouseAdapter (){ 80 public void mouseClicked(MouseEvent e) { 81 TitleWrapperPanel.this.child.dispatchEvent(e); 82 } 83 84 public void mousePressed(MouseEvent e) { 85 TitleWrapperPanel.this.child.dispatchEvent(e); 86 } 87 88 public void mouseReleased(MouseEvent e) { 89 TitleWrapperPanel.this.child.dispatchEvent(e); 90 } 91 }); 92 } 93 94 private void initialize(){ 95 setLayout(new BorderLayout ()); 96 setOpaque(false); 97 SpringLayout titlePanelLayout = new SpringLayout (); 98 AutoSizingPanel titlePanel = new AutoSizingPanel(this.context); 99 titlePanel.setLayout(titlePanelLayout); 100 titlePanel.setHorizontalScaling(true); 101 titlePanel.setOpaque(false); 102 103 expandCollapseButton = new ExpandCollapseButton("-"); 104 if(openByDefault) 105 expandCollapseButton.setText("-"); 106 else 107 expandCollapseButton.setText("+"); 108 109 expandCollapseButton.setWatchForComponent(child); 110 111 titlePanel.add(expandCollapseButton); 112 titlePanelLayout.putConstraint(SpringLayout.WEST, expandCollapseButton, 113 0, SpringLayout.WEST, titlePanel); 114 titlePanelLayout.putConstraint(SpringLayout.NORTH, expandCollapseButton, 115 3, SpringLayout.NORTH, titlePanel); 116 117 118 expandCollapseButton.addActionListener(new ActionListener (){ 119 public void actionPerformed(ActionEvent ae){ 120 String state = expandCollapseButton.getText(); 121 setCursor(new Cursor (Cursor.WAIT_CURSOR)); 122 try{ 123 if(state.equals("-")){ 124 child.setVisible(false); 126 } else { 127 child.addAllChildren(); 128 child.setVisible(true); 129 } 130 setCountString(); 131 }finally{ 132 setCursor(new Cursor (Cursor.DEFAULT_CURSOR)); 133 } 134 } 135 }); 136 137 JLabel titleLabel = new JLabel (titleString); 138 titleLabel.setDropTarget(new DropTarget (titleLabel, new DropTargetListener () { 139 public void dragEnter(DropTargetDragEvent dtde) { 140 TitleWrapperPanel.this.dragEnter(dtde); 141 } 142 public void dragExit(DropTargetEvent dte) { 143 TitleWrapperPanel.this.dragExit(dte); 144 } 145 public void dragOver(DropTargetDragEvent dtde) { 146 TitleWrapperPanel.this.dragOver(dtde); 147 } 148 public void drop(DropTargetDropEvent dtde) { 149 TitleWrapperPanel.this.drop(dtde); 150 } 151 public void dropActionChanged(DropTargetDragEvent dtde) { 152 TitleWrapperPanel.this.dropActionChanged(dtde); 153 } 154 })); 155 Font font = titleLabel.getFont(); 156 font = new Font (font.getName(), Font.BOLD, font.getSize()); 157 titleLabel.setFont(font); 158 titleLabel.setForeground(InstanceDesignConstants.TAG_NAME_COLOR); 159 160 titlePanel.add(titleLabel); 161 titlePanelLayout.putConstraint(SpringLayout.WEST, titleLabel, 162 (TITLE_BEGIN_FUDGE - ExpandCollapseButton.WIDTH), SpringLayout.EAST, expandCollapseButton); 163 titlePanelLayout.putConstraint(SpringLayout.NORTH, titleLabel, 164 0, SpringLayout.NORTH, titlePanel); 165 166 countLabel = getCountLabel(); 167 168 titlePanel.add(countLabel); 169 titlePanelLayout.putConstraint(SpringLayout.WEST, countLabel, 170 5, SpringLayout.EAST, titleLabel); 171 titlePanelLayout.putConstraint(SpringLayout.NORTH, titleLabel, 172 0, SpringLayout.NORTH, titlePanel); 173 174 JSeparator separator = new JSeparator (); 175 176 JPanel tempPanel = new JPanel (new BorderLayout ()); 179 tempPanel.setOpaque(false); 180 tempPanel.add(titlePanel, BorderLayout.CENTER); 181 tempPanel.add(separator, BorderLayout.SOUTH); 182 183 184 185 add(tempPanel, BorderLayout.NORTH); 186 187 add(child, BorderLayout.CENTER); 189 if(openByDefault) 190 child.setVisible(true); 191 else 192 child.setVisible(false); 193 setCountString(); 194 195 child.addComponentListener(new ComponentAdapter () { 196 public void componentHidden(ComponentEvent e) { 197 componentShown(e); 198 } 199 public void componentShown(ComponentEvent e) { 200 SwingUtilities.invokeLater(new Runnable (){ 201 public void run() { 202 setCountString(); 203 } 204 }); 205 } 206 }); 207 } 208 209 public JLabel getCountLabel(){ 210 int size = getChildrenItemsCount(); 211 countLabel = new JLabel (getCountString(size)); 212 countComponent.addPropertyChangeListener( new ModelEventMediator(this, countComponent) { 213 public void _propertyChange(PropertyChangeEvent evt) { 214 setCountString(); 216 } 217 }); 218 countLabel.setForeground(Color.GRAY); 219 return countLabel; 220 } 221 222 223 public void setCountString(){ 224 countLabel.setText(getCountString(getChildrenItemsCount())); 225 } 226 227 public abstract int getChildrenItemsCount(); 228 229 public String getCountString(int i){ 230 String hiddenStr = (child.isVisible() || i == 0) ? "" : " "+locHidden; 231 String str = (i == 1) ? "["+i+" "+locItem+hiddenStr+"]" : "["+i+" "+locItems+hiddenStr+"]"; 232 return str; 233 } 234 235 236 237 private static final String locItem = NbBundle.getMessage(TitleWrapperPanel.class, "LBL_ITEM_STRING"); 238 private static final String locItems = NbBundle.getMessage(TitleWrapperPanel.class, "LBL_ITEMS_STRING"); 239 private static final String locHidden = NbBundle.getMessage(TitleWrapperPanel.class, "LBL_HIDDEN"); 240 public static final int TITLE_BEGIN_FUDGE = ExpandCollapseButton.WIDTH + 2; 241 242 243 public void drop(DropTargetDropEvent event) { 244 context.getNamespacePanel().drop(event); 245 } 246 247 public void dragExit(DropTargetEvent event) { 248 context.getNamespacePanel().dragExit(event); 249 } 250 251 public void dragOver(DropTargetDragEvent event) { 252 context.getNamespacePanel().dragOver(event); 253 } 254 255 public void dragEnter(DropTargetDragEvent event) { 256 context.getNamespacePanel().dragEnter(event); 257 } 258 259 260 public void accept(UIVisitor visitor) { 261 } 262 } 263 | Popular Tags |