⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ktableactionhandler.java

📁 ktable 是一个由java开发的,对控制报表的项目,它最大的特点是使用独特的算法,能支持巨大的报表(千万以上?).
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		}
		public void updateEnabledState() {
			if (m_table != null && !m_table.isDisposed() && 
			        m_table.isMultiSelectMode()) {
				setEnabled(true);
			} else setEnabled(false);
		}
		protected void selectAll(KTableModel model) {
		    Vector sel = new Vector();
		    for (int row=model.getFixedHeaderRowCount(); row<model.getRowCount(); row++)
		        for (int col=model.getFixedHeaderColumnCount(); col<model.getColumnCount(); col++) {
		            Point cell = model.belongsToCell(col, row);
		            if (cell.x==col && cell.y==row)
		                sel.add(cell);
		        }
		    try {
		        m_table.setRedraw(false);
		        m_table.setSelection(new Point[]{}, false);
			    m_table.setSelection((Point[])sel.toArray(new Point[]{}), false);		        
		    } finally {
		        m_table.setRedraw(true);
		    }
		}
    }
    
    protected class KTablePasteAction extends Action {
		protected KTablePasteAction() {
			setId("KTablePasteActionHandler");//$NON-NLS-1$
			setEnabled(false);
			setText("Einf黦en");
		}
		public void run() {
			if (m_table != null && !m_table.isDisposed()) {
			    pasteToSelection(getTextFromClipboard(), m_table.getCellSelection());
			}
		}
        protected String getTextFromClipboard() {
            Clipboard clipboard = new Clipboard(m_table.getDisplay());
            try {
                return clipboard.getContents(TextTransfer.getInstance()).toString();
            } catch (Exception ex) {
                return "";
            } finally {
                clipboard.dispose();
            }
        }
        protected void pasteToSelection(String text, Point[] selection) {
            if (selection==null || selection.length==0) return;
            KTableModel model = m_table.getModel();
            if (model==null) return;
            
            try {
                m_table.setRedraw(false);
                m_table.setSelection(new Point[]{}, false);
                Vector sel = new Vector();
            
                String[][] cellTexts = parseCellTexts(text);
                for (int row=0; row<cellTexts.length; row++)
                    for (int col=0; col<cellTexts[row].length; col++) {
                        model.setContentAt(col+selection[0].x, row+selection[0].y, cellTexts[row][col]); 
                        sel.add(new Point(col+selection[0].x, row+selection[0].y));
                    }
                m_table.setSelection((Point[])sel.toArray(new Point[]{}), false);
            } finally {
                m_table.setRedraw(true);
            }
        }
        protected String[][] parseCellTexts(String text) {
            if (!m_table.isMultiSelectMode()) {
                return new String[][]{{text}};
            } else {
                String[] lines = text.split(PlatformLineDelimiter);
                String[][] cellText = new String[lines.length][];
                for (int line=0; line<lines.length; line++)
                    cellText[line] = lines[line].split(TAB+"");
                return cellText;
            }
        }
        public void updateEnabledState() {
			if (m_table != null && !m_table.isDisposed()) {
				Point[] selection = m_table.getCellSelection();
				if (selection==null)
				    setEnabled(false);
				else if (selection.length>1) // && !m_table.isMultiSelectMode())
				    setEnabled(false);
				else setEnabled(true);
			} else setEnabled(false);
		}
    }
	
	/**
	 * Copies the specified text range to the clipboard.  The table will be placed
	 * in the clipboard in plain text format and RTF format.
	 * @param selection The list of cell indices thats content should be set
	 * to the clipboard.
	 * 
	 * @exception SWTError, see Clipboard.setContents
	 * @see org.eclipse.swt.dnd.Clipboard.setContents
	 */
	protected void setClipboardContent(Point[] selection) throws SWTError {
		//RTFTransfer rtfTransfer = RTFTransfer.getInstance();
		TextTransfer plainTextTransfer = TextTransfer.getInstance();
		HTMLTransfer htmlTransfer = HTMLTransfer.getInstance();
		    
		//String rtfText = getRTFForSelection(selection);
		String plainText = getTextForSelection(selection);
		String htmlText = getHTMLForSelection(selection);

		Clipboard clipboard = new Clipboard(m_table.getDisplay());
		try {
		    clipboard.setContents(
		            new String[]{plainText, htmlText}, //rtfText 
		            new Transfer[]{plainTextTransfer, htmlTransfer}); // rtfTransfer
		} catch (SWTError error) {
			// Copy to clipboard failed. This happens when another application 
			// is accessing the clipboard while we copy. Ignore the error.
			// Rethrow all other errors.
			if (error.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
				throw error;
			}
		} finally {
		    clipboard.dispose();
		}
	}
	
	private Point[] findTableDimensions(Point[] selection) {
	    Point topLeft = new Point(-1,-1);
	    Point bottomRight = new Point(-1, -1);
	    
	    for (int i=0; i<selection.length; i++) {
	        Point cell = selection[i];
	        if (topLeft.x<0) topLeft.x = cell.x;
	        else if (topLeft.x>cell.x) topLeft.x = cell.x;
	        if (bottomRight.x<0) bottomRight.x = cell.x;
	        else if (bottomRight.x<cell.x) bottomRight.x = cell.x;
	        
	        if (topLeft.y<0) topLeft.y = cell.y;
	        else if (topLeft.y>cell.y) topLeft.y = cell.y;
	        if (bottomRight.y<0) bottomRight.y = cell.y;
	        else if (bottomRight.y<cell.y) bottomRight.y = cell.y;
	    }
	    return new Point[]{topLeft, bottomRight};	    
	}
	
	private Point findCellSpanning(int col, int row, KTableModel model) {
	    Point spanning = new Point(1,1);
	    Point cell = new Point(col, row);
	    while (model.belongsToCell(col+spanning.x, row).equals(cell))
	        spanning.x++;
	    
	    while (model.belongsToCell(col, row+spanning.y).equals(cell))
	        spanning.y++;
	    
	    return spanning;
	}
	
	protected String getHTMLForSelection(Point[] selection) {
	    StringBuffer html = new StringBuffer();
	    sortSelectedCells(selection);
	    
	    Point[] dimensions = findTableDimensions(selection);
	    Point topLeft = dimensions[0];
	    Point bottomRight = dimensions[1];
	    
	    KTableModel model = m_table.getModel();
        if (model==null) return "";
	    // add header:
	    html.append("Version:1.0\n");
	    html.append("StartHTML:0000000000\n");
	    html.append("EndHTML:0000000000\n");
	    html.append("StartFragment:0000000000\n");
	    html.append("EndFragment:0000000000\n");
	    html.append("<html><body><table>");
	    

	    Point nextValidCell = selection[0];
	    int selCounter = 1;
	    for (int row = topLeft.y; row<=bottomRight.y; row++) {
	        html.append("<tr>");
	        for (int col = topLeft.x; col<=bottomRight.x; col++) {
	            // may skip the cell when it is spanned by another one.
	            if (model.belongsToCell(col, row).equals(new Point(col, row))) {
	                
	                if (nextValidCell.x == col && nextValidCell.y == row) {
		                html.append("<td");
	                    Point spanning = findCellSpanning(col, row, model);
		                if (spanning.x>1)
		                    html.append(" colspan=\""+spanning.x+"\"");
		                if (spanning.y>1)
		                    html.append(" rowspan=\""+spanning.y+"\"");
	                    html.append(">");
		                
	                    Object content = model.getContentAt(col, row);
	                    html.append(maskHtmlChars(content.toString()));
	                    if (selCounter<selection.length) {
	                        nextValidCell = selection[selCounter];
	                        selCounter++;
	                    }
	                } else
		                html.append("<td>");
	                
	                html.append("</td>");
	            }
	        }
	        html.append("</tr>");
	    }
	    html.append("</table></body></html>");

	    return html.toString();
	}
	
	private String maskHtmlChars(String text) {
	    text = text.replaceAll("&", "&amp;");
	    text = text.replaceAll("

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -