KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > EditorStatusLine


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.texteditor;
13
14 import org.eclipse.swt.graphics.Image;
15
16 import org.eclipse.core.runtime.Assert;
17
18
19 import org.eclipse.jface.action.IStatusLineManager;
20 import org.eclipse.jface.viewers.ISelectionChangedListener;
21 import org.eclipse.jface.viewers.ISelectionProvider;
22 import org.eclipse.jface.viewers.SelectionChangedEvent;
23
24 /**
25  * An editor status line.
26  * The selection provider of the editor triggers the status line to be cleared.
27  * @since 2.1
28  */

29 class EditorStatusLine implements IEditorStatusLine {
30
31     /**
32      * Clears the status line on selection changed.
33      */

34     private class StatusLineClearer implements ISelectionChangedListener {
35         /*
36          * @see ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
37          */

38         public void selectionChanged(SelectionChangedEvent event) {
39             fStatusLineManager.setErrorMessage(null, null);
40             fStatusLineManager.setMessage(null, null);
41
42             Assert.isTrue(this == fStatusLineClearer);
43             uninstallStatusLineClearer();
44         }
45     }
46
47     /** The status line manager. */
48     private final IStatusLineManager fStatusLineManager;
49
50     /** The selection provider. */
51     private final ISelectionProvider fSelectionProvider;
52
53     /** The status line clearer, <code>null</code> if not installed. */
54     private StatusLineClearer fStatusLineClearer;
55
56     /**
57      * Constructor for EditorStatusLine.
58      *
59      * @param statusLineManager the status line manager
60      * @param selectionProvider the selection provider
61      */

62     public EditorStatusLine(IStatusLineManager statusLineManager, ISelectionProvider selectionProvider) {
63
64         Assert.isNotNull(statusLineManager);
65         Assert.isNotNull(selectionProvider);
66
67         fStatusLineManager= statusLineManager;
68         fSelectionProvider= selectionProvider;
69     }
70
71     /**
72      * Returns the status line manager.
73      *
74      * @return the status line manager
75      */

76     public IStatusLineManager getStatusLineManager() {
77         return fStatusLineManager;
78     }
79
80     /**
81      * Returns the selection provider.
82      *
83      * @return the selection provider
84      */

85     public ISelectionProvider getSelectionProvider() {
86         return fSelectionProvider;
87     }
88
89     /*
90      * @see org.eclipse.ui.texteditor.IStatusLine#setMessage(boolean, String, Image)
91      */

92     public void setMessage(boolean error, String JavaDoc message, Image image) {
93
94         if (error)
95             fStatusLineManager.setErrorMessage(image, message);
96         else {
97             // Clear error message
98
fStatusLineManager.setErrorMessage(null, null);
99
100             fStatusLineManager.setMessage(image, message);
101         }
102
103         if (isMessageEmpty(message))
104             uninstallStatusLineClearer();
105         else
106             installStatusLineClearer();
107     }
108
109     /**
110      * Returns whether this given string is empty.
111      *
112      * @param message a string
113      * @return <code>true</code> if the string is <code>null</code>, has 0 length or only white space characters.
114      */

115     private static boolean isMessageEmpty(String JavaDoc message) {
116         return message == null || message.trim().length() == 0;
117     }
118
119     /**
120      * Uninstalls the status line clearer.
121      */

122     private void uninstallStatusLineClearer() {
123         if (fStatusLineClearer == null)
124             return;
125
126         fSelectionProvider.removeSelectionChangedListener(fStatusLineClearer);
127         fStatusLineClearer= null;
128     }
129
130     /**
131      * Installs the status line clearer.
132      */

133     private void installStatusLineClearer() {
134         if (fStatusLineClearer != null)
135             return;
136
137         StatusLineClearer statusLineClearer= new StatusLineClearer();
138         fSelectionProvider.addSelectionChangedListener(statusLineClearer);
139         fStatusLineClearer= statusLineClearer;
140     }
141 }
142
Popular Tags