1 19 20 package org.netbeans.modules.xml.schema.abe; 21 22 import java.awt.Color ; 23 import java.awt.Cursor ; 24 import java.awt.Dimension ; 25 import java.awt.FlowLayout ; 26 import java.awt.dnd.DropTargetDragEvent ; 27 import java.awt.dnd.DropTargetDropEvent ; 28 import java.awt.dnd.DropTargetEvent ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.KeyEvent ; 32 import java.awt.event.KeyListener ; 33 import java.awt.event.MouseAdapter ; 34 import java.awt.event.MouseEvent ; 35 import java.beans.PropertyChangeEvent ; 36 import java.beans.PropertyChangeListener ; 37 import javax.swing.JLabel ; 38 import javax.swing.JTextField ; 39 import javax.swing.border.Border ; 40 import javax.swing.border.EmptyBorder ; 41 import javax.swing.border.LineBorder ; 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 57 58 63 public class AttributePanel extends ABEBaseDropPanel { 64 private static final long serialVersionUID = 7526472295622776147L; 65 private static final Border normalBorder = new 66 EmptyBorder (2,2,2,2); private static final Border selectedBorder = 68 new LineBorder (InstanceDesignConstants.XP_ORANGE, 2); 69 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 evt) { 81 String 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 } 91 92 protected void initMouseListener(){ 93 addMouseListener(new MouseAdapter () { 94 public void mouseReleased(MouseEvent e) { 95 mouseClickedActionHandler(e, true); 96 } 97 98 public void mousePressed(MouseEvent e) { 99 mouseClickedActionHandler(e, true); 100 } 101 102 public void mouseClicked(MouseEvent e) { 103 mouseClickedActionHandler(e, false); 104 } 105 106 }); 107 } 108 109 113 Color attrColor = InstanceDesignConstants.ATTRIBUTE_COLOR; 114 Color attrBGColor = InstanceDesignConstants.NO_BACKGROUND_COLOR; 115 private void initialize() { 116 setLayout(new FlowLayout (FlowLayout.CENTER, 1, HEAD_ROOM_SPACE)); 117 AbstractAttribute attr = getAttribute(); 118 boolean shared = attr.isShared(); 121 boolean readOnly = attr.isReadOnly(); 122 String valueTTP = NbBundle.getMessage(AttributePanel.class, "TTP_ATTR_LABEL_VALUE"); 123 String nameTTP = NbBundle.getMessage(AttributePanel.class, "TTP_ATTR_LABEL_NAME"); 124 String 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 name = attr.getContentModel().getName(); 139 String 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 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 (" = "); 181 equals.setForeground(attrColor); 182 equals.setToolTipText(attrTTP); 183 add(equals); 184 equals.addMouseListener(new MouseAdapter (){ 186 public void mouseReleased(MouseEvent e) { 187 AttributePanel.this.dispatchEvent(e); 188 } 189 190 public void mousePressed(MouseEvent e) { 191 AttributePanel.this.dispatchEvent(e); 192 } 193 194 public void mouseClicked(MouseEvent e) { 195 AttributePanel.this.dispatchEvent(e); 196 } 197 198 }); 199 200 201 202 String 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 () { 223 public void keyPressed(KeyEvent e) { 224 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 e) { 232 } 233 public void keyTyped(KeyEvent e) { 234 if(e.getKeyChar() == e.VK_SPACE){ 235 attributeNameLabel.showEditor(); 236 } 237 } 238 }); 239 } 240 241 protected void mouseClickedActionHandler(MouseEvent 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 if(e.isControlDown()) 251 context.getComponentSelectionManager().addToSelectedComponents(this); 252 else 253 context.getComponentSelectionManager().setSelectedComponent(this); 254 } 255 } 256 257 258 262 public AbstractAttribute getAttribute() { 263 return attribute; 264 } 265 266 public void refreshAttributeParameters(){ 267 attributeNameLabel.setText(getAttribute().toString()); 268 String 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 getPreferredSize() { 281 return new Dimension (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 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 () { 304 public void propertyChange(PropertyChangeEvent evt) { 305 if(evt.getPropertyName().equals(InplaceEditableLabel.PROPERTY_MODE_CHANGE)){ 306 if(evt.getNewValue() == InplaceEditableLabel.Mode.EDIT){ 307 final JTextField field = new JTextField (getAttribute().toString()); 310 field.select(0, field.getText().length()); 311 field.addActionListener(new ActionListener (){ 312 public void actionPerformed(ActionEvent e) { 313 String newName = field.getText(); 314 if(org.netbeans.modules.xml.xam.dom.Utils.isValidNCName(newName)){ 316 field.setCursor(new Cursor (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 (Cursor.DEFAULT_CURSOR)); 325 } 326 } 327 } 328 }); 329 330 if(attribute.isShared() || attribute.isGlobal()){ 331 String 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 str = NbBundle.getMessage(StartTagPanel.class, "MSG_ANY_ATTRIBUTE_EDIT"); 337 attributeNameLabel.setEditInfoText(str, context); 338 }else{ 339 attributeNameLabel.setInlineEditorComponent(field); 341 } 342 }else{ 343 String 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 () { 354 public void propertyChange(PropertyChangeEvent 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 name){ 365 getNBNode().setName(name); 366 } 367 368 private void setAttrValueInModel(String name){ 369 } 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 evt){ 386 refreshAttributeParameters(); 387 } 388 389 390 private void addAttributeToAttrGrp() { 391 393 ContentModel cm = getAttribute().getContentModel(); 394 AXIModel model = context.getAXIModel(); 395 model.startTransaction(); 396 try{ 397 Attribute attr = model.getComponentFactory().createAttribute(); 398 String str = UIUtilities.getUniqueName( 399 InstanceDesignConstants.NEW_ATTRIBUTE_NAME, cm); 400 attr.setName(str); 401 cm.addAttribute(attr); 402 }finally{ 403 model.endTransaction(); 404 } 405 } 407 408 public void drop(DropTargetDropEvent 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 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 event) { 434 DnDHelper.PaletteItem item = DnDHelper.getDraggedPaletteItem(event); 435 if(item != DnDHelper.PaletteItem.ATTRIBUTE){ 436 String 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 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 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 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 (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 (){ 510 public void propertyChange(PropertyChangeEvent evt) { 511 if(evt.getPropertyName().equals(PROP_SELECTED)){ 512 if(((Boolean )evt.getNewValue()).booleanValue()){ 513 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 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 556 private JLabel 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 |