KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > StartTagPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.abe;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Cursor JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.Graphics JavaDoc;
28 import java.awt.Graphics2D JavaDoc;
29 import java.awt.Point JavaDoc;
30 import java.awt.Polygon JavaDoc;
31 import java.awt.Rectangle JavaDoc;
32 import java.awt.dnd.DropTargetDragEvent JavaDoc;
33 import java.awt.dnd.DropTargetDropEvent JavaDoc;
34 import java.awt.dnd.DropTargetEvent JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.ActionListener JavaDoc;
37 import java.awt.event.KeyEvent JavaDoc;
38 import java.awt.event.KeyListener JavaDoc;
39 import java.awt.event.MouseAdapter JavaDoc;
40 import java.awt.event.MouseEvent JavaDoc;
41 import java.beans.PropertyChangeEvent JavaDoc;
42 import java.beans.PropertyChangeListener JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Collections JavaDoc;
46 import java.util.List JavaDoc;
47 import javax.swing.Box JavaDoc;
48 import javax.swing.JLabel JavaDoc;
49 import javax.swing.JPanel JavaDoc;
50 import javax.swing.JTextField JavaDoc;
51 import javax.swing.SpringLayout JavaDoc;
52 import javax.swing.SwingUtilities JavaDoc;
53 import org.netbeans.modules.xml.axi.AXIComponent;
54 import org.netbeans.modules.xml.axi.AXIContainer;
55 import org.netbeans.modules.xml.axi.AXIModel;
56 import org.netbeans.modules.xml.axi.AXIType;
57 import org.netbeans.modules.xml.axi.AbstractAttribute;
58 import org.netbeans.modules.xml.axi.AbstractElement;
59 import org.netbeans.modules.xml.axi.AnyElement;
60 import org.netbeans.modules.xml.axi.Attribute;
61 import org.netbeans.modules.xml.axi.Compositor;
62 import org.netbeans.modules.xml.axi.Element;
63 import org.netbeans.modules.xml.axi.datatype.Datatype;
64 import org.netbeans.modules.xml.schema.abe.action.AttributeOnElementNewType;
65 import org.netbeans.modules.xml.schema.abe.action.ElementOnElementNewType;
66 import org.netbeans.modules.xml.schema.abe.palette.DnDHelper;
67 import org.openide.util.NbBundle;
68 import org.openide.util.datatransfer.NewType;
69
70 /**
71  *
72  *
73  */

74 public class StartTagPanel extends TagPanel {
75     protected static final long serialVersionUID = 7526472295622776147L;
76     
77     private ExpandCollapseButton expandButton;
78     public StartTagPanel(final ElementPanel elementPanel, InstanceUIContext context) {
79         super(elementPanel, context);
80         //add listener for name changes
81
elementPanel.getAXIContainer().addPropertyChangeListener(new ModelEventMediator(this, elementPanel.getAXIContainer()) {
82             public void _propertyChange(PropertyChangeEvent JavaDoc evt) {
83                 if(evt.getPropertyName().equals(Element.PROP_NAME)){
84                     setTagName(getElementPanel().getAXIContainer().getName());
85                     forceSizeRecalculate();
86                     revalidate();
87                     repaint();
88                 }else if(evt.getPropertyName().startsWith(Attribute.PROP_ATTRIBUTE)){
89                     updateAttributes();
90                     if(evt.getNewValue() != null){
91                         //its a new attribute add event
92
if((StartTagPanel.super.context.getUserActedComponent() == StartTagPanel.this)){
93                             showAttributes();
94                             showAttributeEditFor((Attribute) evt.getNewValue());
95                             StartTagPanel.super.context.resetUserActedComponent();
96                         }
97                     }
98                 }else if(evt.getPropertyName().equals(Element.PROP_TYPE)){
99                     //indirectly update the element properties panel
100
updateAttributes();
101                 }
102             }
103         });
104         
105         initialize();
106         initKeyListener();
107     }
108     
109     
110     protected void initKeyListener(){
111         addKeyListener(new KeyListener JavaDoc() {
112             public void keyPressed(KeyEvent JavaDoc e) {
113                 if( e.getKeyCode() == e.VK_F2 ){
114                     tagNameLabel.showEditor();
115                 }
116                 if(context.getFocusTraversalManager().isFocusChangeEvent(e))
117                     context.getFocusTraversalManager().handleEvent(e, StartTagPanel.this);
118             }
119             public void keyReleased(KeyEvent JavaDoc e) {
120             }
121             public void keyTyped(KeyEvent JavaDoc e) {
122                 if(e.getKeyChar() == e.VK_SPACE){
123                     tagNameLabel.showEditor();
124                 }else if(e.getKeyChar() == e.VK_E){
125                     //create a new child element
126
for(NewType nt : getNBNode().getNewTypes()){
127                         if(nt instanceof ElementOnElementNewType){
128                             try { nt.create(); } catch (IOException JavaDoc ex) {}
129                             return;
130                         }
131                     }
132                 }else if(e.getKeyChar() == e.VK_A){
133                     for(NewType nt : getNBNode().getNewTypes()){
134                         //create a new attribute
135
if(nt instanceof AttributeOnElementNewType){
136                             try { nt.create(); } catch (IOException JavaDoc ex) {}
137                             return;
138                         }
139                     }
140                 }
141             }
142         });
143     }
144     
145     
146     boolean initialized = false;
147     SpringLayout JavaDoc startTagPanelLayout;
148     Color JavaDoc tagNameLabelColor = InstanceDesignConstants.TAG_NAME_COLOR;
149     private void initialize() {
150         initialized = true;
151         setOpaque(false);
152         startTagPanelLayout = new SpringLayout JavaDoc();
153         setLayout(startTagPanelLayout);
154         
155         attributeCollapseButton.setToolTipText(NbBundle.getMessage(StartTagPanel.class, "TTP_ATTR_EXPAND_COLLAPSE_BUTTON"));
156         attributeCountLabel.setForeground(InstanceDesignConstants.ITEM_COUNT_COLOR);
157         
158         
159         expandButton = getElementPanel().getExpandButton();
160         add(expandButton);
161         startTagPanelLayout.putConstraint(SpringLayout.WEST, expandButton,
162                 getA(), SpringLayout.WEST, this);
163         startTagPanelLayout.putConstraint(SpringLayout.NORTH, expandButton,
164                 LABEL_HEAD_ROOM_SPACE+5, SpringLayout.NORTH, this);
165         //excludePaintComponentList.add(expandButton);
166

167         
168         tagNameLabel = new InplaceEditableLabel();
169         tagNameLabel.setToolTipText(NbBundle.getMessage(StartTagPanel.class, "TTP_ELEMENT_NAME_LABEL"));
170         
171         tagNameLabelColor = InstanceDesignConstants.TAG_NAME_COLOR;
172         
173         if(getElementPanel().getAXIContainer().isReadOnly() ||
174                 (getElementPanel().getAXIContainer() instanceof AnyElement)){
175             tagNameLabelColor = InstanceDesignConstants.TAG_NAME_READONLY_COLOR;
176             tagNameLabel.setToolTipText(NbBundle.getMessage(StartTagPanel.class, "TTP_ELEMENT_NAME_LABEL_READONLY"));
177         }else{
178             if(getElementPanel().getAXIContainer().isShared()){
179                 tagNameLabelColor = InstanceDesignConstants.TAG_NAME_SHARED_COLOR;
180                 tagNameLabel.setToolTipText(NbBundle.getMessage(StartTagPanel.class, "TTP_ELEMENT_NAME_LABEL_SHARED"));
181             }
182         }
183         
184         /*if(getElementPanel().getAXIContainer().isReadOnly()){
185             tagNameLabel.setIcon(UIUtilities.getImageIcon("import-include-redefine.png"));
186         }*/

187         tagNameLabel.setForeground(tagNameLabelColor);
188         
189         getEndSlash().setForeground(tagNameLabel.getForeground());
190         initTagEditListener();
191         
192         
193         
194         Component JavaDoc hgap = Box.createHorizontalStrut(5);
195         add(hgap);
196         startTagPanelLayout.putConstraint(SpringLayout.WEST, hgap,
197                 getA()+INTER_PANEL_SPACE+8, SpringLayout.WEST, this);
198         startTagPanelLayout.putConstraint(SpringLayout.NORTH, hgap,
199                 LABEL_HEAD_ROOM_SPACE, SpringLayout.NORTH, this);
200         
201         //setup the tagLabel
202
add(tagNameLabel);
203         startTagPanelLayout.putConstraint(SpringLayout.WEST, tagNameLabel,
204                 0, SpringLayout.EAST, hgap);
205         startTagPanelLayout.putConstraint(SpringLayout.NORTH, tagNameLabel,
206                 LABEL_HEAD_ROOM_SPACE, SpringLayout.NORTH, this);
207         
208         hgap = Box.createHorizontalStrut(5);
209         add(hgap);
210         startTagPanelLayout.putConstraint(SpringLayout.WEST, hgap,
211                 0, SpringLayout.EAST, tagNameLabel);
212         startTagPanelLayout.putConstraint(SpringLayout.NORTH, hgap,
213                 LABEL_HEAD_ROOM_SPACE, SpringLayout.NORTH, this);
214         
215         firstRowLastComp = hgap;
216         //addElementPropertiesPanel();
217

218         updateTagName();
219         addNonAttributeComponents();
220         updateAttributes();
221         addSelectionListener();
222     }
223     
224     Component JavaDoc lastNonAtribComponent;
225     Component JavaDoc lastAtribComponent;
226     
227     
228     
229     protected void initTagEditListener(){
230         
231         tagNameLabel.addCtrlClickHandler(new InplaceEditableLabel.CtrlClickHandler(){
232             public void handleCtrlClick() {
233                 getNBNode().showSuperDefinition();
234             }
235         });
236         
237         tagNameLabel.setInputValidator(new InputValidator(){
238             public boolean isStringValid(String JavaDoc input) {
239                 return org.netbeans.modules.xml.xam.dom.Utils.isValidNCName(input);
240             }
241         }, NbBundle.getMessage(StartTagPanel.class, "MSG_NOT_A_NCNAME"));
242         tagNameLabel.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
243             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
244                 if(evt.getPropertyName().equals(tagNameLabel.PROPERTY_MODE_CHANGE)){
245                     if(evt.getNewValue() == InplaceEditableLabel.Mode.EDIT){
246                         //user selected edit give the editor JComponent
247
//show a text field
248
final JTextField JavaDoc field = new JTextField JavaDoc(getElementPanel().getAXIContainer().getName());
249                         field.select(0, field.getText().length());
250                         field.addActionListener(new ActionListener JavaDoc(){
251                             public void actionPerformed(ActionEvent JavaDoc e) {
252                                 String JavaDoc newName = field.getText();
253                                 if(getElementPanel().getAXIContainer().getName().equals(newName))
254                                     return;
255                                 //do validation
256
if(org.netbeans.modules.xml.xam.dom.Utils.isValidNCName(newName)){
257                                     field.setCursor(new Cursor JavaDoc(Cursor.WAIT_CURSOR));
258                                     try{
259                                         if(firstTimeRename)
260                                             getNBNode().setNameInModel(newName);
261                                         else
262                                             setTagNameInModel(newName);
263                                         firstTimeRename = false;
264                                     }finally{
265                                         field.setCursor(new Cursor JavaDoc(Cursor.DEFAULT_CURSOR));
266                                     }
267                                 }
268                             }
269                         });
270                         if(getElementPanel().getAXIContainer().isShared()){
271                             String JavaDoc str = NbBundle.getMessage(StartTagPanel.class, "MSG_SHARED_ELEMENT_EDIT");
272                             tagNameLabel.setEditInfoText(str, context);
273                         }
274                         if(!getElementPanel().getAXIContainer().isReadOnly()){
275                             if(getElementPanel().getAXIContainer() instanceof AnyElement){
276                                 String JavaDoc str = NbBundle.getMessage(StartTagPanel.class, "MSG_ANY_ELEMENT_EDIT");
277                                 tagNameLabel.setEditInfoText(str, context);
278                             }else{
279                                 tagNameLabel.setInlineEditorComponent(field);
280                             }
281                         }else{
282                             String JavaDoc str = NbBundle.getMessage(StartTagPanel.class, "MSG_READONLY_ELEMENT_EDIT");
283                             tagNameLabel.setEditInfoText(str, context);
284                         }
285                     }
286                 }
287             }
288         });
289     }
290     
291     
292     private void setTagNameInModel(String JavaDoc name){
293         getNBNode().setName(name);
294     }
295     
296     
297     /**
298      *
299      *
300      */

301     public void setTagName(String JavaDoc value) {
302         String JavaDoc tagName=value;
303         tagNameLabel.setText(tagName);
304     }
305     
306     
307     /**
308      *
309      *
310      */

311     public void updateTagName() {
312         setTagName(getElementPanel().getAXIContainer().getName());
313     }
314     
315     
316     
317     private void showAttributeEditFor(Attribute attr){
318         if(componentList != null){
319             for(Component JavaDoc child: componentList){
320                 if(child instanceof AttributePanel){
321                     final AttributePanel attrP = (AttributePanel) child;
322                     if(attrP.getAttribute() == attr){
323                         context.getComponentSelectionManager().setSelectedComponent(attrP);
324                         SwingUtilities.invokeLater(new Runnable JavaDoc(){
325                             public void run() {
326                                 UIUtilities.scrollViewTo(attrP, context);
327                                 attrP.showEditorForName(true);
328                             }
329                         });
330                         return;
331                     }
332                 }
333             }
334         }
335     }
336     
337     
338     /**
339      *
340      *
341      */

342     Component JavaDoc firstRowLastComp;
343     public void updateAttributes() {
344         updateAttributeCountLabel();
345         //Remove the existing panels
346
if(componentList != null){
347             for (Component JavaDoc panel: componentList){
348                 //except firstTweener remove everything
349
if(panel != firstTweener){
350                     startTagPanelLayout.removeLayoutComponent(panel);
351                     remove(panel);
352                 }
353             }
354         }
355         
356         componentList = new ArrayList JavaDoc<Component JavaDoc>();
357         //firstTweener has to be added for the drop logic to work
358
componentList.add(firstTweener);
359         
360         int attrCount = getElementPanel().getAXIContainer().getAttributes().size();
361         if(attrCount > 0){
362             if(!attributeCollapseButton.isVisible()){
363                 attributeCollapseButton.setVisible(true);
364                 attributeCountLabel.setVisible(true);
365                 //then may be user dropped an attr. So Default expand.
366
if(context.isUserInducedEventMode())
367                     attributeCollapseButton.setText("-");
368             }
369         }else{
370             if(attributeCollapseButton.isVisible()){
371                 attributeCollapseButton.setVisible(false);
372                 attributeCountLabel.setVisible(false);
373             }
374         }
375         
376         lastAtribComponent = firstTweener;
377         firstRowLastComp = null;
378         if(attributesAreShown){
379             addAttributes();
380             if(firstRowLastComp == null){
381                 //there may be no attrs
382
firstRowLastComp = getEndSlash();
383             }
384         }else{
385             //makesure that the collapse button moves closer to tagNameLabel
386
hgap.setPreferredSize(new Dimension JavaDoc(0,0));
387             firstRowLastComp = getEndSlash();
388         }
389         
390         
391         addEndSlashLabel();
392         addElementPropertiesPanel();
393         revalidate();
394         //repaint();
395
getElementPanel().repaint();
396     }
397     
398     static final String JavaDoc locHidden = NbBundle.getMessage(StartTagPanel.class, "LBL_HIDDEN");
399     static final String JavaDoc locAttr = NbBundle.getMessage(StartTagPanel.class, "LBL_ATTRIBUTE");
400     static final String JavaDoc locAttrs = NbBundle.getMessage(StartTagPanel.class, "LBL_ATTRIBUTES");
401     
402     protected void addAttributeCountLabel(){
403         if(attributesAreShown)
404             //dont show anything if the attributes are seen
405
return;
406         int count = getElementPanel().getAXIContainer().getAttributes().size();
407         if(count > 0){
408             String JavaDoc hidden = (attributesAreShown) ? "" : " "+locHidden;
409             String JavaDoc attributeStr = "";
410             if(!attributesAreShown)
411                 attributeStr = (count == 1) ? " "+locAttr : " "+locAttrs;
412             
413             String JavaDoc str = "["+count+attributeStr+hidden+"]";
414             String JavaDoc countStr = "["+count+"]";
415             attributeCountLabel.setText(countStr);
416             attributeCountLabel.setToolTipText(str);
417             add(attributeCountLabel);
418             startTagPanelLayout.putConstraint(SpringLayout.WEST, attributeCountLabel,
419                     0/*INTER_PANEL_SPACE*/, SpringLayout.EAST, lastAtribComponent);
420             startTagPanelLayout.putConstraint(SpringLayout.NORTH, attributeCountLabel,
421                     (getRowCount() - 1) * getTagHeight() + LABEL_HEAD_ROOM_SPACE, SpringLayout.NORTH, this);
422             //componentList.add(attributeCountLabel);
423
lastAtribComponent = attributeCountLabel;
424         }
425     }
426     
427     private void updateAttributeCountLabel(){
428         int count = getElementPanel().getAXIContainer().getAttributes().size();
429         if(count > 0){
430             String JavaDoc hidden = (attributesAreShown) ? "" : " "+locHidden;
431             String JavaDoc attributeStr = "";
432             if(!attributesAreShown)
433                 attributeStr = (count == 1) ? " "+locAttr : " "+locAttrs;
434             
435             String JavaDoc str = "["+count+attributeStr+hidden+"]";
436             String JavaDoc countStr = "["+count+"]";
437             attributeCountLabel.setText(countStr);
438             attributeCountLabel.setToolTipText(str);
439         }else{
440             attributeCountLabel.setText("");
441             attributeCountLabel.setToolTipText("");
442         }
443     }
444     
445     protected void addEndSlashLabel() {
446         add(getEndSlash());
447         startTagPanelLayout.putConstraint(SpringLayout.WEST, getEndSlash(),
448                 INTER_PANEL_SPACE, SpringLayout.EAST, lastAtribComponent);
449         startTagPanelLayout.putConstraint(SpringLayout.NORTH, getEndSlash(),
450                 getLastRowComponentsHeadRoom()+5 , SpringLayout.NORTH, this);
451         componentList.add(getEndSlash());
452         lastAtribComponent = getEndSlash();
453     }
454     
455     private int getLastRowComponentsHeadRoom(){
456         return (getRowCount() - 1) * (getTagHeight() - TAG_HEIGHT_ADJUSTMENT);
457     }
458     private void addElementPropertiesPanel() {
459         if(elementPropertiesPanel != null)
460             excludePaintComponentList.remove(elementPropertiesPanel);
461         elementPropertiesPanel = getNewAXIContainerPropertiesPanel();
462         add(elementPropertiesPanel);
463         startTagPanelLayout.putConstraint(SpringLayout.WEST, elementPropertiesPanel,
464                 getA()+INTER_PANEL_SPACE+6, SpringLayout.EAST, getEndSlash());
465         startTagPanelLayout.putConstraint(SpringLayout.NORTH, elementPropertiesPanel,
466                 getLastRowComponentsHeadRoom(), SpringLayout.NORTH, this);
467         elementPropertiesPanel.addMouseListener(new MouseAdapter JavaDoc(){
468             public void mouseClicked(MouseEvent JavaDoc e) {
469                 StartTagPanel.this.dispatchEvent(e);
470             }
471             public void mouseReleased(MouseEvent JavaDoc e) {
472                 StartTagPanel.this.dispatchEvent(e);
473             }
474             public void mousePressed(MouseEvent JavaDoc e) {
475                 StartTagPanel.this.dispatchEvent(e);
476             }
477         });
478         componentList.add(elementPropertiesPanel);
479         excludePaintComponentList.add(elementPropertiesPanel);
480     }
481     
482     
483     protected void addNonAttributeComponents() {
484         add(attributeCollapseButton);
485         attributeCollapseButton.setVisible(false);
486         startTagPanelLayout.putConstraint(SpringLayout.WEST, attributeCollapseButton,
487                 INTER_PANEL_SPACE, SpringLayout.EAST, firstRowLastComp);
488         startTagPanelLayout.putConstraint(SpringLayout.NORTH, attributeCollapseButton,
489                 ATTR_HEAD_ROOM_SPACE+2, SpringLayout.NORTH, this);
490         attributeCollapseButton.addActionListener(new ActionListener JavaDoc() {
491             public void actionPerformed(ActionEvent JavaDoc e) {
492                 showOrHideAttrs();
493             }
494         });
495         
496         lastAtribComponent = attributeCollapseButton;
497         addAttributeCountLabel();
498         
499         
500         /*//add a hgap between attr collapse button and first tweener.
501         add(hgap);
502         startTagPanelLayout.putConstraint(SpringLayout.WEST, hgap,
503                 0, SpringLayout.EAST, lastAtribComponent);
504         startTagPanelLayout.putConstraint(SpringLayout.NORTH, hgap,
505                 ATTR_HEAD_ROOM_SPACE, SpringLayout.NORTH, this);*/

506         
507         
508         // Add tweener before first attribute..Always
509
firstTweener = Box.createHorizontalStrut(0);//new TweenerPanel(SwingConstants.VERTICAL, context);
510
//addTweenerListener(firstTweener);
511

512         add(firstTweener);
513         startTagPanelLayout.putConstraint(SpringLayout.WEST, firstTweener,
514                 INTER_PANEL_SPACE, SpringLayout.EAST, lastAtribComponent);
515         startTagPanelLayout.putConstraint(SpringLayout.NORTH, firstTweener,
516                 ATTR_HEAD_ROOM_SPACE, SpringLayout.NORTH, this);
517     }
518     
519     private void showOrHideAttrs(){
520         if(attributesAreShown){
521             hideAttributes();
522         }else{
523             showAttributes();
524         }
525         
526     }
527     
528     public void showAttributes(){
529         if(attributesAreShown)
530             return;
531         attributesAreShown = true;
532         attributeCollapseButton.setText("-");
533         attributeCountLabel.setVisible(false);
534         updateAttributes();
535         
536     }
537     
538     public void hideAttributes(){
539         if(!attributesAreShown)
540             return;
541         attributesAreShown = false;
542         attributeCollapseButton.setText("+");
543         attributeCountLabel.setVisible(true);
544         updateAttributes();
545     }
546     int TAG_HEIGHT_ADJUSTMENT = 5;
547     protected void addAttributes() {
548         int rowCount = 0;
549         int attrPosition = 0;
550         Component JavaDoc tweener;
551         int tagHeight = getTagHeight() - TAG_HEIGHT_ADJUSTMENT;
552         List JavaDoc<AbstractAttribute> attributeList = getElementPanel().getAXIContainer().getAttributes();
553         for (int i = 0; i< attributeList.size(); i++) {
554             //calculate the position of this attr
555
if(i < NO_OF_FIRST_ROW_ATTRS){
556                 //attr must be in the 1st row
557
rowCount = 0;
558                 attrPosition = i % NO_OF_FIRST_ROW_ATTRS;
559             } else{
560                 //these belong to other rows
561
rowCount = (int) Math.ceil((double)(i+1 - NO_OF_FIRST_ROW_ATTRS) / (double)NO_OF_ATTRS_PER_ROW);
562                 attrPosition = (i+1 - NO_OF_FIRST_ROW_ATTRS) % (NO_OF_ATTRS_PER_ROW);
563                 attrPosition--;
564                 if(attrPosition == -1)
565                     attrPosition = NO_OF_ATTRS_PER_ROW - 1;
566             }
567             
568             AttributePanel panel=new AttributePanel(this,attributeList.get(i), context);
569             add(panel);
570             componentList.add(panel);
571             if((attrPosition != 0) || (rowCount == 0)){
572                 startTagPanelLayout.putConstraint(SpringLayout.WEST, panel,
573                         0/*INTER_PANEL_SPACE*/, SpringLayout.EAST, lastAtribComponent);
574             }else{
575                 //this is first attr
576
startTagPanelLayout.putConstraint(SpringLayout.WEST, panel,
577                         firstAttrPos, SpringLayout.WEST, this);
578             }
579             startTagPanelLayout.putConstraint(SpringLayout.NORTH, panel,
580                     rowCount * tagHeight+ATTR_HEAD_ROOM_SPACE + 1, SpringLayout.NORTH, this);
581             
582             
583             tweener = Box.createHorizontalStrut(10);//new TweenerPanel(SwingConstants.VERTICAL, context);
584
//addTweenerListener(tweener);
585

586             add(tweener);
587             componentList.add(tweener);
588             startTagPanelLayout.putConstraint(SpringLayout.WEST, tweener,
589                     0/*INTER_PANEL_SPACE*/, SpringLayout.EAST, panel);
590             startTagPanelLayout.putConstraint(SpringLayout.NORTH, tweener,
591                     rowCount * tagHeight+ATTR_HEAD_ROOM_SPACE, SpringLayout.NORTH, this);
592             
593             lastAtribComponent = tweener;
594             if((i + 1) == NO_OF_FIRST_ROW_ATTRS && ((i+1) != attributeList.size())){
595                 //means first row last element and not last element
596
firstRowLastComp = lastAtribComponent;
597             }
598         }
599     }
600     
601     public int getRowCount(){
602         int tmprowCount = 0;
603         if(!attributesAreShown){
604             return 1;
605         }
606         int atrCount = getElementPanel().getAXIContainer().getAttributes().size();
607         if(atrCount < NO_OF_FIRST_ROW_ATTRS){
608             return 1;
609         }else{
610             atrCount -= NO_OF_FIRST_ROW_ATTRS;
611             return (int) Math.ceil((double) atrCount / (double) NO_OF_ATTRS_PER_ROW) + 1;
612         }
613     }
614     
615     private void addNewAttributeAt(TweenerPanel tweener){
616         int index = componentList.indexOf(tweener);
617         if(index == -1){
618             //must not happen
619
return;
620         }
621         index = index/2;
622         AXIContainer element = getElementPanel().getAXIContainer();
623         AXIModel model = element.getModel();
624         model.startTransaction();
625         try{
626             Attribute attr = model.getComponentFactory().createAttribute();
627             String JavaDoc str = UIUtilities.getUniqueName(
628                     InstanceDesignConstants.NEW_ATTRIBUTE_NAME, element);
629             attr.setName(str);
630             element.addChildAtIndex(attr, index);
631         }finally{
632             model.endTransaction();
633         }
634     }
635     
636     private void addTweenerListener(final TweenerPanel tweener){
637         tweener.addTweenerListener(new TweenerListener(){
638             public boolean dragAccept(DnDHelper.PaletteItem paletteItem) {
639                 //accept only Attributes
640
if(paletteItem != DnDHelper.PaletteItem.ATTRIBUTE){
641                     String JavaDoc str = NbBundle.getMessage(StartTagPanel.class,
642                             "MSG_ATTRIBUTE_TWEENER_DROP_REJECT");
643                     UIUtilities.showErrorMessageFor(str, context, StartTagPanel.this);
644                     return false;
645                 }
646                 String JavaDoc str = NbBundle.getMessage(StartTagPanel.class,
647                         "MSG_ATTRIBUTE_TWEENER_DROP_ACCEPT",
648                         getElementPanel().getAXIContainer().getName());
649                 UIUtilities.showBulbMessageFor(str, context, StartTagPanel.this);
650                 return true;
651             }
652             
653             public void drop(DnDHelper.PaletteItem paletteItem) {
654                 UIUtilities.hideGlassMessage();
655                 addNewAttributeAt(tweener);
656             }
657             
658             public void dragEntered(DnDHelper.PaletteItem paletteItem) {
659             }
660             
661             public void dragExited() {
662                 UIUtilities.hideGlassMessage();
663             }
664         });
665     }
666     
667     
668     public int getInterComponentSpacing() {
669         return INTER_PANEL_SPACE;
670     }
671     
672     public void addElement(){
673         if(context.isUserInducedEventMode()){
674             SwingUtilities.invokeLater(new Runnable JavaDoc(){
675                 public void run() {
676                     getElementPanel().setExpanded(true);
677                 }
678             });
679         }
680         AXIContainer elm = getElementPanel().getAXIContainer();
681         AXIModel model = elm.getModel();
682         Compositor comp = elm.getCompositor();
683         boolean addCompositorFirst =false;
684         //add a compositor first
685
//I explicitly start and end transaction to prevent 2 ElementPanels addition in the UI
686
if(comp == null){
687             comp = model.getComponentFactory().createSequence();
688             addCompositorFirst = true;
689         }
690         Element nelm = model.getComponentFactory().createElement();
691         String JavaDoc str = UIUtilities.getUniqueName(InstanceDesignConstants.NEW_ELEMENT_NAME, comp);
692         nelm.setName(str);
693         try{
694             model.startTransaction();
695             if(addCompositorFirst)
696                 elm.addCompositor(comp);
697             if(comp.getParent() != null && comp.getModel() != null)
698                 comp.addElement(nelm);
699         }finally{
700             model.endTransaction();
701         }
702     }
703     
704     
705     public void addCompositor(Compositor.CompositorType compType){
706         AXIContainer elm = getElementPanel().getAXIContainer();
707         AXIModel model = elm.getModel();
708         Compositor comp = null;
709         switch(compType){
710             case SEQUENCE:
711                 comp = model.getComponentFactory().createSequence();
712                 break;
713             case CHOICE:
714                 comp = model.getComponentFactory().createChoice();
715                 break;
716             case ALL:
717                 comp = model.getComponentFactory().createAll();
718                 break;
719         }
720         addCompositor(comp);
721     }
722     
723     
724     public void addCompositor(DnDHelper.PaletteItem compType){
725         AXIContainer elm = getElementPanel().getAXIContainer();
726         AXIModel model = elm.getModel();
727         Compositor comp = null;
728         switch(compType){
729             case SEQUENCE:
730                 comp = model.getComponentFactory().createSequence();
731                 break;
732             case CHOICE:
733                 comp = model.getComponentFactory().createChoice();
734                 break;
735             case ALL:
736                 comp = model.getComponentFactory().createAll();
737                 break;
738         }
739         addCompositor(comp);
740     }
741     
742     public void addCompositor(Compositor comp){
743         if(comp == null)
744             return;
745         AXIContainer elm = getElementPanel().getAXIContainer();
746         AXIModel model = elm.getModel();
747         
748         model.startTransaction();
749         try{
750             elm.addCompositor(comp);
751         }finally{
752             model.endTransaction();
753         }
754     }
755     
756     public void addAttribute(){
757         AXIContainer elm = getElementPanel().getAXIContainer();
758         AXIModel model = elm.getModel();
759         model.startTransaction();
760         try{
761             Attribute attr = model.getComponentFactory().createAttribute();
762             String JavaDoc str = UIUtilities.getUniqueName(
763                     InstanceDesignConstants.NEW_ATTRIBUTE_NAME, elm);
764             attr.setName(str);
765             elm.addAttribute(attr);
766         }finally{
767             model.endTransaction();
768         }
769     }
770     
771     
772     public void drop(DropTargetDropEvent JavaDoc event) {
773         //hide the bulb message before doing anything
774
UIUtilities.hideGlassMessage();
775         if(getElementPanel().getAXIContainer().isReadOnly()){
776             event.rejectDrop();
777             return;
778         }
779         setDragMode(false);
780         context.setUserInducedEventMode(true, this);
781         try{
782             super.drop(event);
783             if(DnDHelper.getDraggedPaletteItem(event) == DnDHelper.PaletteItem.ELEMENT){
784                 //append the new element
785
addElement();
786             }
787             if(DnDHelper.isCompositor(DnDHelper.getDraggedPaletteItem(event))){
788                 if(getElementPanel().getAXIContainer().getCompositor() != null){
789                     //compositor already added so reject this drop
790
event.rejectDrop();
791                     return;
792                 }else{
793                     //add a compositor;
794
addCompositor(DnDHelper.getDraggedPaletteItem(event));
795                 }
796             }
797             if(DnDHelper.getDraggedPaletteItem(event) == DnDHelper.PaletteItem.ATTRIBUTE){
798                 addAttribute();
799             }
800         }finally{
801             context.setUserInducedEventMode(false);
802         }
803     }
804     
805     public void dragOver(DropTargetDragEvent JavaDoc event) {
806         super.dragOver(event);
807         if(getElementPanel().getAXIContainer().isReadOnly()){
808             String JavaDoc str = NbBundle.getMessage(StartTagPanel.class,
809                     "MSG_READONLY_COMPOSITOR_DROP", getElementPanel().getAXIContainer().getName());
810             UIUtilities.showErrorMessageFor(str, context, this);
811             event.rejectDrag();
812             return;
813         }
814         DnDHelper.PaletteItem item = DnDHelper.getDraggedPaletteItem(event);
815         String JavaDoc messageKey = "MSG_ELEMENT_DROP_ACCEPT";
816         switch(item){
817             case ELEMENT:
818                 if(getElementPanel().getAXIContainer() instanceof Element){
819                     AXIType dt = ((Element)getElementPanel().getAXIContainer()).getType();
820                     if(dt != null){
821                         messageKey = (dt instanceof Datatype) ? "MSG_SIMPLE2COMPLEX_WARNING" :
822                             messageKey;
823                     }
824                 }
825             case ATTRIBUTE:
826                 String JavaDoc[] options = new String JavaDoc[]{
827                     item.toString().toLowerCase()
828                     , getElementPanel().getAXIContainer().getName()
829                 };
830                 String JavaDoc str = NbBundle.getMessage(StartTagPanel.class,
831                         messageKey, options );
832                 UIUtilities.showBulbMessageFor(str, context, this);
833                 return;
834             case CHOICE:
835             case SEQUENCE:
836             case ALL:
837                 if(getElementPanel().getAXIContainer().getCompositor() != null){
838                     options = new String JavaDoc[]{
839                         getElementPanel().getAXIContainer().getCompositor().getType().getName()
840                         , getElementPanel().getAXIContainer().getName()
841                     };
842                     //compositor already added so reject this drag
843
str = NbBundle.getMessage(StartTagPanel.class,
844                             "MSG_ELEMENT_COMPOSITOR_DROP_REJECT", options );
845                     UIUtilities.showErrorMessageFor(str, context, this);
846                     event.rejectDrag();
847                     return;
848                 }else{
849                     options = new String JavaDoc[]{
850                         item.toString().toLowerCase()
851                         , getElementPanel().getAXIContainer().getName()
852                     };
853                     str = NbBundle.getMessage(StartTagPanel.class,
854                             "MSG_ELEMENT_DROP_ACCEPT", options );
855                     UIUtilities.showBulbMessageFor(str, context, this);
856                 }
857                 return;
858             default:
859                 options = new String JavaDoc[]{
860                     getElementPanel().getAXIContainer().getName()
861                 };
862                 str = NbBundle.getMessage(StartTagPanel.class,
863                         "MSG_ELEMENT_DROP_REJECT", options );
864                 UIUtilities.showErrorMessageFor(str, context, this);
865                 event.rejectDrag();
866                 return;
867         }
868     }
869     
870     public void dragEnter(DropTargetDragEvent JavaDoc event) {
871         super.dragEnter(event);
872         setDragMode(true);
873         dragOver(event);
874     }
875     
876     public void dragExit(DropTargetEvent JavaDoc event) {
877         super.dragExit(event);
878         setDragMode(false);
879         UIUtilities.hideGlassMessage();
880     }
881     
882     private void setDragMode(boolean dragMode){
883         if(dragMode){
884             tagNameLabel.setForeground(Color.WHITE);
885             expandButton.setDragMode(true);
886             attributeCollapseButton.setDragMode(true);
887         }else{
888             tagNameLabel.setForeground(tagNameLabelColor);
889             expandButton.setDragMode(false);
890             attributeCollapseButton.setDragMode(false);
891         }
892         
893     }
894     
895     public JLabel JavaDoc getEndSlash() {
896         endSlash.setVisible(false);
897         return endSlash;
898     }
899     
900     
901     public void paintComponent(Graphics JavaDoc g){
902         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) g;
903         super.paintComponent(g2d);
904         
905         chainAttributeGroups(g2d);
906     }
907     
908     public JPanel JavaDoc getNewAXIContainerPropertiesPanel(){
909         return new ElementPropertiesPanel((AbstractElement) getElementPanel().getAXIContainer(), context);
910     }
911     
912     void showTagNameEditor(boolean firstTime) {
913         this.firstTimeRename = firstTime;
914         if(tagNameLabel != null)
915             tagNameLabel.showEditor();
916     }
917     
918     private void addSelectionListener() {
919         addPropertyChangeListener(new PropertyChangeListener JavaDoc(){
920             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
921                 if(evt.getPropertyName().equals(PROP_SELECTED)){
922                     if(((Boolean JavaDoc)evt.getNewValue()).booleanValue()){
923                         //set the tage name color to orange
924
//tagNameLabel.setForeground(InstanceDesignConstants.TAG_NAME_SELECTED_COLOR);
925
Font JavaDoc font = tagNameLabel.getFont();
926                         font = new Font JavaDoc(font.getName(), Font.BOLD, font.getSize());
927                         tagNameLabel.setFont(font);
928                         getElementPanel().repaint();
929                     }else{
930                         //set the tage name color to normal color
931
/*tagNameLabel.setForeground(InstanceDesignConstants.TAG_NAME_COLOR);
932                         if(getElementPanel().getAXIContainer().isReadOnly(context.getAXIModel())){
933                             tagNameLabel.setForeground(InstanceDesignConstants.TAG_NAME_READONLY_COLOR);
934                         }else{
935                             if(getElementPanel().getAXIContainer().getContentModel() != null){
936                                 //tagNameLabel.setForeground(InstanceDesignConstants.TAG_NAME_SELECTED_COLOR);
937                             }
938                         }*/

939                         Font JavaDoc font = tagNameLabel.getFont();
940                         font = new Font JavaDoc(font.getName(), Font.PLAIN, font.getSize());
941                         tagNameLabel.setFont(font);
942                         getElementPanel().repaint();
943                     }
944                     getEndSlash().setForeground(tagNameLabel.getForeground());
945                     getEndSlash().setFont(tagNameLabel.getFont());
946                 }
947             }
948         });
949     }
950     
951     protected void chainAttributeGroups(Graphics2D JavaDoc g2d) {
952         List JavaDoc<AttributePanel> attrPanels = new ArrayList JavaDoc<AttributePanel>();
953         for(Component JavaDoc comp: componentList){
954             if(comp instanceof AttributePanel)
955                 attrPanels.add((AttributePanel) comp);
956         }
957         int i = 0;
958         while(i < attrPanels.size()){
959             if(i+1 < attrPanels.size()){
960                 chainIfNeeded(attrPanels.get(i), attrPanels.get(i+1), g2d);
961             }
962             i++;
963         }
964     }
965     
966     public ABEBaseDropPanel getChildUIComponentFor(AXIComponent axiComponent) {
967         if(!(axiComponent instanceof AbstractAttribute)){
968             //i deal only with attributes. Rest all element panel should care
969
return getElementPanel().getChildUIComponentFor(axiComponent);
970         }
971         if(componentList == null)
972             return null;
973         showAttributes();
974         for(Component JavaDoc comp: componentList){
975             if(comp instanceof AttributePanel){
976                 if( ((AttributePanel)comp).getUIComponentFor(axiComponent) != null){
977                     return (AttributePanel) comp;
978                 }
979             }
980         }
981         return null;
982     }
983     
984     
985     public List JavaDoc<AttributePanel> getAttributePanels(){
986         if(componentList == null)
987             return Collections.EMPTY_LIST;
988         showAttributes();
989         List JavaDoc<AttributePanel> result = new ArrayList JavaDoc<AttributePanel>();
990         for(Component JavaDoc comp: componentList){
991             if(comp instanceof AttributePanel){
992                 result.add((AttributePanel) comp);
993             }
994         }
995         return result;
996     }
997     
998     private void chainIfNeeded(AttributePanel attr1, AttributePanel attr2, Graphics2D JavaDoc g2d) {
999         final int L_H_BOX_W = 5;
1000        final int R_H_BOX_W = 10;
1001        final int chainWidth = AttributePanel.getAttributePanelHeight();
1002        
1003        AbstractAttribute at1 = (AbstractAttribute) attr1.getAXIComponent();
1004        AbstractAttribute at2 = (AbstractAttribute) attr2.getAXIComponent();
1005        
1006        if(at1.getContentModel() != at2.getContentModel())
1007            return;
1008        
1009        
1010        
1011        if(at1.getContentModel() == null){
1012            //both are local attr
1013
return;
1014        }
1015        
1016        if(!at1.isShared()){
1017            //not an attr grp.
1018
return;
1019        }
1020        
1021        Color JavaDoc fillColor = InstanceDesignConstants.ATTR_BG_SHARED_COLOR;
1022        
1023        if(at1.isReadOnly())
1024            fillColor = InstanceDesignConstants.ATTR_BG_READONLY_COLOR;
1025        
1026        Rectangle JavaDoc r1 = attr1.getBounds();
1027        Rectangle JavaDoc r2 = attr2.getBounds();
1028        
1029        Color JavaDoc oldColor = g2d.getColor();
1030        g2d.setColor(fillColor);
1031        
1032        if(r1.y == r2.y){
1033            //attrs are next to each other
1034
//draw a horizontal chain
1035
int x, y, width, height;
1036            x = r1.x + r1.width;
1037            y = r1.y + (r1.height - chainWidth)/2;
1038            width = r2.x - x;
1039            height = chainWidth;
1040            //g2d.drawRect( x, y, width, height);
1041
g2d.fillRect( x, y, width, height);
1042        }else{
1043            //attrs are not in the same line
1044

1045            //draw right broken attr tape
1046
int xfudge = 8;
1047            
1048            Point JavaDoc pt = new Point JavaDoc(r1.x+r1.width+xfudge, r1.y);
1049            List JavaDoc<Point JavaDoc> plist = UIUtilities.getBrokenTapePoints(pt, r1.y+r1.height-1,
1050                    5, 4, true);
1051            Polygon JavaDoc rightTape = new Polygon JavaDoc();
1052            for(Point JavaDoc tpt: plist){
1053                rightTape.addPoint(tpt.x, tpt.y);
1054            }
1055            rightTape.addPoint(r1.x+r1.width, r1.y+r1.height-1);
1056            rightTape.addPoint(r1.x+r1.width, r1.y);
1057            g2d.draw(rightTape);
1058            g2d.fill(rightTape);
1059            
1060            //draw left broken attr tape
1061
xfudge -= 3;
1062            pt = new Point JavaDoc(r2.x - xfudge, r2.y);
1063            plist = UIUtilities.getBrokenTapePoints(pt, r2.y+r2.height-1,
1064                    5, 4, false);
1065            Polygon JavaDoc leftTape = new Polygon JavaDoc();
1066            for(Point JavaDoc tpt: plist){
1067                leftTape.addPoint(tpt.x, tpt.y);
1068            }
1069            leftTape.addPoint(r2.x, r2.y+r2.height-1);
1070            leftTape.addPoint(r2.x, r2.y);
1071            g2d.draw(leftTape);
1072            g2d.fill(leftTape);
1073        }
1074        g2d.setColor(oldColor);
1075    }
1076    
1077    public void accept(UIVisitor visitor) {
1078        visitor.visit(this);
1079    }
1080    
1081    
1082////////////////////////////////////////////////////////////////////////////
1083
// Instance members
1084
////////////////////////////////////////////////////////////////////////////
1085

1086    private InplaceEditableLabel tagNameLabel;
1087    
1088    List JavaDoc<Component JavaDoc> componentList;
1089    private Component JavaDoc firstTweener;
1090    
1091    private static final int ATTR_HEAD_ROOM_SPACE = 3;
1092    private static final int LABEL_HEAD_ROOM_SPACE = (TagPanel.getTagHeight() / 2) -9;
1093    private static final int INTER_PANEL_SPACE = 2;
1094    
1095    private final JLabel JavaDoc endSlash = new JLabel JavaDoc("");
1096    private JPanel JavaDoc elementPropertiesPanel;
1097    RoundExpandCollapseButton attributeCollapseButton = new RoundExpandCollapseButton("+", false);
1098    boolean attributesAreShown = false;
1099    JLabel JavaDoc attributeCountLabel = new JLabel JavaDoc();
1100    public static final int NO_OF_FIRST_ROW_ATTRS = 5;
1101    public static final int NO_OF_ATTRS_PER_ROW = 6;
1102    int firstAttrPos = getA() + INTER_PANEL_SPACE + 12;
1103    JLabel JavaDoc hgap = new JLabel JavaDoc();
1104}
1105
Popular Tags