KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > editor > LdifEditor


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.editor;
22
23
24 import java.io.File JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
28 import org.apache.directory.ldapstudio.browser.common.actions.ValueEditorPreferencesAction;
29 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
30 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin;
31 import org.apache.directory.ldapstudio.browser.core.events.ConnectionUpdateEvent;
32 import org.apache.directory.ldapstudio.browser.core.events.ConnectionUpdateListener;
33 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
34 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
35 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile;
36 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorActivator;
37 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorConstants;
38 import org.apache.directory.ldapstudio.ldifeditor.editor.actions.EditLdifAttributeAction;
39 import org.apache.directory.ldapstudio.ldifeditor.editor.actions.EditLdifRecordAction;
40 import org.apache.directory.ldapstudio.ldifeditor.editor.actions.FormatLdifDocumentAction;
41 import org.apache.directory.ldapstudio.ldifeditor.editor.actions.FormatLdifRecordAction;
42 import org.apache.directory.ldapstudio.ldifeditor.editor.actions.OpenBestValueEditorAction;
43 import org.apache.directory.ldapstudio.ldifeditor.editor.actions.OpenDefaultValueEditorAction;
44 import org.apache.directory.ldapstudio.ldifeditor.editor.actions.OpenValueEditorAction;
45 import org.apache.directory.ldapstudio.ldifeditor.editor.text.LdifPartitionScanner;
46 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogValueEditor;
47 import org.apache.directory.ldapstudio.valueeditors.IValueEditor;
48 import org.apache.directory.ldapstudio.valueeditors.ValueEditorManager;
49 import org.eclipse.core.runtime.CoreException;
50 import org.eclipse.core.runtime.IPath;
51 import org.eclipse.core.runtime.IProgressMonitor;
52 import org.eclipse.core.runtime.IStatus;
53 import org.eclipse.core.runtime.Path;
54 import org.eclipse.jface.action.ActionContributionItem;
55 import org.eclipse.jface.action.IAction;
56 import org.eclipse.jface.action.IContributionItem;
57 import org.eclipse.jface.action.IMenuManager;
58 import org.eclipse.jface.action.IToolBarManager;
59 import org.eclipse.jface.action.MenuManager;
60 import org.eclipse.jface.action.Separator;
61 import org.eclipse.jface.action.ToolBarManager;
62 import org.eclipse.jface.commands.ActionHandler;
63 import org.eclipse.jface.dialogs.IDialogConstants;
64 import org.eclipse.jface.dialogs.MessageDialog;
65 import org.eclipse.jface.preference.IPreferenceStore;
66 import org.eclipse.jface.text.ITextHover;
67 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
68 import org.eclipse.jface.text.source.IAnnotationHover;
69 import org.eclipse.jface.text.source.ISourceViewer;
70 import org.eclipse.jface.text.source.IVerticalRuler;
71 import org.eclipse.jface.text.source.projection.ProjectionSupport;
72 import org.eclipse.jface.text.source.projection.ProjectionViewer;
73 import org.eclipse.jface.util.PropertyChangeEvent;
74 import org.eclipse.jface.window.Window;
75 import org.eclipse.swt.SWT;
76 import org.eclipse.swt.custom.ViewForm;
77 import org.eclipse.swt.events.ModifyEvent;
78 import org.eclipse.swt.events.ModifyListener;
79 import org.eclipse.swt.layout.FillLayout;
80 import org.eclipse.swt.layout.GridData;
81 import org.eclipse.swt.layout.GridLayout;
82 import org.eclipse.swt.widgets.Combo;
83 import org.eclipse.swt.widgets.Composite;
84 import org.eclipse.swt.widgets.FileDialog;
85 import org.eclipse.swt.widgets.Shell;
86 import org.eclipse.swt.widgets.ToolBar;
87 import org.eclipse.ui.IEditorInput;
88 import org.eclipse.ui.IEditorSite;
89 import org.eclipse.ui.IPageLayout;
90 import org.eclipse.ui.IPartListener2;
91 import org.eclipse.ui.IPathEditorInput;
92 import org.eclipse.ui.ISharedImages;
93 import org.eclipse.ui.IWorkbenchPartReference;
94 import org.eclipse.ui.PartInitException;
95 import org.eclipse.ui.PlatformUI;
96 import org.eclipse.ui.commands.ICommandService;
97 import org.eclipse.ui.contexts.IContextActivation;
98 import org.eclipse.ui.contexts.IContextService;
99 import org.eclipse.ui.editors.text.EditorsUI;
100 import org.eclipse.ui.editors.text.TextEditor;
101 import org.eclipse.ui.part.IShowInTargetList;
102 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
103 import org.eclipse.ui.texteditor.ContentAssistAction;
104 import org.eclipse.ui.texteditor.IDocumentProvider;
105 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
106 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
107 import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
108 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
109
110
111 /**
112  * This class implements the LDIF editor
113  *
114  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
115  * @version $Rev$, $Date$
116  */

