KickJava   Java API By Example, From Geeks To Geeks.

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


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.ExtendedOperation;
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 Extended Operations Master/Details Block used in the Extended Operations Page.
58  *
59  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
60  * @version $Rev$, $Date$
61  */

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

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

100     protected void createMasterPart( final IManagedForm managedForm, Composite parent )
101     {
102         FormToolkit toolkit = managedForm.getToolkit();
103
104         // Creating the Section
105
Section section = toolkit.createSection( parent, Section.TITLE_BAR );
106         section.setText( "All Extended Operations" );
107         section.marginWidth = 10;
108         section.marginHeight = 5;
109         Composite client = toolkit.createComposite( section, SWT.WRAP );
110         GridLayout layout = new GridLayout();
111         layout.numColumns = 2;
112         layout.makeColumnsEqualWidth = false;
113         layout.marginWidth = 2;
114         layout.marginHeight = 2;
115         client.setLayout( layout );
116         toolkit.paintBordersFor( client );
117         section.setClient( client );
118
119         // Creatig the Table and Table Viewer
120
Table table = toolkit.createTable( client, SWT.NULL );
121         GridData gd = new GridData( SWT.FILL, SWT.FILL, true, true, 1, 2 );
122         gd.heightHint = 20;
123         gd.widthHint = 100;
124         table.setLayoutData( gd );
125         final SectionPart spart = new SectionPart( section );
126         managedForm.addPart( spart );
127         viewer = new TableViewer( table );
128         viewer.addSelectionChangedListener( new ISelectionChangedListener()
129         {
130             public void selectionChanged( SelectionChangedEvent event )
131             {
132                 managedForm.fireSelectionChanged( spart, event.getSelection() );
133             }
134         } );
135         viewer.setContentProvider( new ArrayContentProvider() );
136         viewer.setLabelProvider( new LabelProvider()
137         {
138             public Image getImage( Object JavaDoc element )
139             {
140                 return AbstractUIPlugin.imageDescriptorFromPlugin( Activator.PLUGIN_ID,
141                     PluginConstants.IMG_EXTENDED_OPERATION ).createImage();
142             }
143         } );
144
145         // Creating the button(s)
146
addButton = toolkit.createButton( client, "Add...", SWT.PUSH ); //$NON-NLS-1$
147
addButton.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, false, false ) );
148
149         deleteButton = toolkit.createButton( client, "Delete", SWT.PUSH );
150         deleteButton.setEnabled( false );
151         deleteButton.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, false, false ) );
152
153         initFromInput();
154         addListeners();
155     }
156
157
158     /**
159      * Initializes the page with the Editor input.
160      */

161     private void initFromInput()
162     {
163         viewer.setInput( extendedOperations );
164     }
165
166
167     /**
168      * Add listeners to UI fields.
169      */

170     private void addListeners()
171     {
172         viewer.addSelectionChangedListener( new ISelectionChangedListener()
173         {
174             public void selectionChanged( SelectionChangedEvent event )
175             {
176                 viewer.refresh();
177
178                 deleteButton.setEnabled( !event.getSelection().isEmpty() );
179             }
180         } );
181
182         addButton.addSelectionListener( new SelectionAdapter()
183         {
184             public void widgetSelected( SelectionEvent e )
185             {
186                 ExtendedOperation newExtendedOperation = new ExtendedOperation( getNewName() );
187                 extendedOperations.add( newExtendedOperation );
188                 viewer.refresh();
189                 viewer.setSelection( new StructuredSelection( newExtendedOperation ) );
190                 setEditorDirty();
191             }
192         } );
193
194         deleteButton.addSelectionListener( new SelectionAdapter()
195         {
196             public void widgetSelected( SelectionEvent e )
197             {
198                 StructuredSelection selection = ( StructuredSelection ) viewer.getSelection();
199                 if ( !selection.isEmpty() )
200                 {
201                     ExtendedOperation extendedOperation = ( ExtendedOperation ) selection.getFirstElement();
202
203                     extendedOperations.remove( extendedOperation );
204                     viewer.refresh();
205                     setEditorDirty();
206                 }
207             }
208         } );
209     }
210
211
212     /**
213      * Gets a new Name for a new Extended Operation.
214      *
215      * @return
216      * a new Name for a new Extended Operation
217      */

218     private String JavaDoc getNewName()
219     {
220         int counter = 1;
221         String JavaDoc name = NEW_NAME;
222         boolean ok = false;
223
224         while ( !ok )
225         {
226             ok = true;
227             name = NEW_NAME + counter;
228
229             for ( ExtendedOperation extendedOperation : extendedOperations )
230             {
231                 if ( extendedOperation.getClassType().equalsIgnoreCase( name ) )
232                 {
233                     ok = false;
234                 }
235             }
236
237             counter++;
238         }
239
240         return name;
241     }
242
243
244     /* (non-Javadoc)
245      * @see org.eclipse.ui.forms.MasterDetailsBlock#createToolBarActions(org.eclipse.ui.forms.IManagedForm)
246      */

247     protected void createToolBarActions( IManagedForm managedForm )
248     {
249         final ScrolledForm form = managedForm.getForm();
250
251         // Horizontal layout Action
252
Action horizontalAction = new Action( "Horizontal layout", Action.AS_RADIO_BUTTON ) { //$NON-NLS-1$
253
public void run()
254             {
255                 sashForm.setOrientation( SWT.HORIZONTAL );
256                 form.reflow( true );
257             }
258         };
259         horizontalAction.setChecked( true );
260         horizontalAction.setToolTipText( "Horizontal Orientation" ); //$NON-NLS-1$
261
horizontalAction.setImageDescriptor( Activator.imageDescriptorFromPlugin( Activator.PLUGIN_ID,
262             PluginConstants.IMG_HORIZONTAL_ORIENTATION ) );
263
264         // Vertical layout Action
265
Action verticalAction = new Action( "Vertical Orientation", Action.AS_RADIO_BUTTON ) { //$NON-NLS-1$
266
public void run()
267             {
268                 sashForm.setOrientation( SWT.VERTICAL );
269                 form.reflow( true );
270             }
271         };
272         verticalAction.setChecked( false );
273         verticalAction.setToolTipText( "Vertical Orientation" ); //$NON-NLS-1$
274
verticalAction.setImageDescriptor( Activator.imageDescriptorFromPlugin( Activator.PLUGIN_ID,
275             PluginConstants.IMG_VERTICAL_ORIENTATION ) );
276
277         form.getToolBarManager().add( horizontalAction );
278         form.getToolBarManager().add( verticalAction );
279     }
280
281
282     /* (non-Javadoc)
283      * @see org.eclipse.ui.forms.MasterDetailsBlock#registerPages(org.eclipse.ui.forms.DetailsPart)
284      */

285     protected void registerPages( DetailsPart detailsPart )
286     {
287         detailsPage = new ExtendedOperationDetailsPage( this );
288         detailsPart.registerPage( ExtendedOperation.class, detailsPage );
289     }
290
291
292     /**
293      * Sets the Editor as dirty.
294      */

295     public void setEditorDirty()
296     {
297         ( ( ServerConfigurationEditor ) page.getEditor() ).setDirty( true );
298     }
299
300
301     /**
302      * Saves the necessary elements to the input model.
303      */

304     public void save()
305     {
306         detailsPage.commit( true );
307         viewer.setInput( extendedOperations );
308     }
309 }
310
Popular Tags