1 20 21 package org.apache.directory.ldapstudio.browser.ui.wizards; 22 23 24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator; 25 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidget; 26 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetActionGroup; 27 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetActionGroupWithAttribute; 28 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetConfiguration; 29 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetUniversalListener; 30 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.OpenDefaultEditorAction; 31 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent; 32 import org.apache.directory.ldapstudio.browser.core.events.EntryUpdateListener; 33 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 34 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute; 35 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyEntry; 36 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 37 import org.apache.directory.ldapstudio.browser.core.model.IValue; 38 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 39 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants; 40 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin; 41 import org.eclipse.jface.viewers.StructuredSelection; 42 import org.eclipse.jface.wizard.WizardPage; 43 import org.eclipse.swt.SWT; 44 import org.eclipse.swt.layout.GridData; 45 import org.eclipse.swt.layout.GridLayout; 46 import org.eclipse.swt.widgets.Composite; 47 48 49 56 public class NewEntryAttributesWizardPage extends WizardPage implements EntryUpdateListener 57 { 58 59 60 private NewEntryWizard wizard; 61 62 63 private EntryEditorWidgetConfiguration configuration; 64 65 66 private EntryEditorWidgetActionGroup actionGroup; 67 68 69 private EntryEditorWidget mainWidget; 70 71 72 private EntryEditorWidgetUniversalListener universalListener; 73 74 75 81 public NewEntryAttributesWizardPage( String pageName, NewEntryWizard wizard ) 82 { 83 super( pageName ); 84 setTitle( "Attributes" ); 85 setDescription( "Please enter the attributes for the entry. Enter at least the MUST attributes." ); 86 setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_ENTRY_WIZARD ) ); 87 setPageComplete( false ); 88 89 this.wizard = wizard; 90 } 91 92 93 96 public void dispose() 97 { 98 if ( configuration != null ) 99 { 100 EventRegistry.removeEntryUpdateListener( this ); 101 universalListener.dispose(); 102 universalListener = null; 103 mainWidget.dispose(); 104 mainWidget = null; 105 actionGroup.dispose(); 106 actionGroup = null; 107 configuration.dispose(); 108 configuration = null; 109 } 110 super.dispose(); 111 } 112 113 114 121 public void setVisible( boolean visible ) 122 { 123 super.setVisible( visible ); 124 125 if ( visible ) 126 { 127 DummyEntry newEntry = wizard.getPrototypeEntry(); 128 IValue editValue = null; 129 130 try 131 { 132 EventRegistry.suspendEventFireingInCurrentThread(); 133 134 String [] oldMust = newEntry.getSubschema().getMustAttributeNames(); 138 for ( int i = 0; i < oldMust.length; i++ ) 139 { 140 IAttribute attribute = newEntry.getAttribute( oldMust[i] ); 141 if ( attribute != null ) 142 { 143 IValue[] values = attribute.getValues(); 144 for ( int v = 0; v < values.length; v++ ) 145 { 146 if ( values[v].isEmpty() ) 147 { 148 attribute.deleteValue( values[v] ); 149 } 150 } 151 if ( attribute.getValueSize() == 0 ) 152 { 153 newEntry.deleteAttribute( attribute ); 154 } 155 } 156 } 157 158 String [] newMust = newEntry.getSubschema().getMustAttributeNames(); 160 for ( int i = 0; i < newMust.length; i++ ) 161 { 162 if ( newEntry.getAttribute( newMust[i] ) == null ) 163 { 164 IAttribute att = new Attribute( newEntry, newMust[i] ); 165 newEntry.addAttribute( att ); 166 att.addEmptyValue(); 167 168 if ( editValue == null ) 169 { 170 editValue = att.getValues()[0]; 171 } 172 } 173 } 174 } 175 catch ( ModelModificationException e ) 176 { 177 e.printStackTrace(); 178 } 179 finally 180 { 181 EventRegistry.resumeEventFireingInCurrentThread(); 182 } 183 184 mainWidget.getViewer().setInput( newEntry ); 186 mainWidget.getViewer().refresh(); 187 validate(); 188 189 mainWidget.getViewer().getControl().setFocus(); 191 192 if ( editValue != null ) 194 { 195 mainWidget.getViewer().setSelection( new StructuredSelection( editValue ), true ); 196 OpenDefaultEditorAction openDefaultEditorAction = actionGroup.getOpenDefaultEditorAction(); 197 if ( openDefaultEditorAction.isEnabled() ) 198 { 199 openDefaultEditorAction.run(); 200 } 201 } 202 } 203 else 204 { 205 mainWidget.getViewer().setInput( "" ); 206 mainWidget.getViewer().refresh(); 207 setPageComplete( false ); 208 } 209 } 210 211 212 215 private void validate() 216 { 217 if ( wizard.getPrototypeEntry() != null && wizard.getPrototypeEntry().isConsistent() ) 218 { 219 setPageComplete( true ); 220 } 221 else 222 { 223 setPageComplete( false ); 224 } 225 } 226 227 228 231 public void createControl( Composite parent ) 232 { 233 Composite composite = new Composite( parent, SWT.NONE ); 234 GridLayout gl = new GridLayout( 1, false ); 235 composite.setLayout( gl ); 236 composite.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 237 238 configuration = new EntryEditorWidgetConfiguration(); 240 241 mainWidget = new EntryEditorWidget( this.configuration ); 243 mainWidget.createWidget( composite ); 244 mainWidget.getViewer().getTree().setFocus(); 245 246 actionGroup = new EntryEditorWidgetActionGroupWithAttribute( mainWidget, configuration ); 248 actionGroup.fillToolBar( mainWidget.getToolBarManager() ); 249 actionGroup.fillMenu( mainWidget.getMenuManager() ); 250 actionGroup.fillContextMenu( mainWidget.getContextMenuManager() ); 251 252 universalListener = new EntryEditorWidgetUniversalListener( mainWidget.getViewer(), actionGroup 254 .getOpenDefaultEditorAction() ); 255 EventRegistry.addEntryUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() ); 256 257 setControl( composite ); 258 } 259 260 261 264 public void entryUpdated( EntryModificationEvent event ) 265 { 266 if ( event.getModifiedEntry() == wizard.getPrototypeEntry() && !isDisposed() && getControl().isVisible() ) 267 { 268 validate(); 269 } 270 } 271 272 273 278 private boolean isDisposed() 279 { 280 return configuration == null; 281 } 282 283 } 284 | Popular Tags |