117 public class LdifEditor extends TextEditor implements ILdifEditor, ConnectionUpdateListener, IPartListener2
118 {
119     protected ViewForm control;
120
121     protected Combo connectionCombo;
122
123     protected ToolBar actionToolBar;
124
125     protected IToolBarManager actionToolBarManager;
126
127     private IConnection connection;
128
129     private ProjectionSupport projectionSupport;
130
131     private LdifOutlinePage outlinePage;
132
133     private ValueEditorManager valueEditorManager;
134
135     private OpenBestValueEditorAction openBestValueEditorAction;
136
137     private OpenValueEditorAction[] openValueEditorActions;
138
139     private ValueEditorPreferencesAction valueEditorPreferencesAction;
140
141
142     /**
143      * Creates a new instance of LdifEditor.
144      */

145     public LdifEditor()
146     {
147         super();
148
149         setSourceViewerConfiguration( new LdifSourceViewerConfiguration( this, true ) );
150         setDocumentProvider( new LdifDocumentProvider() );
151
152         IPreferenceStore editorStore = EditorsUI.getPreferenceStore();
153         IPreferenceStore browserStore = LdifEditorActivator.getDefault().getPreferenceStore();
154         IPreferenceStore combinedStore = new ChainedPreferenceStore( new IPreferenceStore[]
155             { browserStore, editorStore } );
156         setPreferenceStore( combinedStore );
157
158         setHelpContextId( LdifEditorActivator.PLUGIN_ID + "." + "tools_ldif_editor" );
159     }
160
161
162     /**
163      * {@inheritDoc}
164      */

165     protected void handlePreferenceStoreChanged( PropertyChangeEvent event )
166     {
167         try
168         {
169
170             ISourceViewer sourceViewer = getSourceViewer();
171             if ( sourceViewer == null )
172             {
173                 return;
174             }
175
176             int topIndex = getSourceViewer().getTopIndex();
177             getSourceViewer().getDocument().set( getSourceViewer().getDocument().get() );
178             getSourceViewer().setTopIndex( topIndex );
179
180         }
181         finally
182         {
183             super.handlePreferenceStoreChanged( event );
184         }
185     }
186
187
188     /**
189      * {@inheritDoc}
190      */

191     protected String JavaDoc[] collectContextMenuPreferencePages()
192     {
193         String JavaDoc[] ids = super.collectContextMenuPreferencePages();
194         String JavaDoc[] more = new String JavaDoc[ids.length + 4];
195         more[0] = LdifEditorConstants.PREFERENCEPAGEID_LDIFEDITOR;
196         more[1] = LdifEditorConstants.PREFERENCEPAGEID_LDIFEDITOR_CONTENTASSIST;
197         more[2] = LdifEditorConstants.PREFERENCEPAGEID_LDIFEDITOR_SYNTAXCOLORING;
198         more[3] = LdifEditorConstants.PREFERENCEPAGEID_LDIFEDITOR_TEMPLATES;
199         System.arraycopy( ids, 0, more, 4, ids.length );
200         return more;
201     }
202
203
204     /**
205      * Gets the ID of the LDIF Editor
206      *
207      * @return
208      * the ID of the LDIF Editor
209      */

210     public static String JavaDoc getId()
211     {
212         return LdifEditor.class.getName();
213     }
214
215
216     /**
217      * {@inheritDoc}
218      */

219     public void init( IEditorSite site, IEditorInput input ) throws PartInitException
220     {
221         if ( input instanceof IPathEditorInput )
222         {
223             IPathEditorInput pei = ( IPathEditorInput ) input;
224             IPath path = pei.getPath();
225             File JavaDoc javaIoFile = path.toFile();
226             long fileLength = javaIoFile.length();
227             if ( fileLength > ( 1 * 1024 * 1024 ) )
228             {
229                 MessageDialog.openError( site.getShell(), "LDIF file is too big",
230                     "The selected LDIF file is too big. Perhaps it is possilbe to open it in a further version..." );
231                 super.init( site, new NonExistingLdifEditorInput() );
232                 return;
233             }
234         }
235
236         super.init( site, input );
237
238         EventRegistry.addConnectionUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
239         getSite().getPage().addPartListener( this );
240
241         this.valueEditorManager = new ValueEditorManager( getSite().getShell() );
242     }
243
244
245     /**
246      * {@inheritDoc}
247      */

248     public void dispose()
249     {
250
251         this.valueEditorManager.dispose();
252
253         deactivateGlobalActionHandlers();
254
255         EventRegistry.removeConnectionUpdateListener( this );
256         getSite().getPage().removePartListener( this );
257
258         super.dispose();
259     }
260
261
262     /**
263      * {@inheritDoc}
264      */

265     public Object JavaDoc getAdapter( Class JavaDoc required )
266     {
267         if ( IShowInTargetList.class.equals( required ) )
268         {
269             return new IShowInTargetList()
270             {
271                 public String JavaDoc[] getShowInTargetIds()
272                 {
273                     return new String JavaDoc[]
274                         { IPageLayout.ID_RES_NAV };
275                 }
276             };
277         }
278         if ( IContentOutlinePage.class.equals( required ) )
279         {
280             if ( outlinePage == null || outlinePage.getControl() == null || outlinePage.getControl().isDisposed() )
281             {
282                 outlinePage = new LdifOutlinePage( this );
283             }
284             return outlinePage;
285         }
286         if ( ISourceViewer.class.equals( required ) )
287         {
288             return getSourceViewer();
289         }
290         if ( IAnnotationHover.class.equals( required ) )
291         {
292             if ( getSourceViewerConfiguration() != null && getSourceViewer() != null )
293                 return getSourceViewerConfiguration().getAnnotationHover( getSourceViewer() );
294         }
295         if ( ITextHover.class.equals( required ) )
296         {
297             if ( getSourceViewerConfiguration() != null && getSourceViewer() != null )
298                 return getSourceViewerConfiguration().getTextHover( getSourceViewer(), null );
299         }
300         if ( IContentAssistProcessor.class.equals( required ) )
301         {
302             if ( getSourceViewerConfiguration() != null && getSourceViewer() != null )
303                 return getSourceViewerConfiguration().getContentAssistant( getSourceViewer() )
304                     .getContentAssistProcessor( LdifPartitionScanner.LDIF_RECORD );
305         }
306         if ( projectionSupport != null )
307         {
308             Object JavaDoc adapter = projectionSupport.getAdapter( getSourceViewer(), required );
309             if ( adapter != null )
310                 return adapter;
311         }
312         return super.getAdapter( required );
313     }
314
315
316     /**
317      * {@inheritDoc}
318      */

319     protected void editorContextMenuAboutToShow( IMenuManager menu )
320     {
321         super.editorContextMenuAboutToShow( menu );
322
323         IContributionItem[] items = menu.getItems();
324         for ( int i = 0; i < items.length; i++ )
325         {
326             if ( items[i] instanceof ActionContributionItem )
327             {
328                 ActionContributionItem aci = ( ActionContributionItem ) items[i];
329                 if ( aci.getAction() == getAction( ITextEditorActionConstants.SHIFT_LEFT ) )
330                 {
331                     menu.remove( items[i] );
332                 }
333                 if ( aci.getAction() == getAction( ITextEditorActionConstants.SHIFT_RIGHT ) )
334                 {
335                     menu.remove( items[i] );
336                 }
337             }
338         }
339
340         // add Edit actions
341
addAction( menu, ITextEditorActionConstants.GROUP_EDIT, EditLdifAttributeAction.class.getName() );
342         addAction( menu, ITextEditorActionConstants.GROUP_EDIT, OpenDefaultValueEditorAction.class.getName() );
343
344         MenuManager valueEditorMenuManager = new MenuManager( "Edit Value With" );
345         if ( this.openBestValueEditorAction.isEnabled() )
346         {
347             valueEditorMenuManager.add( this.openBestValueEditorAction );
348             valueEditorMenuManager.add( new Separator() );
349         }
350         for ( int i = 0; i < this.openValueEditorActions.length; i++ )
351         {
352             this.openValueEditorActions[i].update();
353             if ( this.openValueEditorActions[i].isEnabled()
354                 && this.openValueEditorActions[i].getValueEditor().getClass() != this.openBestValueEditorAction
355                     .getValueEditor().getClass()
356                 && this.openValueEditorActions[i].getValueEditor() instanceof AbstractDialogValueEditor )
357             {
358                 valueEditorMenuManager.add( this.openValueEditorActions[i] );
359             }
360         }
361         valueEditorMenuManager.add( new Separator() );
362         valueEditorMenuManager.add( this.valueEditorPreferencesAction );
363         menu.appendToGroup( ITextEditorActionConstants.GROUP_EDIT, valueEditorMenuManager );
364
365         addAction( menu, ITextEditorActionConstants.GROUP_EDIT, EditLdifRecordAction.class.getName() );
366
367         // add Format actions
368
MenuManager formatMenuManager = new MenuManager( "Format" );
369         addAction( formatMenuManager, FormatLdifDocumentAction.class.getName() );
370         addAction( formatMenuManager, FormatLdifRecordAction.class.getName() );
371         menu.appendToGroup( ITextEditorActionConstants.GROUP_EDIT, formatMenuManager );
372
373     }
374
375
376     /**
377      * {@inheritDoc}
378      */

379     protected void createActions()
380     {
381         super.createActions();
382
383         // add content assistant
384
ResourceBundle JavaDoc bundle = LdifEditorActivator.getDefault().getResourceBundle();
385         IAction action = new ContentAssistAction( bundle, "ldifeditor__contentassistproposal_", this ); //$NON-NLS-1$
386
action.setActionDefinitionId( ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS );
387         setAction( "ContentAssistProposal", action ); //$NON-NLS-1$
388

389         // add execute action (for tool bar)
390
ExecuteLdifAction executeLdifAction = new ExecuteLdifAction( this );
391         this.actionToolBarManager.add( executeLdifAction );
392         setAction( ExecuteLdifAction.class.getName(), executeLdifAction );
393         this.actionToolBarManager.update( true );
394
395         // add context menu edit actions
396
EditLdifAttributeAction editLdifAttributeAction = new EditLdifAttributeAction( this );
397         setAction( EditLdifAttributeAction.class.getName(), editLdifAttributeAction );
398
399         this.openBestValueEditorAction = new OpenBestValueEditorAction( this );
400         IValueEditor[] valueEditors = valueEditorManager.getAllValueEditors();
401         this.openValueEditorActions = new OpenValueEditorAction[valueEditors.length];
402         for ( int i = 0; i < this.openValueEditorActions.length; i++ )
403         {
404             this.openValueEditorActions[i] = new OpenValueEditorAction( this, valueEditors[i] );
405         }
406         this.valueEditorPreferencesAction = new ValueEditorPreferencesAction();
407
408         OpenDefaultValueEditorAction openDefaultValueEditorAction = new OpenDefaultValueEditorAction( this,
409             openBestValueEditorAction );
410         setAction( OpenDefaultValueEditorAction.class.getName(), openDefaultValueEditorAction );
411
412         EditLdifRecordAction editRecordAction = new EditLdifRecordAction( this );
413         setAction( EditLdifRecordAction.class.getName(), editRecordAction );
414
415         // add context menu format actions
416
FormatLdifDocumentAction formatDocumentAction = new FormatLdifDocumentAction( this );
417         setAction( FormatLdifDocumentAction.class.getName(), formatDocumentAction );
418         FormatLdifRecordAction formatRecordAction = new FormatLdifRecordAction( this );
419         setAction( FormatLdifRecordAction.class.getName(), formatRecordAction );
420
421         // update cut, copy, paste
422
IAction cutAction = getAction( ITextEditorActionConstants.CUT );
423         if ( cutAction != null )
424         {
425             cutAction.setImageDescriptor( PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
426                 ISharedImages.IMG_TOOL_CUT ) );
427         }
428         IAction copyAction = getAction( ITextEditorActionConstants.COPY );
429         if ( copyAction != null )
430         {
431             copyAction.setImageDescriptor( PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
432                 ISharedImages.IMG_TOOL_COPY ) );
433         }
434         IAction pasteAction = getAction( ITextEditorActionConstants.PASTE );
435         if ( pasteAction != null )
436         {
437             pasteAction.setImageDescriptor( PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
438                 ISharedImages.IMG_TOOL_PASTE ) );
439         }
440
441         activateGlobalActionHandlers();
442
443     }
444
445
446     /**
447      * {@inheritDoc}
448      */

449     public void createPartControl( Composite parent )
450     {
451
452         setHelpContextId( LdifEditorActivator.PLUGIN_ID + "." + "tools_ldif_editor" );
453
454         Composite composite = new Composite( parent, SWT.NONE );
455         composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
456         GridLayout layout = new GridLayout();
457         layout.marginWidth = 0;
458         layout.marginHeight = 0;
459         // layout.horizontalSpacing = 0;
460
layout.verticalSpacing = 0;
461         composite.setLayout( layout );
462
463         control = new ViewForm( composite, SWT.NONE );
464         control.setLayoutData( new GridData( GridData.FILL_BOTH ) );
465
466         // infoText = BaseWidgetUtils.createLabeledText(control, "", 1);
467
Composite connectionComboControl = BaseWidgetUtils.createColumnContainer( control, 1, 1 );
468         // connectionComboControl.setLayoutData(new
469
// GridData(GridData.GRAB_HORIZONTAL));
470
connectionCombo = BaseWidgetUtils.createReadonlyCombo( connectionComboControl, new String JavaDoc[0], 0, 1 );
471         connectionCombo.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, true ) );
472         connectionUpdated( null );
473         connectionCombo.addModifyListener( new ModifyListener()
474         {
475             public void modifyText( ModifyEvent e )
476             {
477                 IConnection connection = BrowserCorePlugin.getDefault().getConnectionManager().getConnection(
478                     connectionCombo.getText() );
479                 setConnection( connection );
480                 IAction action = getAction( ExecuteLdifAction.class.getName() );
481                 if ( action != null )
482                 {
483                     action.setEnabled( connection == null );
484                     action.setEnabled( connection != null );
485                     // actionToolBarManager.update(true);
486
}
487             }
488         } );
489         control.setTopLeft( connectionComboControl );
490
491         // tool bar
492
actionToolBar = new ToolBar( control, SWT.FLAT | SWT.RIGHT );
493         actionToolBar.setLayoutData( new GridData( SWT.END, SWT.NONE, true, false ) );
494         actionToolBarManager = new ToolBarManager( actionToolBar );
495         control.setTopCenter( actionToolBar );
496
497         // local menu
498
control.setTopRight( null );
499
500         // content
501
Composite editorComposite = new Composite( control, SWT.NONE );
502         editorComposite.setLayout( new FillLayout() );
503         GridData data = new GridData( GridData.FILL_BOTH );
504         data.widthHint = 450;
505         data.heightHint = 250;
506         editorComposite.setLayoutData( data );
507         super.createPartControl( editorComposite );
508         control.setContent( editorComposite );
509
510         ProjectionViewer projectionViewer = ( ProjectionViewer ) getSourceViewer();
511         projectionSupport = new ProjectionSupport( projectionViewer, getAnnotationAccess(), getSharedColors() );
512         projectionSupport.install();
513         projectionViewer.doOperation( ProjectionViewer.TOGGLE );
514
515     }
516
517
518     /**
519      * {@inheritDoc}
520      */

