KickJava   Java API By Example, From Geeks To Geeks.

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


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.Cursor JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.FlowLayout JavaDoc;
26 import java.awt.dnd.DropTargetDragEvent JavaDoc;
27 import java.awt.dnd.DropTargetDropEvent JavaDoc;
28 import java.awt.dnd.DropTargetEvent JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.awt.event.KeyEvent JavaDoc;
32 import java.awt.event.KeyListener JavaDoc;
33 import java.awt.event.MouseAdapter JavaDoc;
34 import java.awt.event.MouseEvent JavaDoc;
35 import java.beans.PropertyChangeEvent JavaDoc;
36 import java.beans.PropertyChangeListener JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JTextField JavaDoc;
39 import javax.swing.border.Border JavaDoc;
40 import javax.swing.border.EmptyBorder JavaDoc;
41 import javax.swing.border.LineBorder JavaDoc;
42 import org.netbeans.modules.xml.axi.AXIComponent;
43 import org.netbeans.modules.xml.axi.AXIContainer;
44 import org.netbeans.modules.xml.axi.AXIModel;
45 import org.netbeans.modules.xml.axi.AbstractAttribute;
46 import org.netbeans.modules.xml.axi.AnyAttribute;
47 import org.netbeans.modules.xml.axi.Attribute;
48 import org.netbeans.modules.xml.axi.ContentModel;
49 import org.netbeans.modules.xml.schema.abe.nodes.ABEAbstractNode;
50 import org.netbeans.modules.xml.schema.abe.nodes.AttributeNode;
51 import org.netbeans.modules.xml.schema.abe.palette.DnDHelper;
52 import org.openide.util.NbBundle;
53
54 /*import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedLocalAttributeCustomizer;
55 import org.openide.DialogDescriptor;
56 import org.openide.DialogDisplayer;*/

57
58 /**
59  *
60  *
61  * @author Todd Fast, todd.fast@sun.com
62  */

63 public class AttributePanel extends ABEBaseDropPanel {
64     private static final long serialVersionUID = 7526472295622776147L;
65     private static final Border JavaDoc normalBorder = new
66             EmptyBorder JavaDoc(2,2,2,2);//LineBorder(Color.WHITE/*InstanceDesignConstants.TAG_OUTLINE_COLOR*/, 1);
67
private static final Border JavaDoc selectedBorder =
68             new LineBorder JavaDoc(InstanceDesignConstants.XP_ORANGE, 2);
69     /**
70      * Default constructor
71      *
72      */

73     public AttributePanel(StartTagPanel elementPanel, AbstractAttribute attribute
74             , final InstanceUIContext context) {
75         super(context);
76         this.startTagPanel=elementPanel;
77         this.attribute=attribute;
78         initialize();
79         attribute.addPropertyChangeListener(new ModelEventMediator(this, attribute) {
80             public void _propertyChange(PropertyChangeEvent JavaDoc evt) {
81                 String JavaDoc str = evt.getPropertyName();
82                 if(str.equals(Attribute.PROP_NAME) || str.equals(Attribute.PROP_TYPE))
83                     attributePropertyChangeAction(evt);
84             }
85         });
86         initKeyListener();
87         addSelectionListener();
88         makeNBNode();
89         //installFocusManager();
90
}
91     
92     protected void initMouseListener(){
93         addMouseListener(new MouseAdapter JavaDoc() {
94             public void mouseReleased(MouseEvent JavaDoc e) {
95                 mouseClickedActionHandler(e, true);
96             }
97             
98             public void mousePressed(MouseEvent JavaDoc e) {
99                 mouseClickedActionHandler(e, true);
100             }
101             
102             public void mouseClicked(MouseEvent JavaDoc e) {
103                 mouseClickedActionHandler(e, false);
104             }
105             
106         });
107     }
108     
109     /**
110      *
111      *
112      */

113     Color JavaDoc attrColor = InstanceDesignConstants.ATTRIBUTE_COLOR;
114     Color JavaDoc attrBGColor = InstanceDesignConstants.NO_BACKGROUND_COLOR;
115     private void initialize() {
116         setLayout(new FlowLayout JavaDoc(FlowLayout.CENTER, 1, HEAD_ROOM_SPACE));
117         AbstractAttribute attr = getAttribute();
118         boolean shared = attr.isShared();/*attr.getContentModel() == null ? false :
119             ((attr.getContentModel().getType() == ContentModel.ContentModelType.ATTRIBUTE_GROUP) ?
120                 true : false);*/

121         boolean readOnly = attr.isReadOnly();
122         String JavaDoc valueTTP = NbBundle.getMessage(AttributePanel.class, "TTP_ATTR_LABEL_VALUE");
123         String JavaDoc nameTTP = NbBundle.getMessage(AttributePanel.class, "TTP_ATTR_LABEL_NAME");
124         String JavaDoc attrTTP = NbBundle.getMessage(AttributePanel.class, "TTP_ATTR");
125         setOpaque(false);
126         setBorder(normalBorder);
127         if(shared || attr.isGlobal()){
128             attrBGColor = InstanceDesignConstants.ATTR_BG_SHARED_COLOR;
129             setBackground(attrBGColor);
130             setOpaque(true);
131             attrColor = InstanceDesignConstants.ATTRIBUTE_COLOR;
132             if(attr.isGlobal()){
133                 valueTTP = NbBundle.getMessage(AttributePanel.class, "TTP_ATTR_LABEL_VALUE_GLOBAL");
134                 nameTTP = NbBundle.getMessage(AttributePanel.class, "TTP_ATTR_LABEL_NAME_GLOBAL");
135                 attrTTP = NbBundle.getMessage(AttributePanel.class, "TTP_ATTR_GLOBAL");
136             }else{
137                 if(attr.getContentModel() != null){
138                     String JavaDoc name = attr.getContentModel().getName();
139                     String JavaDoc typeStr = attr.getContentModel().getType().equals(ContentModel.ContentModelType.ATTRIBUTE_GROUP) ?
140                         "LBL_GLOBAL_ATTRIBUTE_GROUP" : "LBL_GLOBAL_COMPLEX_TYPE";
141                     
142                     typeStr = NbBundle.getMessage(AttributePanel.class, typeStr);
143                     
144                     valueTTP = NbBundle.getMessage(AttributePanel.class,
145                             "TTP_ATTR_LABEL_VALUE_SHARED", typeStr, name);
146                     nameTTP = NbBundle.getMessage(AttributePanel.class,
147                             "TTP_ATTR_LABEL_NAME_SHARED", typeStr, name);
148                     attrTTP = NbBundle.getMessage(AttributePanel.class,
149                             "TTP_ATTR_SHARED", typeStr, name);
150                 }else{
151                     String JavaDoc elmName = attr.getParentElement().getName();
152                     valueTTP = NbBundle.getMessage(AttributePanel.class,
153                             "TTP_ATTR_LABEL_VALUE_SHARED_NO_CM", elmName);
154                     nameTTP = NbBundle.getMessage(AttributePanel.class,
155                             "TTP_ATTR_LABEL_NAME_SHARED_NO_CM", elmName);
156                     attrTTP = NbBundle.getMessage(AttributePanel.class,
157                             "TTP_ATTR_SHARED_NO_CM", elmName);
158                 }
159             }
160         }
161         if(readOnly){
162             attrBGColor = InstanceDesignConstants.ATTR_BG_READONLY_COLOR;
163             setBackground(attrBGColor);
164             setOpaque(true);
165             attrColor = InstanceDesignConstants.TAG_NAME_READONLY_COLOR;
166             valueTTP = NbBundle.getMessage(AttributePanel.class, "TTP_ATTR_LABEL_VALUE_READONLY");
167             nameTTP = NbBundle.getMessage(AttributePanel.class, "TTP_ATTR_LABEL_NAME_READONLY");
168             attrTTP = NbBundle.getMessage(AttributePanel.class, "TTP_ATTR_READONLY");
169         }
170         
171         setToolTipText(attrTTP);
172         
173         attributeNameLabel=new InplaceEditableLabel(attr.getName());
174         attributeNameLabel.setForeground(attrColor);
175         attributeNameLabel.setToolTipText(nameTTP);
176         add(attributeNameLabel);
177         
178         
179         
180         equals = new JLabel JavaDoc(" = ");
181         equals.setForeground(attrColor);
182         equals.setToolTipText(attrTTP);
183         add(equals);
184         //transmit mouse click events on eq sign to the attr panel
185
equals.addMouseListener(new MouseAdapter JavaDoc(){
186             public void mouseReleased(MouseEvent JavaDoc e) {
187                 AttributePanel.this.dispatchEvent(e);
188             }
189             
190             public void mousePressed(MouseEvent JavaDoc e) {
191                 AttributePanel.this.dispatchEvent(e);
192             }
193             
194             public void mouseClicked(MouseEvent JavaDoc e) {
195                 AttributePanel.this.dispatchEvent(e);
196             }
197             
198         });
199         
200         
201         
202         String JavaDoc value="?";
203         if(attr instanceof Attribute) {
204             Attribute a = (Attribute)attr;
205             if ( a.getType()!=null)
206                 value=a.getType().getName();
207         }
208         
209         attributeValueLabel=new InplaceEditableLabel(value);
210         attributeValueLabel.setForeground(attrColor);
211         attributeValueLabel.setToolTipText(valueTTP);
212         add(attributeValueLabel);
213         
214         
215         
216         refreshAttributeParameters();
217         initAttributeEditListeners();
218         initMouseListener();
219     }
220     
221     protected void initKeyListener(){
222         addKeyListener(new KeyListener JavaDoc() {
223             public void keyPressed(KeyEvent JavaDoc e) {
224                 //name edit
225
if( e.getKeyCode() == e.VK_F2 ){
226                     attributeNameLabel.showEditor();
227                 }
228                 if(context.getFocusTraversalManager().isFocusChangeEvent(e))
229                     context.getFocusTraversalManager().handleEvent(e, AttributePanel.this);
230             }
231             public void keyReleased(KeyEvent JavaDoc e) {
232             }
233             public void keyTyped(KeyEvent JavaDoc e) {
234                 if(e.getKeyChar() == e.VK_SPACE){
235                     attributeNameLabel.showEditor();
236                 }
237             }
238         });
239     }
240     
241     protected void mouseClickedActionHandler(MouseEvent JavaDoc e, boolean handelPopupOnly) {
242         if(e.getClickCount() == 1){
243             if(e.isPopupTrigger()){
244                 context.getMultiComponentActionManager().showPopupMenu(e, this);
245                 return;
246             }
247             if(handelPopupOnly)
248                 return;
249             //the attr is selected
250
if(e.isControlDown())
251                 context.getComponentSelectionManager().addToSelectedComponents(this);
252             else
253                 context.getComponentSelectionManager().setSelectedComponent(this);
254         }
255     }
256     
257     
258     /**
259      *
260      *
261      */

262     public AbstractAttribute getAttribute() {
263         return attribute;
264     }
265     
266     public void refreshAttributeParameters(){
267         attributeNameLabel.setText(getAttribute().toString());
268         String JavaDoc value="?";
269         AbstractAttribute attr = getAttribute();
270         if(attr instanceof Attribute) {
271             Attribute a = (Attribute)attr;
272             if ( a.getType()!=null)
273                 value=a.getType().getName();
274         }
275         
276         attributeValueLabel.setText(value);
277     }
278     
279     
280     public Dimension JavaDoc getPreferredSize() {
281         return new Dimension JavaDoc(super.getPreferredSize().width, getAttributePanelHeight());
282     }
283     
284     private void initAttributeEditListeners(){
285         initAttributeNameEditListener();
286         initAttributeValueEditListener();
287     }
288     
289     
290     protected void initAttributeNameEditListener(){
291         
292         attributeNameLabel.addCtrlClickHandler(new InplaceEditableLabel.CtrlClickHandler(){
293             public void handleCtrlClick() {
294                 getNBNode().showSuperDefinition();
295             }
296         });
297         
298         attributeNameLabel.setInputValidator(new InputValidator(){
299             public boolean isStringValid(String JavaDoc input) {
300                 return org.netbeans.modules.xml.xam.dom.Utils.isValidNCName(input);
301             }
302         }, NbBundle.getMessage(AttributePanel.class, "MSG_NOT_A_NCNAME"));
303         attributeNameLabel.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
304             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
305                 if(evt.getPropertyName().equals(InplaceEditableLabel.PROPERTY_MODE_CHANGE)){
306                     if(evt.getNewValue() == InplaceEditableLabel.Mode.EDIT){
307                         //user selected edit give the editor JComponent
308
//show a text field
309
final JTextField JavaDoc field = new JTextField JavaDoc(getAttribute().toString());
310                         field.select(0, field.getText().length());
311                         field.addActionListener(new ActionListener JavaDoc(){
312                             public void actionPerformed(ActionEvent JavaDoc e) {
313                                 String JavaDoc newName = field.getText();
314                                 //do validation
315
if(org.netbeans.modules.xml.xam.dom.Utils.isValidNCName(newName)){
316                                     field.setCursor(new Cursor JavaDoc(Cursor.WAIT_CURSOR));
317                                     try{
318                                         if(firstTimeRename)
319                                             getNBNode().setNameInModel(newName);
320                                         else
321                                             setAttrNameInModel(newName);
322                                         firstTimeRename = false;
323                                     }finally{
324                                         field.setCursor(new Cursor JavaDoc(Cursor.DEFAULT_CURSOR));
325                                     }
326                                 }
327                             }
328                         });
329                         
330                         if(attribute.isShared() || attribute.isGlobal()){
331                             String JavaDoc str = NbBundle.getMessage(StartTagPanel.class, "MSG_SHARED_ATTRIBUTE_EDIT");
332                             attributeNameLabel.setEditInfoText(str, context);
333                         }
334                         if(!attribute.isReadOnly()){
335                             if(attribute instanceof AnyAttribute){
336                                 String JavaDoc str = NbBundle.getMessage(StartTagPanel.class, "MSG_ANY_ATTRIBUTE_EDIT");
337                                 attributeNameLabel.setEditInfoText(str, context);
338                             }else{
339                                 //attributeNameLabel.setWidthMagnificationFactor(1);
340
attributeNameLabel.setInlineEditorComponent(field);
341                             }
342                         }else{
343                             String JavaDoc str = NbBundle.getMessage(StartTagPanel.class, "MSG_READONLY_ATTRIBUTE_EDIT");
344                             attributeNameLabel.setEditInfoText(str, context);
345                         }
346                     }
347                 }
348             }
349         });
350     }
351     
352     protected void initAttributeValueEditListener(){
353         attributeValueLabel.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
354             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
355                 if(evt.getPropertyName().equals(InplaceEditableLabel.PROPERTY_MODE_CHANGE)){
356                     if(evt.getNewValue() == InplaceEditableLabel.Mode.EDIT){
357                         attributeValueLabel.hideEditor();
358                     }
359                 }
360             }
361         });
362     }
363     
364     private void setAttrNameInModel(String JavaDoc name){
365         getNBNode().setName(name);
366     }
367     
368     private void setAttrValueInModel(String JavaDoc name){
369         //TODO
370
}
371     
372     public void removeAttribute(){
373         AXIContainer elm = startTagPanel.getElementPanel().getAXIContainer();
374         AXIModel model = elm.getModel();
375         if(model != null){
376             model.startTransaction();
377             try{
378                 elm.removeAttribute(attribute);
379             }finally{
380                 model.endTransaction();
381             }
382         }
383     }
384     
385     private void attributePropertyChangeAction(PropertyChangeEvent JavaDoc evt){
386         refreshAttributeParameters();
387     }
388     
389     
390     private void addAttributeToAttrGrp() {
391         /*if(getAttribute().getContentModel().getType() ==
392                 ContentModel.ContentModelType.ATTRIBUTE_GROUP){*/

393         ContentModel cm = getAttribute().getContentModel();
394         AXIModel model = context.getAXIModel();
395         model.startTransaction();
396         try{
397             Attribute attr = model.getComponentFactory().createAttribute();
398             String JavaDoc str = UIUtilities.getUniqueName(
399                     InstanceDesignConstants.NEW_ATTRIBUTE_NAME, cm);
400             attr.setName(str);
401             cm.addAttribute(attr);
402         }finally{
403             model.endTransaction();
404         }
405         //}
406
}
407     
408     public void drop(DropTargetDropEvent JavaDoc event) {
409         removeDragFeedback();
410         ContentModel cm = getAttribute().getContentModel();
411         if((cm != null) && (cm.getType() ==
412                 ContentModel.ContentModelType.ATTRIBUTE_GROUP)){
413             addAttributeToAttrGrp();
414         }else{
415             event.rejectDrop();
416         }
417     }
418     
419     public void dragOver(DropTargetDragEvent JavaDoc event) {
420         DnDHelper.PaletteItem item = DnDHelper.getDraggedPaletteItem(event);
421         if(item != DnDHelper.PaletteItem.ATTRIBUTE){
422             event.rejectDrag();
423             return;
424         }
425         ContentModel cm = getAttribute().getContentModel();
426         if((cm != null) && (cm.getType() ==
427                 ContentModel.ContentModelType.ATTRIBUTE_GROUP)){
428             return;
429         }
430         event.rejectDrag();
431     }
432     
433     public void dragEnter(DropTargetDragEvent JavaDoc event) {
434         DnDHelper.PaletteItem item = DnDHelper.getDraggedPaletteItem(event);
435         if(item != DnDHelper.PaletteItem.ATTRIBUTE){
436             String JavaDoc str = NbBundle.getMessage(AttributePanel.class,
437                     "MSG_ATTRIBUTE_PANEL_DROP_REJECT_WRONG_COMPONENT", this.attribute.getName());
438             UIUtilities.showErrorMessageFor(str, context, this);
439             event.rejectDrag();
440         }
441         
442         setDragFeedback();
443         ContentModel cm = getAttribute().getContentModel();
444         if((cm != null) && (cm.getType() ==
445                 ContentModel.ContentModelType.ATTRIBUTE_GROUP)){
446             String JavaDoc str = NbBundle.getMessage(AttributePanel.class,
447                     "MSG_ATTRIBUTE_PANEL_DROP_ACCEPT", getAttribute().getContentModel().getName());
448             UIUtilities.showBulbMessageFor(str, context, this);
449             return;
450         }
451         String JavaDoc str = NbBundle.getMessage(AttributePanel.class,
452                 "MSG_ATTRIBUTE_PANEL_DROP_REJECT", this.attribute.getName());
453         UIUtilities.showErrorMessageFor(str, context, this);
454         event.rejectDrag();
455     }
456     
457     public void dragExit(DropTargetEvent JavaDoc event) {
458         removeDragFeedback();
459     }
460     
461     
462     public void setDragFeedback(){
463         setBackground(InstanceDesignConstants.DARK_BLUE);
464         setOpaque(true);
465         attributeNameLabel.setForeground(Color.WHITE);
466         attributeValueLabel.setForeground(Color.WHITE);
467         equals.setForeground(Color.WHITE);
468         setBorder(new LineBorder JavaDoc(InstanceDesignConstants.DARK_BLUE, 2));
469         revalidate();
470     }
471     
472     public void removeDragFeedback(){
473         UIUtilities.hideGlassMessage();
474         if(attrBGColor == InstanceDesignConstants.NO_BACKGROUND_COLOR)
475             setOpaque(false);
476         else
477             setBackground(attrBGColor);
478         attributeNameLabel.setForeground(attrColor);
479         attributeValueLabel.setForeground(attrColor);
480         equals.setForeground(attrColor);
481         setBorder(normalBorder);
482         revalidate();
483     }
484     
485     void showEditorForName(boolean firstTimeRename) {
486         this.firstTimeRename = firstTimeRename;
487         attributeNameLabel.showEditor();
488     }
489     
490     public static int getAttributePanelHeight() {
491         return ATTR_HEIGHT;
492     }
493     
494     protected void makeNBNode() {
495         attributeNode = new AttributeNode(attribute, context);
496         if(attribute.isReadOnly())
497             attributeNode.setReadOnly(true);
498     }
499     
500     public ABEAbstractNode getNBNode() {
501         return attributeNode;
502     }
503     
504     public AXIComponent getAXIComponent(){
505         return attribute;
506     }
507     
508     private void addSelectionListener() {
509         addPropertyChangeListener(new PropertyChangeListener JavaDoc(){
510             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
511                 if(evt.getPropertyName().equals(PROP_SELECTED)){
512                     if(((Boolean JavaDoc)evt.getNewValue()).booleanValue()){
513                         /*Font font = attributeNameLabel.getFont();
514                         font = new Font(font.getName(), Font.BOLD, font.getSize());
515                         attributeNameLabel.setFont(font);
516                         attributeValueLabel.setFont(font);
517                         equals.setFont(font);*/

518                         
519                         attributeNameLabel.setForeground(Color.BLACK);
520                         attributeValueLabel.setForeground(Color.BLACK);
521                         equals.setForeground(Color.BLACK);
522                         setBorder(selectedBorder);
523                         revalidate();
524                     }else{
525                         attributeNameLabel.setForeground(attrColor);
526                         attributeValueLabel.setForeground(attrColor);
527                         equals.setForeground(attrColor);
528                         setBorder(normalBorder);
529                         revalidate();
530                     }
531                     
532                 }
533             }
534         });
535     }
536     
537     public void accept(UIVisitor visitor) {
538         visitor.visit(this);
539     }
540     
541     
542     
543     ////////////////////////////////////////////////////////////////////////////
544
// Class members
545
////////////////////////////////////////////////////////////////////////////
546

547     private static final int ATTR_HEIGHT = TagPanel.getTagHeight() - 10;
548     private static final int INTER_LABEL_SPACE = 5;
549     public static final int HEAD_ROOM_SPACE = (getAttributePanelHeight()/2) - 11;
550     
551     
552     ////////////////////////////////////////////////////////////////////////////
553
// Instance members
554
////////////////////////////////////////////////////////////////////////////
555

556     private JLabel JavaDoc equals;
557     private StartTagPanel startTagPanel;
558     private AbstractAttribute attribute;
559     private InplaceEditableLabel attributeNameLabel;
560     private InplaceEditableLabel attributeValueLabel;
561     private AttributeNode attributeNode;
562     
563 }
564
Popular Tags