2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.xml.schema.abe;
44
import java.awt.BasicStroke;
45
import java.awt.BorderLayout;
46
import java.awt.Color;
47
import java.awt.Component;
48
import java.awt.Cursor;
49
import java.awt.Dimension;
50
import java.awt.Graphics;
51
import java.awt.Graphics2D;
52
import java.awt.Point;
53
import java.awt.Rectangle;
54
import java.awt.Stroke;
55
import java.awt.event.ActionEvent;
56
import java.awt.event.ActionListener;
57
import java.awt.event.ComponentAdapter;
58
import java.awt.event.ComponentEvent;
59
import java.beans.PropertyChangeEvent;
60
import java.beans.PropertyChangeListener;
61
import java.io.IOException;
62
import java.util.ArrayList;
63
import java.util.List;
64
import javax.swing.Box;
65
import javax.swing.JPanel;
66
import javax.swing.SpringLayout;
67
import javax.swing.SwingUtilities;
68
import org.netbeans.modules.xml.axi.AXIComponent;
69
import org.netbeans.modules.xml.axi.AXIContainer;
70
import org.netbeans.modules.xml.axi.AXIModel;
71
import org.netbeans.modules.xml.axi.AbstractAttribute;
72
import org.netbeans.modules.xml.axi.AbstractElement;
73
import org.netbeans.modules.xml.axi.AnyElement;
74
import org.netbeans.modules.xml.axi.Compositor;
75
import org.netbeans.modules.xml.schema.abe.nodes.ABEAbstractNode;
76
import org.netbeans.modules.xml.schema.abe.nodes.AnyElementNode;
77
import org.netbeans.modules.xml.schema.abe.nodes.ElementNode;
78
import org.openide.nodes.Node;
80
public class ElementPanel extends ABEBaseDropPanel{
81
private static final long serialVersionUID = 7526472295622776147L;
83
public ElementPanel(InstanceUIContext context,
84
AXIContainer element, ContainerPanel parentCompositorPanel) {
86
this.parentCompositorPanel=parentCompositorPanel;
87
this.axiContainer=element;
88
//setBorder(new LineBorder(Color.RED));
91
_setName(element.getName());
99
private void initialize() {
101
setLayout(new BorderLayout());
104
startTag = getNewStartTagPanel(this, context);
105
add(startTag,BorderLayout.NORTH);
107
//dont show expand button if not needed
108
if(axiContainer.getCompositor() == null){
109
expandButton.setVisible(false);
113
private void addElementListener(){
114
axiContainer.addPropertyChangeListener(new ModelEventMediator(this, axiContainer){
115
public void _propertyChange(PropertyChangeEvent evt) {
116
if(evt.getPropertyName().equals(Compositor.PROP_COMPOSITOR)) {
118
if((evt.getOldValue() == null) && (evt.getNewValue() != null)){
119
//new Compositor added
120
addCompositor((Compositor) evt.getNewValue());
121
}else if((evt.getNewValue() == null) && (evt.getOldValue() != null)){
122
//old Compositor removed
123
removeCompositor((Compositor) evt.getOldValue());
125
} else if(evt.getPropertyName().equals(
126
org.netbeans.modules.xml.axi.Element.PROP_TYPE)){
127
startTag.updateAttributes();
133
private void initButton(){
134
expandButton = new ExpandCollapseButton("+");
135
expandButton.addActionListener(new ActionListener() {
136
public void actionPerformed(ActionEvent e) {
137
handleExpandOrCollapse();
143
private void handleExpandOrCollapse(){
144
if(!expandButton.isExpanded()){
153
public void expandChild(){
154
SwingUtilities.invokeLater(new Runnable(){
156
if(expandButton.isExpanded())
157
expandButton.setText("-");
158
setCursor(new Cursor(Cursor.WAIT_CURSOR));
160
if(fadeinPanel != null){
161
//children already added just show
162
fadeinPanel.setVisible(true);
164
//children not added create them and then show
170
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
178
public void collapseChild(){
179
if(fadeinPanel != null){
180
//children already added just hide
181
fadeinPanel.setVisible(false);
187
private void createChild(){
188
for(AXIComponent axiComp: axiContainer.getChildren()){
189
if(axiComp instanceof Compositor){
190
Compositor compositor = (Compositor) axiComp;
191
CompositorPanel child = new CompositorPanel(getUIContext(),
198
Component lastComponent = null;
199
SpringLayout childCompositorPanelLayout;
200
public void append(ContainerPanel child) {
201
if (childCompositorPanel==null) {
202
fadeinPanel = new JPanel();
203
expandButton.setWatchForComponent(fadeinPanel);
204
fadeinPanel.setOpaque(false);
205
fadeinPanel.setLayout(new BorderLayout());
206
//add a horizontal strut for compensating the expand collapse button
207
fadeinPanel.add(Box.createHorizontalStrut((int)
208
expandButton.getPreferredSize().getWidth()), BorderLayout.WEST);
209
childCompositorPanel = new AutoSizingPanel(context);
210
childCompositorPanel.setVerticalScaling(true);
211
childCompositorPanel.setOpaque(false);
212
childCompositorPanelLayout = new SpringLayout();
213
childCompositorPanel.setLayout(childCompositorPanelLayout);
214
fadeinPanel.add(childCompositorPanel, BorderLayout.CENTER);
216
childCompositorPanel.add(child);
217
childCompositorPanelLayout.putConstraint(SpringLayout.NORTH, child, 0,
218
SpringLayout.NORTH, childCompositorPanel);
219
childCompositorPanelLayout.putConstraint(SpringLayout.WEST, child, 0,
220
SpringLayout.WEST, childCompositorPanel);
222
add(fadeinPanel,BorderLayout.CENTER);
223
lastComponent = child;
225
childCompositorPanel.add(child);
226
childCompositorPanelLayout.putConstraint(SpringLayout.NORTH, child, 0,
227
SpringLayout.SOUTH, lastComponent);
228
childCompositorPanelLayout.putConstraint(SpringLayout.WEST, child, 0,
229
SpringLayout.WEST, childCompositorPanel);
230
lastComponent = child;
234
private void removeChild(CompositorPanel component){
235
if(childCompositorPanel != null){
236
childCompositorPanel.remove(component);
237
if(childCompositorPanel.getComponents().length <= 0){
239
expandButton.setVisible(false);
240
childCompositorPanel = null;
249
private void addCompositor(Compositor compositor){
251
expandButton.setVisible(true);
255
private void removeCompositor(Compositor compositor){
256
if(childCompositorPanel == null)
258
for(Component comp: childCompositorPanel.getComponents()){
259
CompositorPanel cp = (CompositorPanel) comp;
260
if(cp.getAXIParent().getPeer() == compositor.getPeer()){
267
public void removeElement() {
268
AXIContainer axiContainer = getAXIContainer();
269
AXIModel model = axiContainer.getModel();
271
model.startTransaction();
273
getAXIContainer().getParent().removeChild(getAXIContainer());
275
model.endTransaction();
280
public boolean isExpanded() {
281
return fadeinPanel !=null ? fadeinPanel.isVisible() : false;
285
public void setExpanded(boolean value) {
286
if (fadeinPanel != null) {
287
fadeinPanel.setVisible(value);
291
public String getName() {
296
public void setName(String value) {
301
private void _setName(String value) {
307
public AXIContainer getAXIContainer() {
311
public String getAnnotation() {
315
public void setAnnotation(String value) {
316
_setAnnotation(value);
319
private void _setAnnotation(String value) {
323
public TagPanel getStartTagPanel() {
328
TweenerPanel getTweenerPanel() {
333
//Following set of methods needed for the tag size calculation and horizontal bar display logic
334
public Dimension getPreferredSize() {
336
return getMinimumSize();
340
public Dimension getMinimumSize() {
343
for(Component child: this.getComponents()){
344
if(!child.isVisible())
346
Dimension dim = child.getPreferredSize();
347
height += dim.height;// + getInterComponentSpacing();
348
int thisW = dim.width ;
349
width = width < thisW ? thisW : width;
351
if( (fadeinPanel != null) && fadeinPanel.isVisible() ){
355
return new Dimension(width, height);
359
List<String> getAttributes() {
364
void addAttribute(String value) {
365
attributes.add(value);
369
public void paintComponent(Graphics g){
370
final Graphics2D g2d = (Graphics2D) g;
371
super.paintComponent(g2d);
372
// SwingUtilities.invokeLater(new Runnable(){
373
// public void run() {
374
if(context.getComponentSelectionManager().getSelectedComponentList().contains(startTag))
375
drawBoundingBox(g2d);
381
public InstanceUIContext getUIContext() {
385
void showNameEditor(boolean firstTime) {
386
startTag.showTagNameEditor(firstTime);
389
protected void makeNBNode() {
390
if(getAXIContainer() instanceof AnyElement)
391
elementNode = new AnyElementNode((AnyElement) getAXIContainer(), context);
393
elementNode = new ElementNode((AbstractElement) getAXIContainer(), context);
394
/*if(getAXIContainer().isReadOnly(getAXIContainer().getOriginal().getModel()))
395
((ABEAbstractNode)elementNode).setReadOnly(true);*/
398
public ABEAbstractNode getNBNode() {
402
protected StartTagPanel getNewStartTagPanel(ElementPanel elementPanel, InstanceUIContext context) {
403
return new StartTagPanel(elementPanel, context);
407
ExpandCollapseButton getExpandButton() {
411
private void drawBoundingBox(Graphics2D g2d) {
412
if( (fadeinPanel == null) || !fadeinPanel.isVisible() )
414
if( (childCompositorPanel == null) ||
415
(childCompositorPanel.getComponents().length < 1))
417
//this refreshes the point detail of the tag
418
startTag.getTagShape();
419
//Point rightBottomPoint = startTag.getRightBottomPoint();
420
Point leftBottomPoint = startTag.getLeftNosePoint();
421
Point rightNosePoint = startTag.getRightNosePoint();
423
Stroke oldStroke = g2d.getStroke();
424
Color oldColor = g2d.getColor();
425
g2d.setColor(InstanceDesignConstants.XP_ORANGE);
426
g2d.setStroke(new BasicStroke(1));
428
Rectangle rect = fadeinPanel.getBounds();
429
Rectangle myrect = getBounds();
433
x1 = leftBottomPoint.x;
434
y1 = leftBottomPoint.y;
436
x2 = leftBottomPoint.x;
437
y2 = y1 + myrect.height - leftBottomPoint.y - 2;
438
/*y2 = y1 + (rect.y > leftBottomPoint.y ? rect.y - leftBottomPoint.y
439
: leftBottomPoint.y - rect.y) + rect.height;*/
440
g2d.drawLine(x1, y1, x2, y2);
445
x2 = (rect.x + rect.width) > rightNosePoint.x ? (rect.x + rect.width): rightNosePoint.x + 5;
447
g2d.drawLine(x1, y1, x2, y2);
453
y2 = rightNosePoint.y;
454
g2d.drawLine(x1-1, y1, x2-1, y2);
459
x2 = rightNosePoint.x; y2 = rightNosePoint.y;
460
g2d.drawLine(x1, y1, x2, y2);
462
g2d.setStroke(oldStroke);
463
g2d.setColor(oldColor);
469
public ABEBaseDropPanel getUIComponentFor(AXIComponent axiComponent) {
470
ABEBaseDropPanel comp = super.getUIComponentFor(axiComponent);
472
//startTag panel is everything so just return that.
478
public ABEBaseDropPanel getChildUIComponentFor(AXIComponent axiComponent){
480
ABEBaseDropPanel result = null;
481
//search for attribute
482
if(axiComponent instanceof AbstractAttribute){
483
//pass on the call to start tag panel
484
result = startTag.getChildUIComponentFor(axiComponent);
486
//expand before analysing the content
488
//must be element or seq
489
if(childCompositorPanel == null)
492
expandButton.setText("-");
493
for(Component comp : childCompositorPanel.getComponents()){
494
if(comp instanceof CompositorPanel){
496
result = ((ABEBaseDropPanel)comp).getUIComponentFor(axiComponent);
505
public AXIComponent getAXIComponent() {
506
return getAXIContainer();
509
public void accept(UIVisitor visitor) {
513
public ContainerPanel getParentContainerPanel(){
514
return this.parentCompositorPanel;
517
////////////////////////////////////////////////////////////////////////////
519
////////////////////////////////////////////////////////////////////////////
521
private ContainerPanel parentCompositorPanel;
522
private AutoSizingPanel childCompositorPanel;
523
private JPanel fadeinPanel;
524
private AXIContainer axiContainer;
526
private String annotation;
527
private StartTagPanel startTag;
528
private JPanel startTagPanel;
529
private TweenerPanel tweenerPanel;
530
private List<String> attributes=new ArrayList<String>();
531
private ExpandCollapseButton expandButton;
532
private ABEAbstractNode elementNode;