📄 listcanvas.java
字号:
/* ---------------------------------------------------------------------- The SINUS Firewall -- a TCP/IP packet filter for Linux Written within the SINUS project at the University of Zurich, SWITCH, Telekurs Payserv AG, ETH Zurich. originally based on the sf Firewall Software (C) 1996 by Robert Muchsel and Roland Schmid. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. SINUS Firewall resources: SINUS Homepage: http://www.ifi.unizh.ch/ikm/SINUS/ Firewall Homepage: http://www.ifi.unizh.ch/ikm/SINUS/firewall.html Frequently asked questions: http://www.ifi.unizh.ch/ikm/SINUS/sf_faq.html Mailing list for comments, questions, bug reports: firewall@ifi.unizh.ch ---------------------------------------------------------------------- */package sfclasses;import java.awt.*;import java.awt.event.*;import java.util.*;/** * <b>List Canvas</b><br> * This is an abstract class. Subclasses of ListCanvas can be used as * client area of ListPanel. For examples of subclasses see ShowCanvas * and TcpCanvas in FirewallAction.java or RuleCanvas in RuleEditor.java. * @version 1.0 23 Jan 1997 * @author Roland E. Schmid */abstract public class ListCanvas extends Canvas { /** * Initialize canvas. * @param listEntries Vector of Items to be displayed by the canvas. */ public ListCanvas(Vector listEntries) { this.listEntries = listEntries; addMouseListener(new LCMouseAdapter(this)); addMouseMotionListener(new LCMouseMotionAdapter(this)); addKeyListener(new LCKeyAdapter()); } /** * Draw canvas.<br> */ public synchronized void paint(Graphics g) { Object listObj; // current object int objIdx; // index of current object in vector int ypos = 0; // current position of first line of entry int maxlines; // maximum number of lines for current entry FontMetrics fm = g.getFontMetrics(); int fontheight = fm.getHeight()+1; int maxascent = fm.getMaxAscent()+1; objEndY = new int[listEntries.size()]; g.clearRect(0, 0, canvas_width, canvas_height); // draw vertical lines for (int col=1; col < column_no; col++) g.drawLine(column_pos[col], 0, column_pos[col], canvas_height); int lineCount; Enumeration en = listEntries.elements(); while (en.hasMoreElements()) { listObj = (Object)en.nextElement(); objIdx = listEntries.indexOf(listObj); maxlines = 0; // draw entries for (int col=0; col < column_no; col++) { lineCount = drawColumn(g, listObj, col, ypos, fm, fontheight, maxascent); if (lineCount > maxlines) maxlines = lineCount; } // for // draw multi column text lineCount = drawColumn(g, listObj, -1, ypos+maxlines*fontheight, fm, fontheight, maxascent); maxlines += lineCount; ypos += maxlines*fontheight; g.drawLine(0, ypos - offset_y, canvas_width, ypos - offset_y); ypos++; g.drawLine(0, ypos - offset_y, canvas_width, ypos - offset_y); // save y position objEndY[objIdx] = ypos; if (ypos - offset_y > canvas_height) break; } // while g.drawLine(0, 0, canvas_width, 0); // highlight selected entry if (selectedObject != -1) { g.setXORMode(getBackground()); if (selectedObject == 0) g.fillRect(0, 1 - offset_y, canvas_width, objEndY[0]-2); else g.fillRect(0, objEndY[selectedObject-1] + 1 - offset_y, canvas_width, objEndY[selectedObject] - objEndY[selectedObject - 1] - 2); } requestFocus(); } // paint public synchronized void update(Graphics g) { paint(g); } public int getMaxY() { Object listObj; // current object int objIdx; // index of current object in vector int ypos = 0; // current position of first line of entry int maxlines; // maximum number of lines for current entry Graphics g = getGraphics(); FontMetrics fm = g.getFontMetrics(); g.dispose(); int fontheight = fm.getHeight()+1; int maxascent = fm.getMaxAscent()+1; int lineCount; Enumeration en = listEntries.elements(); while (en.hasMoreElements()) { listObj = en.nextElement(); maxlines = 0; for (int col=0; col < column_no; col++) { lineCount = drawColumn(null, listObj, col, ypos, fm, fontheight, maxascent); if (lineCount > maxlines) maxlines = lineCount; } // for lineCount = drawColumn(null, listObj, -1, ypos+maxlines*fontheight, fm, fontheight, maxascent); maxlines += lineCount; ypos += maxlines*fontheight + 1; } // while return ypos; } abstract protected int drawColumn(Graphics g, Object listObj, int col, int ypos, FontMetrics fm, int fontheight, int maxascent); public void setOffset(int offset) { if (offset >= 0) offset_y = offset; else offset_y = 0; repaint(); } /** * event handler adapter classes */class LCMouseAdapter extends MouseAdapter { ListCanvas owner; public LCMouseAdapter(ListCanvas lc) { owner = lc; } public void mousePressed(MouseEvent me) { owner.LCmousePressed(me.getX(), me.getY()); return; } // mousePressed public void mouseReleased(MouseEvent me) { owner.LCmouseReleased(me.getX(), me.getY()); return; } // mouseReleased public void mouseExited(MouseEvent me) { owner.LCmouseExited(); return; } // mouseExited} // class LCMouseAdapterclass LCMouseMotionAdapter extends MouseMotionAdapter { ListCanvas owner; public LCMouseMotionAdapter(ListCanvas lc) { owner = lc; } public void mouseDragged(MouseEvent me) { owner.LCmouseDragged(me.getX(), me.getY()); return; } // mouseDragged public void mouseMoved(MouseEvent me) { owner.LCmouseMoved(me.getX(), me.getY()); return; } // mouseMoved} // class LCMouseMotionAdapter // column resize public void LCmousePressed(int X, int Y) { if (parent == null) parent = Utils.getParentFrame(this); resize = 0; for (int i=1; i < column_no; i++) if (Math.abs(column_pos[i]-X) <= 1) resize = i; if (resize != 0) { // cursor is located above column separator parent.setCursor(new Cursor(Cursor.MOVE_CURSOR)); return; } int y = Y + offset_y; int newsel; for (newsel=0; newsel < objEndY.length && objEndY[newsel] < y; newsel++); if (newsel == objEndY.length) newsel = -1; if (newsel == selectedObject) // same entry selected as before return; // Parent frame may handle double clicks highlightEntry(); selectedObject = newsel; highlightEntry(); return; // parent frame may handle double clicks } // LCmousePressed public void LCmouseReleased(int X, int Y) { if (parent == null) parent = Utils.getParentFrame(this); if (resize != 0) { resize = 0; parent.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } return; } // LCmouseReleased public void LCmouseExited() { if (parent == null) parent = Utils.getParentFrame(this); if (resize == 0) parent.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); return; } // LCmouseExited public void LCmouseMoved(int X, int Y) { if (parent == null) parent = Utils.getParentFrame(this); for (int i=1; i < column_no; i++) if (Math.abs(column_pos[i]-X) <= 1) { parent.setCursor(new Cursor(Cursor.TEXT_CURSOR)); return; } parent.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); return; } // LCmouseMoved public void LCmouseDragged(int X, int Y) { if (resize == 0) return; if (selectedObject != -1) deselect(); int x = X; if (X != column_pos[resize]) { if (X <= 0) x = 1; if (X >= canvas_width) x = canvas_width - 1; if (resize+1 < column_pos.length) { if (X >= column_pos[resize+1]) x = column_pos[resize+1] - 1; if (X <= column_pos[resize-1]) x = column_pos[resize-1] + 1; } else if (X <= column_pos[resize-1]) x = column_pos[resize-1] + 1; column_size[resize] += column_pos[resize] - x; column_size[resize-1] -= column_pos[resize] - x; int sharesum = column_share[resize] + column_share[resize-1]; column_share[resize] = (column_size[resize] * sharesum / (column_size[resize] + column_size[resize-1])); column_share[resize-1] = sharesum - column_share[resize]; column_pos[resize] = x; listHeader.repaint(); repaint(column_pos[resize-1], 0, resize+1 < column_pos.length ? column_pos[resize+1] : canvas_width, canvas_height); } return; } // LCmouseDraggedclass LCKeyAdapter extends KeyAdapter { public void keyPressed(KeyEvent ke) { if (ke.getKeyCode() == KeyEvent.VK_ESCAPE) { // Esc pressed deselect(); return; } } // keyPressed} // class LCKeyAdapter private void highlightEntry() { if (selectedObject == -1) return; Graphics g = getGraphics(); g.setXORMode(getBackground()); if (selectedObject == 0) g.fillRect(0, 1 - offset_y, canvas_width, objEndY[0]-2); else g.fillRect(0, objEndY[selectedObject-1] + 1 - offset_y, canvas_width, objEndY[selectedObject] - objEndY[selectedObject - 1] - 2); g.dispose(); } /** * called when panel size changes (e.g. on startup) */ public synchronized void setBounds(int x, int y, int width, int height) { // call setBounds() of superclass super.setBounds(x, y, width, height); canvas_width = width; canvas_height = height; for (int i = 0; i < column_no; i++) { column_size[i] = Math.round((float)width / 100 * (float)column_share[i]); if (i==0) column_pos[i] = 0; else column_pos[i] = column_pos[i-1] + column_size[i-1]; } ((ListPanel)getParent()).updateScrollbars(canvas_width, canvas_height); } protected static String clipString(String s, int width, FontMetrics fm) { if (width < 4) return ""; StringBuffer sb = new StringBuffer(s); int len = s.length(); while (fm.stringWidth(sb.toString()) > width) { try { len--; sb.setLength(len); } catch (StringIndexOutOfBoundsException e) { return ""; } } return sb.toString(); } protected Object getSelectedEntry() { if (selectedObject == -1) return null; else return listEntries.elementAt(selectedObject); } protected void selectEntry(Object obj) { int idx = listEntries.indexOf(obj); if (idx >= 0) selectedObject = idx; } protected void deselect() { highlightEntry(); selectedObject = -1; } public void setHeader(ListHeader listHeader) { this.listHeader = listHeader; } public int[] getShare() { return column_share; } int column_no; // number of columns int column_share[]; // column sizes in percent Vector listEntries; int column_size[]; int column_pos[]; int canvas_width, canvas_height; int offset_y; private int resize = 0; private int objEndY[]; private int selectedObject = -1; private ListHeader listHeader; private Frame parent = null;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -