KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > apacheds > configuration > editor > InterceptorsMasterDetailsBlock


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.directory.ldapstudio.apacheds.configuration.editor;
21
22
23 import java.util.List JavaDoc;
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.Interceptor;
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 /**
57  * This class represents the Interceptors Master/Details Block used in the Interceptors Page.
58  *
59  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
60  * @version $Rev$, $Date$
61  */

62 public class InterceptorsMasterDetailsBlock extends MasterDetailsBlock
63 {
64     /** The associated page */
65     private FormPage page;
66
67     /** The input Server Configuration */
68     private ServerConfiguration serverConfiguration;
69
70     /** The Interceptors List */
71     private List JavaDoc<Interceptor> interceptors;
72
73     /** The Details Page */
74     private InterceptorDetailsPage detailsPage;
75
76     private static final String JavaDoc NEW_NAME = "New Interceptor ";
77
78     // UI Fields
79
private TableViewer viewer;
80     private Button addButton;
81     private Button deleteButton;
82     private Button upButton;
83     private Button downButton;
84
85
86     /**
87      * Creates a new instance of InterceptorsMasterDetailsBlock.
88      *
89      * @param page
90      */

91     public InterceptorsMasterDetailsBlock( FormPage page )
92     {
93         this.page = page;
94         serverConfiguration = ( ( ServerConfigurationEditorInput ) page.getEditorInput() ).getServerConfiguration();
95         interceptors = serverConfiguration.getInterceptors();
96     }
97
98
99     /* (non-Javadoc)
100      * @see org.eclipse.ui.forms.MasterDetailsBlock#createMasterPart(org.eclipse.ui.forms.IManagedForm, org.eclipse.swt.widgets.Composite)
101      */

102     protected void createMasterPart( final IManagedForm managedForm, Composite parent )
103     {
104         FormToolkit toolkit = managedForm.getToolkit();
105
106         // Creating the Section
107
Section section = toolkit.createSection( parent, Section.TITLE_BAR | Section.DESCRIPTION );
108         section.setText( "All Interceptors" );
109         section
110             .setDescription( " Set the Interceptors used in the server. Use the \"Up\" and \"Down\" buttons to change the order." );
111         section.marginWidth = 10;
112         section.marginHeight = 5;
113         Composite client = toolkit.createComposite( section, SWT.WRAP );
114         GridLayout layout = new GridLayout();
115         layout.numColumns = 2;
116         layout.makeColumnsEqualWidth = false;
117         layout.marginWidth = 2;
118         layout.marginHeight = 2;
119         client.setLayout( layout );
120         toolkit.paintBordersFor( client );
121         section.setClient( client );
122
123         // Creatig the Table and Table Viewer
124
Table table = toolkit.createTable( client, SWT.NULL );
125         GridData gd = new GridData( SWT.FILL, SWT.FILL, true, true, 1, 4 );
126         gd.heightHint = 20;
127         gd.widthHint = 100;
128         table.setLayoutData( gd );
129         final SectionPart spart = new SectionPart( section );
130         managedForm.addPart( spart );
131         viewer = new TableViewer( table );
132         viewer.addSelectionChangedListener( new ISelectionChangedListener()
133         {
134             public void selectionChanged( SelectionChangedEvent event )
135             {
136                 managedForm.fireSelectionChanged( spart, event.getSelection() );
137             }
138         } );
139         viewer.setContentProvider( new ArrayContentProvider() );
140         viewer.setLabelProvider( new LabelProvider()
141         {
142             public Image getImage( Object JavaDoc element )
143             {
144                 return AbstractUIPlugin
145                     .imageDescriptorFromPlugin( Activator.PLUGIN_ID, PluginConstants.IMG_INTERCEPTOR ).createImage();
146             }
147         } );
148
149         // Creating the button(s)
150
addButton = toolkit.createButton( client, "Add...", SWT.PUSH ); //$NON-NLS-1$
151
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         upButton = toolkit.createButton( client, "Up", SWT.PUSH );
158         upButton.setEnabled( false );
159         upButton.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, false, false ) );
160
161         downButton = toolkit.createButton( client, "Down", SWT.PUSH );
162         downButton.setEnabled( false );
163         downButton.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, false, false ) );
164
165         initFromInput();
166         addListeners();
167     }
168
169
170     /**
171      * Initializes the page with the Editor input.
172      */

