KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > net > qsadmin > gui > CmdConsole


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.net.qsadmin.gui;
16
17 import java.awt.*;
18 import java.awt.event.*;
19 import javax.swing.*;
20 import javax.swing.event.*;
21 import javax.swing.border.*;
22 import java.io.*;
23
24 /**
25  * Command Console
26  * @author Akshathkumar Shetty
27  */

28 public class CmdConsole extends JPanel {
29     private static final String JavaDoc NEW_LINE = "\r\n";
30     private QSAdminMain qsadminMain;
31
32     private JPanel centerPanel;
33     private JPanel textPanel;
34     private JPanel sendPanel;
35     private JPanel buttonPanel;
36
37     private JLabel convLabel = new JLabel("Conversation with host");
38     private Border connectedBorder = BorderFactory.createTitledBorder(
39         new EtchedBorder(), "Connected To < NONE >");
40     private JTextArea messagesField = new JTextArea();
41     
42     private JLabel sendLabel = new JLabel("Message");
43     private JTextField sendField = new JTextField();
44     
45     private JButton sendButton = new JButton("Send");
46     private JButton saveButton = new JButton("Save");
47     private JButton clearButton = new JButton("Clear");
48     
49     private GridBagConstraints gbc = new GridBagConstraints();
50
51     public CmdConsole(QSAdminMain qsadminMain) {
52         Container cp = this;
53         this.qsadminMain = qsadminMain;
54
55         textPanel = new JPanel();
56         textPanel.setLayout(new BorderLayout(0,5));
57         textPanel.add(convLabel,BorderLayout.NORTH);
58         messagesField.setEditable(false);
59         messagesField.setBackground(Color.BLACK);
60         messagesField.setForeground(Color.GREEN);
61         JScrollPane jsp = new JScrollPane(messagesField);
62         textPanel.add(jsp);
63         textPanel.setBorder(BorderFactory.createEmptyBorder(3,3,0,3));
64
65         sendPanel = new JPanel();
66         sendPanel.setLayout(new GridBagLayout());
67         gbc.weighty = 0.0;
68         gbc.weightx = 0.0;
69         gbc.gridx = 0;
70         gbc.gridy = 0;
71         gbc.gridheight = 1;
72         gbc.gridwidth = 1;
73         gbc.anchor = GridBagConstraints.EAST;
74         gbc.fill = GridBagConstraints.NONE;
75         sendPanel.add(sendLabel, gbc);
76         gbc.gridx = 1;
77         gbc.weightx = 1.0;
78         gbc.fill = GridBagConstraints.HORIZONTAL;
79         sendField.setEditable(false);
80         sendPanel.add(sendField, gbc);
81         gbc.gridx = 2;
82         gbc.weightx = 0.0;
83         gbc.fill = GridBagConstraints.NONE;
84         sendButton.setEnabled(false);
85         sendButton.setToolTipText("Send text to host");
86         ActionListener sendListener = new ActionListener() {
87             public void actionPerformed(ActionEvent e) {
88                 String JavaDoc msg = sendField.getText();
89                 if(!msg.equals(""))
90                     sendMessage(msg);
91                 else {
92                     int value = JOptionPane.showConfirmDialog(
93                         CmdConsole.this, "Send Blank Line ?",
94                         "Send Data To Server",
95                         JOptionPane.YES_NO_OPTION);
96                     if (value == JOptionPane.YES_OPTION)
97                         sendMessage(msg);
98                 }
99             }
100         };
101         sendButton.addActionListener(sendListener);
102         sendField.addActionListener(sendListener);
103         sendPanel.add(sendButton, gbc);
104         sendPanel.setBorder(
105             new CompoundBorder(
106                 BorderFactory.createEmptyBorder(0,0,0,3),
107                 BorderFactory.createTitledBorder("Send")));
108
109         buttonPanel = new JPanel();
110         buttonPanel.setLayout(new GridBagLayout());
111         gbc.weighty = 0.0;
112         gbc.weightx = 1.0;
113         gbc.gridx = 0;
114         gbc.gridy = 0;
115         gbc.gridheight = 2;
116         gbc.gridwidth = 1;
117         gbc.anchor = GridBagConstraints.EAST;
118         gbc.fill = GridBagConstraints.BOTH;
119         buttonPanel.add(sendPanel, gbc);
120         gbc.weighty = 0.0;
121         gbc.weightx = 0.0;
122         gbc.gridx = 1;
123         gbc.gridheight = 1;
124         gbc.fill = GridBagConstraints.HORIZONTAL;
125         saveButton.setToolTipText("Save conversation with host to a file");
126         saveButton.setMnemonic('S');
127         ActionListener saveListener = new ActionListener() {
128             public void actionPerformed(ActionEvent e) {
129                 String JavaDoc text = messagesField.getText();
130                 if(text.equals("")) {
131                     error("Nothing to save","Save to file");
132                     return;
133                 }
134                 String JavaDoc fileName="";
135                 JFileChooser chooser = new JFileChooser();
136                 chooser.setCurrentDirectory(new File("."));
137                 int returnVal = chooser.showSaveDialog(CmdConsole.this);
138                 if(returnVal == JFileChooser.APPROVE_OPTION) {
139                    fileName=chooser.getSelectedFile().getAbsolutePath();
140                    try {
141                         writeFile(fileName,text);
142                    }
143                    catch (Exception JavaDoc ioe) {
144                        JOptionPane.showMessageDialog(CmdConsole.this,
145                            ""+ioe.getMessage(),
146                            "Error saving to file..",
147                            JOptionPane.ERROR_MESSAGE);
148                    }
149                 }
150             }
151         };
152         saveButton.addActionListener(saveListener);
153         buttonPanel.add(saveButton, gbc);
154         gbc.gridy = 1;
155         clearButton.setToolTipText("Clear conversation with host");
156         clearButton.setMnemonic('C');
157         ActionListener clearListener = new ActionListener() {
158             public void actionPerformed(ActionEvent e) {
159                 messagesField.setText("");
160             }
161         };
162         clearButton.addActionListener(clearListener);
163         buttonPanel.add(clearButton, gbc);
164         buttonPanel.setBorder(BorderFactory.createEmptyBorder(3,0,0,3));
165
166         centerPanel = new JPanel();
167         centerPanel.setLayout(new BorderLayout(0,0));
168         centerPanel.add(buttonPanel,BorderLayout.SOUTH);
169         centerPanel.add(textPanel,BorderLayout.CENTER);
170
171         CompoundBorder cb=new CompoundBorder(
172             BorderFactory.createEmptyBorder(5,10,10,10),
173             connectedBorder);
174         centerPanel.setBorder(cb);
175
176         cp.setLayout(new BorderLayout());
177         cp.add(centerPanel,BorderLayout.CENTER);
178     }
179
180     public void append(String JavaDoc msg) {
181         setSendEdit(qsadminMain.isConnected());
182         messagesField.append(msg+NEW_LINE);
183         messagesField.setCaretPosition(messagesField.getText().length());
184     }
185
186     public void sendMessage(String JavaDoc s) {
187         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
188         try {
189             qsadminMain.sendCommand(s, true);
190             sendField.setText("");
191             setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
192         } catch (Exception JavaDoc e) {
193             setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
194             JOptionPane.showMessageDialog(CmdConsole.this,
195                 e.getMessage(),"Error Sending Message",
196                 JOptionPane.ERROR_MESSAGE);
197         }
198     }
199
200     private void changeBorder(String JavaDoc ip) {
201         if(ip==null || ip.equals(""))
202             connectedBorder = BorderFactory.createTitledBorder(
203                 new EtchedBorder(), "Connected To < NONE >");
204         else
205             connectedBorder = BorderFactory.createTitledBorder(
206                 new EtchedBorder(), "Connected To < "+ip+" >");
207         CompoundBorder cb=new CompoundBorder(
208             BorderFactory.createEmptyBorder(5,10,10,10),
209             connectedBorder);
210         centerPanel.setBorder(cb);
211         invalidate();
212         repaint();
213     }
214
215     public void error(String JavaDoc error) {
216         error(error, null);
217     }
218     public void error(String JavaDoc error, String JavaDoc heading) {
219         if(error==null || error.equals(""))
220             return;
221         if(heading==null)
222             heading = "Error";
223         JOptionPane.showMessageDialog(CmdConsole.this,
224                error, heading, JOptionPane.ERROR_MESSAGE);
225     }
226     
227     public static void writeFile(String JavaDoc fileName, String JavaDoc text)
228         throws IOException {
229         PrintWriter out = null;
230         try {
231             out = new PrintWriter(
232                 new BufferedWriter(new FileWriter(fileName)));
233             out.print(text);
234         } finally {
235             if(out!=null) out.close();
236         }
237     }
238
239     public void setSendEdit(boolean flag) {
240         sendButton.setEnabled(flag);
241         sendField.setEditable(flag);
242     }
243
244     public void updateConnectionStatus(boolean connected) {
245         if(connected==false) {
246             changeBorder(null);
247         } else {
248             changeBorder(qsadminMain.getIpAddress());
249         }
250     }
251 }
252
Popular Tags