📄 codeui.java
字号:
/* * Copyright (c) 2007, Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: CodeUI.java,v 1.5 2008/11/03 18:10:52 fros4943 Exp $ */package se.sics.cooja.mspmote.plugins;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.io.File;import java.util.Enumeration;import java.util.Vector;import javax.swing.*;import org.apache.log4j.Logger;import se.sics.cooja.mspmote.plugins.MspCodeWatcher.Breakpoints;import se.sics.mspsim.extutil.highlight.CScanner;import se.sics.mspsim.extutil.highlight.Token;import se.sics.mspsim.extutil.highlight.TokenTypes;/** * Displays source code and allows a user to add and remove breakpoints. * * @author Fredrik Osterlind */public class CodeUI extends JPanel { private static Logger logger = Logger.getLogger(CodeUI.class); private JPanel panel = null; private JList codeList = null; private File currentFile = null; private Breakpoints breakpoints = null; private Token tokensArray[][] = null; private int tokensStartPos[] = null; /** * @param breakpoints Breakpoints */ public CodeUI(Breakpoints breakpoints) { this.breakpoints = breakpoints; setLayout(new BorderLayout()); panel = new JPanel(new BorderLayout()); add(panel, BorderLayout.CENTER); displayNoCode(); breakpoints.addBreakpointListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (codeList != null) { codeList.updateUI(); } } }); } /** * Remove any shown source code. */ public void displayNoCode() { // Display "no code" message SwingUtilities.invokeLater(new Runnable() { public void run() { panel.removeAll(); panel.repaint(); } }); currentFile = null; return; } private void createTokens(Vector<String> codeData) { /* Merge code lines */ String code = ""; for (String line: codeData) { code += line + "\n"; } /* Scan code */ CScanner cScanner = new CScanner(); cScanner.change(0, 0, code.length()); int nrTokens; nrTokens = cScanner.scan(code.toCharArray(), 0, code.length()); /* Extract tokens */ Vector<Token> codeTokensVector = new Vector<Token>(); for (int i=0; i < nrTokens; i++) { Token token = cScanner.getToken(i); codeTokensVector.add(token); } /* Create new line token array */ Token newTokensArray[][] = new Token[codeData.size()][]; int[] newTokensStartPos = new int[codeData.size()]; int lineStart=0, lineEnd=-1; Enumeration<Token> tokensEnum = codeTokensVector.elements(); Token currentToken = tokensEnum.nextElement(); for (int i=0; i < newTokensArray.length; i++) { lineStart = lineEnd + 1; lineEnd = lineStart + codeData.get(i).length(); newTokensStartPos[i] = lineStart;; /* Advance tokens until correct line */ while (currentToken.position + currentToken.symbol.name.length() < lineStart) { if (!tokensEnum.hasMoreElements()) { break; } currentToken = tokensEnum.nextElement(); } /* Advance tokens until last token on line */ Vector<Token> lineTokens = new Vector<Token>(); while (currentToken.position < lineEnd) { lineTokens.add(currentToken); if (!tokensEnum.hasMoreElements()) { break; } currentToken = tokensEnum.nextElement(); } if (currentToken == null) { break; } /* Store line tokens */ Token[] lineTokensArray = new Token[lineTokens.size()]; for (int j=0; j < lineTokens.size(); j++) { lineTokensArray[j] = lineTokens.get(j); } newTokensArray[i] = lineTokensArray; } /* Start using tokens array */ tokensArray = newTokensArray; tokensStartPos = newTokensStartPos; } /** * Display given source code and mark given line. * * @param codeFile Source code file * @param codeData Source code * @param lineNr Line numer */ public void displayNewCode(final File codeFile, final Vector<String> codeData, final int lineNr) { currentFile = codeFile; if (codeData == null || codeData.size() == 0) { displayNoCode(); return; } SwingUtilities.invokeLater(new Runnable() { public void run() { // Display code codeList = new JList(new CodeListModel(codeData)); codeList.setBackground(Color.WHITE); codeList.setFont(new Font("courier", 0, 12)); codeList.setCellRenderer(new CodeCellRenderer(lineNr)); codeList.addMouseListener(new MouseListener() { public void mousePressed(MouseEvent e) { handleMouseEvent(e); } public void mouseReleased(MouseEvent e) { handleMouseEvent(e); } public void mouseEntered(MouseEvent e) { handleMouseEvent(e); } public void mouseExited(MouseEvent e) { handleMouseEvent(e); } public void mouseClicked(MouseEvent e) { handleMouseEvent(e); } }); panel.removeAll(); panel.add(codeList); createTokens(codeData); displayLine(lineNr); } }); } /** * Mark given line number in shown source code. * * @param lineNumber Line number */ public void displayLine(final int lineNumber) { if (codeList == null) { return; } SwingUtilities.invokeLater(new Runnable() { public void run() { if (lineNumber > 0) { ((CodeCellRenderer) codeList.getCellRenderer()).changeCurrentLine(lineNumber); int index = lineNumber - 1; codeList.setSelectedIndex(index); codeList.ensureIndexIsVisible(Math.max(0, index-3)); codeList.ensureIndexIsVisible(Math.min(index+3, codeList.getModel().getSize())); codeList.ensureIndexIsVisible(index); } codeList.updateUI(); } }); } private void handleMouseEvent(MouseEvent event) { if (event.isPopupTrigger()) { Point menuLocation = codeList.getPopupLocation(event); if (menuLocation == null) { menuLocation = new Point( codeList.getLocationOnScreen().x + event.getX(),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -