1 20 package org.apache.directory.ldapstudio.apacheds.configuration.editor; 21 22 23 import java.util.List ; 24 25 import org.apache.directory.ldapstudio.apacheds.configuration.Activator; 26 import org.apache.directory.ldapstudio.apacheds.configuration.PluginConstants; 27 import org.apache.directory.ldapstudio.apacheds.configuration.model.Partition; 28 import org.apache.directory.ldapstudio.apacheds.configuration.model.ServerConfiguration; 29 import org.eclipse.jface.action.Action; 30 import org.eclipse.jface.viewers.ArrayContentProvider; 31 import org.eclipse.jface.viewers.ISelectionChangedListener; 32 import org.eclipse.jface.viewers.LabelProvider; 33 import org.eclipse.jface.viewers.SelectionChangedEvent; 34 import org.eclipse.jface.viewers.StructuredSelection; 35 import org.eclipse.jface.viewers.TableViewer; 36 import org.eclipse.swt.SWT; 37 import org.eclipse.swt.events.SelectionAdapter; 38 import org.eclipse.swt.events.SelectionEvent; 39 import org.eclipse.swt.graphics.Image; 40 import org.eclipse.swt.layout.GridData; 41 import org.eclipse.swt.layout.GridLayout; 42 import org.eclipse.swt.widgets.Button; 43 import org.eclipse.swt.widgets.Composite; 44 import org.eclipse.swt.widgets.Table; 45 import org.eclipse.ui.forms.DetailsPart; 46 import org.eclipse.ui.forms.IManagedForm; 47 import org.eclipse.ui.forms.MasterDetailsBlock; 48 import org.eclipse.ui.forms.SectionPart; 49 import org.eclipse.ui.forms.editor.FormPage; 50 import org.eclipse.ui.forms.widgets.FormToolkit; 51 import org.eclipse.ui.forms.widgets.ScrolledForm; 52 import org.eclipse.ui.forms.widgets.Section; 53 import org.eclipse.ui.plugin.AbstractUIPlugin; 54 55 56 62 public class PartitionsMasterDetailsBlock extends MasterDetailsBlock 63 { 64 65 private FormPage page; 66 67 68 private ServerConfiguration serverConfiguration; 69 70 71 private List <Partition> partitions; 72 73 74 private PartitionDetailsPage detailsPage; 75 76 private static final String NEW_NAME = "New Partition "; 77 78 private TableViewer viewer; 80 private Button addButton; 81 private Button deleteButton; 82 83 84 90 public PartitionsMasterDetailsBlock( FormPage page ) 91 { 92 this.page = page; 93 serverConfiguration = ( ( ServerConfigurationEditorInput ) page.getEditorInput() ).getServerConfiguration(); 94 partitions = serverConfiguration.getPartitions(); 95 } 96 97 98 101 protected void createMasterPart( final IManagedForm managedForm, Composite parent ) 102 { 103 FormToolkit toolkit = managedForm.getToolkit(); 104 105 Section section = toolkit.createSection( parent, Section.TITLE_BAR ); 107 section.setText( "All Partitions" ); 108 section.marginWidth = 10; 109 section.marginHeight = 5; 110 Composite client = toolkit.createComposite( section, SWT.WRAP ); 111 GridLayout layout = new GridLayout(); 112 layout.numColumns = 2; 113 layout.makeColumnsEqualWidth = false; 114 layout.marginWidth = 2; 115 layout.marginHeight = 2; 116 client.setLayout( layout ); 117 toolkit.paintBordersFor( client ); 118 section.setClient( client ); 119 120 Table table = toolkit.createTable( client, SWT.NULL ); 122 GridData gd = new GridData( SWT.FILL, SWT.FILL, true, true, 1, 2 ); 123 gd.heightHint = 20; 124 gd.widthHint = 100; 125 table.setLayoutData( gd ); 126 final SectionPart spart = new SectionPart( section ); 127 managedForm.addPart( spart ); 128 viewer = new TableViewer( table ); 129 viewer.addSelectionChangedListener( new ISelectionChangedListener() 130 { 131 public void selectionChanged( SelectionChangedEvent event ) 132 { 133 managedForm.fireSelectionChanged( spart, event.getSelection() ); 134 } 135 } ); 136 viewer.setContentProvider( new ArrayContentProvider() ); 137 viewer.setLabelProvider( new LabelProvider() 138 { 139 public Image getImage( Object element ) 140 { 141 142 return AbstractUIPlugin.imageDescriptorFromPlugin( 143 Activator.PLUGIN_ID, 144 ( ( Partition ) element ).isSystemPartition() ? PluginConstants.IMG_PARTITION_SYSTEM 145 : PluginConstants.IMG_PARTITION ).createImage(); 146 } 147 } ); 148 149 addButton = toolkit.createButton( client, "Add...", SWT.PUSH ); addButton.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, false, false ) ); 152 153 deleteButton = toolkit.createButton( client, "Delete", SWT.PUSH ); 154 deleteButton.setEnabled( false ); 155 deleteButton.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, false, false ) ); 156 157 initFromInput(); 158 addListeners(); 159 } 160 161 162 165 private void initFromInput() 166 { 167 viewer.setInput( partitions ); 168 } 169 170 171 174 private void addListeners() 175 { 176 viewer.addSelectionChangedListener( new ISelectionChangedListener() 177 { 178 public void selectionChanged( SelectionChangedEvent event ) 179 { 180 viewer.refresh(); 181 182 deleteButton.setEnabled( !event.getSelection().isEmpty() ); 183 StructuredSelection selection = ( StructuredSelection ) viewer.getSelection(); 184 if ( !selection.isEmpty() ) 185 { 186 Partition partition = ( Partition ) selection.getFirstElement(); 187 if ( partition.isSystemPartition() ) 188 { 189 deleteButton.setEnabled( false ); 190 } 191 } 192 } 193 } ); 194 195 addButton.addSelectionListener( new SelectionAdapter() 196 { 197 public void widgetSelected( SelectionEvent e ) 198 { 199 Partition newPartition = new Partition( getNewName() ); 200 partitions.add( newPartition ); 201 viewer.refresh(); 202 viewer.setSelection( new StructuredSelection( newPartition ) ); 203 setEditorDirty(); 204 } 205 } ); 206 207 deleteButton.addSelectionListener( new SelectionAdapter() 208 { 209 public void widgetSelected( SelectionEvent e ) 210 { 211 StructuredSelection selection = ( StructuredSelection ) viewer.getSelection(); 212 if ( !selection.isEmpty() ) 213 { 214 Partition partition = ( Partition ) selection.getFirstElement(); 215 if ( !partition.isSystemPartition() ) 216 { 217 partitions.remove( partition ); 218 viewer.refresh(); 219 setEditorDirty(); 220 } 221 } 222 } 223 } ); 224 } 225 226 227 233 private String getNewName() 234 { 235 int counter = 1; 236 String name = NEW_NAME; 237 boolean ok = false; 238 239 while ( !ok ) 240 { 241 ok = true; 242 name = NEW_NAME + counter; 243 244 for ( Partition partition : partitions ) 245 { 246 if ( partition.getName().equalsIgnoreCase( name ) ) 247 { 248 ok = false; 249 } 250 } 251 counter++; 252 } 253 254 return name; 255 } 256 257 258 261 protected void createToolBarActions( IManagedForm managedForm ) 262 { 263 final ScrolledForm form = managedForm.getForm(); 264 265 Action horizontalAction = new Action( "Horizontal layout", Action.AS_RADIO_BUTTON ) { public void run() 268 { 269 sashForm.setOrientation( SWT.HORIZONTAL ); 270 form.reflow( true ); 271 } 272 }; 273 horizontalAction.setChecked( true ); 274 horizontalAction.setToolTipText( "Horizontal Orientation" ); horizontalAction.setImageDescriptor( Activator.imageDescriptorFromPlugin( Activator.PLUGIN_ID, 276 PluginConstants.IMG_HORIZONTAL_ORIENTATION ) ); 277 278 Action verticalAction = new Action( "Vertical Orientation", Action.AS_RADIO_BUTTON ) { public void run() 281 { 282 sashForm.setOrientation( SWT.VERTICAL ); 283 form.reflow( true ); 284 } 285 }; 286 verticalAction.setChecked( false ); 287 verticalAction.setToolTipText( "Vertical Orientation" ); verticalAction.setImageDescriptor( Activator.imageDescriptorFromPlugin( Activator.PLUGIN_ID, 289 PluginConstants.IMG_VERTICAL_ORIENTATION ) ); 290 291 form.getToolBarManager().add( horizontalAction ); 292 form.getToolBarManager().add( verticalAction ); 293 } 294 295 296 299 protected void registerPages( DetailsPart detailsPart ) 300 { 301 detailsPage = new PartitionDetailsPage( this ); 302 detailsPart.registerPage( Partition.class, detailsPage ); 303 } 304 305 306 309 public void setEditorDirty() 310 { 311 ( ( ServerConfigurationEditor ) page.getEditor() ).setDirty( true ); 312 } 313 314 315 318 public void save() 319 { 320 detailsPage.commit( true ); 321 viewer.setInput( partitions ); 322 } 323 } 324 | Popular Tags |