📄 swingshellconsole.java
字号:
* {@inheritDoc} * * <p/>Terminates the current console window. If this was the last window * open, the program is terminated. **/ public synchronized void destroy() { super.destroy(); if (frame != null) { frame.dispose(); frame = null; } panel = null; } /** * {@inheritDoc} **/ public String read() throws InterruptedIOException { synchronized( lines ) { while( (null != panel) && lines.isEmpty() ) { try { lines.wait( 0 ); } catch (InterruptedException woken ) { Thread.interrupted(); InterruptedIOException wake = new InterruptedIOException( "Interrupted" ); wake.initCause( woken ); throw wake; } } if( null == panel ) { return null; } return (String) lines.remove( 0 ); } } /** * {@inheritDoc} **/ public synchronized void write(String msg) { try { text.getCaret().setVisible(false); text.insert(msg, textLength); textLength += msg.length(); text.setCaretPosition(textLength + promptLength + lineInsert); text.getCaret().setVisible(true); } catch (Throwable ohno) { LOG.log(java.util.logging.Level.SEVERE, "Failure : TextLength=" + textLength + " promptLength=" + promptLength + " lineLength=" + lineLength + " text=" + text.getText().length(), ohno); } } /** * {@inheritDoc} **/ public synchronized void clear() { try { text.setText(""); textLength = 0; promptLength = 0; lineLength = 0; lineInsert = 0; text.setCaretPosition(textLength + promptLength + lineInsert); text.getCaret().setVisible(true); } catch (Throwable ohno) { LOG.log(java.util.logging.Level.SEVERE, "Failure : TextLength=" + textLength + " promptLength=" + promptLength + " lineLength=" + lineLength + " text=" + text.getText().length(), ohno); } } /** * {@inheritDoc} **/ public synchronized void setPrompt( String newPrompt ) { try { text.replaceRange(newPrompt, textLength, textLength + promptLength); promptLength = newPrompt.length(); text.setCaretPosition(textLength + promptLength + lineInsert); text.getCaret().setVisible(true); } catch (Throwable ohno) { LOG.log(java.util.logging.Level.SEVERE, "Failure : TextLength=" + textLength + " promptLength=" + promptLength + " lineLength=" + lineLength + " text=" + text.getText().length(), ohno); } } /** * {@inheritDoc} **/ public synchronized void setCommandLine( String cmd ) { try { text.replaceRange(cmd, textLength + promptLength, textLength + promptLength + lineLength); lineLength = cmd.length(); lineInsert = lineLength; text.setCaretPosition(textLength + promptLength + lineInsert); text.getCaret().setVisible(true); } catch (Throwable ohno) { LOG.log(java.util.logging.Level.SEVERE, "Failure : TextLength=" + textLength + " promptLength=" + promptLength + " lineLength=" + lineLength + " text=" + text.getText().length(), ohno); } } /** * {@inheritDoc} **/ public synchronized void setStatusGroup( PeerGroup group ) { // remove listeners if( null != statusKeeper ) { MembershipService membership = statusKeeper.statusGroup.getMembershipService(); membership.removePropertyChangeListener( "defaultCredential", statusKeeper.membershipProperties ); statusKeeper.membershipProperties = null; RendezVousService rendezVous = statusKeeper.statusGroup.getRendezVousService(); rendezVous.removeListener( statusKeeper.rendezvousEventListener ); statusKeeper.rendezvousEventListener = null; if( rendezVous instanceof RendezVousServiceInterface ) { RendezVousServiceInterface stdRdv = (RendezVousServiceInterface) rendezVous; PeerView rpv = stdRdv.getPeerView(); rpv.removeListener( statusKeeper.peerviewEventListener ); statusKeeper.peerviewEventListener = null; } statusKeeper = null; } // install listeners if( null != group ) { statusKeeper = new StatusKeeper( group ); MembershipService membership = statusKeeper.statusGroup.getMembershipService(); statusKeeper.membershipProperties = new MembershipPropertyListener(); membership.addPropertyChangeListener( "defaultCredential", statusKeeper.membershipProperties ); try { statusKeeper.credential = (membership.getDefaultCredential() != null); } catch( PeerGroupException failed ) { statusKeeper.credential = false; } RendezVousService rendezVous = statusKeeper.statusGroup.getRendezVousService(); statusKeeper.rendezvousEventListener = new RendezvousEventListener(); rendezVous.addListener( statusKeeper.rendezvousEventListener ); statusKeeper.rendezvous = rendezVous.isRendezVous(); statusKeeper.connectedClients = rendezVous.getConnectedPeerIDs().size(); statusKeeper.connectedRdv = Collections.list(rendezVous.getConnectedRendezVous()).size(); if( rendezVous instanceof RendezVousServiceInterface ) { RendezVousServiceInterface stdRdv = (RendezVousServiceInterface) rendezVous; PeerView rpv = stdRdv.getPeerView(); if( null != rpv ) { statusKeeper.peerviewEventListener = new PeerViewEventListener(); rpv.addListener( new PeerViewEventListener() ); statusKeeper.peerview = statusKeeper.statusGroup.getRendezVousService().getLocalWalkView().size(); } } updateStatusString(); } } /** * {@inheritDoc} **/ public String getCursorDownName() { return KeyEvent.getKeyText(KeyEvent.VK_DOWN); } /** * {@inheritDoc} **/ public String getCursorUpName() { return KeyEvent.getKeyText(KeyEvent.VK_UP); } /** * Handle key actions **/ private class keyHandler extends KeyAdapter { /** * {@inheritDoc} **/ public synchronized void keyPressed(KeyEvent e) { int val = e.getKeyCode(); char ch = e.getKeyChar(); int mod = e.getModifiers(); boolean consumed = false; // There may be user confusion about Ctrl-C: since this is a shell, // users might expect Ctrl-C to terminate the currently running // command. At this writing, we don't have command termination, so // we'll go ahead and grab Ctrl-C for copy. (Suggest "ESC" for // termination...) try { if ((mod & InputEvent.CTRL_MASK) != 0) { consumed = control(val, ch, mod); } else { if (KeyEvent.CHAR_UNDEFINED == ch) { consumed = handling(val, ch, mod); } else { consumed = typing(val, ch, mod); } } if (consumed) { text.setCaretPosition(textLength + promptLength + lineInsert); text.getCaret().setVisible(true); // consume the event so that it doesn't get processed by the TextArea control. e.consume(); } } catch (Throwable ohno) { LOG.log(java.util.logging.Level.SEVERE, "Failure : TextLength=" + textLength + " promptLength=" + promptLength + " lineLength=" + lineLength + " text=" + text.getText().length(), ohno); } } } /** * Handles non-character editing of the command line. Handling is as follows: * * <p/><ul> * <li> Ctrl-C - copys the current selection to the Clipboard.</li> * <li> Ctrl-V - Inserts text from the ClipBoard into the current line.</li> * <li> Ctrl-D - Sends an EOT command.</li> * <li> Ctrl-L - Clear the text area.</li> * <li> Ctrl-U - Clear the command line</li> * <li> Ctrl-bksp - Clear the command line</li> * </ul> * * <p/>There may be user confusion about Ctrl-C: since this is a shell, users * might expect Ctrl-C to terminate the currently running command. * At this writing, we don't have command termination, so we'll go ahead * and grab Ctrl-C for copy. (Suggest Esc for termination...) * *@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 control(int val, char ch, int modifiers) { switch (val) { case KeyEvent.VK_C: if (LOG.isLoggable(java.util.logging.Level.INFO)) { LOG.finer("--> COPY <--"); } copy(); return true; case KeyEvent.VK_V: if (LOG.isLoggable(java.util.logging.Level.INFO)) { LOG.finer("--> PASTE <--"); } paste(); return true; // Let's try a ^D quit... case KeyEvent.VK_D: if (LOG.isLoggable(java.util.logging.Level.INFO)) { LOG.finer("--> QUIT <--"); } setCommandLine("\004"); submit(true); setCommandLine(""); return true; case KeyEvent.VK_L: if (LOG.isLoggable(java.util.logging.Level.INFO)) { LOG.finer("--> CLEAR <--"); } setCommandLine("clear"); submit(true); return true; case KeyEvent.VK_U: case KeyEvent.VK_BACK_SPACE: setCommandLine(""); return true; default: return false; } } /** * Handles non-character editing of the command line. Handling is as follows: * * <p/><ul> * <li> Cursor keys left and right - move the caret and update the * current insertLine value * <li> <Home> - Move cursor to the begining of the current line. * <li> <End> - Move cursor to the end of the current line. * </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 handling(int val, char ch, int modifiers) { switch (val) { case KeyEvent.VK_HOME : lineInsert = 0; return true; case KeyEvent.VK_END : lineInsert = lineLength; return true; case KeyEvent.VK_KP_LEFT : case KeyEvent.VK_LEFT : lineInsert--; if (lineInsert < 0) { lineInsert = 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -