~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/schema/abe/src/org/netbeans/modules/xml/schema/abe/ElementPanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.xml.schema.abe;
 
43
 
 
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;
 
79
 
 
80
public class ElementPanel extends ABEBaseDropPanel{
 
81
    private static final long serialVersionUID = 7526472295622776147L;
 
82
    
 
83
    public ElementPanel(InstanceUIContext context,
 
84
            AXIContainer element, ContainerPanel parentCompositorPanel) {
 
85
        super(context);
 
86
        this.parentCompositorPanel=parentCompositorPanel;
 
87
        this.axiContainer=element;
 
88
        //setBorder(new LineBorder(Color.RED));
 
89
        initialize();
 
90
        
 
91
        _setName(element.getName());
 
92
        
 
93
        addElementListener();
 
94
        
 
95
        makeNBNode();
 
96
    }
 
97
    
 
98
    
 
99
    private void initialize() {
 
100
        initButton();
 
101
        setLayout(new BorderLayout());
 
102
        setOpaque(false);
 
103
        
 
104
        startTag = getNewStartTagPanel(this, context);
 
105
        add(startTag,BorderLayout.NORTH);
 
106
        
 
107
        //dont show expand button if not needed
 
108
        if(axiContainer.getCompositor() == null){
 
109
            expandButton.setVisible(false);
 
110
        }
 
111
    }
 
112
    
 
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)) {
 
117
                        //a compositor event
 
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());
 
124
                        }
 
125
                } else if(evt.getPropertyName().equals(
 
126
                        org.netbeans.modules.xml.axi.Element.PROP_TYPE)){
 
127
                    startTag.updateAttributes();
 
128
                }
 
129
            }
 
130
        });
 
131
    }
 
132
    
 
133
    private void initButton(){
 
134
        expandButton = new ExpandCollapseButton("+");
 
135
        expandButton.addActionListener(new ActionListener() {
 
136
            public void actionPerformed(ActionEvent e) {
 
137
                handleExpandOrCollapse();
 
138
            }
 
139
        });
 
140
    }
 
141
    
 
142
    
 
143
    private void handleExpandOrCollapse(){
 
144
        if(!expandButton.isExpanded()){
 
145
            //expand
 
146
            expandChild();
 
147
        }else{
 
148
            //collapse
 
149
            collapseChild();
 
150
        }
 
151
    }
 
152
    
 
153
    public void expandChild(){
 
154
        SwingUtilities.invokeLater(new Runnable(){
 
155
            public void run() {
 
156
                if(expandButton.isExpanded())
 
157
                    expandButton.setText("-");
 
158
                setCursor(new Cursor(Cursor.WAIT_CURSOR));
 
159
                try{
 
160
                    if(fadeinPanel != null){
 
161
                        //children already added just show
 
162
                        fadeinPanel.setVisible(true);
 
163
                    }else{
 
164
                        //children not added create them and then show
 
165
                        createChild();
 
166
                        setExpanded(true);
 
167
                    }
 
168
                }finally{
 
169
                    //always reset back
 
170
                    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
 
171
                }
 
172
                revalidate();
 
173
                repaint();
 
174
            }
 
175
        });
 
176
    }
 
177
    
 
178
    public void collapseChild(){
 
179
        if(fadeinPanel != null){
 
180
            //children already added just hide
 
181
            fadeinPanel.setVisible(false);
 
182
            revalidate();
 
183
            repaint();
 
184
        }
 
185
    }
 
186
    
 
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(),
 
192
                        compositor, this);
 
193
                append(child);
 
194
            }
 
195
        }
 
196
    }
 
197
    
 
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);
 
215
            
 
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);
 
221
            
 
222
            add(fadeinPanel,BorderLayout.CENTER);
 
223
            lastComponent = child;
 
224
        }else{
 
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;
 
231
        }
 
232
    }
 
233
    
 
234
    private void removeChild(CompositorPanel component){
 
235
        if(childCompositorPanel != null){
 
236
            childCompositorPanel.remove(component);
 
237
            if(childCompositorPanel.getComponents().length <= 0){
 
238
                remove(fadeinPanel);
 
239
                expandButton.setVisible(false);
 
240
                childCompositorPanel = null;
 
241
            }
 
242
            revalidate();
 
243
            repaint();
 
244
        }
 
245
    }
 
246
    
 
247
    
 
248
    
 
249
    private void addCompositor(Compositor compositor){
 
250
        createChild();
 
251
        expandButton.setVisible(true);
 
252
        setExpanded(false);
 
253
    }
 
254
    
 
255
    private void removeCompositor(Compositor compositor){
 
256
        if(childCompositorPanel == null)
 
257
            return;
 
258
        for(Component comp: childCompositorPanel.getComponents()){
 
259
            CompositorPanel cp = (CompositorPanel) comp;
 
260
            if(cp.getAXIParent().getPeer() == compositor.getPeer()){
 
261
                removeChild(cp);
 
262
            }
 
263
        }
 
264
    }
 
265
    
 
266
    
 
267
    public void removeElement() {
 
268
        AXIContainer axiContainer = getAXIContainer();
 
269
        AXIModel model = axiContainer.getModel();
 
270
        if(model != null){
 
271
            model.startTransaction();
 
272
            try{
 
273
                getAXIContainer().getParent().removeChild(getAXIContainer());
 
274
            }finally{
 
275
                model.endTransaction();
 
276
            }
 
277
        }
 
278
    }
 
279
    
 
280
    public boolean isExpanded() {
 
281
        return fadeinPanel !=null ? fadeinPanel.isVisible() : false;
 
282
    }
 
283
    
 
284
    
 
285
    public void setExpanded(boolean value) {
 
286
        if (fadeinPanel != null) {
 
287
            fadeinPanel.setVisible(value);
 
288
        }
 
289
    }
 
290
    
 
291
    public String getName() {
 
292
        return name;
 
293
    }
 
294
    
 
295
    
 
296
    public void setName(String value) {
 
297
        _setName(value);
 
298
    }
 
299
    
 
300
    
 
301
    private void _setName(String value) {
 
302
        name=value;
 
303
        repaint();
 
304
    }
 
305
    
 
306
    
 
307
    public AXIContainer getAXIContainer() {
 
308
        return axiContainer;
 
309
    }
 
310
    
 
311
    public String getAnnotation() {
 
312
        return annotation;
 
313
    }
 
314
    
 
315
    public void setAnnotation(String value) {
 
316
        _setAnnotation(value);
 
317
    }
 
318
    
 
319
    private void _setAnnotation(String value) {
 
320
        annotation=value;
 
321
    }
 
322
    
 
323
    public TagPanel getStartTagPanel() {
 
324
        return startTag;
 
325
    }
 
326
    
 
327
    
 
328
    TweenerPanel getTweenerPanel() {
 
329
        return tweenerPanel;
 
330
    }
 
331
    
 
332
    
 
333
    //Following set of methods needed for the tag size calculation and horizontal bar display logic
 
334
    public Dimension getPreferredSize() {
 
335
        synchronized(this){        
 
336
            return getMinimumSize();
 
337
        }
 
338
    }
 
339
    
 
340
    public Dimension getMinimumSize() {
 
341
        int width = 0;
 
342
        int height = 0;
 
343
        for(Component child: this.getComponents()){
 
344
            if(!child.isVisible())
 
345
                continue;
 
346
            Dimension dim = child.getPreferredSize();
 
347
            height += dim.height;// + getInterComponentSpacing();
 
348
            int thisW = dim.width ;
 
349
            width = width < thisW ? thisW : width;
 
350
        }
 
351
        if( (fadeinPanel != null) && fadeinPanel.isVisible() ){
 
352
            height +=4;
 
353
            width += 10;
 
354
        }
 
355
        return new Dimension(width, height);
 
356
    }
 
357
    
 
358
    
 
359
    List<String> getAttributes() {
 
360
        return attributes;
 
361
    }
 
362
    
 
363
    
 
364
    void addAttribute(String value) {
 
365
        attributes.add(value);
 
366
        repaint();
 
367
    }
 
368
    
 
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);
 
376
//            }
 
377
//        });
 
378
        
 
379
    }
 
380
    
 
381
    public InstanceUIContext getUIContext() {
 
382
        return context;
 
383
    }
 
384
    
 
385
    void showNameEditor(boolean firstTime) {
 
386
        startTag.showTagNameEditor(firstTime);
 
387
    }
 
388
    
 
389
    protected void makeNBNode() {
 
390
        if(getAXIContainer() instanceof AnyElement)
 
391
            elementNode = new AnyElementNode((AnyElement) getAXIContainer(), context);
 
392
        else
 
393
            elementNode = new ElementNode((AbstractElement) getAXIContainer(), context);
 
394
        /*if(getAXIContainer().isReadOnly(getAXIContainer().getOriginal().getModel()))
 
395
            ((ABEAbstractNode)elementNode).setReadOnly(true);*/
 
396
    }
 
