KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > dialogs > LdifEntryEditorDialog


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.ldifeditor.dialogs;
22
23
24 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidget;
25 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetActionGroup;
26 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetActionGroupWithAttribute;
27 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetConfiguration;
28 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetUniversalListener;
29 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyConnection;
30 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
31 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
32 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
33 import org.apache.directory.ldapstudio.browser.core.model.NameException;
34 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeAddRecord;
35 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord;
36 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifRecord;
37 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
38 import org.apache.directory.ldapstudio.browser.core.utils.ModelConverter;
39 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorActivator;
40 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorConstants;
41 import org.eclipse.jface.dialogs.Dialog;
42 import org.eclipse.jface.dialogs.IDialogConstants;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.Shell;
47
48
49 public class LdifEntryEditorDialog extends Dialog
50 {
51
52     public static final String JavaDoc DIALOG_TITLE = "LDIF Record Editor";
53
54     public static final int MAX_WIDTH = 450;
55
56     public static final int MAX_HEIGHT = 250;
57
58     private IConnection connection;
59
60     private LdifRecord ldifRecord;
61
62     private IEntry entry;
63
64     private EntryEditorWidgetConfiguration configuration;
65
66     private EntryEditorWidgetActionGroup actionGroup;
67
68     private EntryEditorWidget mainWidget;
69
70     private EntryEditorWidgetUniversalListener universalListener;
71
72
73     public LdifEntryEditorDialog( Shell parentShell, IConnection connection, LdifContentRecord ldifRecord )
74     {
75         this( parentShell, connection, ldifRecord, null );
76     }
77
78
79     public LdifEntryEditorDialog( Shell parentShell, IConnection connection, LdifChangeAddRecord ldifRecord )
80     {
81         this( parentShell, connection, ldifRecord, null );
82     }
83
84
85     private LdifEntryEditorDialog( Shell parentShell, IConnection connection, LdifRecord ldifRecord, String JavaDoc s )
86     {
87         super( parentShell );
88         setShellStyle( getShellStyle() | SWT.RESIZE );
89         this.ldifRecord = ldifRecord;
90
91         this.connection = new DummyConnection( ( connection != null && connection.getSchema() != null ) ? connection
92             .getSchema() : Schema.DEFAULT_SCHEMA );
93     }
94
95
96     protected void configureShell( Shell shell )
97     {
98         super.configureShell( shell );
99         shell.setText( DIALOG_TITLE );
100         shell.setImage( LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_BROWSER_LDIFEDITOR ) );
101     }
102
103
104     protected void createButtonsForButtonBar( Composite parent )
105     {
106         createButton( parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false );
107         createButton( parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false );
108
109         getShell().update();
110         getShell().layout( true, true );
111     }
112
113
114     protected void buttonPressed( int buttonId )
115     {
116
117         if ( IDialogConstants.OK_ID == buttonId )
118         {
119             if ( this.ldifRecord instanceof LdifContentRecord )
120             {
121                 this.ldifRecord = ModelConverter.entryToLdifContentRecord( entry );
122             }
123             else if ( this.ldifRecord instanceof LdifChangeAddRecord )
124             {
125                 this.ldifRecord = ModelConverter.entryToLdifChangeAddRecord( entry );
126             }
127         }
128
129         super.buttonPressed( buttonId );
130     }
131
132
133     public void create()
134     {
135         super.create();
136         // this.actionGroup.activateGlobalActionHandlers();
137
}
138
139
140     public boolean close()
141     {
142         boolean returnValue = super.close();
143         if ( returnValue )
144         {
145             this.dispose();
146         }
147         return returnValue;
148     }
149
150
151     public void dispose()
152     {
153         if ( this.configuration != null )
154         {
155             this.universalListener.dispose();
156             this.universalListener = null;
157             this.mainWidget.dispose();
158             this.mainWidget = null;
159             this.actionGroup.deactivateGlobalActionHandlers();
160             this.actionGroup.dispose();
161             this.actionGroup = null;
162             this.configuration.dispose();
163             this.configuration = null;
164         }
165     }
166
167
168     protected Control createDialogArea( Composite parent )
169     {
170
171         Composite composite = ( Composite ) super.createDialogArea( parent );
172
173         // create configuration
174
this.configuration = new EntryEditorWidgetConfiguration();
175
176         // create main widget
177
this.mainWidget = new EntryEditorWidget( this.configuration );
178         this.mainWidget.createWidget( composite );
179         this.mainWidget.getViewer().getTree().setFocus();
180
181         // create actions
182
this.actionGroup = new EntryEditorWidgetActionGroupWithAttribute( this.mainWidget, this.configuration );
183         this.actionGroup.fillToolBar( this.mainWidget.getToolBarManager() );
184         this.actionGroup.fillMenu( this.mainWidget.getMenuManager() );
185         this.actionGroup.fillContextMenu( this.mainWidget.getContextMenuManager() );
186         this.actionGroup.activateGlobalActionHandlers();
187
188         // create the listener
189
this.universalListener = new EntryEditorWidgetUniversalListener( this.mainWidget.getViewer(), this.actionGroup
190             .getOpenDefaultEditorAction() );
191
192         try
193         {
194
195             if ( ldifRecord instanceof LdifContentRecord )
196             {
197                 entry = ModelConverter.ldifContentRecordToEntry( ( LdifContentRecord ) ldifRecord, connection );
198             }
199             else if ( ldifRecord instanceof LdifChangeAddRecord )
200             {
201                 entry = ModelConverter.ldifChangeAddRecordToEntry( ( LdifChangeAddRecord ) ldifRecord, connection );
202             }
203
204             if ( entry != null )
205             {
206                 this.mainWidget.getViewer().setInput( entry );
207             }
208
209         }
210         catch ( ModelModificationException e )
211         {
212             e.printStackTrace();
213         }
214         catch ( NameException e )
215         {
216             e.printStackTrace();
217         }
218
219         applyDialogFont( composite );
220         return composite;
221     }
222
223
224     public LdifRecord getLdifRecord()
225     {
226         return ldifRecord;
227     }
228
229 }
230
Popular Tags