KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > dialogs > preferences > LdifEditorSyntaxColoringPreferencePage


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.preferences;
22
23
24 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
25 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
26 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
27 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile;
28 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorActivator;
29 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorConstants;
30 import org.apache.directory.ldapstudio.ldifeditor.editor.ILdifEditor;
31 import org.apache.directory.ldapstudio.ldifeditor.widgets.LdifEditorWidget;
32 import org.eclipse.jface.preference.ColorSelector;
33 import org.eclipse.jface.preference.IPreferenceStore;
34 import org.eclipse.jface.preference.PreferenceConverter;
35 import org.eclipse.jface.preference.PreferencePage;
36 import org.eclipse.jface.text.TextAttribute;
37 import org.eclipse.jface.util.IPropertyChangeListener;
38 import org.eclipse.jface.util.PropertyChangeEvent;
39 import org.eclipse.jface.viewers.ArrayContentProvider;
40 import org.eclipse.jface.viewers.ISelectionChangedListener;
41 import org.eclipse.jface.viewers.IStructuredSelection;
42 import org.eclipse.jface.viewers.LabelProvider;
43 import org.eclipse.jface.viewers.SelectionChangedEvent;
44 import org.eclipse.jface.viewers.StructuredSelection;
45 import org.eclipse.jface.viewers.TableViewer;
46 import org.eclipse.swt.SWT;
47 import org.eclipse.swt.events.SelectionAdapter;
48 import org.eclipse.swt.events.SelectionEvent;
49 import org.eclipse.swt.graphics.RGB;
50 import org.eclipse.swt.layout.GridData;
51 import org.eclipse.swt.layout.GridLayout;
52 import org.eclipse.swt.widgets.Button;
53 import org.eclipse.swt.widgets.Composite;
54 import org.eclipse.swt.widgets.Control;
55 import org.eclipse.ui.IWorkbench;
56 import org.eclipse.ui.IWorkbenchPreferencePage;
57
58
59 public class LdifEditorSyntaxColoringPreferencePage extends PreferencePage implements IWorkbenchPreferencePage,
60     ILdifEditor
61 {
62
63     private static final String JavaDoc LDIF_INITIAL = "" + "# Content record" + BrowserCoreConstants.LINE_SEPARATOR
64         + "dn: cn=content record" + BrowserCoreConstants.LINE_SEPARATOR + "objectClass: person"
65         + BrowserCoreConstants.LINE_SEPARATOR + "cn: content record" + BrowserCoreConstants.LINE_SEPARATOR
66         + "cn;lang-ja:: 5Za25qWt6YOo" + BrowserCoreConstants.LINE_SEPARATOR + "" + BrowserCoreConstants.LINE_SEPARATOR
67
68         + "# Add record with control" + BrowserCoreConstants.LINE_SEPARATOR + "dn: cn=add record"
69         + BrowserCoreConstants.LINE_SEPARATOR + "control: 1.2.3.4 true: controlValue"
70         + BrowserCoreConstants.LINE_SEPARATOR + "changetype: add" + BrowserCoreConstants.LINE_SEPARATOR
71         + "objectClass: person" + BrowserCoreConstants.LINE_SEPARATOR + "cn: add record"
72         + BrowserCoreConstants.LINE_SEPARATOR + "" + BrowserCoreConstants.LINE_SEPARATOR
73
74         + "# Modify record" + BrowserCoreConstants.LINE_SEPARATOR + "dn: cn=modify record"
75         + BrowserCoreConstants.LINE_SEPARATOR + "changetype: modify" + BrowserCoreConstants.LINE_SEPARATOR + "add: cn"
76         + BrowserCoreConstants.LINE_SEPARATOR + "cn: modify record" + BrowserCoreConstants.LINE_SEPARATOR + "-"
77         + BrowserCoreConstants.LINE_SEPARATOR + "delete: cn" + BrowserCoreConstants.LINE_SEPARATOR + "-"
78         + BrowserCoreConstants.LINE_SEPARATOR + "replace: cn" + BrowserCoreConstants.LINE_SEPARATOR
79         + "cn: modify record" + BrowserCoreConstants.LINE_SEPARATOR + "-" + BrowserCoreConstants.LINE_SEPARATOR + ""
80         + BrowserCoreConstants.LINE_SEPARATOR
81
82         + "# Delete record" + BrowserCoreConstants.LINE_SEPARATOR + "dn: cn=delete record"
83         + BrowserCoreConstants.LINE_SEPARATOR + "changetype: delete" + BrowserCoreConstants.LINE_SEPARATOR + ""
84         + BrowserCoreConstants.LINE_SEPARATOR
85
86         + "# Modify DN record" + BrowserCoreConstants.LINE_SEPARATOR + "dn: cn=moddn record"
87         + BrowserCoreConstants.LINE_SEPARATOR + "changetype: moddn" + BrowserCoreConstants.LINE_SEPARATOR
88         + "newrdn: cn=new rdn" + BrowserCoreConstants.LINE_SEPARATOR + "deleteoldrdn: 1"
89         + BrowserCoreConstants.LINE_SEPARATOR + "newsuperior: cn=new superior" + BrowserCoreConstants.LINE_SEPARATOR
90         + "" + BrowserCoreConstants.LINE_SEPARATOR;
91
92     private LdifEditorWidget ldifEditorWidget;
93
94     private SyntaxItem[] syntaxItems;
95
96     private ColorSelector colorSelector;
97
98     private Button boldCheckBox;
99
100     private Button italicCheckBox;
101
102     private Button underlineCheckBox;
103
104     private Button strikethroughCheckBox;
105
106     private TableViewer syntaxItemViewer;
107
108     private class SyntaxItem
109     {
110         String JavaDoc displayName;
111
112         String JavaDoc key;
113
114         RGB rgb;
115
116         boolean bold;
117
118         boolean italic;
119
120         boolean strikethrough;
121
122         boolean underline;
123
124
125         SyntaxItem( String JavaDoc displayName, String JavaDoc key )
126         {
127             this.displayName = displayName;
128             this.key = key;
129             loadPreferences();
130         }
131
132
133         int getStyle()
134         {
135             int style = SWT.NORMAL;
136             if ( bold )
137                 style |= SWT.BOLD;
138             if ( italic )
139                 style |= SWT.ITALIC;
140             if ( strikethrough )
141                 style |= TextAttribute.STRIKETHROUGH;
142             if ( underline )
143                 style |= TextAttribute.UNDERLINE;
144             return style;
145         }
146
147
148         void setStyle( int style )
149         {
150             this.bold = ( style & SWT.BOLD ) != SWT.NORMAL;
151             this.italic = ( style & SWT.ITALIC ) != SWT.NORMAL;
152             this.strikethrough = ( style & TextAttribute.STRIKETHROUGH ) != SWT.NORMAL;
153             this.underline = ( style & TextAttribute.UNDERLINE ) != SWT.NORMAL;
154         }
155
156
157         void loadPreferences()
158         {
159             IPreferenceStore store = LdifEditorActivator.getDefault().getPreferenceStore();
160             this.rgb = PreferenceConverter.getColor( store, key
161                 + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_RGB_SUFFIX );
162             int style = store.getInt( key + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_STYLE_SUFFIX );
163             setStyle( style );
164         }
165
166
167         void savePreferences()
168         {
169             IPreferenceStore store = LdifEditorActivator.getDefault().getPreferenceStore();
170             PreferenceConverter.setValue( store, key + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_RGB_SUFFIX, rgb );
171             store.setValue( key + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_STYLE_SUFFIX, getStyle() );
172         }
173
174
175         void loadDefaultPreferences()
176         {
177             IPreferenceStore store = LdifEditorActivator.getDefault().getPreferenceStore();
178             this.rgb = PreferenceConverter.getDefaultColor( store, key
179                 + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_RGB_SUFFIX );
180             int style = store.getDefaultInt( key + LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_STYLE_SUFFIX );
181             setStyle( style );
182         }
183
184
185         public String JavaDoc toString()
186         {
187             return displayName;
188         }
189     }
190
191
192     public LdifEditorSyntaxColoringPreferencePage()
193     {
194         super( "Syntax Coloring" );
195         super.setPreferenceStore( LdifEditorActivator.getDefault().getPreferenceStore() );
196         // super.setDescription("");
197
}
198
199
200     public void init( IWorkbench workbench )
201     {
202     }
203
204
205     public void dispose()
206     {
207         ldifEditorWidget.dispose();
208         super.dispose();
209     }
210
211
212     protected Control createContents( Composite parent )
213     {
214
215         Composite composite = new Composite( parent, SWT.NONE );
216         GridLayout layout = new GridLayout( 1, false );
217         layout.marginWidth = 0;
218         layout.marginHeight = 0;
219         layout.marginLeft = 0;
220         layout.marginRight = 0;
221         layout.marginTop = 0;
222         layout.marginBottom = 0;
223         composite.setLayout( layout );
224         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
225         composite.setLayoutData( gd );
226
227         BaseWidgetUtils.createSpacer( composite, 1 );
228         BaseWidgetUtils.createSpacer( composite, 1 );
229
230         createSyntaxPage( composite );
231         createPreviewer( composite );
232
233         syntaxItems = new SyntaxItem[10];
234         syntaxItems[0] = new SyntaxItem( "Comments", LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_COMMENT );
235         syntaxItems[1] = new SyntaxItem( "DN", LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_DN );
236         syntaxItems[2] = new SyntaxItem( "Attribute Descriptions",
237             LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_ATTRIBUTE );
238         syntaxItems[3] = new SyntaxItem( "Value Types", LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_VALUETYPE );
239         syntaxItems[4] = new SyntaxItem( "Values", LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_VALUE );
240         syntaxItems[5] = new SyntaxItem( "Keywords (w/o changetypes)",
241             LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_KEYWORD );
242         syntaxItems[6] = new SyntaxItem( "Changetype 'add'",
243             LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_CHANGETYPEADD );
244         syntaxItems[7] = new SyntaxItem( "Changetype 'modify'",
245             LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_CHANGETYPEMODIFY );
246         syntaxItems[8] = new SyntaxItem( "Changetype 'delete'",
247             LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_CHANGETYPEDELETE );
248         syntaxItems[9] = new SyntaxItem( "Changetype 'moddn'",
249             LdifEditorConstants.PREFERENCE_LDIFEDITOR_SYNTAX_CHANGETYPEMODDN );
250         syntaxItemViewer.setInput( syntaxItems );
251         syntaxItemViewer.setSelection( new StructuredSelection( syntaxItems[0] ) );
252
253         return composite;
254     }
255
256
257     private void createSyntaxPage( Composite parent )
258     {
259
260         BaseWidgetUtils.createLabel( parent, "Element:", 1 );
261
262         Composite editorComposite = BaseWidgetUtils.createColumnContainer( parent, 2, 1 );
263
264         syntaxItemViewer = new TableViewer( editorComposite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER
265             | SWT.FULL_SELECTION );
266         syntaxItemViewer.setLabelProvider( new LabelProvider() );
267         syntaxItemViewer.setContentProvider( new ArrayContentProvider() );
268         // colorListViewer.setSorter(new WorkbenchViewerSorter());
269
GridData gd = new GridData( GridData.FILL_BOTH );
270         gd.heightHint = convertHeightInCharsToPixels( 5 );
271         syntaxItemViewer.getControl().setLayoutData( gd );
272
273         Composite stylesComposite = BaseWidgetUtils.createColumnContainer( editorComposite, 1, 1 );
274         stylesComposite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
275         Composite colorComposite = BaseWidgetUtils.createColumnContainer( stylesComposite, 2, 1 );
276         BaseWidgetUtils.createLabel( colorComposite, "Color:", 1 );
277         colorSelector = new ColorSelector( colorComposite );
278         boldCheckBox = BaseWidgetUtils.createCheckbox( stylesComposite, "Bold", 1 );
279         italicCheckBox = BaseWidgetUtils.createCheckbox( stylesComposite, "Italic", 1 );
280         strikethroughCheckBox = BaseWidgetUtils.createCheckbox( stylesComposite, "Strikethrough", 1 );
281         underlineCheckBox = BaseWidgetUtils.createCheckbox( stylesComposite, "Underline", 1 );
282
283         syntaxItemViewer.addSelectionChangedListener( new ISelectionChangedListener()
284         {
285             public void selectionChanged( SelectionChangedEvent event )
286             {
287                 handleSyntaxItemViewerSelectionEvent();
288             }
289         } );
290         colorSelector.addListener( new IPropertyChangeListener()
291         {
292             public void propertyChange( PropertyChangeEvent event )
293             {
294                 handleColorSelectorEvent();
295             }
296         } );
297         boldCheckBox.addSelectionListener( new SelectionAdapter()
298         {
299             public void widgetSelected( SelectionEvent e )
300             {
301                 handleBoldSelectionEvent();
302             }
303         } );
304         italicCheckBox.addSelectionListener( new SelectionAdapter()
305         {
306             public void widgetSelected( SelectionEvent e )
307             {
308                 handleItalicSelectionEvent();
309             }
310         } );
311         strikethroughCheckBox.addSelectionListener( new SelectionAdapter()
312         {
313             public void widgetSelected( SelectionEvent e )
314             {
315                 handleStrikethroughSelectionEvent();
316             }
317         } );
318         underlineCheckBox.addSelectionListener( new SelectionAdapter()
319         {
320             public void widgetSelected( SelectionEvent e )
321             {
322                 handleUnderlineSelectionEvent();
323             }
324         } );
325
326     }
327
328
329     private void handleUnderlineSelectionEvent()
330     {
331         SyntaxItem item = getSyntaxItem();
332         if ( item != null )
333         {
334             item.underline = underlineCheckBox.getSelection();
335             setTextAttribute( item );
336         }
337     }
338
339
340     private void handleStrikethroughSelectionEvent()
341     {
342         SyntaxItem item = getSyntaxItem();
343         if ( item != null )
344         {
345             item.strikethrough = strikethroughCheckBox.getSelection();
346             setTextAttribute( item );
347         }
348     }
349
350
351     private void handleItalicSelectionEvent()
352     {
353         SyntaxItem item = getSyntaxItem();
354         if ( item != null )
355         {
356             item.italic = italicCheckBox.getSelection();
357             setTextAttribute( item );
358         }
359     }
360
361
362     private void handleBoldSelectionEvent()
363     {
364         SyntaxItem item = getSyntaxItem();
365         if ( item != null )
366         {
367             item.bold = boldCheckBox.getSelection();
368             setTextAttribute( item );
369         }
370     }
371
372
373     private void handleColorSelectorEvent()
374     {
375         SyntaxItem item = getSyntaxItem();
376         if ( item != null )
377         {
378             item.rgb = colorSelector.getColorValue();
379             setTextAttribute( item );
380         }
381     }
382
383
384     private void handleSyntaxItemViewerSelectionEvent()
385     {
386         SyntaxItem item = getSyntaxItem();
387         if ( item != null )
388         {
389             colorSelector.setColorValue( item.rgb );
390             boldCheckBox.setSelection( item.bold );
391             italicCheckBox.setSelection( item.italic );
392             strikethroughCheckBox.setSelection( item.strikethrough );
393             underlineCheckBox.setSelection( item.underline );
394         }
395     }
396
397
398     private SyntaxItem getSyntaxItem()
399     {
400         SyntaxItem item = ( SyntaxItem ) ( ( IStructuredSelection ) syntaxItemViewer.getSelection() ).getFirstElement();
401         return item;
402     }
403
404
405     private void setTextAttribute( SyntaxItem item )
406     {
407         ldifEditorWidget.getSourceViewerConfiguration().setTextAttribute( item.key, item.rgb, item.getStyle() );
408
409         int topIndex = ldifEditorWidget.getSourceViewer().getTopIndex();
410         // ldifEditorWidget.getSourceViewer().getDocument().set("");
411
ldifEditorWidget.getSourceViewer().getDocument().set( LDIF_INITIAL );
412         ldifEditorWidget.getSourceViewer().setTopIndex( topIndex );
413     }
414
415
416     private void createPreviewer( Composite parent )
417     {
418
419         BaseWidgetUtils.createLabel( parent, "Preview:", 1 );
420
421         ldifEditorWidget = new LdifEditorWidget( null, LDIF_INITIAL, false );
422         ldifEditorWidget.createWidget( parent );
423         ldifEditorWidget.getSourceViewer().setEditable( false );
424
425         GridData gd = new GridData( GridData.FILL_BOTH );
426         gd.widthHint = convertWidthInCharsToPixels( 20 );
427         gd.heightHint = convertHeightInCharsToPixels( 5 );
428         ldifEditorWidget.getSourceViewer().getControl().setLayoutData( gd );
429
430     }
431
432
433     public IConnection getConnection()
434     {
435         return ldifEditorWidget.getConnection();
436     }
437
438
439     public LdifFile getLdifModel()
440     {
441         return ldifEditorWidget.getLdifModel();
442     }
443
444
445     public boolean performOk()
446     {
447         for ( int i = 0; i < syntaxItems.length; i++ )
448         {
449             syntaxItems[i].savePreferences();
450         }
451         return true;
452     }
453
454
455     protected void performDefaults()
456     {
457         for ( int i = 0; i < syntaxItems.length; i++ )
458         {
459             syntaxItems[i].loadDefaultPreferences();
460             setTextAttribute( syntaxItems[i] );
461         }
462         handleSyntaxItemViewerSelectionEvent();
463         super.performDefaults();
464     }
465
466
467     public Object JavaDoc getAdapter( Class JavaDoc adapter )
468     {
469         return null;
470     }
471
472 }
473
Popular Tags