1 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 41 public class SectionContainer extends javax.swing.JPanel implements NodeSectionPanel, ContainerPanel { 42 43 private Node activeNode=null; 46 private SectionView sectionView; 47 private String 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 title) { 55 this(sectionView, root, title, true); 56 } 57 58 public SectionContainer(SectionView sectionView, Node root, String 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 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 90 public Node getNode() { 91 return root; 92 } 93 94 public Node getRoot() { 95 return root; 96 } 97 98 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 110 public void scroll() { 111 Utils.scrollToVisible(this); 112 } 113 114 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 127 public boolean isActive() { 128 return active; 129 } 130 131 133 private void mapSection(Node key, NodeSectionPanel panel){ 134 sectionView.mapSection(key,panel); 135 } 136 137 139 private void deleteSection(Node key){ 140 sectionView.deleteSection(key); 141 } 142 143 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 158 public void addSection(NodeSectionPanel section){ 159 GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 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 172 public void removeSection(NodeSectionPanel section) { 173 int panelIndex = section.getIndex(); 174 contentPanel.remove((JPanel)section); 175 176 java.awt.Component [] components = contentPanel.getComponents(); 178 java.util.AbstractList removedPanels = new java.util.ArrayList (); 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 gridBagConstraints = new java.awt.GridBagConstraints (); 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 contentPanel.add((JPanel)pan,gridBagConstraints); 200 } 201 deleteSection(section.getNode()); 202 sectionCount--; 203 } 204 205 210 private void initComponents() { 212 java.awt.GridBagConstraints gridBagConstraints; 213 214 foldButton = new javax.swing.JToggleButton (); 215 headerSeparator = new javax.swing.JSeparator (); 216 contentPanel = new javax.swing.JPanel (); 217 actionPanel = new javax.swing.JPanel (); 218 fillerLine = new javax.swing.JSeparator (); 219 fillerEnd = new javax.swing.JSeparator (); 220 titlePanel = new javax.swing.JPanel (); 221 titleButton = new javax.swing.JButton (); 222 223 setLayout(new java.awt.GridBagLayout ()); 224 225 foldButton.setIcon(new javax.swing.ImageIcon (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 (getClass().getResource("/org/netbeans/modules/xml/multiview/resources/minus.gif"))); 231 foldButton.addActionListener(new java.awt.event.ActionListener () { 232 public void actionPerformed(java.awt.event.ActionEvent evt) { 233 foldButtonActionPerformed(evt); 234 } 235 }); 236 237 gridBagConstraints = new java.awt.GridBagConstraints (); 238 gridBagConstraints.gridx = 0; 239 gridBagConstraints.gridy = 0; 240 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST; 241 gridBagConstraints.insets = new java.awt.Insets (6, 2, 0, 2); 242 add(foldButton, gridBagConstraints); 243 244 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (2, 0, 0, 0); 251 add(headerSeparator, gridBagConstraints); 252 253 contentPanel.setLayout(new java.awt.GridBagLayout ()); 254 255 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (2, 0, 0, 0); 262 add(contentPanel, gridBagConstraints); 263 264 actionPanel.setLayout(new java.awt.FlowLayout (java.awt.FlowLayout.RIGHT, 2, 0)); 265 266 gridBagConstraints = new java.awt.GridBagConstraints (); 267 gridBagConstraints.gridx = 2; 268 gridBagConstraints.gridy = 0; 269 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 270 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 0); 271 add(actionPanel, gridBagConstraints); 272 273 fillerLine.setOrientation(javax.swing.SwingConstants.VERTICAL); 274 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (0, 6, 0, 0); 281 add(fillerLine, gridBagConstraints); 282 283 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (0, 6, 0, 2); 289 add(fillerEnd, gridBagConstraints); 290 291 titlePanel.setLayout(new java.awt.BorderLayout ()); 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 (0, 4, 0, 4)); 299 titleButton.addActionListener(new java.awt.event.ActionListener () { 300 public void actionPerformed(java.awt.event.ActionEvent evt) { 301 titleButtonActionPerformed(evt); 302 } 303 }); 304 305 titlePanel.add(titleButton, java.awt.BorderLayout.CENTER); 306 307 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (6, 0, 0, 0); 313 add(titlePanel, gridBagConstraints); 314 315 } 317 private void titleButtonActionPerformed(java.awt.event.ActionEvent evt) { 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 } 336 private void foldButtonActionPerformed(java.awt.event.ActionEvent evt) { contentPanel.setVisible(foldButton.isSelected()); 339 fillerLine.setVisible(foldButton.isSelected()); 340 fillerEnd.setVisible(foldButton.isSelected()); 341 setIcon(foldButton.isSelected()); 342 } 344 345 private javax.swing.JPanel actionPanel; 347 private javax.swing.JPanel contentPanel; 348 private javax.swing.JSeparator fillerEnd; 349 private javax.swing.JSeparator fillerLine; 350 private javax.swing.JToggleButton foldButton; 351 private javax.swing.JSeparator headerSeparator; 352 private javax.swing.JButton titleButton; 353 private javax.swing.JPanel titlePanel; 354 356 357 public void setIndex(int index) { 358 this.index=index; 359 } 360 361 362 public int getIndex() { 363 return index; 364 } 365 366 private javax.swing.JButton [] headerButtons; 367 368 public void setHeaderActions(Action[] actions) { 369 headerButtons = new javax.swing.JButton [actions.length]; 370 for (int i=0;i<actions.length;i++) { 371 headerButtons[i] = new javax.swing.JButton (actions[i]); 372 headerButtons[i].setMargin(new java.awt.Insets (0,14,0,14)); 373 headerButtons[i].setOpaque(false); 374 actionPanel.add(headerButtons[i]); 375 } 376 } 377 378 public javax.swing.JButton [] 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 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 (image)); 394 } 395 } 396 397 398 } 399 | Popular Tags |