397
    
 
398
    public ABEAbstractNode getNBNode() {
 
399
        return elementNode;
 
400
    }
 
401
    
 
402
    protected StartTagPanel getNewStartTagPanel(ElementPanel elementPanel, InstanceUIContext context) {
 
403
        return new StartTagPanel(elementPanel, context);
 
404
    }
 
405
    
 
406
    
 
407
    ExpandCollapseButton getExpandButton() {
 
408
        return expandButton;
 
409
    }
 
410
    
 
411
    private void drawBoundingBox(Graphics2D g2d) {
 
412
        if( (fadeinPanel == null) || !fadeinPanel.isVisible() )
 
413
            return;
 
414
        if( (childCompositorPanel == null) ||
 
415
                (childCompositorPanel.getComponents().length < 1))
 
416
            return;
 
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();
 
422
        
 
423
        Stroke oldStroke = g2d.getStroke();
 
424
        Color oldColor = g2d.getColor();
 
425
        g2d.setColor(InstanceDesignConstants.XP_ORANGE);
 
426
        g2d.setStroke(new BasicStroke(1));
 
427
        
 
428
        Rectangle rect = fadeinPanel.getBounds();
 
429
        Rectangle myrect = getBounds();
 
430
        
 
431
        int x1, y1, x2, y2;
 
432
        //left Top
 
433
        x1 = leftBottomPoint.x;
 
434
        y1 = leftBottomPoint.y;
 
435
        //left Bottom
 
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);
 
441
        
 
442
        //left Bottom
 
443
        x1 = x2; y1 = y2;
 
444
        //right Bottom
 
445
        x2 = (rect.x + rect.width) > rightNosePoint.x ? (rect.x + rect.width): rightNosePoint.x + 5;
 
446
        y2 = y1;
 
447
        g2d.drawLine(x1, y1, x2, y2);
 
448
        
 
449
        //right Bottom
 
450
        x1 = x2; y1 = y2;
 
451
        //right Top
 
452
        x2 = x1;
 
453
        y2 = rightNosePoint.y;
 
454
        g2d.drawLine(x1-1, y1, x2-1, y2);
 
455
        
 
456
        //right Top
 
457
        x1 = x2; y1 = y2;
 
458
        //end Point
 
459
        x2 = rightNosePoint.x; y2 = rightNosePoint.y;
 
460
        g2d.drawLine(x1, y1, x2, y2);
 
461
        
 
462
        g2d.setStroke(oldStroke);
 
463
        g2d.setColor(oldColor);
 
464
    }
 
465
    
 
466
    
 
467
    
 
468
    
 
469
    public ABEBaseDropPanel getUIComponentFor(AXIComponent axiComponent) {
 
470
        ABEBaseDropPanel comp = super.getUIComponentFor(axiComponent);
 
471
        if(comp == this){
 
472
            //startTag panel is everything so just return that.
 
473
            return startTag;
 
474
        }
 
475
        return null;
 
476
    }
 
477
    
 
478
    public ABEBaseDropPanel getChildUIComponentFor(AXIComponent axiComponent){
 
479
        //search myself
 
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);
 
485
        }else{
 
486
            //expand before analysing the content
 
487
            expandChild();
 
488
            //must be element or seq
 
489
            if(childCompositorPanel == null)
 
490
                return null;
 
491
            
 
492
            expandButton.setText("-");
 
493
            for(Component comp : childCompositorPanel.getComponents()){
 
494
                if(comp instanceof CompositorPanel){
 
495
                    
 
496
                    result = ((ABEBaseDropPanel)comp).getUIComponentFor(axiComponent);
 
497
                    if(result != null)
 
498
                        break;
 
499
                }
 
500
            }
 
501
        }
 
502
        return result;
 
503
    }
 
504
    
 
505
    public AXIComponent getAXIComponent() {
 
506
        return getAXIContainer();
 
507
    }
 
508
    
 
509
    public void accept(UIVisitor visitor) {
 
510
        visitor.visit(this);
 
511
    }
 
512
    
 
513
    public ContainerPanel getParentContainerPanel(){
 
514
        return this.parentCompositorPanel;
 
515
    }
 
516
    
 
517
////////////////////////////////////////////////////////////////////////////
 
518
// Instance members
 
519
////////////////////////////////////////////////////////////////////////////
 
520
    
 
521
    private ContainerPanel parentCompositorPanel;
 
522
    private AutoSizingPanel childCompositorPanel;
 
523
    private JPanel  fadeinPanel;
 
524
    private AXIContainer axiContainer;
 
525
    private String name;
 
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;
 
533
}