1 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 ; 30 import java.awt.event.FocusEvent ; 31 import java.awt.event.FocusListener ; 32 import java.beans.VetoableChangeListener ; 33 import java.beans.PropertyChangeEvent ; 34 import java.beans.PropertyVetoException ; 35 36 43 public class SectionPanel extends javax.swing.JPanel implements NodeSectionPanel, ErrorLocator { 44 45 private SectionView sectionView; 46 private String title; 47 private Node node; 48 private boolean active; 49 private SectionInnerPanel innerPanel; 50 private Object key; 51 private int index; 52 53 private FocusListener sectionFocusListener = new FocusAdapter () { 54 public void focusGained(FocusEvent e) { 55 setActive(true); 56 } 57 }; 58 private ToolBarDesignEditor toolBarDesignEditor; 59 60 68 public SectionPanel(SectionView sectionView, Node explorerNode, Object key) { 69 this(sectionView, explorerNode, key, false); 70 } 71 72 80 public SectionPanel(SectionView sectionView, Node explorerNode, Object key, boolean autoExpand) { 81 this(sectionView, explorerNode, explorerNode.getDisplayName(), key, autoExpand); 82 } 83 84 91 public SectionPanel(SectionView sectionView, Node node, String title, Object key) { 92 this(sectionView, node, title, key, false); 93 } 94 95 107 public SectionPanel(SectionView sectionView, Node node, String title, 108 Object 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 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 153 public SectionPanel(SectionView sectionView, Node node, String title, Object 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 () { 166 public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException { 167 if (ToolBarDesignEditor.PROPERTY_FLUSH_DATA.equals(evt.getPropertyName()) && 168 evt.getNewValue() == null) { 169 if (innerPanel != null && !innerPanel.canClose()) { 170 throw new PropertyVetoException ("", evt); 171 } 172 } 173 } 174 }); 175 } 176 } 177 if (innerPanel != null) { 178 return; 179 } 180 innerPanel = createInnerpanel(); 181 java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 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 (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 getTitle() { 212 return title; 213 } 214 215 public void setTitle(String title) { 216 titleButton.setText(title); 217 this.title = title; 218 } 219 220 223 public Node getNode() { 224 return node; 225 } 226 227 230 public void open() { 231 foldButton.setSelected(true); 232 openInnerPanel(); 233 } 234 235 238 public void scroll() { 239 Utils.scrollToVisible(this); 240 } 241 242 245 public void setActive(boolean active) { 246 titlePanel.setBackground( 248 active ? SectionVisualTheme.getSectionHeaderActiveColor() : SectionVisualTheme.getSectionHeaderColor()); 249 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 266 public boolean isActive() { 267 return active; 268 } 269 270 275 private void initComponents() { 277 java.awt.GridBagConstraints gridBagConstraints; 278 279 foldButton = new javax.swing.JToggleButton (); 280 headerSeparator = new javax.swing.JSeparator (); 281 actionPanel = new javax.swing.JPanel (); 282 fillerLine = new javax.swing.JSeparator (); 283 fillerEnd = new javax.swing.JSeparator (); 284 titlePanel = new javax.swing.JPanel (); 285 titleButton = new javax.swing.JButton (); 286 287 setLayout(new java.awt.GridBagLayout ()); 288 289 foldButton.setIcon(new javax.swing.ImageIcon (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 (getClass().getResource("/org/netbeans/modules/xml/multiview/resources/minus.gif"))); 296 foldButton.addActionListener(new java.awt.event.ActionListener () { 297 public void actionPerformed(java.awt.event.ActionEvent evt) { 298 foldButtonActionPerformed(evt); 299 } 300 }); 301 302 gridBagConstraints = new java.awt.GridBagConstraints (); 303 gridBagConstraints.gridx = 0; 304 gridBagConstraints.gridy = 0; 305 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST; 306 gridBagConstraints.insets = new java.awt.Insets (6, 2, 0, 2); 307 add(foldButton, gridBagConstraints); 308 309 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (2, 0, 0, 0); 316 add(headerSeparator, gridBagConstraints); 317 318 actionPanel.setLayout(new java.awt.FlowLayout (java.awt.FlowLayout.RIGHT, 2, 0)); 319 320 gridBagConstraints = new java.awt.GridBagConstraints (); 321 gridBagConstraints.gridx = 2; 322 gridBagConstraints.gridy = 0; 323 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 324 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 0); 325 add(actionPanel, gridBagConstraints); 326 327 fillerLine.setOrientation(javax.swing.SwingConstants.VERTICAL); 328 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (0, 6, 0, 0); 335 add(fillerLine, gridBagConstraints); 336 337 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (0, 6, 0, 2); 343 add(fillerEnd, gridBagConstraints); 344 345 titlePanel.setLayout(new java.awt.BorderLayout ()); 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 (0, 4, 0, 4)); 352 titleButton.addActionListener(new java.awt.event.ActionListener () { 353 public void actionPerformed(java.awt.event.ActionEvent evt) { 354 titleButtonActionPerformed(evt); 355 } 356 }); 357 358 titlePanel.add(titleButton, java.awt.BorderLayout.CENTER); 359 360 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (6, 0, 0, 0); 366 add(titlePanel, gridBagConstraints); 367 368 } 370 private void titleButtonActionPerformed(java.awt.event.ActionEvent evt) { 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 } 384 private void foldButtonActionPerformed(java.awt.event.ActionEvent evt) { if (foldButton.isSelected()) { 386 openInnerPanel(); 387 } else { 388 closeInnerPanel(); 389 } 390 } 392 393 private javax.swing.JPanel actionPanel; 395 private javax.swing.JSeparator fillerEnd; 396 private javax.swing.JSeparator fillerLine; 397 private javax.swing.JToggleButton foldButton; 398 private javax.swing.JSeparator headerSeparator; 399 private javax.swing.JButton titleButton; 400 private javax.swing.JPanel titlePanel; 401 403 public void setKey(Object key) { 404 this.key = key; 405 } 406 407 public Object getKey() { 408 return key; 409 } 410 411 public JComponent getErrorComponent(String 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 { 466 private SectionPanel panel; 467 public HeaderButton(SectionPanel panel, Action action) { 468 super(action); 469 this.panel=panel; 470 setMargin(new java.awt.Insets (0,14,0,14)); 471 setEnabled(false); 472 } 473 public SectionPanel getSectionPanel() { 474 return panel; 475 } 476 } 477 478 } 479 | Popular Tags |