/** * @return the clipboard content as a String (DataFlavor.stringFlavor) Code snippet adapted from * jEdit (Registers.java), http://www.jedit.org. Returns null if clipboard is empty. */ public static String getClipboardStringContent(Clipboard clipboard) { // Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); try { String selection = (String) (clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor)); if (selection == null) return null; boolean trailingEOL = (selection.endsWith("\n") || selection.endsWith(System.getProperty("line.separator"))); // Some Java versions return the clipboard contents using the native line separator, // so have to convert it here , see jEdit's "registers.java" BufferedReader in = new BufferedReader(new StringReader(selection)); StringBuffer buf = new StringBuffer(); String line; while ((line = in.readLine()) != null) { buf.append(line); buf.append('\n'); } // remove trailing \n if (!trailingEOL) buf.setLength(buf.length() - 1); return buf.toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
/** EdiDialog constructor comment. */ public void keyReleased(java.awt.event.KeyEvent e) { if (e.getSource() == list) { switch (e.getKeyCode()) { case KeyEvent.VK_DELETE: case KeyEvent.VK_BACK_SPACE: if (keys.length() > 0) keys.setLength(keys.length() - 1); break; case KeyEvent.VK_ESCAPE: dispose(); break; case KeyEvent.VK_ENTER: dispose(); actionOK(); break; case KeyEvent.VK_SPACE: actionAdd(); break; default: // keys.append((char) e.getKeyChar()); list.ensureIndexIsVisible(list.getSelectedIndex()); } // if (debug) // System.out.println("keys: " + keys); return; } switch (e.getKeyCode()) { case KeyEvent.VK_ENTER: case KeyEvent.VK_ESCAPE: dispose(); break; default: break; } super.keyReleased(e); }
// The main procedure public static void main(String args[]) { String s; initGUI(); while (true) { try { // Poll every ~10 ms Thread.sleep(10); } catch (InterruptedException e) { } switch (connectionStatus) { case BEGIN_CONNECT: try { // Try to set up a server if host if (isHost) { hostServer = new ServerSocket(port); socket = hostServer.accept(); } // If guest, try to connect to the server else { socket = new Socket(hostIP, port); } in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); changeStatusTS(CONNECTED, true); } // If error, clean up and output an error message catch (IOException e) { cleanUp(); changeStatusTS(DISCONNECTED, false); } break; case CONNECTED: try { // Send data if (toSend.length() != 0) { out.print(toSend); out.flush(); toSend.setLength(0); changeStatusTS(NULL, true); } // Receive data if (in.ready()) { s = in.readLine(); if ((s != null) && (s.length() != 0)) { // Check if it is the end of a trasmission if (s.equals(END_CHAT_SESSION)) { changeStatusTS(DISCONNECTING, true); } // Otherwise, receive what text else { appendToChatBox("INCOMING: " + s + "\n"); changeStatusTS(NULL, true); } } } } catch (IOException e) { cleanUp(); changeStatusTS(DISCONNECTED, false); } break; case DISCONNECTING: // Tell other chatter to disconnect as well out.print(END_CHAT_SESSION); out.flush(); // Clean up (close all streams/sockets) cleanUp(); changeStatusTS(DISCONNECTED, true); break; default: break; // do nothing } } }
// Checks the current state and sets the enables/disables // accordingly public void run() { switch (connectionStatus) { case DISCONNECTED: connectButton.setEnabled(true); disconnectButton.setEnabled(false); ipField.setEnabled(true); portField.setEnabled(true); hostOption.setEnabled(true); guestOption.setEnabled(true); chatLine.setText(""); chatLine.setEnabled(false); statusColor.setBackground(Color.red); break; case DISCONNECTING: connectButton.setEnabled(false); disconnectButton.setEnabled(false); ipField.setEnabled(false); portField.setEnabled(false); hostOption.setEnabled(false); guestOption.setEnabled(false); chatLine.setEnabled(false); statusColor.setBackground(Color.orange); break; case CONNECTED: connectButton.setEnabled(false); disconnectButton.setEnabled(true); ipField.setEnabled(false); portField.setEnabled(false); hostOption.setEnabled(false); guestOption.setEnabled(false); chatLine.setEnabled(true); statusColor.setBackground(Color.green); break; case BEGIN_CONNECT: connectButton.setEnabled(false); disconnectButton.setEnabled(false); ipField.setEnabled(false); portField.setEnabled(false); hostOption.setEnabled(false); guestOption.setEnabled(false); chatLine.setEnabled(false); chatLine.grabFocus(); statusColor.setBackground(Color.orange); break; } // Make sure that the button/text field states are consistent // with the internal states ipField.setText(hostIP); portField.setText((new Integer(port)).toString()); hostOption.setSelected(isHost); guestOption.setSelected(!isHost); statusField.setText(statusString); chatText.append(toAppend.toString()); toAppend.setLength(0); mainFrame.repaint(); }
public void echoCmd(LibgistCommand cmd) { if (!showCmdsItem.getState()) { return; } echoBuffer.setLength(0); // reset switch (cmd.cmdType) { case LibgistCommand.INSERT: echoBuffer.append("insert \"").append(cmd.key); echoBuffer.append("\" \"").append(cmd.data).append("\""); break; case LibgistCommand.REMOVE: echoBuffer.append("delete \"").append(cmd.qual); echoBuffer.append("\""); break; case LibgistCommand.FETCH: echoBuffer.append("select "); if (cmd.fetchLimit > 0) { echoBuffer.append(cmd.fetchLimit).append(" "); } echoBuffer.append("\"").append(cmd.qual).append("\""); break; case LibgistCommand.CREATE: echoBuffer.append("create ").append(cmd.indexName); echoBuffer.append(" ").append(cmd.extension); break; case LibgistCommand.OPEN: echoBuffer.append("open ").append(cmd.indexName); break; case LibgistCommand.CLOSE: echoBuffer.append("close"); break; case LibgistCommand.FLUSH: echoBuffer.append("flush"); break; case LibgistCommand.OPENANL: echoBuffer.append("openanl ").append(cmd.analysisFile.getPath()); break; case LibgistCommand.CLOSEANL: echoBuffer.append("closeanl"); break; case LibgistCommand.CREATEANL: echoBuffer.append("createanl"); break; case LibgistCommand.SCRIPT: echoBuffer.append("executing script..."); break; default: // something wrong here } consoleFrame.echoCmd(echoBuffer.toString()); }