173     private void initFromInput()
174     {
175         viewer.setInput( interceptors );
176     }
177
178
179     /**
180      * Add listeners to UI fields.
181      */

182     private void addListeners()
183     {
184         viewer.addSelectionChangedListener( new ISelectionChangedListener()
185         {
186             public void selectionChanged( SelectionChangedEvent event )
187             {
188                 viewer.refresh();
189
190                 deleteButton.setEnabled( !event.getSelection().isEmpty() );
191
192                 enableDisableUpDownButtons();
193             }
194         } );
195
196         addButton.addSelectionListener( new SelectionAdapter()
197         {
198             public void widgetSelected( SelectionEvent e )
199             {
200                 Interceptor newInterceptor = new Interceptor( getNewName() );
201                 interceptors.add( newInterceptor );
202                 viewer.refresh();
203                 viewer.setSelection( new StructuredSelection( newInterceptor ) );
204                 setEditorDirty();
205             }
206         } );
207
208         deleteButton.addSelectionListener( new SelectionAdapter()
209         {
210             public void widgetSelected( SelectionEvent e )
211             {
212                 StructuredSelection selection = ( StructuredSelection ) viewer.getSelection();
213                 if ( !selection.isEmpty() )
214                 {
215                     Interceptor interceptor = ( Interceptor ) selection.getFirstElement();
216
217                     interceptors.remove( interceptor );
218                     viewer.refresh();
219                     setEditorDirty();
220                 }
221             }
222         } );
223
224         upButton.addSelectionListener( new SelectionAdapter()
225         {
226             public void widgetSelected( SelectionEvent e )
227             {
228                 StructuredSelection selection = ( StructuredSelection ) viewer.getSelection();
229                 if ( !selection.isEmpty() )
230                 {
231                     Interceptor interceptor = ( Interceptor ) selection.getFirstElement();
232
233                     int index = interceptors.indexOf( interceptor );
234                     if ( index > 0 )
235                     {
236                         Interceptor interceptorBefore = interceptors.get( index - 1 );
237                         if ( interceptorBefore != null )
238                         {
239                             interceptors.set( index - 1, interceptor );
240                             interceptors.set( index, interceptorBefore );
241
242                             viewer.refresh();
243                             setEditorDirty();
244                             enableDisableUpDownButtons();
245                         }
246                     }
247                 }
248             }
249         } );
250
251         downButton.addSelectionListener( new SelectionAdapter()
252         {
253             public void widgetSelected( SelectionEvent e )
254             {
255                 StructuredSelection selection = ( StructuredSelection ) viewer.getSelection();
256                 if ( !selection.isEmpty() )
257                 {
258                     Interceptor interceptor = ( Interceptor ) selection.getFirstElement();
259
260                     int index = interceptors.indexOf( interceptor );
261                     if ( index < ( interceptors.size() - 1 ) )
262                     {
263                         Interceptor interceptorAfter = interceptors.get( index + 1 );
264                         if ( interceptorAfter != null )
265                         {
266                             interceptors.set( index + 1, interceptor );
267                             interceptors.set( index, interceptorAfter );
268
269                             viewer.refresh();
270                             setEditorDirty();
271                             enableDisableUpDownButtons();
272                         }
273                     }
274                 }
275             }
276         } );
277     }
278
279
280     /**
281      * Gets a new Name for a new Extended Operation.
282      *
283      * @return
284      * a new Name for a new Extended Operation
285      */

