001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.studio.apacheds;
021
022
023 import org.eclipse.jface.preference.IPreferenceStore;
024 import org.eclipse.jface.preference.PreferenceConverter;
025 import org.eclipse.jface.resource.ImageDescriptor;
026 import org.eclipse.swt.SWT;
027 import org.eclipse.swt.graphics.Color;
028 import org.eclipse.swt.graphics.FontData;
029 import org.eclipse.swt.graphics.RGB;
030 import org.eclipse.ui.console.MessageConsole;
031 import org.eclipse.ui.console.MessageConsoleStream;
032
033
034 /**
035 * A console that displays log messages.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 * @version $Rev$, $Date$
039 */
040 public class LogMessageConsole extends MessageConsole
041 {
042 /** The preference store*/
043 private IPreferenceStore preferenceStore;
044
045 /** The stream for Debug level */
046 private MessageConsoleStream debugMessageConsoleStream;
047 /** The stream for Info level */
048 private MessageConsoleStream infoMessageConsoleStream;
049 /** The stream for Warn level */
050 private MessageConsoleStream warnMessageConsoleStream;
051 /** The stream for Error level */
052 private MessageConsoleStream errorMessageConsoleStream;
053 /** The stream for Fatal level */
054 private MessageConsoleStream fatalMessageConsoleStream;
055
056
057 /**
058 * Creates a new instance of LogMessageConsole.
059 *
060 * @param name
061 * console name
062 * @param imageDescriptor
063 * console image descriptor or null
064 */
065 public LogMessageConsole( String name, ImageDescriptor imageDescriptor )
066 {
067 super( name, imageDescriptor );
068
069 preferenceStore = ApacheDsPlugin.getDefault().getPreferenceStore();
070 }
071
072
073 /**
074 * Creates a new instance of LogMessageConsole.
075 *
076 * @param name
077 * console name
078 */
079 public LogMessageConsole( String name )
080 {
081 super( name, null );
082
083 preferenceStore = ApacheDsPlugin.getDefault().getPreferenceStore();
084 }
085
086
087 /**
088 * Gets the Debug stream.
089 *
090 * @return
091 * the Debug stream
092 */
093 public MessageConsoleStream getDebugConsoleMessageStream()
094 {
095 if ( debugMessageConsoleStream == null )
096 {
097 createDebugMessageConsoleStream();
098 }
099
100 return debugMessageConsoleStream;
101 }
102
103
104 /**
105 * Creates the Debug stream and set the Color and Font settings to it.
106 */
107 private void createDebugMessageConsoleStream()
108 {
109 // Creating the stream
110 debugMessageConsoleStream = newMessageStream();
111
112 // Setting the Color and Font settings
113 setColorAndFontSettingsToMessageConsoleStream( debugMessageConsoleStream,
114 ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_DEBUG_COLOR,
115 ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_DEBUG_FONT );
116 }
117
118
119 /**
120 * Gets the Info stream.
121 *
122 * @return
123 * the Info stream
124 */
125 public MessageConsoleStream getInfoConsoleMessageStream()
126 {
127 if ( infoMessageConsoleStream == null )
128 {
129 createInfoMessageConsoleStream();
130 }
131
132 return infoMessageConsoleStream;
133 }
134
135
136 /**
137 * Creates the Info stream and set the Color and Font settings to it.
138 */
139 private void createInfoMessageConsoleStream()
140 {
141 // Creating the stream
142 infoMessageConsoleStream = newMessageStream();
143
144 // Setting the Color and Font settings
145 setColorAndFontSettingsToMessageConsoleStream( infoMessageConsoleStream,
146 ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_INFO_COLOR,
147 ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_INFO_FONT );
148 }
149
150
151 /**
152 * Gets the Warn stream.
153 *
154 * @return
155 * the Warn stream
156 */
157 public MessageConsoleStream getWarnConsoleMessageStream()
158 {
159 if ( warnMessageConsoleStream == null )
160 {
161 createWarnMessageConsoleStream();
162 }
163
164 return warnMessageConsoleStream;
165 }
166
167
168 /**
169 * Creates the Warn stream and set the Color and Font settings to it.
170 */
171 private void createWarnMessageConsoleStream()
172 {
173 // Creating the stream
174 warnMessageConsoleStream = newMessageStream();
175
176 // Setting the Color and Font settings
177 setColorAndFontSettingsToMessageConsoleStream( warnMessageConsoleStream,
178 ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_WARN_COLOR,
179 ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_WARN_FONT );
180 }
181
182
183 /**
184 * Gets the Error stream.
185 *
186 * @return
187 * the Error stream
188 */
189 public MessageConsoleStream getErrorConsoleMessageStream()
190 {
191 if ( errorMessageConsoleStream == null )
192 {
193 createErrorMessageConsoleStream();
194 }
195
196 return errorMessageConsoleStream;
197 }
198
199
200 /**
201 * Creates the Error stream and set the Color and Font settings to it.
202 */
203 private void createErrorMessageConsoleStream()
204 {
205 // Creating the stream
206 errorMessageConsoleStream = newMessageStream();
207
208 // Setting the Color and Font settings
209 setColorAndFontSettingsToMessageConsoleStream( errorMessageConsoleStream,
210 ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_ERROR_COLOR,
211 ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_ERROR_FONT );
212 }
213
214
215 /**
216 * Gets the Fatal stream.
217 *
218 * @return
219 * the Fatal stream
220 */
221 public MessageConsoleStream getFatalConsoleMessageStream()
222 {
223 if ( fatalMessageConsoleStream == null )
224 {
225 createFatalMessageConsoleStream();
226 }
227
228 return fatalMessageConsoleStream;
229 }
230
231
232 /**
233 * Creates the Fatal stream and set the Color and Font settings to it.
234 */
235 private void createFatalMessageConsoleStream()
236 {
237 // Creating the stream
238 fatalMessageConsoleStream = newMessageStream();
239
240 // Setting the Color and Font settings
241 setColorAndFontSettingsToMessageConsoleStream( fatalMessageConsoleStream,
242 ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_FATAL_COLOR,
243 ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_FATAL_FONT );
244 }
245
246
247 /**
248 * Sets the Color and Font settings to the given stream.
249 *
250 * @param messageConsoleStream
251 * the stream
252 * @param colorPreferenceName
253 * the preference name for the color
254 * @param fontPreferenceName
255 * the preference name for the font
256 */
257 private void setColorAndFontSettingsToMessageConsoleStream( MessageConsoleStream messageConsoleStream,
258 String colorPreferenceName, String fontPreferenceName )
259 {
260 // Getting Color and Font settings from the preference store
261 RGB rgb = PreferenceConverter.getColor( preferenceStore, colorPreferenceName );
262 FontData[] fontDatas = PreferenceConverter.getFontDataArray( preferenceStore, fontPreferenceName );
263
264 // Creating a style to apply to the font
265 int style = SWT.NORMAL;
266 if ( PreferenceStoreUtils.isBold( fontDatas ) )
267 {
268 style |= SWT.BOLD;
269 }
270 if ( PreferenceStoreUtils.isItalic( fontDatas ) )
271 {
272 style |= SWT.ITALIC;
273 }
274
275 // Applying settings to the stream
276 messageConsoleStream.setColor( new Color( ApacheDsPlugin.getDefault().getWorkbench().getDisplay(), rgb ) );
277 messageConsoleStream.setFontStyle( style );
278 }
279 }