001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.net.qsadmin.gui;
016:
017: import java.awt.*;
018: import java.awt.event.*;
019: import javax.swing.*;
020: import javax.swing.event.*;
021: import javax.swing.border.*;
022: import java.io.*;
023:
024: /**
025: * Command Console
026: * @author Akshathkumar Shetty
027: */
028: public class CmdConsole extends JPanel {
029: private static final String NEW_LINE = "\r\n";
030: private QSAdminMain qsadminMain;
031:
032: private JPanel centerPanel;
033: private JPanel textPanel;
034: private JPanel sendPanel;
035: private JPanel buttonPanel;
036:
037: private JLabel convLabel = new JLabel("Conversation with host");
038: private Border connectedBorder = BorderFactory.createTitledBorder(
039: new EtchedBorder(), "Connected To < NONE >");
040: private JTextArea messagesField = new JTextArea();
041:
042: private JLabel sendLabel = new JLabel("Message");
043: private JTextField sendField = new JTextField();
044:
045: private JButton sendButton = new JButton("Send");
046: private JButton saveButton = new JButton("Save");
047: private JButton clearButton = new JButton("Clear");
048:
049: private GridBagConstraints gbc = new GridBagConstraints();
050:
051: public CmdConsole(QSAdminMain qsadminMain) {
052: Container cp = this ;
053: this .qsadminMain = qsadminMain;
054:
055: textPanel = new JPanel();
056: textPanel.setLayout(new BorderLayout(0, 5));
057: textPanel.add(convLabel, BorderLayout.NORTH);
058: messagesField.setEditable(false);
059: messagesField.setBackground(Color.BLACK);
060: messagesField.setForeground(Color.GREEN);
061: JScrollPane jsp = new JScrollPane(messagesField);
062: textPanel.add(jsp);
063: textPanel
064: .setBorder(BorderFactory.createEmptyBorder(3, 3, 0, 3));
065:
066: sendPanel = new JPanel();
067: sendPanel.setLayout(new GridBagLayout());
068: gbc.weighty = 0.0;
069: gbc.weightx = 0.0;
070: gbc.gridx = 0;
071: gbc.gridy = 0;
072: gbc.gridheight = 1;
073: gbc.gridwidth = 1;
074: gbc.anchor = GridBagConstraints.EAST;
075: gbc.fill = GridBagConstraints.NONE;
076: sendPanel.add(sendLabel, gbc);
077: gbc.gridx = 1;
078: gbc.weightx = 1.0;
079: gbc.fill = GridBagConstraints.HORIZONTAL;
080: sendField.setEditable(false);
081: sendPanel.add(sendField, gbc);
082: gbc.gridx = 2;
083: gbc.weightx = 0.0;
084: gbc.fill = GridBagConstraints.NONE;
085: sendButton.setEnabled(false);
086: sendButton.setToolTipText("Send text to host");
087: ActionListener sendListener = new ActionListener() {
088: public void actionPerformed(ActionEvent e) {
089: String msg = sendField.getText();
090: if (!msg.equals(""))
091: sendMessage(msg);
092: else {
093: int value = JOptionPane.showConfirmDialog(
094: CmdConsole.this , "Send Blank Line ?",
095: "Send Data To Server",
096: JOptionPane.YES_NO_OPTION);
097: if (value == JOptionPane.YES_OPTION)
098: sendMessage(msg);
099: }
100: }
101: };
102: sendButton.addActionListener(sendListener);
103: sendField.addActionListener(sendListener);
104: sendPanel.add(sendButton, gbc);
105: sendPanel.setBorder(new CompoundBorder(BorderFactory
106: .createEmptyBorder(0, 0, 0, 3), BorderFactory
107: .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
126: .setToolTipText("Save conversation with host to a file");
127: saveButton.setMnemonic('S');
128: ActionListener saveListener = new ActionListener() {
129: public void actionPerformed(ActionEvent e) {
130: String text = messagesField.getText();
131: if (text.equals("")) {
132: error("Nothing to save", "Save to file");
133: return;
134: }
135: String fileName = "";
136: JFileChooser chooser = new JFileChooser();
137: chooser.setCurrentDirectory(new File("."));
138: int returnVal = chooser.showSaveDialog(CmdConsole.this );
139: if (returnVal == JFileChooser.APPROVE_OPTION) {
140: fileName = chooser.getSelectedFile()
141: .getAbsolutePath();
142: try {
143: writeFile(fileName, text);
144: } catch (Exception ioe) {
145: JOptionPane.showMessageDialog(CmdConsole.this ,
146: "" + ioe.getMessage(),
147: "Error saving to file..",
148: JOptionPane.ERROR_MESSAGE);
149: }
150: }
151: }
152: };
153: saveButton.addActionListener(saveListener);
154: buttonPanel.add(saveButton, gbc);
155: gbc.gridy = 1;
156: clearButton.setToolTipText("Clear conversation with host");
157: clearButton.setMnemonic('C');
158: ActionListener clearListener = new ActionListener() {
159: public void actionPerformed(ActionEvent e) {
160: messagesField.setText("");
161: }
162: };
163: clearButton.addActionListener(clearListener);
164: buttonPanel.add(clearButton, gbc);
165: buttonPanel.setBorder(BorderFactory.createEmptyBorder(3, 0, 0,
166: 3));
167:
168: centerPanel = new JPanel();
169: centerPanel.setLayout(new BorderLayout(0, 0));
170: centerPanel.add(buttonPanel, BorderLayout.SOUTH);
171: centerPanel.add(textPanel, BorderLayout.CENTER);
172:
173: CompoundBorder cb = new CompoundBorder(BorderFactory
174: .createEmptyBorder(5, 10, 10, 10), connectedBorder);
175: centerPanel.setBorder(cb);
176:
177: cp.setLayout(new BorderLayout());
178: cp.add(centerPanel, BorderLayout.CENTER);
179: }
180:
181: public void append(String msg) {
182: setSendEdit(qsadminMain.isConnected());
183: messagesField.append(msg + NEW_LINE);
184: messagesField
185: .setCaretPosition(messagesField.getText().length());
186: }
187:
188: public void sendMessage(String s) {
189: setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
190: try {
191: qsadminMain.sendCommand(s, true);
192: sendField.setText("");
193: setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
194: } catch (Exception e) {
195: setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
196: JOptionPane.showMessageDialog(CmdConsole.this , e
197: .getMessage(), "Error Sending Message",
198: JOptionPane.ERROR_MESSAGE);
199: }
200: }
201:
202: private void changeBorder(String ip) {
203: if (ip == null || ip.equals(""))
204: connectedBorder = BorderFactory.createTitledBorder(
205: new EtchedBorder(), "Connected To < NONE >");
206: else
207: connectedBorder = BorderFactory.createTitledBorder(
208: new EtchedBorder(), "Connected To < " + ip + " >");
209: CompoundBorder cb = new CompoundBorder(BorderFactory
210: .createEmptyBorder(5, 10, 10, 10), connectedBorder);
211: centerPanel.setBorder(cb);
212: invalidate();
213: repaint();
214: }
215:
216: public void error(String error) {
217: error(error, null);
218: }
219:
220: public void error(String error, String heading) {
221: if (error == null || error.equals(""))
222: return;
223: if (heading == null)
224: heading = "Error";
225: JOptionPane.showMessageDialog(CmdConsole.this , error, heading,
226: JOptionPane.ERROR_MESSAGE);
227: }
228:
229: public static void writeFile(String fileName, String text)
230: throws IOException {
231: PrintWriter out = null;
232: try {
233: out = new PrintWriter(new BufferedWriter(new FileWriter(
234: fileName)));
235: out.print(text);
236: } finally {
237: if (out != null)
238: out.close();
239: }
240: }
241:
242: public void setSendEdit(boolean flag) {
243: sendButton.setEnabled(flag);
244: sendField.setEditable(flag);
245: }
246:
247: public void updateConnectionStatus(boolean connected) {
248: if (connected == false) {
249: changeBorder(null);
250: } else {
251: changeBorder(qsadminMain.getIpAddress());
252: }
253: }
254: }
|