286     private String JavaDoc getNewName()
287     {
288         int counter = 1;
289         String JavaDoc name = NEW_NAME;
290         boolean ok = false;
291
292         while ( !ok )
293         {
294             ok = true;
295             name = NEW_NAME + counter;
296
297             for ( Interceptor interceptor : interceptors )
298             {
299                 if ( interceptor.getName().equalsIgnoreCase( name ) )
300                 {
301                     ok = false;
302                 }
303             }
304
305             counter++;
306         }
307
308         return name;
309     }
310
311
312     /**
313      * Enables or Disables the Up and Down Buttons.
314      */

315     private void enableDisableUpDownButtons()
316     {
317         StructuredSelection selection = ( StructuredSelection ) viewer.getSelection();
318
319         upButton.setEnabled( !selection.isEmpty() );
320         downButton.setEnabled( !selection.isEmpty() );
321         if ( !selection.isEmpty() )
322         {
323             Interceptor interceptor = ( Interceptor ) selection.getFirstElement();
324             upButton.setEnabled( interceptors.indexOf( interceptor ) != 0 );
325             downButton.setEnabled( interceptors.indexOf( interceptor ) != ( interceptors.size() - 1 ) );
326         }
327     }
328
329
330     /* (non-Javadoc)
331      * @see org.eclipse.ui.forms.MasterDetailsBlock#createToolBarActions(org.eclipse.ui.forms.IManagedForm)
332      */

333     protected void createToolBarActions( IManagedForm managedForm )
334     {
335         final ScrolledForm form = managedForm.getForm();
336
337         // Horizontal layout Action
338
Action horizontalAction = new Action( "Horizontal layout", Action.AS_RADIO_BUTTON ) { //$NON-NLS-1$
339
public void run()
340             {
341                 sashForm.setOrientation( SWT.HORIZONTAL );
342                 form.reflow( true );
343             }
344         };
345         horizontalAction.setChecked( true );
346         horizontalAction.setToolTipText( "Horizontal Orientation" ); //$NON-NLS-1$
347
horizontalAction.setImageDescriptor( Activator.imageDescriptorFromPlugin( Activator.PLUGIN_ID,
348             PluginConstants.IMG_HORIZONTAL_ORIENTATION ) );
349
350         // Vertical layout Action
351
Action verticalAction = new Action( "Vertical Orientation", Action.AS_RADIO_BUTTON ) { //$NON-NLS-1$
352
public void run()
353             {
354                 sashForm.setOrientation( SWT.VERTICAL );
355                 form.reflow( true );
356             }
357         };
358         verticalAction.setChecked( false );
359         verticalAction.setToolTipText( "Vertical Orientation" ); //$NON-NLS-1$
360
verticalAction.setImageDescriptor( Activator.imageDescriptorFromPlugin( Activator.PLUGIN_ID,
361             PluginConstants.IMG_VERTICAL_ORIENTATION ) );
362
363         form.getToolBarManager().add( horizontalAction );
364         form.getToolBarManager().add( verticalAction );
365     }
366
367
368     /* (non-Javadoc)
369      * @see org.eclipse.ui.forms.MasterDetailsBlock#registerPages(org.eclipse.ui.forms.DetailsPart)
370      */

371     protected void registerPages( DetailsPart detailsPart )
372     {
373         detailsPage = new InterceptorDetailsPage( this );
374         detailsPart.registerPage( Interceptor.class, detailsPage );
375     }
376
377
378     /**
379      * Sets the Editor as dirty.
380      */

381     public void setEditorDirty()
382     {
383         ( ( ServerConfigurationEditor ) page.getEditor() ).setDirty( true );
384     }
385
386
387     /**
388      * Saves the necessary elements to the input model.
389      */

390     public void save()
391     {
392         detailsPage.commit( true );
393         viewer.setInput( interceptors );
394     }
395 }
396
Popular Tags