KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > editors > schemabrowser > SchemaPage


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
21 package org.apache.directory.ldapstudio.browser.ui.editors.schemabrowser;
22
23
24 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
25 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
26 import org.apache.directory.ldapstudio.browser.core.model.schema.SchemaPart;
27 import org.eclipse.jface.action.Separator;
28 import org.eclipse.jface.resource.JFaceResources;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.ISelectionChangedListener;
31 import org.eclipse.jface.viewers.IStructuredContentProvider;
32 import org.eclipse.jface.viewers.ITableLabelProvider;
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.jface.viewers.ViewerFilter;
37 import org.eclipse.jface.viewers.ViewerSorter;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.custom.SashForm;
40 import org.eclipse.swt.events.ModifyEvent;
41 import org.eclipse.swt.events.ModifyListener;
42 import org.eclipse.swt.layout.FillLayout;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Control;
47 import org.eclipse.swt.widgets.Table;
48 import org.eclipse.swt.widgets.Text;
49 import org.eclipse.ui.forms.FormColors;
50 import org.eclipse.ui.forms.widgets.Form;
51 import org.eclipse.ui.forms.widgets.FormToolkit;
52 import org.eclipse.ui.forms.widgets.ScrolledForm;
53 import org.eclipse.ui.forms.widgets.Section;
54
55
56 /**
57  * A base implementation used from all schema master pages.
58  *
59  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
60  * @version $Rev$, $Date$
61  */

62 public abstract class SchemaPage
63 {
64
65     /** The connection combo */
66     protected ConnectionComboContributionItem connectionCombo;
67
68     /** The show default schema action */
69     protected ShowDefaultSchemaAction showDefaultSchemaAction;
70
71     /** The reload schema action */
72     protected ReloadSchemaAction reloadSchemaAction;
73
74     /** The schema browser */
75     protected SchemaBrowser schemaBrowser;
76
77     /** The toolkit used to create controls */
78     protected FormToolkit toolkit;
79
80     /** The outer form */
81     protected Form form;
82
83     /** The sash form, used to split the master and detail form */
84     protected SashForm sashForm;
85
86     /** The master form, contains the schema element list */
87     protected ScrolledForm masterForm;
88
89     /** The detail form, contains the schema details */
90     protected ScrolledForm detailForm;
91
92     /** The schema details page */
93     protected SchemaDetailsPage detailsPage;
94
95     /** The section of the master form */
96     protected Section section;
97
98     /** The filter field of the master form */
99     protected Text filterText;
100
101     /** The list with all schema elements */
102     protected TableViewer viewer;
103
104     /** Flag indicating if the viewer's selection is changed programatically */
105     protected boolean inChange;
106
107
108     /**
109      * Creates a new instance of SchemaPage.
110      *
111      * @param schemaBrowser the schema browser
112      */

113     public SchemaPage( SchemaBrowser schemaBrowser )
114     {
115         this.schemaBrowser = schemaBrowser;
116         this.inChange = false;
117     }
118
119
120     /**
121      * Refreshes this schema page.
122      */

123     public void refresh()
124     {
125         Schema schema = null;
126         if ( showDefaultSchemaAction.isChecked() )
127         {
128             schema = Schema.DEFAULT_SCHEMA;
129         }
130         else if ( getConnection() != null )
131         {
132             schema = getConnection().getSchema();
133         }
134
135         if ( viewer.getInput() != schema )
136         {
137             viewer.setInput( schema );
138             viewer.setSelection( StructuredSelection.EMPTY );
139         }
140
141         form.setText( getTitle() );
142         viewer.refresh();
143     }
144
145
146     /**
147      * Gets the title of this schema page.
148      *
149      * @return the title
150      */

151     protected abstract String JavaDoc getTitle();
152
153
154     /**
155      * Gets the filter description.
156      *
157      * @return the filter description
158      */

159     protected abstract String JavaDoc getFilterDescription();
160
161
162     /**
163      * Gets the content provider.
164      *
165      * @return the content provider
166      */

167     protected abstract IStructuredContentProvider getContentProvider();
168
169
170     /**
171      * Gets the label provider.
172      *
173      * @return the label provider
174      */

175     protected abstract ITableLabelProvider getLabelProvider();
176
177
178     /**
179      * Gets the sorter.
180      *
181      * @return the sorter
182      */

183     protected abstract ViewerSorter getSorter();
184
185
186     /**
187      * Gets the filter.
188      *
189      * @return the filter
190      */

191     protected abstract ViewerFilter getFilter();
192
193
194     /**
195      * Gets the details page.
196      *
197      * @return the details page
198      */

199     protected abstract SchemaDetailsPage getDetailsPage();
200
201
202     /**
203      * Creates the master page.
204      *
205      * @param body the parent composite
206      */

207     //protected abstract void createMaster( Composite body );
208
private void createMaster( Composite parent )
209     {
210         // create section
211
section = toolkit.createSection( parent, Section.DESCRIPTION );
212         section.marginWidth = 10;
213         section.marginHeight = 12;
214         section.setText( getTitle() );
215         section.setDescription( getFilterDescription() );
216         toolkit.createCompositeSeparator( section );
217
218         // create client
219
Composite client = toolkit.createComposite( section, SWT.WRAP );
220         GridLayout layout = new GridLayout( 2, false );
221         layout.marginWidth = 5;
222         layout.marginHeight = 5;
223         client.setLayout( layout );
224         section.setClient( client );
225
226         // create filter field
227
toolkit.createLabel( client, "Filter:" );
228         this.filterText = toolkit.createText( client, "", SWT.NONE );
229         this.filterText.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
230         this.filterText.setData( FormToolkit.KEY_DRAW_BORDER, FormToolkit.TREE_BORDER );
231         this.filterText.addModifyListener( new ModifyListener()
232         {
233             public void modifyText( ModifyEvent e )
234             {
235                 viewer.refresh();
236             }
237         } );
238
239         // create table
240
Table t = toolkit.createTable( client, SWT.NONE );
241         GridData gd = new GridData( GridData.FILL_BOTH );
242         gd.horizontalSpan = 2;
243         gd.heightHint = 20;
244         gd.widthHint = 100;
245         t.setLayoutData( gd );
246         toolkit.paintBordersFor( client );
247
248         // setup viewer
249
viewer = new TableViewer( t );
250         viewer.setContentProvider( getContentProvider() );
251         viewer.setLabelProvider( getLabelProvider() );
252         viewer.setSorter( getSorter() );
253         viewer.addFilter( getFilter() );
254     }
255
256
257     /**
258      * Creates the detail page.
259      *
260      * @param body the parent composite
261      */

262     private void createDetail( Composite body )
263     {
264         detailsPage = getDetailsPage();
265         detailsPage.createContents( this.detailForm );
266     }
267
268
269     /**
270      * Selects the given object in the list. Causes also an input
271      * change of the details page.
272      *
273      * @param obj the object to select
274      */

275     public void select( Object JavaDoc obj )
276     {
277         ISelection newSelection = new StructuredSelection( obj );
278         ISelection oldSelection = this.viewer.getSelection();
279
280         if ( !newSelection.equals( oldSelection ) )
281         {
282             inChange = true;
283             this.viewer.setSelection( newSelection, true );
284             if ( this.viewer.getSelection().isEmpty() )
285             {
286                 this.filterText.setText( "" );
287                 this.viewer.setSelection( newSelection, true );
288             }
289             inChange = false;
290         }
291     }
292
293
294     /**
295      * Disposed this page and the details page.
296      */

297     public void dispose()
298     {
299         this.detailsPage.dispose();
300
301         this.schemaBrowser = null;
302         this.toolkit.dispose();
303         this.toolkit = null;
304     }
305
306
307     /**
308      * Creates this schema page and details page.
309      *
310      * @param parent the parent composite
311      * @return the created composite.
312      */

313     Control createControl( Composite parent )
314     {
315         this.toolkit = new FormToolkit( parent.getDisplay() );
316         this.form = this.toolkit.createForm( parent );
317         this.form.getBody().setLayout( new FillLayout() );
318
319         this.sashForm = new SashForm( this.form.getBody(), SWT.HORIZONTAL );
320         this.sashForm.setLayout( new FillLayout() );
321
322         this.masterForm = this.toolkit.createScrolledForm( this.sashForm );
323         this.detailForm = new ScrolledForm( this.sashForm, SWT.V_SCROLL | this.toolkit.getOrientation() );
324         this.detailForm.setExpandHorizontal( true );
325         this.detailForm.setExpandVertical( true );
326         this.detailForm.setBackground( this.toolkit.getColors().getBackground() );
327         this.detailForm.setForeground( this.toolkit.getColors().getColor( FormColors.TITLE ) );
328         this.detailForm.setFont( JFaceResources.getHeaderFont() );
329         this.sashForm.setWeights( new int[]
330             { 50, 50 } );
331
332         this.masterForm.getBody().setLayout( new FillLayout() );
333         this.createMaster( this.masterForm.getBody() );
334
335         this.detailForm.getBody().setLayout( new FillLayout() );
336         this.createDetail( this.detailForm.getBody() );
337         viewer.addSelectionChangedListener( new ISelectionChangedListener()
338         {
339             public void selectionChanged( SelectionChangedEvent event )
340             {
341                 ISelection selection = event.getSelection();
342                 if ( selection.isEmpty() )
343                 {
344                     detailsPage.setInput( null );
345                 }
346                 else
347                 {
348                     Object JavaDoc obj = ( ( StructuredSelection ) selection ).getFirstElement();
349                     detailsPage.setInput( obj );
350
351                     // Do not set the input of the schema browser if
352
// the selection was changed programatically.
353
if ( !inChange && obj instanceof SchemaPart )
354                     {
355                         schemaBrowser.setInput( new SchemaBrowserInput( getConnection(), ( SchemaPart ) obj ) );
356                     }
357                 }
358             }
359         } );
360
361         connectionCombo = new ConnectionComboContributionItem( this );
362         this.form.getToolBarManager().add( connectionCombo );
363         this.form.getToolBarManager().add( new Separator() );
364         showDefaultSchemaAction = new ShowDefaultSchemaAction( schemaBrowser );
365         this.form.getToolBarManager().add( showDefaultSchemaAction );
366         this.form.getToolBarManager().add( new Separator() );
367         reloadSchemaAction = new ReloadSchemaAction( this );
368         this.form.getToolBarManager().add( reloadSchemaAction );
369         this.form.updateToolBar();
370
371         this.refresh();
372
373         return this.form;
374     }
375
376
377     /**
378      * Gets the schema browser.
379      *
380      * @return the schema browser
381      */

382     public SchemaBrowser getSchemaBrowser()
383     {
384         return schemaBrowser;
385     }
386
387
388     /**
389      * Gets the connection.
390      *
391      * @return the connection
392      */

393     public IConnection getConnection()
394     {
395         return connectionCombo.getConnection();
396     }
397
398
399     /**
400      * Sets the connection.
401      *
402      * @param connection the connection
403      */

404     public void setConnection( IConnection connection )
405     {
406         connectionCombo.setConnection( connection );
407         reloadSchemaAction.updateEnabledState();
408         refresh();
409     }
410
411
412     /**
413      * Checks if is show default schema.
414      *
415      * @return true, if is show default schema
416      */

417     public boolean isShowDefaultSchema()
418     {
419         return showDefaultSchemaAction.isChecked();
420     }
421
422
423     /**
424      * Sets the show default schema flag.
425      *
426      * @param b the show default schema flag
427      */

428     public void setShowDefaultSchema( boolean b )
429     {
430         showDefaultSchemaAction.setChecked( b );
431         connectionCombo.updateEnabledState();
432         reloadSchemaAction.updateEnabledState();
433         refresh();
434     }
435 }
436
Popular Tags