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

📄 cell.java

📁 定要上载质量高而定要上载质量高而定要上载质量高而定要上载质量高而定要上定要上载质量高而定要上载质量高而载质量高而
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package net.sf.fjreport.cell;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MediaTracker;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Locale;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.sf.fjreport.FJReport;
import net.sf.fjreport.datepicker.FJDateField;
import net.sf.fjreport.io.ReportImageFileFilter;
import net.sf.fjreport.util.StringResource;


public class Cell extends Rectangle{

	private JComponent editor;
	private boolean readOnly;
	private Cell instance = this;
	private static Font defaultFont = new Font("Dialog", 0, 24);
	
	//indices of 4 lines composing of the cell. top, left, bottom, right 
	
	// id = topLine.id * 512^3 + leftLine.id * 512^2 + bottomLine.id * 512 + 
	private long id;
	private FJReport report;
	
	private String name = "";
	private Object value = "";
	private Image imgValue;

	private int type;
	public static final int TYPE_NULL = 0;
	public static final int TYPE_LABEL = 1;
	public static final int TYPE_TEXTFIELD = 2;
	public static final int TYPE_TEXTAREA = 3;
	public static final int TYPE_COMBOBOX = 4;
	public static final int TYPE_CHECKBOX = 5;
	public static final int TYPE_DATEFIELD = 6;
	public static final int TYPE_IMAGE = 7;
	public static final int TYPE_PANEL = 8;
	public static final int TYPE_CUSTOM = 9;
	
	private int alignment;
	public static final int ALIGN_LEFT = 0;
	public static final int ALIGN_CENTER = 1;
	public static final int ALIGN_RIGHT = 2;
	private Font font = defaultFont;

	private boolean selected;
	
	private String[] comboxOptions;
	private static final Insets DEFAULT_MARGIN = new Insets(0, 0, 0, 0);
	private Insets margin = DEFAULT_MARGIN;
	
	private static final int innerMargin = 3;
	private static final Insets insets = new Insets(innerMargin, innerMargin, innerMargin, innerMargin);
	
	public boolean isSelected() {
		return selected;
	}

	public void render(int w, int h, Graphics2D g2d){
		if (width <= 0 || height <= 0) return;
		if (isSelected()) {
			Color color = g2d.getColor();
			g2d.setColor(new Color(224, 224, 224));
			g2d.fillRect(x + margin.left, y + margin.top, 
					width - margin.left - margin.right, 
					height - margin.top - margin.bottom);
			g2d.setColor(color);
		}
		drawContent(g2d);
	}
	
	public void drawContent(Graphics2D g2d) {
		int x, y, height, width;
		x = this.x + margin.left;
		y = this.y + margin.top;
		width = this.width - margin.left - margin.right;
		height = this.height - margin.top - margin.bottom;
		if (height == 0 || width == 0) return;
		if (type == TYPE_IMAGE  && (report != null && report.getState() != FJReport.CELL_STATE)) {
			if (imgValue != null)
			g2d.drawImage(imgValue, x, y, null);
			return;
		}
		if (isEmpty()) return;
		if (type == this.TYPE_CHECKBOX) {
			if (editor == null) {
				registerEditor();
			}
			g2d.translate(x, y);
			editor.paint(g2d);
			g2d.translate(-x, -y);
			return;
		}
		String displayStr = null;
		if (type == this.TYPE_LABEL || report == null || report.getState() != FJReport.CELL_STATE)
			displayStr = getStrValue();
		else {
			displayStr = getName();
			g2d.setColor(Color.RED);
		}
		if (displayStr == null) return;
		g2d.setFont(font);
		FontMetrics fm = g2d.getFontMetrics(font);
		Rectangle2D rect = fm.getStringBounds(displayStr, g2d);
		String[] strs = null;
		strs = displayStr.split("\n");
		if (strs == null || strs.length == 0) return;
		if (strs.length == 1) {	// single line
			if (alignment == ALIGN_CENTER)
				g2d.drawString(displayStr,
						(int)(x + (width - rect.getWidth())/2),
						(int)(y + (height - rect.getHeight())/2 - rect.getMinY()));
			else if (alignment == ALIGN_LEFT) 
				g2d.drawString(displayStr,
						x + innerMargin,
						(int)(y + (height - rect.getHeight())/2 - rect.getMinY()));
			else if (alignment == ALIGN_RIGHT)
				g2d.drawString(displayStr,
						(int)(x + width - rect.getWidth() - innerMargin),
						(int)(y + (height - rect.getHeight())/2 - rect.getMinY()));
		} else {		// multi line
//			int hIncrement = (height - innerMargin - innerMargin - (int)rect.getHeight()) / (strs.length - 1);
			int hIncrement = (int)rect.getHeight();
			int baseY = (int) (y + innerMargin - rect.getMinY());
			if (alignment == ALIGN_LEFT)
				for (int i = 0; i < strs.length; i++) {
					g2d.drawString(strs[i],	x + innerMargin, baseY);
					baseY += hIncrement;
				}
			else if (alignment == ALIGN_CENTER) {
				for (int i = 0; i < strs.length; i++) {
					rect = fm.getStringBounds(strs[i], g2d);
					g2d.drawString(strs[i],	(int)(x + (width - rect.getWidth())/2), baseY);
					baseY += hIncrement;
				}
			}
			else if (alignment == ALIGN_RIGHT) {
				for (int i = 0; i < strs.length; i++) {
					rect = fm.getStringBounds(strs[i], g2d);
					g2d.drawString(strs[i],	(int)(x + width - rect.getWidth() - innerMargin), baseY);
					baseY += hIncrement;
				}
			}
		}
		g2d.setColor(Color.BLACK);
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
//		if (report == null) return;
//		if (report.getState() == FJReport.EDIT_STATE) {
//			if (selected) {
//				report.removeAll();
//				if (editor == null) createEditor();
//				if (editor != null) {
//					setEditorValue(value);
//					report.add(editor);
//					editor.setBounds(this);
//					editor.grabFocus();
//				}
//				report.updateUI();
//			} else {
//				if (editor != null) {
//					value = getEditorValue();
//					report.remove(editor);
//				}
//			}
//		}
	}

