View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
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
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Component;
24  import java.awt.event.ActionEvent;
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.FileReader;
28  import java.io.IOException;
29  
30  import javax.swing.AbstractAction;
31  import javax.swing.Action;
32  import javax.swing.JButton;
33  import javax.swing.JTabbedPane;
34  
35  import org.xnap.XNap;
36  import org.xnap.cmdl.Command;
37  import org.xnap.cmdl.Completer;
38  import org.xnap.cmdl.Console;
39  import org.xnap.cmdl.Executer;
40  import org.xnap.gui.component.DefaultDialog;
41  import org.xnap.util.Debug;
42  
43  public class ConsoleDialog extends DefaultDialog implements Console {
44      
45      //--- Data field(s) ---
46  
47      private static ConsoleDialog me = new ConsoleDialog();
48  	
49      private JTabbedPane jtp;
50      private ConsolePane cpConsole;
51      private ConsolePane cpError;
52      private DebugPanel jpDebug;
53  
54      //--- Constructor(s) ---
55  	    
56      private ConsoleDialog() 
57      {
58  		super(BUTTON_APPLY);
59  
60          setTitle(XNap.tr("XNap Console"));
61  		setModal(false);
62  
63  		jtp = new JTabbedPane();
64  		getMainPanel().setLayout(new BorderLayout());
65  		getMainPanel().add(jtp, BorderLayout.CENTER);
66  
67  		// console
68  		cpConsole = new ConsolePane();
69  		jtp.addTab(XNap.tr("Console"), cpConsole);
70  
71  		// debug
72  		jpDebug = new DebugPanel();
73  		jtp.addTab(XNap.tr("Debug"), jpDebug);
74  
75  		// error.log
76  		cpError = new ConsolePane(false);
77  		jtp.addTab("error.log", cpError);
78  	
79  		// button
80          JButton jbHide = new JButton(new HideAction());
81  		getButtonPanel().add(jbHide);
82  
83  		setSize(600, 400);
84      }
85  
86      // --- Methods ---
87  
88      public static ConsoleDialog getInstance()
89      {
90  		return me;
91      }
92  
93      public boolean apply()
94      {
95  		jpDebug.apply();
96  		return true;
97      }
98  
99      public boolean isEchoing()
100     {
101 		return false;
102     }
103 
104     public static void showDialog(Component c, boolean showErrorLog)
105     {
106 		if (c != null) {
107 			me.setLocationRelativeTo(c);
108 		}
109 
110 		me.updateErrorLog();
111 		if (showErrorLog) {
112 			me.jtp.setSelectedComponent(me.cpError);
113 		}
114 
115 		me.setVisible(true);
116     }
117 
118     public static void showDialog(Component c)
119     {
120 		showDialog(c, false);
121     }
122 
123     public void printLine(String text)
124     {
125 		cpConsole.appendLater(text);
126     }
127 
128     public String readLine(String prompt)
129     {
130 		return null;
131     }
132 
133     public Completer getCompleter() 
134     {
135 		return null;
136     }
137 
138     public void updateErrorLog()
139     {
140 		File f = Debug.getErrorFile();
141 
142 		if (f == null)
143 			return;
144 
145 		StringBuffer sb = new StringBuffer();
146 
147 		try {
148 			BufferedReader in = new BufferedReader(new FileReader(f));
149 			
150 			while (in.ready()) {
151 				sb.append(in.readLine() + "\n");
152 
153 			}
154 		}
155 		catch(IOException io) {
156 		}
157 
158 		cpError.setText(sb.toString());
159     }
160 
161     /***
162      * Handles the hide button.
163      */
164     private class HideAction extends AbstractAction {
165 
166         public HideAction() 
167 		{
168 			putValue(Action.NAME, XNap.tr("Hide"));
169 			putValue(Action.SHORT_DESCRIPTION, XNap.tr("Hides this dialog."));
170 		}
171 	
172         public void actionPerformed(ActionEvent event) 
173 		{
174 			setVisible(false);
175         }
176 	
177     }
178 
179 	/***
180 	 *  @see xnap.cmdl.Console#getCommand(java.lang.String)
181 	 */
182 	public Command getCommand(String commandName) {
183 		return Executer.getCommand(commandName);
184 	}
185 
186 	/***
187 	 *  @see xnap.cmdl.Console#getCommands()
188 	 */
189 	public Command[] getCommands() {
190 		return Executer.getCommands();
191 	}
192 
193 	/***
194 	 *  @see xnap.cmdl.Console#println(java.lang.String)
195 	 */
196 	public void println(String text) {
197 		cpConsole.appendLater(text + "\n");
198 	}
199 
200 	/***
201 	 *  @see xnap.cmdl.Console#readln(java.lang.String)
202 	 */
203 	public String readln(String prompt) {
204 		return null;
205 	}
206 
207 }