KickJava   Java API By Example, From Geeks To Geeks.

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


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

62 public class PartitionsMasterDetailsBlock 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<Partition> partitions;
72
73     /** The Details Page */
74     private PartitionDetailsPage detailsPage;
75
76     private static final String JavaDoc NEW_NAME = "New Partition ";
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 PartitionsMasterDetailsBlock.
86      *
87      * @param page
88      * the associated page
89      */

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

101     protected void createMasterPart( final IManagedForm managedForm, Composite parent )
102     {
103         FormToolkit toolkit = managedForm.getToolkit();
104
105         // Creating the Section
106
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         // Creatig the Table and Table Viewer
121
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 JavaDoc 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         // 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         initFromInput();
158         addListeners();
159     }
160
161
162     /**
163      * Initializes the page with the Editor input.
164      */

165     private void initFromInput()
166     {
167         viewer.setInput( partitions );
168     }
169
170
171     /**
172      * Add listeners to UI fields.
173      */

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     /**
228      * Gets a new Name for a new Extended Operation.
229      *
230      * @return
231      * a new Name for a new Extended Operation
232      */

233     private String JavaDoc getNewName()
234     {
235         int counter = 1;
236         String JavaDoc 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     /* (non-Javadoc)
259      * @see org.eclipse.ui.forms.MasterDetailsBlock#createToolBarActions(org.eclipse.ui.forms.IManagedForm)
260      */

261     protected void createToolBarActions( IManagedForm managedForm )
262     {
263         final ScrolledForm form = managedForm.getForm();
264
265         // Horizontal layout Action
266
Action horizontalAction = new Action( "Horizontal layout", Action.AS_RADIO_BUTTON ) { //$NON-NLS-1$
267
public void run()
268             {
269                 sashForm.setOrientation( SWT.HORIZONTAL );
270                 form.reflow( true );
271             }
272         };
273         horizontalAction.setChecked( true );
274         horizontalAction.setToolTipText( "Horizontal Orientation" ); //$NON-NLS-1$
275
horizontalAction.setImageDescriptor( Activator.imageDescriptorFromPlugin( Activator.PLUGIN_ID,
276             PluginConstants.IMG_HORIZONTAL_ORIENTATION ) );
277
278         // Vertical layout Action
279
Action verticalAction = new Action( "Vertical Orientation", Action.AS_RADIO_BUTTON ) { //$NON-NLS-1$
280
public void run()
281             {
282                 sashForm.setOrientation( SWT.VERTICAL );
283                 form.reflow( true );
284             }
285         };
286         verticalAction.setChecked( false );
287         verticalAction.setToolTipText( "Vertical Orientation" ); //$NON-NLS-1$
288
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     /* (non-Javadoc)
297      * @see org.eclipse.ui.forms.MasterDetailsBlock#registerPages(org.eclipse.ui.forms.DetailsPart)
298      */

299     protected void registerPages( DetailsPart detailsPart )
300     {
301         detailsPage = new PartitionDetailsPage( this );
302         detailsPart.registerPage( Partition.class, detailsPage );
303     }
304
305
306     /**
307      * Sets the Editor as dirty.
308      */

309     public void setEditorDirty()
310     {
311         ( ( ServerConfigurationEditor ) page.getEditor() ).setDirty( true );
312     }
313
314
315     /**
316      * Saves the necessary elements to the input model.
317      */

318     public void save()
319     {
320         detailsPage.commit( true );
321         viewer.setInput( partitions );
322     }
323 }
324
Popular Tags