📄 swingshellconsole.java
字号:
return true; case KeyEvent.VK_KP_RIGHT : case KeyEvent.VK_RIGHT : lineInsert++; if (lineInsert > lineLength) { lineInsert = lineLength; } return true; case KeyEvent.VK_KP_UP: case KeyEvent.VK_UP: setCommandLine( getCursorUpName() ); submit(false); return true; case KeyEvent.VK_KP_DOWN : case KeyEvent.VK_DOWN : setCommandLine(getCursorDownName()); submit(false); return true; default : return false; } } /** * Handles the editing of the command line. Handling is as follows: * * <p/><ul> * <li> backspace - Delete the character ahead of lineInsert from input line.</li> * <li> delete - Delete the character after lineInsert from input line.</li> * <li>enter - Finish the input line by calling <code>submit()</code>.</li> * <li>otherwise insert the character.</li> * </ul> * *@param val the KeyCode value of the key pressed *@param ch the character associated with the key pressed *@param modifiers any modifiers that might have been pressed **/ private boolean typing(int val, char ch, int modifiers) { switch (ch) { case KeyEvent.VK_BACK_SPACE : if (lineInsert >= 1 && lineInsert <= lineLength) { text.replaceRange( "", textLength + promptLength + lineInsert - 1, textLength + promptLength + lineInsert); lineInsert--; lineLength--; } return true; case KeyEvent.VK_DELETE : if (lineInsert < lineLength) { text.replaceRange( "", textLength + promptLength + lineInsert, textLength + promptLength + lineInsert + 1); lineLength--; } return true; case KeyEvent.VK_ENTER : submit(true); return true; default : text.insert( Character.toString( ch ), textLength + promptLength + lineInsert++ ); lineLength++; return true; } } /** * Copy the selection to the system clipboard. **/ private void copy() { String selection = text.getSelectedText(); if ( (null != selection) && (selection.length() > 0) ) { StringSelection select = new StringSelection(selection); Clipboard clip = text.getToolkit().getSystemClipboard(); clip.setContents(select, select); } } /** * Paste text from the clipboard into the shell. Text is added to at the * end of the current command line. If the clipboard contents is non-text, * we'll bail out silently. */ private void paste() { Clipboard cb = text.getToolkit().getSystemClipboard(); Transferable trans = cb.getContents(this); if (trans == null) { return; } String cbText = null; try { cbText = (String) trans.getTransferData(DataFlavor.stringFlavor); } catch (UnsupportedFlavorException e) { return; } catch (IOException e) { return; } if (cbText == null) { return; } // Add the clipboard text to the end of the current command line. // If there are multiple lines in the clipboard, we paste and // execute each line as if the user entered it and and hit return. int current = 0; boolean fullLine = true; do { int lineEnd = cbText.indexOf( '\n', current ); if ( -1 == lineEnd ) { lineEnd = cbText.length(); fullLine = false; } // Append text to the current line. String aLine = cbText.substring( current, lineEnd ); text.insert( aLine, textLength + promptLength + lineInsert ); lineInsert += aLine.length(); lineLength += aLine.length(); if( fullLine ) { submit( true ); } current = lineEnd + 1; } while( current < cbText.length() ); } /** * Finishes an input line and provides it as input to the console reader. * * @param appendNewLine Clear the line and append a newline **/ private void submit( boolean appendNewLine ) { synchronized( lines ) { try { lines.add( text.getText( textLength + promptLength, lineLength ) + "\n" ); } catch( BadLocationException ble ) { IllegalArgumentException badLoc = new IllegalArgumentException( "bad location" ); badLoc.initCause( ble ); throw badLoc; } if (appendNewLine) { text.append("\n"); textLength += promptLength + lineLength + 1; promptLength = 0; lineLength = 0; lineInsert = 0; } lines.notify(); } } /** * Container for status statistics **/ private static class StatusKeeper { final PeerGroup statusGroup; boolean credential = false; boolean rendezvous = false; int peerview = 0; int connectedClients = 0; int clientReconnects = 0; int clientDisconnects = 0; int clientFailures = 0; int connectedRdv = 0; int rdvReconnects = 0; int rdvDisconnects = 0; int rdvFailures = 0; SwingShellConsole.MembershipPropertyListener membershipProperties = null; SwingShellConsole.RendezvousEventListener rendezvousEventListener = null; PeerViewListener peerviewEventListener = null; StatusKeeper( PeerGroup group ) { statusGroup = group; } } private void updateStatusString() { String status = (statusKeeper.credential ? " AUTH" : " auth" ) + " : " + (statusKeeper.rendezvous ? "RDV " : "EDGE " ) + " pv:" +statusKeeper.peerview + " rdv: " + statusKeeper.connectedRdv + " / " + statusKeeper.rdvReconnects + ":" + statusKeeper.rdvDisconnects + ":" + statusKeeper.rdvFailures + " client: " +statusKeeper.connectedClients + " / " + statusKeeper.clientReconnects + ":" + statusKeeper.clientDisconnects + ":" + statusKeeper.clientFailures ; Runtime vm = Runtime.getRuntime(); String vmStats = Long.toString( vm.freeMemory() / 1024) + "k/" + Long.toString( vm.totalMemory() / 1024) + "k"; statusStart.setText( status ); statusEnd.setText( vmStats ); } /** * Monitors property changed events for Membership Service **/ private class MembershipPropertyListener implements PropertyChangeListener { /** * {@inheritDoc} **/ public synchronized void propertyChange(PropertyChangeEvent evt) { Credential cred = (Credential) evt.getNewValue(); statusKeeper.credential = ( null != cred ); updateStatusString(); } } /** * Monitors property changed events for Membership Service **/ private class RendezvousEventListener implements RendezvousListener { /** * {@inheritDoc} **/ public synchronized void rendezvousEvent(RendezvousEvent event) { int theEventType = event.getType(); if (LOG.isLoggable(java.util.logging.Level.FINE)) { LOG.finer("[" + statusKeeper.statusGroup.getPeerGroupName() + "] Processing " + event); } switch (theEventType) { case RendezvousEvent.RDVCONNECT: statusKeeper.connectedRdv++; break; case RendezvousEvent.RDVRECONNECT: statusKeeper.rdvReconnects++; break; case RendezvousEvent.RDVFAILED: statusKeeper.rdvFailures++; statusKeeper.connectedRdv--; break; case RendezvousEvent.RDVDISCONNECT: statusKeeper.rdvDisconnects++; statusKeeper.connectedRdv--; break; case RendezvousEvent.CLIENTCONNECT: statusKeeper.connectedClients++; break; case RendezvousEvent.CLIENTRECONNECT: statusKeeper.clientReconnects++; break; case RendezvousEvent.CLIENTFAILED: statusKeeper.clientFailures++; statusKeeper.connectedClients--; break; case RendezvousEvent.CLIENTDISCONNECT: statusKeeper.clientDisconnects++; statusKeeper.connectedClients--; break; case RendezvousEvent.BECAMERDV: statusKeeper.rendezvous = true; break; case RendezvousEvent.BECAMEEDGE: statusKeeper.rendezvous = false; break; default: if (LOG.isLoggable(java.util.logging.Level.WARNING)) { LOG.log(Level.WARNING, "[" + statusKeeper.statusGroup.getPeerGroupName() + "] Unexpected RDV event : " + event); } break; } updateStatusString(); } } /** * Monitors property changed events for Membership Service **/ private class PeerViewEventListener implements PeerViewListener { /** * {@inheritDoc} **/ public synchronized void peerViewEvent(PeerViewEvent event) { int theEventType = event.getType(); if (LOG.isLoggable(java.util.logging.Level.FINE)) { LOG.finer("[" + statusKeeper.statusGroup.getPeerGroupName() + "] Processing " + event); } switch (theEventType) { case PeerViewEvent.ADD: statusKeeper.peerview++; break; case PeerViewEvent.FAIL: statusKeeper.peerview--; break; case PeerViewEvent.REMOVE: statusKeeper.peerview--; break; default: if (LOG.isLoggable(java.util.logging.Level.WARNING)) { LOG.log(Level.WARNING, "[" + statusKeeper.statusGroup.getPeerGroupName() + "] Unexpected PeerView event : " + event); } break; } updateStatusString(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -