📄 terminalwin.java
字号:
"value for '" + key + "' must be an integer"); } } else if(key.equals("save-lines")) { try { int sl = Integer.parseInt(value); if(sl < 0 || sl > 8192) throw new NumberFormatException(); setSaveLines(sl); } catch (NumberFormatException e) { throw new IllegalArgumentException( "value for '" + key + "' must be an integer (0-8192)"); } } else if(key.equals("scrollbar")) { if(myPanel != null) { if(value.equals("left") || value.equals("right")) { if(scrollbar != null) { myPanel.remove(scrollbar); if(value.equals("right")) myPanel.add(scrollbar, BorderLayout.EAST); else myPanel.add(scrollbar, BorderLayout.WEST); haveScrollbar = true; updateScrollbarValues(); // !!! REMOVE // scrollbar.setWindowSide(value); ownerFrame.pack(); requestFocus(); } } else if(value.equals("none")) { if(scrollbar != null) myPanel.remove(scrollbar); ownerFrame.pack(); requestFocus(); haveScrollbar = false; } else { throw new IllegalArgumentException( "scrollbar can be right, left or none"); } } } else if(key.equals("bg-color") || key.equals("fg-color") || key.equals("cursor-color")) { Color c; try { if(Character.isDigit(value.charAt(0))) { c = getTermRGBColor(value); } else { c = getTermColor(value); } } catch (NumberFormatException e) { throw new IllegalArgumentException( "valid colors: 'color-name' or '<r>,<g>,<b>'"); } if(key.equals("bg-color")) { origBgColor = c; setBackground(origBgColor); } else if(key.equals("cursor-color")) { cursorColor = c; } else { origFgColor = c; setForeground(origFgColor); } makeAllDirty(false); } else if(key.equals("resize-gravity")) { int rg; if(value.equals("top")) { rg = GRAVITY_NORTHWEST; } else if(value.equals("bottom")) { rg = GRAVITY_SOUTHWEST; } else { throw new IllegalArgumentException( "reszize gravity can be 'top' or 'bottom'"); } this.resizeGravity = rg; } else if(key.equals("delete-send")) { if(value.equals("DEL")) delCharacter = (char)0x7f; else if(value.equals("BS")) { delCharacter = (char)0x08; } else { throw new IllegalArgumentException( "delete character can be 'DEL' or 'BS'"); } } else if(key.equals("backspace-send")) { if(value.equals("DEL")) bsCharacter = (char)0x7f; else if(value.equals("BS")) { bsCharacter = (char)0x08; } else { throw new IllegalArgumentException( "backspace character can be 'DEL' or 'BS'"); } } else if(key.equals("geometry")) { setGeometry(value, true); } else if(key.equals("select-delim")) { if(!(value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"')) { value = "\"" + value + "\""; } selectDelims = value.substring(1, value.length()); } else if(key.equals("paste-button")) { if(value.equals("middle")) { pasteButton = InputEvent.BUTTON2_MASK; } else if(value.equals("right")) { pasteButton = InputEvent.BUTTON3_MASK; } else if(value.equals("shift+left")) { pasteButton = InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK; } else { throw new IllegalArgumentException( "mouse paste button can be 'middle' or 'right'"); } } else if(key.equals("input-charset")) { setInputCharset(value); } else if(key.equals("line-space-delta")) { lineSpaceDelta = Integer.parseInt(value); } else { throw new NoSuchElementException( "unknown terminal-property '" + key + "'"); } } props.put(key, value); if(!isEqual) { propsChanged = true; updateMenus(); } } public static Color getTermRGBColor(String value) throws NumberFormatException { int r, g, b, c1, c2; Color c; c1 = value.indexOf(','); c2 = value.lastIndexOf(','); if(c1 == -1 || c2 == -1) throw new NumberFormatException(); r = Integer.parseInt(value.substring(0, c1).trim()); g = Integer.parseInt(value.substring(c1 + 1, c2).trim()); b = Integer.parseInt(value.substring(c2 + 1).trim()); c = new Color(r, g, b); return c; } public static Color getTermColor(String name) throws IllegalArgumentException { int i; for(i = 0; i < termColors.length; i++) { if(termColorNames[i].equalsIgnoreCase(name)) break; } if(i == termColors.length) throw new IllegalArgumentException("Unknown color: " + name); return termColors[i]; } // // !!! Ouch !!! // public void setGeometry(String geometry, boolean doResize) throws IllegalArgumentException { int ro, co, xPos, yPos, xSz, ySz, delX, delY, delim = geometry.indexOf('x'); delX = geometry.indexOf('+'); delY = geometry.indexOf('-'); if(delY != -1) delX = ((delX > delY || delX == -1) ? delY : delX); try { if(delim == -1) throw new Exception(); co = Integer.parseInt(geometry.substring(0, delim).trim()); ro = Integer.parseInt( geometry.substring(delim + 1, (delX == -1 ? geometry.length() : delX)).trim()); xSz = (2 * borderWidth ) + (charWidth * co); ySz = (2 * borderHeight) + (charHeight * ro); if(delX != -1) { delY = geometry.indexOf('+', delX + 1); if(delY == -1) { delY = geometry.indexOf('-', delX + 1); if(delY == -1) throw new Exception(); } Dimension sDim = Toolkit.getDefaultToolkit().getScreenSize(); Insets fIns = ownerFrame.getInsets(); int sbSz = (haveScrollbar?scrollbar.getSize().width:0); xPos =Integer.parseInt(geometry.substring(delX+1,delY).trim()); yPos =Integer.parseInt(geometry.substring(delY + 1).trim()); if(geometry.charAt(delX) == '-') xPos = sDim.width-xSz-xPos-fIns.left-fIns.right-sbSz; if(geometry.charAt(delY) == '-') yPos = sDim.height - ySz - yPos - fIns.top - fIns.bottom; savedGeomPos = geometry.substring(delX).trim(); // !!! We can only calculate right position when ownerFrame // isShowing otherwise the insets and everything is not set, // then we set pendingShow to be called later // from componentShown // if(isShowing()) ownerFrame.setLocation(xPos, yPos); } else { savedGeomPos = ""; } } catch(Exception e) { throw new IllegalArgumentException( "geometry must be '<cols>x<rows>[pos]', e.g. '80x24+0-0'"); } if(doResize) { setSize(xSz, ySz); if(isShowing()) { componentResized(null); ownerFrame.pack(); requestFocus(); } else { pendingShow = true; setWindowSize(ro, co); resetWindow(); clearScreen(); } } // Force a repaint repaintPending = false; super.repaint(); } final private void setFont(String name, int size) { plainFont = new Font(name, Font.PLAIN, size); boldFont = new Font(name, Font.BOLD, size); super.setFont(plainFont); getDimensionOfText(0, 0); if(isShowing()) { componentResized(null); } } public void setFont(Font font) { setFont(font.getName(), font.getSize()); } public void setTitle(String title) { if(title != null && !title.equals(this.title)) { this.title = title; signalWindowChanged(rows, cols, vpixels, hpixels); } } public String getTitle() { return title; } public void setPopupButton(int buttonNum) { switch(buttonNum) { case 1: popupButton = InputEvent.BUTTON1_MASK; break; case 2: popupButton = InputEvent.BUTTON2_MASK; break; case 3: popupButton = InputEvent.BUTTON3_MASK; break; default: break; } } public PopupMenu getPopupMenu(String header) { if(popupmenu != null) return popupmenu; popupmenu = new PopupMenu(header); this.add(popupmenu); return popupmenu; } void updateScrollbarValues() { if(haveScrollbar) { scrollbar.setValues(visTop, rows, 0, saveVisTop + rows); scrollbar.setBlockIncrement(rows); } } public Panel getPanelWithScrollbar() { if(myPanel != null) return myPanel; haveScrollbar = true; scrollbar = new Scrollbar(Scrollbar.VERTICAL); updateScrollbarValues(); scrollbar.addAdjustmentListener(this); myPanel = new Panel(new BorderLayout()); myPanel.add(this, BorderLayout.CENTER); String sb = getProperty("scrollbar"); if(sb.equals("left")) myPanel.add(scrollbar, BorderLayout.WEST); else if(sb.equals("right")) myPanel.add(scrollbar, BorderLayout.EAST); else haveScrollbar = false; // No scrollbar /* !!! REMOVE if(haveScrollbar) scrollbar.setWindowSide(sb); */ return myPanel; } private final void setSaveLines(int n) { int oldSaveLines = saveLines; int fromRow, toRow, copyRows; boolean outOfMemory = false; n = (n < 0 ? 0 : n); n = (n > 8192 ? 8192 : n); if(saveLines != n) { char[][] oldScreen = screen; int[][] oldAttributes = attributes; boolean[] oldAutowraps = autowraps; saveLines = n; try { setWindowSize(rows, cols); } catch (OutOfMemoryError e) { saveLines = oldSaveLines; setWindowSize(rows, cols); outOfMemory = true; } toRow = 0; if(oldSaveLines < saveLines) { fromRow = 0; copyRows = oldSaveLines + rows; } else { if(saveVisTop <= saveLines) { fromRow = 0; copyRows = saveVisTop + rows; } else { fromRow = saveVisTop - saveLines; copyRows = saveLines + rows; saveVisTop -= fromRow; } } System.arraycopy(oldScreen, fromRow, screen, toRow, copyRows); System.arraycopy(oldAttributes, fromRow, attributes, toRow, copyRows); System.arraycopy(oldAutowraps, fromRow, autowraps, toRow, copyRows); visTop = saveVisTop; updateScrollbarValues(); if(outOfMemory) { outOfMemory = false; write("\n\rOut of memory allocating scrollback buffer, " + "reverting to " + saveLines + " lines!"); } } } public void clearSaveLines() { int fromRow; char[][] oldScreen = screen; int[][] oldAttributes = attributes; boolean[] oldAutowraps = autowraps; setWindowSize(rows, cols); fromRow = saveVisTop; System.arraycopy(oldScreen, saveVisTop, screen, 0, rows); System.arraycopy(oldAttributes, saveVisTop, attributes, 0, rows); System.arraycopy(oldAutowraps, saveVisTop, autowraps, 0, rows); saveVisTop = 0; visTop = 0; updateScrollbarValues(); makeAllDirty(true); } public void setWindowSize(int rows, int cols) { if(DEBUG) System.out.println("setWindowSize: " + cols + "x" + rows); this.rows = rows; this.cols = cols; screen = new char[rows + saveLines][cols]; attributes = new int[rows + saveLines][cols]; autowraps = new boolean[rows + saveLines]; } public void setInterpreter(TerminalInterpreter interpreter) { if(interpreter != null) this.interpreter = interpreter; } public void setInputCharset(String charset) throws IllegalArgumentException { try { this.translator = TerminalCharsetFactory.create(charset); } catch (TerminalCharsetException e) { throw new IllegalArgumentException( "unknown input-charset '" + charset + "'"); } } // // Terminal interface // public String terminalType() { return interpreter.terminalType(); } public int rows() { return rows; } public int cols() { return cols; } public int vpixels() { return vpixels; } public int hpixels() { return hpixels; } protected final void makeAllDirty(boolean instantUpdate) { updateDirtyArea(0, 0, rows, cols); if(instantUpdate && isShowing()) { Graphics g = getGraphics(); if(g != null) update(g); } else { repaint(); } } protected synchronized final void updateDirtyArea(int top, int left, int bottom, int right) { if(top < dirtyTop) dirtyTop = top; if(bottom > dirtyBottom) dirtyBottom = bottom; if(left < dirtyLeft) dirtyLeft = left; if(right > dirtyRight) dirtyRight = right; } public final void write(char c) { synchronized(writeLock) { if(outListeners != null) { int n = outListeners.size(); for(int i = 0; i < n; i++) { TerminalOutputListener outListener = (TerminalOutputListener) outListeners.elementAt(i); if(outListener != null) { outListener.write(c); } } } if(printerActive) { if(mc4[mc4MatchIdx] == c) { mc4MatchIdx++; mc4MatchIdx %= 4; } else { if(mc4MatchIdx > 0) { for(int i = 0; i < mc4MatchIdx; i++) { printer.write(mc4[i]); } mc4MatchIdx = 0; } printer.write(c); } } if(visTop != saveVisTop && termOptions[OPT_SCROLL_SI]) { repaintPending = false; // To force repaint() visTop = saveVisTop; if(haveScrollbar) scrollbar.setValue(visTop); updateDirtyArea(0, 0, rows, cols); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -