KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > widgets > ACIItemTabFolderComposite


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.aciitemeditor.widgets;
21
22
23 import java.text.ParseException JavaDoc;
24
25 import org.apache.directory.ldapstudio.aciitemeditor.ACIItemValueWithContext;
26 import org.apache.directory.ldapstudio.aciitemeditor.Activator;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.jface.dialogs.ErrorDialog;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.FillLayout;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.TabFolder;
38 import org.eclipse.swt.widgets.TabItem;
39
40
41 /**
42  * This composite contains the tabs with visual and source editor.
43  * It also manages the synchronization between these two tabs.
44  *
45  *
46  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
47  * @version $Rev$, $Date$
48  */

49 public class ACIItemTabFolderComposite extends Composite
50 {
51
52     /** The index of the visual tab */
53     public static final int VISUAL_TAB_INDEX = 0;
54
55     /** The index of the source tab */
56     public static final int SOURCE_TAB_INDEX = 1;
57
58     /** The tab folder */
59     private TabFolder tabFolder;
60
61     /** The visual tab */
62     private TabItem visualTab;
63
64     /** The inner container of the visual tab */
65     private Composite visualContainer;
66
67     /** The visual editor composite */
68     private ACIItemVisualEditorComposite visualComposite;
69
70     /** Tehe source tab */
71     private TabItem sourceTab;
72
73     /** The inner container of the visual tab */
74     private Composite sourceContainer;
75
76     /** The source editor composite */
77     private ACIItemSourceEditorComposite sourceComposite;
78
79
80     /**
81      * Creates a new instance of TabFolderComposite.
82      *
83      * @param parent
84      * @param style
85      */

86     public ACIItemTabFolderComposite( Composite parent, int style )
87     {
88         super( parent, style );
89         setLayoutData( new GridData( GridData.FILL_BOTH ) );
90         GridLayout layout = new GridLayout( 1, false );
91         layout.marginWidth = 0;
92         layout.marginHeight = 0;
93         setLayout( layout );
94
95         createTabFolder();
96
97         createVisualTab();
98
99         createSourceTab();
100
101         initListeners();
102     }
103
104
105     /**
106      * Initializes the listeners.
107      *
108      */

109     private void initListeners()
110     {
111         tabFolder.addSelectionListener( new SelectionAdapter()
112         {
113             public void widgetSelected( SelectionEvent e )
114             {
115                 tabSelected();
116             }
117         } );
118     }
119
120
121     /**
122      * Creates the source tab and configures the source editor.
123      *
124      */

125     private void createSourceTab()
126     {
127         // create inner container
128
sourceContainer = new Composite( tabFolder, SWT.BORDER );
129         sourceContainer.setLayout( new FillLayout() );
130
131         // create source editor
132
sourceComposite = new ACIItemSourceEditorComposite( sourceContainer, SWT.NONE );
133
134         // create tab
135
sourceTab = new TabItem( tabFolder, SWT.NONE, SOURCE_TAB_INDEX );
136         sourceTab.setText( Messages.getString( "ACIItemTabFolderComposite.source.tab" ) ); //$NON-NLS-1$
137
sourceTab.setControl( sourceContainer );
138     }
139
140
141     /**
142      * Creates the visual tab and the GUI editor.
143      *
144      */

145     private void createVisualTab()
146     {
147         // create inner container
148
visualContainer = new Composite( tabFolder, SWT.NONE );
149         visualContainer.setLayout( new FillLayout() );
150
151         // create the visual ACIItem composite
152
visualComposite = new ACIItemVisualEditorComposite( visualContainer, SWT.NONE );
153
154         // create tab
155
visualTab = new TabItem( tabFolder, SWT.NONE, VISUAL_TAB_INDEX );
156         visualTab.setText( Messages.getString( "ACIItemTabFolderComposite.visual.tab" ) ); //$NON-NLS-1$
157
visualTab.setControl( visualContainer );
158     }
159
160
161     /**
162      * Creates the tab folder and the listeners.
163      *
164      */

165     private void createTabFolder()
166     {
167         tabFolder = new TabFolder( this, SWT.TOP );
168         GridLayout mainLayout = new GridLayout();
169         mainLayout.marginWidth = 0;
170         mainLayout.marginHeight = 0;
171         tabFolder.setLayout( mainLayout );
172         tabFolder.setLayoutData( new GridData( GridData.FILL_BOTH ) );
173     }
174
175
176     /**
177      * Called, when a tab is selected. This method manages the synchronization
178      * between visual and source editor.
179      */

180     private void tabSelected()
181     {
182         int index = tabFolder.getSelectionIndex();
183
184         if ( index == SOURCE_TAB_INDEX )
185         {
186             // switched to source tab: serialize visual and set to source
187
// on parse error: print message and return to visual tab
188
try
189             {
190                 String JavaDoc input = visualComposite.getInput();
191                 sourceComposite.setInput( input );
192             }
193             catch ( ParseException JavaDoc pe )
194             {
195                 IStatus status = new Status( IStatus.ERROR, Activator.PLUGIN_ID, 1, Messages
196                     .getString( "ACIItemTabFolderComposite.error.onVisualEditor" ), pe ); //$NON-NLS-1$
197
ErrorDialog.openError( getShell(),
198                     Messages.getString( "ACIItemTabFolderComposite.error.title" ), null, status ); //$NON-NLS-1$
199
tabFolder.setSelection( VISUAL_TAB_INDEX );
200             }
201         }
202         else if ( index == VISUAL_TAB_INDEX )
203         {
204             // switched to visual tab: parse source and populate to visual
205
// on parse error: print message and return to source tab
206
try
207             {
208                 String JavaDoc input = sourceComposite.getInput();
209                 visualComposite.setInput( input );
210             }
211             catch ( ParseException JavaDoc pe )
212             {
213                 IStatus status = new Status( IStatus.ERROR, Activator.PLUGIN_ID, 1, Messages
214                     .getString( "ACIItemTabFolderComposite.error.onSourceEditor" ), pe ); //$NON-NLS-1$
215
ErrorDialog.openError( getShell(),
216                     Messages.getString( "ACIItemTabFolderComposite.error.title" ), null, status ); //$NON-NLS-1$
217
tabFolder.setSelection( SOURCE_TAB_INDEX );
218             }
219         }
220     }
221
222
223     /**
224      * Sets the input to both the source editor and to the visual editor.
225      * If the syntax is invalid the source editor is activated.
226      *
227      * @param input The string representation of the ACI item
228      */

229     public void setInput( String JavaDoc input )
230     {
231         // set input to source editor
232
sourceComposite.forceSetInput( input );
233
234         // set input to visual editor, on parse error switch to source editor
235
try
236         {
237             visualComposite.setInput( input );
238         }
239         catch ( ParseException JavaDoc pe )
240         {
241             IStatus status = new Status( IStatus.ERROR, Activator.PLUGIN_ID, 1, Messages
242                 .getString( "ACIItemTabFolderComposite.error.onInput" ), pe ); //$NON-NLS-1$
243
ErrorDialog.openError( getShell(),
244                 Messages.getString( "ACIItemTabFolderComposite.error.title" ), null, status ); //$NON-NLS-1$
245

246             tabFolder.setSelection( SOURCE_TAB_INDEX );
247         }
248     }
249
250
251     /**
252      * Returns the string representation of the ACI item.
253      * A syntax check is performed before returning the input, an
254      * invalid syntax causes a ParseException.
255      *
256      * @return the valid string representation of the ACI item
257      * @throws ParseException it the syntax check fails.
258      */

259     public String JavaDoc getInput() throws ParseException JavaDoc
260     {
261         int index = tabFolder.getSelectionIndex();
262         if ( index == VISUAL_TAB_INDEX )
263         {
264             String JavaDoc input = visualComposite.getInput();
265             return input;
266         }
267         else
268         {
269             String JavaDoc input = sourceComposite.getInput();
270             return input;
271         }
272     }
273
274
275     /**
276      * Sets the context.
277      *
278      * @param context the context
279      */

280     public void setContext( ACIItemValueWithContext context )
281     {
282         sourceComposite.setContext( context );
283         visualComposite.setContext( context );
284     }
285
286
287     /**
288      * Formats the content.
289      */

290     public void format()
291     {
292         sourceComposite.format();
293         //visualComposite.format();
294
}
295
296 }
297
Popular Tags