KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > dialogs > SelectEntryDialog


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.common.dialogs;
22
23
24 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserActionGroup;
25 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserConfiguration;
26 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserUniversalListener;
27 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserWidget;
28 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
29 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
30
31 import org.eclipse.jface.dialogs.Dialog;
32 import org.eclipse.jface.dialogs.IDialogConstants;
33 import org.eclipse.jface.viewers.ISelectionChangedListener;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.jface.viewers.SelectionChangedEvent;
36 import org.eclipse.jface.viewers.StructuredSelection;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.Shell;
43
44
45 public class SelectEntryDialog extends Dialog
46 {
47
48     private String JavaDoc title;
49
50     private Image image;
51
52     private IEntry rootEntry;
53
54     private IEntry initialEntry;
55
56     private IEntry selectedEntry;
57
58     private BrowserConfiguration configuration;
59
60     private BrowserUniversalListener universalListener;
61
62     private BrowserActionGroup actionGroup;
63
64     private BrowserWidget mainWidget;
65
66
67     public SelectEntryDialog( Shell parentShell, String JavaDoc title, IEntry rootEntry, IEntry initialEntry )
68     {
69         super( parentShell );
70         super.setShellStyle( super.getShellStyle() | SWT.RESIZE );
71         this.title = title;
72         this.rootEntry = rootEntry;
73         this.initialEntry = initialEntry;
74         this.selectedEntry = null;
75     }
76
77
78     public SelectEntryDialog( Shell parentShell, String JavaDoc title, Image image, IEntry rootEntry, IEntry initialEntry )
79     {
80         this( parentShell, title, rootEntry, initialEntry );
81         this.image = image;
82     }
83
84
85     protected void configureShell( Shell shell )
86     {
87         super.configureShell( shell );
88         shell.setText( title );
89         shell.setImage( image );
90     }
91
92
93     public boolean close()
94     {
95         if ( this.mainWidget != null )
96         {
97             this.configuration.dispose();
98             this.configuration = null;
99             this.actionGroup.deactivateGlobalActionHandlers();
100             this.actionGroup.dispose();
101             this.actionGroup = null;
102             this.universalListener.dispose();
103             this.universalListener = null;
104             this.mainWidget.dispose();
105             this.mainWidget = null;
106         }
107         return super.close();
108     }
109
110
111     protected void okPressed()
112     {
113         this.selectedEntry = initialEntry;
114         super.okPressed();
115     }
116
117
118     protected void cancelPressed()
119     {
120         this.selectedEntry = null;
121         super.cancelPressed();
122     }
123
124
125     protected void createButtonsForButtonBar( Composite parent )
126     {
127         createButton( parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false );
128         createButton( parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false );
129     }
130
131
132     protected Control createDialogArea( Composite parent )
133     {
134
135         Composite composite = ( Composite ) super.createDialogArea( parent );
136         GridData gd = new GridData( GridData.FILL_BOTH );
137         gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH );
138         gd.heightHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH );
139         composite.setLayoutData( gd );
140
141         // create configuration
142
this.configuration = new BrowserConfiguration();
143
144         // create main widget
145
this.mainWidget = new BrowserWidget( this.configuration, null );
146         this.mainWidget.createWidget( composite );
147         this.mainWidget.setInput( new IEntry[]
148             { rootEntry } );
149
150         // create actions and context menu (and register global actions)
151
this.actionGroup = new BrowserActionGroup( this.mainWidget, this.configuration );
152         this.actionGroup.fillToolBar( this.mainWidget.getToolBarManager() );
153         this.actionGroup.fillMenu( this.mainWidget.getMenuManager() );
154         this.actionGroup.fillContextMenu( this.mainWidget.getContextMenuManager() );
155         this.actionGroup.activateGlobalActionHandlers();
156
157         // create the listener
158
this.universalListener = new BrowserUniversalListener( this.mainWidget.getViewer() );
159
160         this.mainWidget.getViewer().addSelectionChangedListener( new ISelectionChangedListener()
161         {
162             public void selectionChanged( SelectionChangedEvent event )
163             {
164                 if ( !event.getSelection().isEmpty() )
165                 {
166                     Object JavaDoc o = ( ( IStructuredSelection ) event.getSelection() ).getFirstElement();
167                     if ( o instanceof IEntry )
168                     {
169                         initialEntry = ( IEntry ) o;
170                     }
171                 }
172             }
173         } );
174
175         this.mainWidget.getViewer().expandToLevel( 2 );
176         if ( this.initialEntry != null )
177         {
178             IEntry entry = this.initialEntry;
179             this.mainWidget.getViewer().reveal( entry );
180             this.mainWidget.getViewer().refresh( entry, true );
181             this.mainWidget.getViewer().setSelection( new StructuredSelection( entry ), true );
182             this.mainWidget.getViewer().setSelection( new StructuredSelection( entry ), true );
183         }
184
185         applyDialogFont( composite );
186
187         this.mainWidget.setFocus();
188
189         return composite;
190
191     }
192
193
194     public IEntry getSelectedEntry()
195     {
196         return this.selectedEntry;
197     }
198
199 }
200
Popular Tags