521     protected ISourceViewer createSourceViewer( Composite parent, IVerticalRuler ruler, int styles )
522     {
523         getAnnotationAccess();
524         getOverviewRuler();
525         ISourceViewer viewer = new ProjectionViewer( parent, ruler, getOverviewRuler(), true, styles );
526         getSourceViewerDecorationSupport( viewer );
527
528         return viewer;
529     }
530
531
532     /**
533      * {@inheritDoc}
534      */

535     protected void configureSourceViewerDecorationSupport( SourceViewerDecorationSupport support )
536     {
537         super.configureSourceViewerDecorationSupport( support );
538     }
539
540
541     /**
542      * {@inheritDoc}
543      */

544     public LdifFile getLdifModel()
545     {
546         IDocumentProvider provider = getDocumentProvider();
547         if ( provider instanceof LdifDocumentProvider )
548         {
549             return ( ( LdifDocumentProvider ) provider ).getLdifModel();
550         }
551         else
552         {
553             return null;
554         }
555     }
556
557
558     /**
559      * This method is used to notify the LDIF Editor that the Outline Page has been closed.
560      */

561     public void outlinePageClosed()
562     {
563         projectionSupport.dispose();
564         outlinePage = null;
565     }
566
567
568     /**
569      * {@inheritDoc}
570      */

571     public IConnection getConnection()
572     {
573         return this.connection;
574     }
575
576
577     /**
578      * Sets the Connection
579      *
580      * @param connection
581      * the Connection to set
582      */

583     private void setConnection( IConnection connection )
584     {
585         this.connection = connection;
586         getEditorSite().getActionBars().getStatusLineManager().setMessage(
587             "Used Connection: " + ( this.connection == null ? "-" : this.connection.getName() ) );
588         // getStatusField("ldapconnection").setText();
589
}
590
591
592     /**
593      * {@inheritDoc}
594      */

595     public void connectionUpdated( ConnectionUpdateEvent connectionUpdateEvent )
596     {
597         IConnection[] connections = BrowserCorePlugin.getDefault().getConnectionManager().getConnections();
598         String JavaDoc[] names = new String JavaDoc[connections.length + 1];
599         names[0] = "";
600         for ( int i = 0; i < connections.length; i++ )
601         {
602             names[i + 1] = connections[i].getName();
603         }
604         String JavaDoc old = this.connectionCombo.getText();
605         this.connectionCombo.setItems( names );
606         this.connectionCombo.setText( old );
607         connectionCombo.setVisibleItemCount( Math.max( names.length, 20 ) );
608
609     }
610
611
612     /**
613      * This implementation checks if the input is of type
614      * NonExistingLdifEditorInput. In that case doSaveAs() is
615      * called to prompt for a new file name and location.
616      */

