1 20 21 package org.apache.directory.ldapstudio.browser.common.actions; 22 23 24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator; 25 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants; 26 import org.apache.directory.ldapstudio.browser.common.wizards.AttributeWizard; 27 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute; 28 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 29 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 30 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 31 import org.eclipse.jface.dialogs.MessageDialog; 32 import org.eclipse.jface.resource.ImageDescriptor; 33 import org.eclipse.jface.wizard.WizardDialog; 34 import org.eclipse.swt.widgets.Display; 35 36 37 43 public class NewAttributeAction extends BrowserAction 44 { 45 48 public NewAttributeAction() 49 { 50 super(); 51 } 52 53 54 57 public void dispose() 58 { 59 super.dispose(); 60 } 61 62 63 66 public void run() 67 { 68 69 IEntry entry = null; 70 if ( getInput() != null && getInput() instanceof IEntry ) 71 { 72 entry = ( IEntry ) getInput(); 73 } 74 else if ( getSelectedEntries().length > 0 ) 75 { 76 entry = getSelectedEntries()[0]; 77 } 78 else if ( getSelectedAttributes().length > 0 ) 79 { 80 entry = getSelectedAttributes()[0].getEntry(); 81 } 82 else if ( getSelectedValues().length > 0 ) 83 { 84 entry = getSelectedValues()[0].getAttribute().getEntry(); 85 } 86 87 if ( entry != null ) 88 { 89 AttributeWizard wizard = new AttributeWizard( "New Attribute", true, true, null, entry ); 90 WizardDialog dialog = new WizardDialog( getShell(), wizard ); 91 dialog.setBlockOnOpen( true ); 92 dialog.create(); 93 if ( dialog.open() == WizardDialog.OK ) 94 { 95 String newAttributeDescription = wizard.getAttributeDescription(); 96 if ( newAttributeDescription != null && !"".equals( newAttributeDescription ) ) 97 { 98 try 99 { 100 IAttribute att = entry.getAttribute( newAttributeDescription ); 101 if ( att == null ) 102 { 103 att = new Attribute( entry, newAttributeDescription ); 104 entry.addAttribute( att ) ; 105 } 106 107 att.addEmptyValue(); 108 } 109 catch ( ModelModificationException mme ) 110 { 111 MessageDialog.openError( Display.getDefault().getActiveShell(), "Error While Adding Attribute", 112 mme.getMessage() ); 113 } 114 } 115 } 116 } 117 } 118 119 120 123 public String getText() 124 { 125 return "New Attribute..."; 126 } 127 128 129 132 public ImageDescriptor getImageDescriptor() 133 { 134 return BrowserCommonActivator.getDefault().getImageDescriptor( BrowserCommonConstants.IMG_ATTRIBUTE_ADD ); 135 } 136 137 138 141 public String getCommandId() 142 { 143 return "org.apache.directory.ldapstudio.browser.action.addAttribute"; 144 } 145 146 147 150 public boolean isEnabled() 151 { 152 153 if ( ( getSelectedSearchResults().length == 1 && getSelectedAttributes().length > 0 ) ) 154 { 155 return false; 156 } 157 158 return ( ( getInput() != null && getInput() instanceof IEntry ) || getSelectedEntries().length == 1 159 || getSelectedAttributes().length > 0 || getSelectedValues().length > 0 ); 160 } 161 } 162 | Popular Tags |