	public Cell(FJReport report, int x1, int y1, int x2, int y2, int top, int left, int bottom, int right){
		super(x1, y1, x2-x1, y2-y1);
		this.report = report;
		id = top * 256 * 256 * 256 + left * 256 * 256 + bottom * 256 + right; 
		font = report.getFont();
	}
	
	public Cell(int x, int y, int w, int h) {
		super(x, y, w, h);
	}
	public Cell(int type) {
		super();
		this.type = type;
	}

	public JComponent getEditor() {
		return editor;
	}

	public void setEditor(JComponent editor) {
		this.editor = editor;
		setSelected(isSelected());
	}

	/**
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @return if two cells are same
	 */
	public boolean equil(int x1, int y1, int x2, int y2) {  // if intersect >= 1/2 then equil
		// TODO Auto-generated method stub
		int intersectW = Math.min(x+width, x2) - Math.max(x, x1);
		if (intersectW <= 0) return false;
		int intersectH = Math.min(y+height, y2) - Math.max(y, y1);
		if (intersectH <= 0) return false;
		return intersectW * intersectH * 2 >= width * height;
//		return (i == topLineIndex && j == leftLineIndex && i2 == bottomLineIndex && j2 == rightLineIndex);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		if (type == this.type) return;
		if ( isEmpty() ) {
			setEditor(null);
			return;
		} else if (editor != null ){
			editor = null;
		}
		this.type = type;
	}
	
	public void createEditor(){
		if (type == TYPE_LABEL) {
//			editor = new JLabel();
		}
		else if (type == TYPE_TEXTFIELD) {
			editor = new JTextField();
			((JTextField)editor).setBorder(null);
			((JTextField)editor).setMargin(insets);
		}
		else if (type == TYPE_TEXTAREA) {
			editor = new JTextArea();
			((JTextArea)editor).setBorder(null);
			((JTextArea)editor).setMargin(insets);
			((JTextArea)editor).setWrapStyleWord(true);
			((JTextArea)editor).setLineWrap(true);
		}
		else if (type == TYPE_COMBOBOX) {
			editor = new JComboBox();
			if (comboxOptions != null && comboxOptions.length > 0) {
				for (int i = 0; i < comboxOptions.length; i++) {
					((JComboBox)editor).addItem(comboxOptions[i]);
				}
			}
//			((JComboBox)editor).setEditable(true);
		}
		else if (type == TYPE_CHECKBOX) {
			editor = new JCheckBox();
			((JCheckBox)editor).setMargin(insets);
		}
		else if (type == TYPE_DATEFIELD) {
			editor = new FJDateField();
			((FJDateField)editor).setMargin(insets);
		} 
		else if (type == TYPE_IMAGE) {
			editor = new JPanel() {
				public void paint(Graphics g) {
					super.paint(g);
					if (imgValue != null) g.drawImage(imgValue, 0, 0, null);
				}
			};
			((JPanel)editor).setFocusable(true);

⌨️ 快捷键说明

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