617     public void doSave( IProgressMonitor progressMonitor )
618     {
619         final IEditorInput input = getEditorInput();
620         if ( input instanceof NonExistingLdifEditorInput )
621         {
622             super.doSaveAs();
623             return;
624         }
625
626         super.doSave( progressMonitor );
627     }
628
629
630     /**
631      * The input could be one of the following types:
632      * - NonExistingLdifEditorInput: New file, not yet saved
633      * - PathEditorInput: file opened with our internal "Open File.." action
634      * - FileEditorInput: file is within workspace
635      * - JavaFileEditorInput: file opend with "Open File..." action from org.eclipse.ui.editor
636      *
637      * In RCP the FileDialog appears.
638      * In IDE the super implementation is called.
639      * To detect if this plugin runs in IDE the org.eclipse.ui.ide extension point is checked.
640      *
641      */

642     protected void performSaveAs( IProgressMonitor progressMonitor )
643     {
644         // detect IDE or RCP:
645
// check if perspective org.eclipse.ui.resourcePerspective is available
646
boolean isIDE = BrowserCommonActivator.isIDEEnvironment();
647
648         if ( isIDE )
649         {
650             // Just call super implementation for now
651
IPreferenceStore store = EditorsUI.getPreferenceStore();
652             String JavaDoc key = getEditorSite().getId() + ".internal.delegateSaveAs"; // $NON-NLS-1$
653
store.setValue( key, true );
654             super.performSaveAs( progressMonitor );
655         }
656         else
657         {
658             // Open FileDialog
659
Shell shell = getSite().getShell();
660             final IEditorInput input = getEditorInput();
661
662             IDocumentProvider provider = getDocumentProvider();
663             final IEditorInput newInput;
664
665             FileDialog dialog = new FileDialog( shell, SWT.SAVE );
666
667             String JavaDoc path = dialog.open();
668             if ( path == null )
669             {
670                 if ( progressMonitor != null )
671                 {
672                     progressMonitor.setCanceled( true );
673                 }
674                 return;
675             }
676
677             // Check whether file exists and if so, confirm overwrite
678
final File JavaDoc externalFile = new File JavaDoc( path );
679             if ( externalFile.exists() )
680             {
681                 MessageDialog overwriteDialog = new MessageDialog( shell, "Overwrite", null, "Overwrite?",
682                     MessageDialog.WARNING, new String JavaDoc[]
683                         { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 1 ); // 'No' is the default
684
if ( overwriteDialog.open() != Window.OK )
685                 {
686                     if ( progressMonitor != null )
687                     {
688                         progressMonitor.setCanceled( true );
689                         return;
690                     }
691                 }
692             }
693
694             IPath iPath = new Path( path );
695             newInput = new PathEditorInput( iPath );
696
697             boolean success = false;
698             try
699             {
700                 provider.aboutToChange( newInput );
701                 provider.saveDocument( progressMonitor, newInput, provider.getDocument( input ), true );
702                 success = true;
703             }
704             catch ( CoreException x )
705             {
706                 final IStatus status = x.getStatus();
707                 if ( status == null || status.getSeverity() != IStatus.CANCEL )
708                 {
709                     String JavaDoc title = "Error in Save As...";
710                     String JavaDoc msg = "Error in Save As... " + x.getMessage();
711                     MessageDialog.openError( shell, title, msg );
712                 }
713             }
714             finally
715             {
716                 provider.changed( newInput );
717                 if ( success )
718                 {
719                     setInput( newInput );
720                 }
721             }
722
723             if ( progressMonitor != null )
724             {
725                 progressMonitor.setCanceled( !success );
726             }
727         }
728
729     }
730
731     private IContextActivation contextActivation;
732
733
734     /**
735      * {@inheritDoc}
736      */

737     public void partDeactivated( IWorkbenchPartReference partRef )
738     {
739         if ( partRef.getPart( false ) == this && contextActivation != null )
740         {
741
742             this.deactivateGlobalActionHandlers();
743
744             IContextService contextService = ( IContextService ) PlatformUI.getWorkbench().getAdapter(
745                 IContextService.class );
746             contextService.deactivateContext( contextActivation );
747             contextActivation = null;
748         }
749     }
750
751
752     /**
753      * {@inheritDoc}
754      */

755     public void partActivated( IWorkbenchPartReference partRef )
756     {
757         if ( partRef.getPart( false ) == this )
758         {
759
760             IContextService contextService = ( IContextService ) PlatformUI.getWorkbench().getAdapter(
761                 IContextService.class );
762             contextActivation = contextService
763                 .activateContext( "org.apache.directory.ldapstudio.browser.action.context" );
764
765             this.activateGlobalActionHandlers();
766         }
767     }
768
769
770     /**
771      * {@inheritDoc}
772      */

773     public void partBroughtToTop( IWorkbenchPartReference partRef )
774     {
775     }
776
777
778     /**
779      * {@inheritDoc}
780      */

781     public void partClosed( IWorkbenchPartReference partRef )
782     {
783     }
784
785
786     /**
787      * {@inheritDoc}
788      */

789     public void partHidden( IWorkbenchPartReference partRef )
790     {
791     }
792
793
794     /**
795      * {@inheritDoc}
796      */

797     public void partInputChanged( IWorkbenchPartReference partRef )
798     {
799     }
800
801
802     /**
803      * {@inheritDoc}
804      */

805     public void partOpened( IWorkbenchPartReference partRef )
806     {
807     }
808
809
810     /**
811      * {@inheritDoc}
812      */

813     public void partVisible( IWorkbenchPartReference partRef )
814     {
815     }
816
817
818     /**
819      * Activates global action handlers
820      */

821     public void activateGlobalActionHandlers()
822     {
823         ICommandService commandService = ( ICommandService ) PlatformUI.getWorkbench().getAdapter(
824             ICommandService.class );
825         if ( commandService != null )
826         {
827             IAction elaa = getAction( EditLdifAttributeAction.class.getName() );
828             commandService.getCommand( elaa.getActionDefinitionId() ).setHandler( new ActionHandler( elaa ) );
829             IAction elva = getAction( OpenDefaultValueEditorAction.class.getName() );
830             commandService.getCommand( elva.getActionDefinitionId() ).setHandler( new ActionHandler( elva ) );
831             IAction elra = getAction( EditLdifRecordAction.class.getName() );
832             commandService.getCommand( elra.getActionDefinitionId() ).setHandler( new ActionHandler( elra ) );
833         }
834     }
835
836
837     /**
838      * Deactivates global action handlers
839      */

840     public void deactivateGlobalActionHandlers()
841     {
842         ICommandService commandService = ( ICommandService ) PlatformUI.getWorkbench().getAdapter(
843             ICommandService.class );
844         if ( commandService != null )
845         {
846             IAction elaa = getAction( EditLdifAttributeAction.class.getName() );
847             commandService.getCommand( elaa.getActionDefinitionId() ).setHandler( null );
848             IAction elva = getAction( OpenDefaultValueEditorAction.class.getName() );
849             commandService.getCommand( elva.getActionDefinitionId() ).setHandler( null );
850             IAction elra = getAction( EditLdifRecordAction.class.getName() );
851             commandService.getCommand( elra.getActionDefinitionId() ).setHandler( null );
852         }
853     }
854
855
856     /**
857      * Gets the Value Editor Manager
858      *
859      * @return
860      * the Value Editor Manager
861      */

862     public ValueEditorManager getValueEditorManager()
863     {
864         return valueEditorManager;
865     }
866 }
867
Popular Tags