View Javadoc

1   package org.devaki.nextobjects.ui.main;
2   /*
3   
4   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
5   
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by the
8   Free Software Foundation; either version 2 of the License, or (at your
9   option) any later version.
10  
11  This program is distributed in the hope that it will
12  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc., 59
18  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  
20  */
21  import java.awt.Font;
22  import javax.swing.JScrollPane;
23  import javax.swing.JTabbedPane;
24  import javax.swing.SwingConstants;
25  import org.apache.log4j.Appender;
26  import org.apache.log4j.Category;
27  import org.apache.log4j.PatternLayout;
28  import org.apache.log4j.WriterAppender;
29  import org.devaki.nextobjects.ui.components.CustomTextArea;
30  import org.devaki.nextobjects.ui.components.JTextAreaOutputStream;
31  import org.devaki.nextobjects.ui.editor.SqlEditor;
32  import org.devaki.nextobjects.util.NOGraphicsUtil;
33  import org.devaki.nextobjects.util.NOPreferences;
34  import org.devaki.nextobjects.util.LogWriter;
35  /***
36   * The bottom panel containing tabbed panes
37  * @author     <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
38   */
39  public final class NOLog
40  {
41      /***
42       * The bottom tabbed pane on the main window
43       **/
44      private static JTabbedPane jTabbedPane =
45          new JTabbedPane(SwingConstants.BOTTOM);
46      /***
47       * The textarea to pass the log stream
48       */
49      private static CustomTextArea ta = new CustomTextArea("", "", true, false);
50      /***
51       * The container of the textarea made to handle a (log) stream
52       */
53      private static JTextAreaOutputStream jTextOutStream =
54          new JTextAreaOutputStream(ta);
55      /***
56       * The logger
57       */
58      private static Category root = Category.getRoot();
59      /***
60       * The lowriter
61       */
62      private static LogWriter writer;
63      /***
64       * The pattern for loggin
65       */
66      private static PatternLayout layout;
67      /***
68       * The write appender
69       */
70      private static WriterAppender appender;
71      /***
72       * Set up tabbedpane, font, colors, appenders, log4j
73       */
74      private NOLog()
75      {
76      }
77      /***
78      * Show a given pane
79      * It is called when sql generation started.
80      * @param i the index
81       */
82      public static void showPane(final int i)
83      {
84          //actually do nothing
85          jTabbedPane.setSelectedIndex(i);
86      }
87      /***
88       * Initialize the tabbed pane
89       * Set loggin textarea from preferences
90       * Prepare the SQL lexer
91       */
92      public static void init()
93      {
94          // setup the textarea
95          Font font;
96          try
97          {
98              font =
99                  new Font(
100                     NOPreferences.getProperty("nextobjects.log.font"),
101                     Integer.parseInt(
102                         NOPreferences.getProperty(
103                             "nextobjects.log.font.style")),
104                     Integer.parseInt(
105                         NOPreferences.getProperty(
106                             "nextobjects.log.font.size")));
107         }
108         catch (Exception e)
109         {
110             System.err.println("Invalid font definition in properties");
111             font = new Font("monospaced", 1, 12);
112         }
113         ta.setFont(font);
114         ta.setForeground(
115             NOGraphicsUtil.hexString2Color(
116                 NOPreferences.getProperty("nextobjects.logger.color")));
117         ta.setBackground(
118             NOGraphicsUtil.hexString2Color(
119                 NOPreferences.getProperty("nextobjects.logger.bgcolor")));
120         // Define that the layout is a grid bag layout
121         //Assembling components
122         JScrollPane scrollPane = new JScrollPane(ta);
123         // 'jTabbedPane'
124         NOLog.jTabbedPane.add("Log        ", scrollPane);
125         NOLog.jTabbedPane.add("SQL Viewer ", new SqlEditor());
126         writer = new LogWriter(jTextOutStream);
127         appender = new WriterAppender(layout, writer);
128         Appender a1 = root.getAppender("A1");
129         if (a1 == null)
130         {
131             layout =
132                 new PatternLayout(
133                     NOPreferences.getProperty("nextobjects.logger.pattern"));
134         }
135         else
136         {
137             layout = (PatternLayout) a1.getLayout();
138         }
139         appender.setLayout(layout);
140         root.addAppender(appender);
141     }
142     /***
143      * Get the JTextAreaOutputStream
144      * @return the JTextAreaOutputStream
145      */
146     public static JTextAreaOutputStream getJTextOutStream()
147     {
148         return jTextOutStream;
149     }
150     /***
151      * Set  the JTextAreaOutputStream
152      * @param stream  the JTextAreaOutputStream
153      */
154     public static void setJTextOutStream(final JTextAreaOutputStream stream)
155     {
156         jTextOutStream = stream;
157     }
158     /***
159      * Return a reference to the tabbed pane
160      * @return the tabbed pane
161      */
162     public static JTabbedPane getJTabbedPane()
163     {
164         return jTabbedPane;
165     }
166 }